1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Bootstrap.java,v $
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
;
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
57 The service manager can create a few basic services, whose implementations are:
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>
67 Other services can be inserted into the service manager by
68 using its XSet interface:
70 XSet xSet = UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
71 // insert the service manager
72 xSet.insert( aSingleComponentFactory );
75 public class Bootstrap
{
77 private static void insertBasicFactories(
78 XSet xSet
, XImplementationLoader xImpLoader
)
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
)
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 ) );
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 );
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 );
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
);
155 * Bootstraps a servicemanager with the jurt base components registered.
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()
174 return defaultBootstrap_InitialComponentContext( null, null );
176 /** Bootstraps the initial component context from a native UNO installation.
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
)
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();
196 while (keys
.hasMoreElements())
198 String name
= (String
)keys
.nextElement();
200 pairs
[ n
++ ] = (String
)bootstrap_parameters
.get( name
);
206 NativeLibraryLoader
.loadLibrary( Bootstrap
.class.getClassLoader(), "juh" );
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
)
221 * Bootstraps the component context from a UNO installation.
223 * @return a bootstrapped component context.
227 public static final XComponentContext
bootstrap()
228 throws BootstrapException
{
230 XComponentContext xContext
= null;
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
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
);
278 String sConnect
= "uno:pipe,name=" + sPipeName
+
279 ";urp;StarOffice.ComponentContext";
281 // wait until office is started
282 for (int i
= 0;; ++i
) {
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!" );
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:
295 throw new BootstrapException(ex
.toString());
297 Thread
.currentThread().sleep( 500 );
300 } catch ( BootstrapException e
) {
302 } catch ( java
.lang
.RuntimeException e
) {
304 } catch ( java
.lang
.Exception e
) {
305 throw new BootstrapException( e
);
311 private static void pipe(
312 final InputStream in
, final PrintStream out
, final String prefix
) {
314 new Thread( "Pipe: " + prefix
) {
316 BufferedReader r
= new BufferedReader(
317 new InputStreamReader( in
) );
320 String s
= r
.readLine();
324 out
.println( prefix
+ s
);
326 } catch ( java
.io
.IOException e
) {
327 e
.printStackTrace( System
.err
);