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