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