Update ooo320-m1
[ooovba.git] / javaunohelper / com / sun / star / comp / helper / Bootstrap.java
blob4c30cbb553972b124f09e744b034c57e4791de99
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Bootstrap.java,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.comp.helper;
33 import com.sun.star.bridge.UnoUrlResolver;
34 import com.sun.star.bridge.XUnoUrlResolver;
35 import com.sun.star.comp.loader.JavaLoader;
36 import com.sun.star.container.XSet;
37 import com.sun.star.lang.XInitialization;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.lang.XMultiComponentFactory;
40 import com.sun.star.lang.XSingleComponentFactory;
41 import com.sun.star.lib.util.NativeLibraryLoader;
42 import com.sun.star.loader.XImplementationLoader;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XComponentContext;
46 import java.io.BufferedReader;
47 import java.io.File;
48 import java.io.InputStream;
49 import java.io.InputStreamReader;
50 import java.io.PrintStream;
51 import java.util.Enumeration;
52 import java.util.Hashtable;
53 import java.util.Random;
55 /** Bootstrap offers functionality to obtain a context or simply
56 a service manager.
57 The service manager can create a few basic services, whose implementations are:
58 <ul>
59 <li>com.sun.star.comp.loader.JavaLoader</li>
60 <li>com.sun.star.comp.urlresolver.UrlResolver</li>
61 <li>com.sun.star.comp.bridgefactory.BridgeFactory</li>
62 <li>com.sun.star.comp.connections.Connector</li>
63 <li>com.sun.star.comp.connections.Acceptor</li>
64 <li>com.sun.star.comp.servicemanager.ServiceManager</li>
65 </ul>
67 Other services can be inserted into the service manager by
68 using its XSet interface:
69 <pre>
70 XSet xSet = UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
71 // insert the service manager
72 xSet.insert( aSingleComponentFactory );
73 </pre>
74 */
75 public class Bootstrap {
77 private static void insertBasicFactories(
78 XSet xSet, XImplementationLoader xImpLoader )
79 throws Exception
81 // insert the factory of the loader
82 xSet.insert( xImpLoader.activate(
83 "com.sun.star.comp.loader.JavaLoader", null, null, null ) );
85 // insert the factory of the URLResolver
86 xSet.insert( xImpLoader.activate(
87 "com.sun.star.comp.urlresolver.UrlResolver", null, null, null ) );
89 // insert the bridgefactory
90 xSet.insert( xImpLoader.activate(
91 "com.sun.star.comp.bridgefactory.BridgeFactory", null, null, null ) );
93 // insert the connector
94 xSet.insert( xImpLoader.activate(
95 "com.sun.star.comp.connections.Connector", null, null, null ) );
97 // insert the acceptor
98 xSet.insert( xImpLoader.activate(
99 "com.sun.star.comp.connections.Acceptor", null, null, null ) );
102 /** Bootstraps an initial component context with service manager and basic
103 jurt components inserted.
104 @param context_entries the hash table contains mappings of entry names (type string) to
105 context entries (type class ComponentContextEntry).
106 @return a new context.
108 static public XComponentContext createInitialComponentContext( Hashtable context_entries )
109 throws Exception
111 XImplementationLoader xImpLoader = UnoRuntime.queryInterface(
112 XImplementationLoader.class, new JavaLoader() );
114 // Get the factory of the ServiceManager
115 XSingleComponentFactory smgr_fac = UnoRuntime.queryInterface(
116 XSingleComponentFactory.class, xImpLoader.activate(
117 "com.sun.star.comp.servicemanager.ServiceManager", null, null, null ) );
119 // Create an instance of the ServiceManager
120 XMultiComponentFactory xSMgr = UnoRuntime.queryInterface(
121 XMultiComponentFactory.class, smgr_fac.createInstanceWithContext( null ) );
123 // post init loader
124 XInitialization xInit = UnoRuntime.queryInterface(
125 XInitialization.class, xImpLoader );
126 Object[] args = new Object [] { xSMgr };
127 xInit.initialize( args );
129 // initial component context
130 if (context_entries == null)
131 context_entries = new Hashtable( 1 );
132 // add smgr
133 context_entries.put(
134 "/singletons/com.sun.star.lang.theServiceManager",
135 new ComponentContextEntry( null, xSMgr ) );
136 // ... xxx todo: add standard entries
137 XComponentContext xContext = new ComponentContext( context_entries, null );
139 // post init smgr
140 xInit = UnoRuntime.queryInterface(
141 XInitialization.class, xSMgr );
142 args = new Object [] { null, xContext }; // no registry, default context
143 xInit.initialize( args );
145 XSet xSet = UnoRuntime.queryInterface( XSet.class, xSMgr );
146 // insert the service manager
147 xSet.insert( smgr_fac );
148 // and basic jurt factories
149 insertBasicFactories( xSet, xImpLoader );
151 return xContext;
155 * Bootstraps a servicemanager with the jurt base components registered.
156 * <p>
157 * @return a freshly boostrapped service manager
158 * @see com.sun.star.lang.ServiceManager
160 static public XMultiServiceFactory createSimpleServiceManager() throws Exception
162 return UnoRuntime.queryInterface(
163 XMultiServiceFactory.class, createInitialComponentContext( null ).getServiceManager() );
167 /** Bootstraps the initial component context from a native UNO installation.
169 @see cppuhelper/defaultBootstrap_InitialComponentContext()
171 static public final XComponentContext defaultBootstrap_InitialComponentContext()
172 throws Exception
174 return defaultBootstrap_InitialComponentContext( null, null );
176 /** Bootstraps the initial component context from a native UNO installation.
178 @param ini_file
179 ini_file (may be null: uno.rc besides cppuhelper lib)
180 @param bootstrap_parameters
181 bootstrap parameters (maybe null)
183 @see cppuhelper/defaultBootstrap_InitialComponentContext()
185 static public final XComponentContext defaultBootstrap_InitialComponentContext(
186 String ini_file, Hashtable bootstrap_parameters )
187 throws Exception
189 // jni convenience: easier to iterate over array than calling Hashtable
190 String pairs [] = null;
191 if (null != bootstrap_parameters)
193 pairs = new String [ 2 * bootstrap_parameters.size() ];
194 Enumeration keys = bootstrap_parameters.keys();
195 int n = 0;
196 while (keys.hasMoreElements())
198 String name = (String)keys.nextElement();
199 pairs[ n++ ] = name;
200 pairs[ n++ ] = (String)bootstrap_parameters.get( name );
204 if (! m_loaded_juh)
206 NativeLibraryLoader.loadLibrary( Bootstrap.class.getClassLoader(), "juh" );
207 m_loaded_juh = true;
209 return UnoRuntime.queryInterface(
210 XComponentContext.class,
211 cppuhelper_bootstrap(
212 ini_file, pairs, Bootstrap.class.getClassLoader() ) );
215 static private boolean m_loaded_juh = false;
216 static private native Object cppuhelper_bootstrap(
217 String ini_file, String bootstrap_parameters [], ClassLoader loader )
218 throws Exception;
221 * Bootstraps the component context from a UNO installation.
223 * @return a bootstrapped component context.
225 * @since UDK 3.1.0
227 public static final XComponentContext bootstrap()
228 throws BootstrapException {
230 XComponentContext xContext = null;
232 try {
233 // create default local component context
234 XComponentContext xLocalContext =
235 createInitialComponentContext( null );
236 if ( xLocalContext == null )
237 throw new BootstrapException( "no local component context!" );
239 // find office executable relative to this class's class loader
240 String sOffice =
241 System.getProperty( "os.name" ).startsWith( "Windows" ) ?
242 "soffice.exe" : "soffice";
243 File fOffice = NativeLibraryLoader.getResource(
244 Bootstrap.class.getClassLoader(), sOffice );
245 if ( fOffice == null )
246 throw new BootstrapException( "no office executable found!" );
248 // create random pipe name
249 String sPipeName = "uno" +
250 Long.toString( (new Random()).nextLong() & 0x7fffffffffffffffL );
252 // create call with arguments
253 String[] cmdArray = new String[7];
254 cmdArray[0] = fOffice.getPath();
255 cmdArray[1] = "-nologo";
256 cmdArray[2] = "-nodefault";
257 cmdArray[3] = "-norestore";
258 cmdArray[4] = "-nocrashreport";
259 cmdArray[5] = "-nolockcheck";
260 cmdArray[6] = "-accept=pipe,name=" + sPipeName + ";urp;";
262 // start office process
263 Process p = Runtime.getRuntime().exec( cmdArray );
264 pipe( p.getInputStream(), System.out, "CO> " );
265 pipe( p.getErrorStream(), System.err, "CE> " );
267 // initial service manager
268 XMultiComponentFactory xLocalServiceManager =
269 xLocalContext.getServiceManager();
270 if ( xLocalServiceManager == null )
271 throw new BootstrapException( "no initial service manager!" );
273 // create a URL resolver
274 XUnoUrlResolver xUrlResolver =
275 UnoUrlResolver.create( xLocalContext );
277 // connection string
278 String sConnect = "uno:pipe,name=" + sPipeName +
279 ";urp;StarOffice.ComponentContext";
281 // wait until office is started
282 for (int i = 0;; ++i) {
283 try {
284 // try to connect to office
285 Object context = xUrlResolver.resolve( sConnect );
286 xContext = UnoRuntime.queryInterface(
287 XComponentContext.class, context);
288 if ( xContext == null )
289 throw new BootstrapException( "no component context!" );
290 break;
291 } catch ( com.sun.star.connection.NoConnectException ex ) {
292 // Wait 500 ms, then try to connect again, but do not wait
293 // longer than 5 min (= 600 * 500 ms) total:
294 if (i == 600) {
295 throw new BootstrapException(ex.toString());
297 Thread.currentThread().sleep( 500 );
300 } catch ( BootstrapException e ) {
301 throw e;
302 } catch ( java.lang.RuntimeException e ) {
303 throw e;
304 } catch ( java.lang.Exception e ) {
305 throw new BootstrapException( e );
308 return xContext;
311 private static void pipe(
312 final InputStream in, final PrintStream out, final String prefix ) {
314 new Thread( "Pipe: " + prefix) {
315 public void run() {
316 BufferedReader r = new BufferedReader(
317 new InputStreamReader( in ) );
318 try {
319 for ( ; ; ) {
320 String s = r.readLine();
321 if ( s == null ) {
322 break;
324 out.println( prefix + s );
326 } catch ( java.io.IOException e ) {
327 e.printStackTrace( System.err );
330 }.start();