1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
24cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
25cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
26cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
27cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
28cdf0e10cSrcweir import com.sun.star.test.XSomethingB;
29cdf0e10cSrcweir import com.sun.star.test.SomethingB;
30cdf0e10cSrcweir import com.sun.star.lang.XSingleComponentFactory;
31cdf0e10cSrcweir import com.sun.star.container.XSet;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // sample starbasic code, you can execute it after you have connected to the office.
34cdf0e10cSrcweir //  Sub Main
35cdf0e10cSrcweir //  	o = createUnoService( "com.sun.star.test.SomethingB" )
36cdf0e10cSrcweir //  	msgbox o.methodOne( "from the office !" )
37cdf0e10cSrcweir //  End Sub
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public class TestJavaComponent
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 
insertIntoServiceManager( XMultiComponentFactory serviceManager, Object singleFactory )42cdf0e10cSrcweir     public static void insertIntoServiceManager(
43cdf0e10cSrcweir         XMultiComponentFactory serviceManager, Object singleFactory )
44cdf0e10cSrcweir         throws com.sun.star.uno.Exception
45cdf0e10cSrcweir     {
46cdf0e10cSrcweir         XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager );
47cdf0e10cSrcweir         set.insert( singleFactory );
48cdf0e10cSrcweir     }
49cdf0e10cSrcweir 
removeFromServiceManager( XMultiComponentFactory serviceManager, Object singleFactory )50cdf0e10cSrcweir     public static void removeFromServiceManager(
51cdf0e10cSrcweir         XMultiComponentFactory serviceManager, Object singleFactory )
52cdf0e10cSrcweir         throws com.sun.star.uno.Exception
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         XSet set = (XSet ) UnoRuntime.queryInterface( XSet.class, serviceManager );
55cdf0e10cSrcweir         set.remove( singleFactory );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
main(String[] args)59cdf0e10cSrcweir     public static void main(String[] args) throws java.lang.Exception
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         try {
62cdf0e10cSrcweir             boolean bLocal = false;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir             XMultiComponentFactory xUsedServiceManager = null;
65cdf0e10cSrcweir             XComponentContext xUsedComponentContext = null;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir             if( args.length == 1 && args[0].equals( "local" ))
68cdf0e10cSrcweir             {
69cdf0e10cSrcweir                 XComponentContext xLocalComponentContext =
70cdf0e10cSrcweir                     Bootstrap.createInitialComponentContext( null );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir                 // initial serviceManager
73cdf0e10cSrcweir                 XMultiComponentFactory xLocalServiceManager =
74cdf0e10cSrcweir                     xLocalComponentContext.getServiceManager();
75cdf0e10cSrcweir 
76cdf0e10cSrcweir                 bLocal = true;
77cdf0e10cSrcweir                 xUsedServiceManager = xLocalServiceManager;
78cdf0e10cSrcweir                 xUsedComponentContext = xLocalComponentContext;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir                 System.out.println( "Using local servicemanager" );
81cdf0e10cSrcweir             } else {
82cdf0e10cSrcweir                 // get the remote office component context
83cdf0e10cSrcweir                 xUsedComponentContext =
84cdf0e10cSrcweir                     com.sun.star.comp.helper.Bootstrap.bootstrap();
85cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
86cdf0e10cSrcweir 
87cdf0e10cSrcweir                 xUsedServiceManager = xUsedComponentContext.getServiceManager();
88cdf0e10cSrcweir                 System.out.println( "Using remote servicemanager" );
89cdf0e10cSrcweir             }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             if ( xUsedServiceManager == null )
92cdf0e10cSrcweir             {
93cdf0e10cSrcweir                 System.out.println( "ERROR: no service manager" );
94cdf0e10cSrcweir                 System.exit(0);
95cdf0e10cSrcweir             }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             Object factory = new Object();
98cdf0e10cSrcweir             if ( bLocal )
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 // retrieve the factory for the component implementation
101cdf0e10cSrcweir                 factory = TestServiceProvider.__getServiceFactory(
102cdf0e10cSrcweir                     "TestComponentB", null, null);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir                 // insert the factory into the local servicemanager
105cdf0e10cSrcweir                 // From now on, the service can be instantiated !
106cdf0e10cSrcweir                 insertIntoServiceManager( xUsedServiceManager, factory );
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             XSomethingB xSomethingB = SomethingB.create(xUsedComponentContext);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             // and call the test method.
112cdf0e10cSrcweir             String s= xSomethingB.methodTwo("Hello World!");
113cdf0e10cSrcweir             System.out.println(s);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             if ( bLocal )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir                 // remove it again from the servicemanager,
118cdf0e10cSrcweir                 removeFromServiceManager( xUsedServiceManager, factory );
119cdf0e10cSrcweir             }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         catch ( Exception e )
123cdf0e10cSrcweir         {
124cdf0e10cSrcweir             System.out.println( "UNO Exception caught: " + e );
125cdf0e10cSrcweir             System.out.println( "Message: " + e.getMessage() );
126cdf0e10cSrcweir             e.printStackTrace(System.out);
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         // quit, even when a remote bridge is running
130cdf0e10cSrcweir         System.exit(0);
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir }
133