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 import com.sun.star.uno.XComponentContext; 24 import com.sun.star.comp.helper.Bootstrap; 25 import com.sun.star.lang.XMultiComponentFactory; 26 import com.sun.star.uno.UnoRuntime; 27 import com.sun.star.bridge.XUnoUrlResolver; 28 import com.sun.star.test.XSomethingB; 29 import com.sun.star.test.SomethingB; 30 import com.sun.star.lang.XSingleComponentFactory; 31 import com.sun.star.container.XSet; 32 33 // sample starbasic code, you can execute it after you have connected to the office. 34 // Sub Main 35 // o = createUnoService( "com.sun.star.test.SomethingB" ) 36 // msgbox o.methodOne( "from the office !" ) 37 // End Sub 38 39 public class TestJavaComponent 40 { 41 insertIntoServiceManager( XMultiComponentFactory serviceManager, Object singleFactory )42 public static void insertIntoServiceManager( 43 XMultiComponentFactory serviceManager, Object singleFactory ) 44 throws com.sun.star.uno.Exception 45 { 46 XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager ); 47 set.insert( singleFactory ); 48 } 49 removeFromServiceManager( XMultiComponentFactory serviceManager, Object singleFactory )50 public static void removeFromServiceManager( 51 XMultiComponentFactory serviceManager, Object singleFactory ) 52 throws com.sun.star.uno.Exception 53 { 54 XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager ); 55 set.remove( singleFactory ); 56 57 } 58 main(String[] args)59 public static void main(String[] args) throws java.lang.Exception 60 { 61 try { 62 boolean bLocal = false; 63 64 XMultiComponentFactory xUsedServiceManager = null; 65 XComponentContext xUsedComponentContext = null; 66 67 if( args.length == 1 && args[0].equals( "local" )) 68 { 69 XComponentContext xLocalComponentContext = 70 Bootstrap.createInitialComponentContext( null ); 71 72 // initial serviceManager 73 XMultiComponentFactory xLocalServiceManager = 74 xLocalComponentContext.getServiceManager(); 75 76 bLocal = true; 77 xUsedServiceManager = xLocalServiceManager; 78 xUsedComponentContext = xLocalComponentContext; 79 80 System.out.println( "Using local servicemanager" ); 81 } else { 82 // get the remote office component context 83 xUsedComponentContext = 84 com.sun.star.comp.helper.Bootstrap.bootstrap(); 85 System.out.println("Connected to a running office ..."); 86 87 xUsedServiceManager = xUsedComponentContext.getServiceManager(); 88 System.out.println( "Using remote servicemanager" ); 89 } 90 91 if ( xUsedServiceManager == null ) 92 { 93 System.out.println( "ERROR: no service manager" ); 94 System.exit(0); 95 } 96 97 Object factory = new Object(); 98 if ( bLocal ) 99 { 100 // retrieve the factory for the component implementation 101 factory = TestServiceProvider.__getServiceFactory( 102 "TestComponentB", null, null); 103 104 // insert the factory into the local servicemanager 105 // From now on, the service can be instantiated ! 106 insertIntoServiceManager( xUsedServiceManager, factory ); 107 } 108 109 XSomethingB xSomethingB = SomethingB.create(xUsedComponentContext); 110 111 // and call the test method. 112 String s= xSomethingB.methodTwo("Hello World!"); 113 System.out.println(s); 114 115 if ( bLocal ) 116 { 117 // remove it again from the servicemanager, 118 removeFromServiceManager( xUsedServiceManager, factory ); 119 } 120 121 } 122 catch ( Exception e ) 123 { 124 System.out.println( "UNO Exception caught: " + e ); 125 System.out.println( "Message: " + e.getMessage() ); 126 e.printStackTrace(System.out); 127 } 128 129 // quit, even when a remote bridge is running 130 System.exit(0); 131 } 132 } 133