1 /************************************************************************* 2 * 3 * The Contents of this file are made available subject to the terms of 4 * the BSD license. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 *************************************************************************/ 34 import com.sun.star.uno.XComponentContext; 35 import com.sun.star.comp.helper.Bootstrap; 36 import com.sun.star.lang.XMultiComponentFactory; 37 import com.sun.star.uno.UnoRuntime; 38 import com.sun.star.bridge.XUnoUrlResolver; 39 import com.sun.star.test.XSomethingB; 40 import com.sun.star.test.SomethingB; 41 import com.sun.star.lang.XSingleComponentFactory; 42 import com.sun.star.container.XSet; 43 44 // sample starbasic code, you can execute it after you have connected to the office. 45 // Sub Main 46 // o = createUnoService( "com.sun.star.test.SomethingB" ) 47 // msgbox o.methodOne( "from the office !" ) 48 // End Sub 49 50 public class TestJavaComponent 51 { 52 53 public static void insertIntoServiceManager( 54 XMultiComponentFactory serviceManager, Object singleFactory ) 55 throws com.sun.star.uno.Exception 56 { 57 XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager ); 58 set.insert( singleFactory ); 59 } 60 61 public static void removeFromServiceManager( 62 XMultiComponentFactory serviceManager, Object singleFactory ) 63 throws com.sun.star.uno.Exception 64 { 65 XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager ); 66 set.remove( singleFactory ); 67 68 } 69 70 public static void main(String[] args) throws java.lang.Exception 71 { 72 try { 73 boolean bLocal = false; 74 75 XMultiComponentFactory xUsedServiceManager = null; 76 XComponentContext xUsedComponentContext = null; 77 78 if( args.length == 1 && args[0].equals( "local" )) 79 { 80 XComponentContext xLocalComponentContext = 81 Bootstrap.createInitialComponentContext( null ); 82 83 // initial serviceManager 84 XMultiComponentFactory xLocalServiceManager = 85 xLocalComponentContext.getServiceManager(); 86 87 bLocal = true; 88 xUsedServiceManager = xLocalServiceManager; 89 xUsedComponentContext = xLocalComponentContext; 90 91 System.out.println( "Using local servicemanager" ); 92 } else { 93 // get the remote office component context 94 xUsedComponentContext = 95 com.sun.star.comp.helper.Bootstrap.bootstrap(); 96 System.out.println("Connected to a running office ..."); 97 98 xUsedServiceManager = xUsedComponentContext.getServiceManager(); 99 System.out.println( "Using remote servicemanager" ); 100 } 101 102 if ( xUsedServiceManager == null ) 103 { 104 System.out.println( "ERROR: no service manager" ); 105 System.exit(0); 106 } 107 108 Object factory = new Object(); 109 if ( bLocal ) 110 { 111 // retrieve the factory for the component implementation 112 factory = TestServiceProvider.__getServiceFactory( 113 "TestComponentB", null, null); 114 115 // insert the factory into the local servicemanager 116 // From now on, the service can be instantiated ! 117 insertIntoServiceManager( xUsedServiceManager, factory ); 118 } 119 120 XSomethingB xSomethingB = SomethingB.create(xUsedComponentContext); 121 122 // and call the test method. 123 String s= xSomethingB.methodTwo("Hello World!"); 124 System.out.println(s); 125 126 if ( bLocal ) 127 { 128 // remove it again from the servicemanager, 129 removeFromServiceManager( xUsedServiceManager, factory ); 130 } 131 132 } 133 catch ( Exception e ) 134 { 135 System.out.println( "UNO Exception caught: " + e ); 136 System.out.println( "Message: " + e.getMessage() ); 137 e.printStackTrace(System.out); 138 } 139 140 // quit, even when a remote bridge is running 141 System.exit(0); 142 } 143 } 144