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