1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 using System;
29 using System.Collections;
30 using uno;
31 using uno.util;
32 using unoidl.com.sun.star.uno;
33 using unoidl.com.sun.star.lang;
34 using unoidl.com.sun.star.container;
35 
36 
37 //==============================================================================
38 internal class Factory :
39     WeakComponentBase, XSingleComponentFactory, XServiceInfo
40 {
41     private String m_service;
42     private Type m_type;
43     private System.Reflection.ConstructorInfo m_ctor;
44 
45     public Factory( Type type, String service )
46     {
47         m_service = service;
48         m_type = type;
49         m_ctor = type.GetConstructor(
50             new Type [] { typeof (XComponentContext) } );
51     }
52 
53     public Object createInstanceWithContext( XComponentContext xContext )
54     {
55         return m_ctor.Invoke( new Object [] { xContext } );
56     }
57 
58     public Object createInstanceWithArgumentsAndContext(
59         uno.Any [] args, XComponentContext xContext )
60     {
61         return m_ctor.Invoke( new Object [] { xContext } );
62     }
63 
64     public bool supportsService( String name )
65     {
66         return m_service.Equals( name );
67     }
68 
69     public String [] getSupportedServiceNames()
70     {
71         return new String [] { m_service };
72     }
73 
74     public String getImplementationName()
75     {
76         return m_type.ToString();
77     }
78 }
79 
80 
81 /** This executable does the same as the batch file starting via uno.exe,
82     but via bootstrapping native UNO.
83 */
84 public class BridgeTest
85 {
86     public static int Main( String [] args )
87     {
88 //       System.Diagnostics.Debugger.Launch();
89         try
90 		{
91 			string bootstrap_ini = "cli_bridgetest_inprocess.ini";
92 			if (args.Length > 0)
93 			{
94 				if (args[0] == "/?")
95 				{
96 					Console.WriteLine(
97 						"\n\ncli_bridgetest_inprocess [bootstrap file] \n\n"
98 						+ "bootstrap file \n"
99 						+ "\t contains the entries UNO_TYPES and UNO_SERVICES.\n"
100 						+ "\t If a file is not provided than it is assumed that a\n"
101 						+ "\t cli_bridgetest_inprocess.ini file can be found in the\n "
102 						+ "\t current working directory.\n"
103 						);
104 					return 0;
105 				}
106 				else
107 				{
108 					bootstrap_ini = args[0];
109 				}
110 			}
111 
112             // bootstrap native UNO
113             XComponentContext xContext =
114                 Bootstrap.defaultBootstrap_InitialComponentContext(
115                     bootstrap_ini, null );
116 
117             using (new uno.util.DisposeGuard( (XComponent) xContext ))
118             {
119                 XSet xSet = (XSet) xContext.getServiceManager();
120                 xSet.insert(
121                     new uno.Any(
122                         typeof (XSingleComponentFactory),
123                         new Factory(
124                             typeof (cs_testobj.BridgeTestObject),
125                             "com.sun.star.test.bridge.cli_uno.CsTestObject" ) ) );
126                 xSet.insert(
127                     new uno.Any(
128                         typeof (XSingleComponentFactory),
129                         new Factory(
130                             typeof (vb_testobj.VBBridgeTestObject),
131                             "com.sun.star.test.bridge.cli_uno.VbTestObject" ) ) );
132                 xSet.insert(
133                     new uno.Any(
134                         typeof (XSingleComponentFactory),
135                         new Factory(
136                             typeof (cpp_bridgetest.BridgeTest),
137                             "com.sun.star.test.bridge.cli_uno.CppBridgeTest" ) ) );
138                 xSet.insert(
139                     new uno.Any(
140                         typeof (XSingleComponentFactory),
141                         new Factory(
142                             typeof (cs_testobj.BridgeTest),
143                             "com.sun.star.test.bridge.cli_uno.CsBridgeTest" ) ) );
144                 xSet.insert(
145                     new uno.Any(
146                         typeof (XSingleComponentFactory),
147                         new Factory(
148                             typeof (vb_bridetest.BridgeTest),
149                             "com.sun.star.test.bridge.cli_uno.VbBridgeTest" ) ) );
150 
151                 // I.
152                 // direct unbridged test
153                 // get client object via singleton entry
154                 Object test_client;
155                 XMain xClient;
156                 test_client = new cs_testobj.BridgeTest( xContext );
157                 xClient = (XMain) test_client;
158                 Console.WriteLine(
159                     "\n[cli bridgetest] 1. C# client calls C# object");
160                 // run with CLI target object
161                 xClient.run(
162                     new String [] {
163                     "com.sun.star.test.bridge.cli_uno.CsTestObject" } );
164 
165                 // II:
166                 // uno -ro uno_services.rdb -ro uno_types.rdb
167                 //     -s com.sun.star.test.bridge.BridgeTest
168                 //     -- com.sun.star.test.bridge.cli_uno.TestObject
169 
170                 // get native client
171                 test_client =
172                     xContext.getServiceManager().createInstanceWithContext(
173                         "com.sun.star.test.bridge.BridgeTest", xContext );
174                 xClient = (XMain) test_client;
175                 Console.WriteLine(
176                     "\n[cli bridgetest] 2. C++ client (native) calls C# object");
177                 // run with CLI target object
178                 xClient.run(
179                     new String [] {
180                     "com.sun.star.test.bridge.cli_uno.CsTestObject",
181 					"noCurrentContext"} );
182 
183                 // III:
184                 // uno -ro uno_services.rdb -ro uno_types.rdb
185                 //     -s com.sun.star.test.bridge.cli_uno.BridgeTest
186                 //     -- com.sun.star.test.bridge.CppTestObject
187 
188                 // get CLI client
189                 test_client =
190                     xContext.getServiceManager().createInstanceWithContext(
191                         "com.sun.star.test.bridge.cli_uno.CsBridgeTest",
192                         xContext );
193                 xClient = (XMain) test_client;
194                 Console.WriteLine(
195                     "\n[cli bridgetest] 3. C# client calls C++ object (native)");
196                 // run with native target object
197                 xClient.run(
198                     new String [] { "com.sun.star.test.bridge.CppTestObject" } );
199 
200                 // IV:
201                 // uno -ro uno_services.rdb -ro uno_types.rdb
202                 //     -s com.sun.star.test.bridge.cli_uno.VbBridgeTest
203                 //     -- com.sun.star.test.bridge.CppTestObject
204                 // get CLI client
205                 test_client =
206                     xContext.getServiceManager().createInstanceWithContext(
207                         "com.sun.star.test.bridge.cli_uno.VbBridgeTest",
208                         xContext );
209                 xClient = (XMain) test_client;
210                 Console.WriteLine(
211                     "\n[cli bridgetest] 4. Visual Basic client calls C++ (native) object" );
212                 // run with native target object
213                 xClient.run(
214                     new String [] { "com.sun.star.test.bridge.CppTestObject" } );
215 
216                 // V:
217                 // uno -ro uno_services.rdb -ro uno_types.rdb
218                 //     -s com.sun.star.test.bridge.BridgeTest
219                 //     -- com.sun.star.test.bridge.cli_uno.VbTestObject
220                 // get CLI client
221 //                 test_client =
222 //                     xContext.getServiceManager().createInstanceWithContext(
223 //                         "com.sun.star.test.bridge.BridgeTest", xContext );
224 //                 xClient = (XMain) test_client;
225 //                 Console.WriteLine(
226 //                     "[cli bridgetest] Visual Basic client: {0}",
227 //                     xClient.ToString() );
228 //                 // run with native target object
229 //                 xClient.run(
230 //                     new String [] {
231 //                     "com.sun.star.test.bridge.cli_uno.VbTestObject" } );
232 
233                 // VI:
234                 // uno -ro uno_services.rdb -ro uno_types.rdb
235                 // -s com.sun.star.test.bridge.cli_uno.CppBridgeTest
236                 // -- com.sun.star.test.bridge.CppTestObject
237                 test_client =
238                     xContext.getServiceManager().createInstanceWithContext(
239                         "com.sun.star.test.bridge.cli_uno.CppBridgeTest",
240                         xContext );
241                 xClient = (XMain) test_client;
242                 Console.WriteLine(
243                     "\n[cli bridgetest] 6. CLI C++ client calls C++ object (native)");
244                 // run with native target object
245                 xClient.run(
246                     new String [] { "com.sun.star.test.bridge.CppTestObject" } );
247             }
248         }
249         catch (System.Exception exc)
250         {
251             GC.WaitForPendingFinalizers();
252             System.Console.WriteLine( exc );
253             return -1;
254         }
255 
256         GC.WaitForPendingFinalizers();
257         System.Console.WriteLine( "====> all tests ok." );
258         return 0;
259     }
260 }
261