1 // -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 
3 /*
4  * This file is part of the LibreOffice project.
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * This file incorporates work covered by the following license notice:
11  *
12  *   Licensed to the Apache Software Foundation (ASF) under one or more
13  *   contributor license agreements. See the NOTICE file distributed
14  *   with this work for additional information regarding copyright
15  *   ownership. The ASF licenses this file to you under the Apache
16  *   License, Version 2.0 (the "License"); you may not use this file
17  *   except in compliance with the License. You may obtain a copy of
18  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
19  */
20 
21 package com.sun.star.comp.helper;
22 
23 import com.sun.star.bridge.UnoUrlResolver;
24 import com.sun.star.bridge.XUnoUrlResolver;
25 import com.sun.star.comp.loader.JavaLoader;
26 import com.sun.star.comp.servicemanager.ServiceManager;
27 import com.sun.star.container.XSet;
28 import com.sun.star.lang.XInitialization;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.lang.XMultiComponentFactory;
31 import com.sun.star.lib.util.NativeLibraryLoader;
32 import com.sun.star.loader.XImplementationLoader;
33 import com.sun.star.uno.UnoRuntime;
34 import com.sun.star.uno.XComponentContext;
35 
36 import java.io.BufferedReader;
37 import java.io.File;
38 import java.io.InputStream;
39 import java.io.InputStreamReader;
40 import java.io.PrintStream;
41 import java.io.UnsupportedEncodingException;
42 import java.util.HashMap;
43 import java.util.Hashtable;
44 import java.util.Map;
45 import java.util.Random;
46 
47 /** Bootstrap offers functionality to obtain a context or simply
48     a service manager.
49     The service manager can create a few basic services, whose implementations  are:
50     <ul>
51     <li>com.sun.star.comp.loader.JavaLoader</li>
52     <li>com.sun.star.comp.urlresolver.UrlResolver</li>
53     <li>com.sun.star.comp.bridgefactory.BridgeFactory</li>
54     <li>com.sun.star.comp.connections.Connector</li>
55     <li>com.sun.star.comp.connections.Acceptor</li>
56     <li>com.sun.star.comp.servicemanager.ServiceManager</li>
57     </ul>
58 
59     Other services can be inserted into the service manager by
60     using its XSet interface:
61     <pre>
62         XSet xSet = UnoRuntime.queryInterface( XSet.class, aMultiComponentFactory );
63         // insert the service manager
64         xSet.insert( aSingleComponentFactory );
65     </pre>
66 */
67 public class Bootstrap {
68 
insertBasicFactories( XSet xSet, XImplementationLoader xImpLoader )69     private static void insertBasicFactories(
70         XSet xSet, XImplementationLoader xImpLoader )
71         throws Exception
72     {
73         // insert the factory of the loader
74         xSet.insert( xImpLoader.activate(
75             "com.sun.star.comp.loader.JavaLoader", null, null, null ) );
76 
77         // insert the factory of the URLResolver
78         xSet.insert( xImpLoader.activate(
79             "com.sun.star.comp.urlresolver.UrlResolver", null, null, null ) );
80 
81         // insert the bridgefactory
82         xSet.insert( xImpLoader.activate(
83             "com.sun.star.comp.bridgefactory.BridgeFactory", null, null, null ) );
84 
85         // insert the connector
86         xSet.insert( xImpLoader.activate(
87             "com.sun.star.comp.connections.Connector", null, null, null ) );
88 
89         // insert the acceptor
90         xSet.insert( xImpLoader.activate(
91             "com.sun.star.comp.connections.Acceptor", null, null, null ) );
92     }
93 
94     /**
95      * Returns an array of default commandline options to start bootstrapped
96      * instance of soffice with. You may use it in connection with bootstrap
97      * method for example like this:
98      * <pre>
99      *     List list = Arrays.asList( Bootstrap.getDefaultOptions() );
100      *     list.remove("--nologo");
101      *     list.remove("--nodefault");
102      *     list.add("--invisible");
103      *
104      *     Bootstrap.bootstrap( list.toArray( new String[list.size()] );
105      * </pre>
106      *
107      * @return an array of default commandline options
108      * @see #bootstrap( String[] )
109      * @since LibreOffice 5.1
110      */
getDefaultOptions()111     public static final String[] getDefaultOptions()
112     {
113         return new String[]
114         {
115             "--nologo",
116             "--nodefault",
117             "--norestore",
118             "--nolockcheck"
119         };
120     }
121 
122     /**
123        backwards compatibility stub.
124         @param context_entries the hash table contains mappings of entry names (type string) to
125         context entries (type class ComponentContextEntry).
126         @throws Exception if things go awry.
127         @return a new context.
128      */
createInitialComponentContext( Hashtable<String, Object> context_entries )129     public static XComponentContext createInitialComponentContext( Hashtable<String, Object> context_entries )
130             throws Exception
131     {
132         return createInitialComponentContext((Map<String, Object>) context_entries);
133     }
134     /** Bootstraps an initial component context with service manager and basic
135         jurt components inserted.
136         @param context_entries the hash table contains mappings of entry names (type string) to
137         context entries (type class ComponentContextEntry).
138         @throws Exception if things go awry.
139         @return a new context.
140     */
createInitialComponentContext( Map<String, Object> context_entries )141     public static XComponentContext createInitialComponentContext( Map<String, Object> context_entries )
142         throws Exception
143     {
144         ServiceManager xSMgr = new ServiceManager();
145 
146         XImplementationLoader xImpLoader = UnoRuntime.queryInterface(
147             XImplementationLoader.class, new JavaLoader() );
148         XInitialization xInit = UnoRuntime.queryInterface(
149             XInitialization.class, xImpLoader );
150         Object[] args = new Object [] { xSMgr };
151         xInit.initialize( args );
152 
153         // initial component context
154         if (context_entries == null)
155             context_entries = new HashMap<String,Object>( 1 );
156         // add smgr
157         context_entries.put(
158             "/singletons/com.sun.star.lang.theServiceManager",
159             new ComponentContextEntry( null, xSMgr ) );
160         // ... xxx todo: add standard entries
161         XComponentContext xContext = new ComponentContext( context_entries, null );
162 
163         xSMgr.setDefaultContext(xContext);
164 
165         XSet xSet = UnoRuntime.queryInterface( XSet.class, xSMgr );
166         // insert basic jurt factories
167         insertBasicFactories( xSet, xImpLoader );
168 
169         return xContext;
170     }
171 
172     /**
173      * Bootstraps a servicemanager with the jurt base components registered.
174      *
175      * See also UNOIDL <code>com.sun.star.lang.ServiceManager</code>.
176      *
177      * @throws Exception if things go awry.
178      * @return     a freshly bootstrapped service manager
179      */
createSimpleServiceManager()180     public static XMultiServiceFactory createSimpleServiceManager() throws Exception
181     {
182         return UnoRuntime.queryInterface(
183             XMultiServiceFactory.class, createInitialComponentContext( (Map<String, Object>) null ).getServiceManager() );
184     }
185 
186 
187     /** Bootstraps the initial component context from a native UNO installation.
188 
189         @throws Exception if things go awry.
190         @return a freshly bootstrapped component context.
191 
192         See also
193         <code>cppuhelper/defaultBootstrap_InitialComponentContext()</code>.
194     */
defaultBootstrap_InitialComponentContext()195     public static final XComponentContext defaultBootstrap_InitialComponentContext()
196         throws Exception
197     {
198         return defaultBootstrap_InitialComponentContext( (String) null, (Map<String,String>) null );
199     }
200     /**
201      * Backwards compatibility stub.
202      *
203      * @param ini_file
204      *        ini_file (may be null: uno.rc besides cppuhelper lib)
205      * @param bootstrap_parameters
206      *        bootstrap parameters (maybe null)
207      *
208      * @throws Exception if things go awry.
209      * @return a freshly bootstrapped component context.
210      */
defaultBootstrap_InitialComponentContext( String ini_file, Hashtable<String,String> bootstrap_parameters )211     public static final XComponentContext defaultBootstrap_InitialComponentContext(
212             String ini_file, Hashtable<String,String> bootstrap_parameters )
213             throws Exception
214     {
215         return defaultBootstrap_InitialComponentContext(ini_file, (Map<String,String>) bootstrap_parameters);
216 
217     }
218     /** Bootstraps the initial component context from a native UNO installation.
219 
220         See also
221         <code>cppuhelper/defaultBootstrap_InitialComponentContext()</code>.
222 
223         @param ini_file
224                ini_file (may be null: uno.rc besides cppuhelper lib)
225         @param bootstrap_parameters
226                bootstrap parameters (maybe null)
227 
228         @throws Exception if things go awry.
229         @return a freshly bootstrapped component context.
230     */
defaultBootstrap_InitialComponentContext( String ini_file, Map<String,String> bootstrap_parameters )231     public static final XComponentContext defaultBootstrap_InitialComponentContext(
232         String ini_file, Map<String,String> bootstrap_parameters )
233         throws Exception
234     {
235         // jni convenience: easier to iterate over array than calling Hashtable
236         String pairs [] = null;
237         if (null != bootstrap_parameters)
238         {
239             pairs = new String [ 2 * bootstrap_parameters.size() ];
240             int n = 0;
241             for (Map.Entry<String, String> bootstrap_parameter : bootstrap_parameters.entrySet()) {
242                 pairs[ n++ ] = bootstrap_parameter.getKey();
243                 pairs[ n++ ] = bootstrap_parameter.getValue();
244             }
245         }
246 
247         if (! m_loaded_juh)
248         {
249             if ("The Android Project".equals(System.getProperty("java.vendor")))
250             {
251                 // Find out if we are configured with DISABLE_DYNLOADING or
252                 // not. Try to load the lo-bootstrap shared library which
253                 // won't exist in the DISABLE_DYNLOADING case. (And which will
254                 // be already loaded otherwise, so nothing unexpected happens
255                 // that case.) Yeah, this would be simpler if I just could be
256                 // bothered to keep a separate branch for DISABLE_DYNLOADING
257                 // on Android, merging in master periodically, until I know
258                 // for sure whether it is what I want, or not.
259 
260                 boolean disable_dynloading = false;
261                 try {
262                     System.loadLibrary( "lo-bootstrap" );
263                 } catch ( UnsatisfiedLinkError e ) {
264                     disable_dynloading = true;
265                 }
266 
267                 if (!disable_dynloading)
268                     {
269                         NativeLibraryLoader.loadLibrary( Bootstrap.class.getClassLoader(), "juh" );
270                     }
271             }
272             else
273             {
274                 NativeLibraryLoader.loadLibrary( Bootstrap.class.getClassLoader(), "juh" );
275             }
276             m_loaded_juh = true;
277         }
278         return UnoRuntime.queryInterface(
279             XComponentContext.class,
280             cppuhelper_bootstrap(
281                 ini_file, pairs, Bootstrap.class.getClassLoader() ) );
282     }
283 
284     private static boolean m_loaded_juh = false;
cppuhelper_bootstrap( String ini_file, String bootstrap_parameters [], ClassLoader loader )285     private static native Object cppuhelper_bootstrap(
286         String ini_file, String bootstrap_parameters [], ClassLoader loader )
287         throws Exception;
288 
289     /**
290      * Bootstraps the component context from a UNO installation.
291      *
292      * @throws BootstrapException if things go awry.
293      *
294      * @return a bootstrapped component context.
295      *
296      * @since UDK 3.1.0
297      */
bootstrap()298     public static final XComponentContext bootstrap()
299         throws BootstrapException {
300 
301         String[] defaultArgArray = getDefaultOptions();
302         return bootstrap( defaultArgArray );
303     }
304 
305     /**
306      * Bootstraps the component context from a UNO installation.
307      *
308      * @param argArray
309      *        an array of strings - commandline options to start instance of
310      *        soffice with
311      * @see #getDefaultOptions()
312      *
313      * @throws BootstrapException if things go awry.
314      *
315      * @return a bootstrapped component context.
316      *
317      * @since LibreOffice 5.1
318      */
bootstrap( String[] argArray )319     public static final XComponentContext bootstrap( String[] argArray )
320         throws BootstrapException {
321 
322         XComponentContext xContext = null;
323 
324         try {
325             // create default local component context
326             XComponentContext xLocalContext =
327                 createInitialComponentContext( (Map<String, Object>) null );
328             if ( xLocalContext == null )
329                 throw new BootstrapException( "no local component context!" );
330 
331             // find office executable relative to this class's class loader
332             String sOffice =
333                 System.getProperty( "os.name" ).startsWith( "Windows" ) ?
334                 "soffice.exe" : "soffice";
335             File fOffice = NativeLibraryLoader.getResource(
336                 Bootstrap.class.getClassLoader(), sOffice );
337             if ( fOffice == null )
338                 throw new BootstrapException( "no office executable found!" );
339 
340             // create random pipe name
341             String sPipeName = "uno" +
342                 Long.toString(randomPipeName.nextLong() & 0x7fffffffffffffffL);
343 
344             // create call with arguments
345             String[] cmdArray = new String[ argArray.length + 2 ];
346             cmdArray[0] = fOffice.getPath();
347             cmdArray[1] = ( "--accept=pipe,name=" + sPipeName + ";urp;" );
348 
349             System.arraycopy( argArray, 0, cmdArray, 2, argArray.length );
350 
351             // start office process
352             Process p = Runtime.getRuntime().exec( cmdArray );
353             pipe( p.getInputStream(), System.out, "CO> " );
354             pipe( p.getErrorStream(), System.err, "CE> " );
355 
356             // initial service manager
357             XMultiComponentFactory xLocalServiceManager =
358                 xLocalContext.getServiceManager();
359             if ( xLocalServiceManager == null )
360                 throw new BootstrapException( "no initial service manager!" );
361 
362             // create a URL resolver
363             XUnoUrlResolver xUrlResolver =
364                 UnoUrlResolver.create( xLocalContext );
365 
366             // connection string
367             String sConnect = "uno:pipe,name=" + sPipeName +
368                 ";urp;StarOffice.ComponentContext";
369 
370             // wait until office is started
371             for (int i = 0;; ++i) {
372                 try {
373                     // try to connect to office
374                     Object context = xUrlResolver.resolve( sConnect );
375                     xContext = UnoRuntime.queryInterface(
376                         XComponentContext.class, context);
377                     if ( xContext == null )
378                         throw new BootstrapException( "no component context!" );
379                     break;
380                 } catch ( com.sun.star.connection.NoConnectException ex ) {
381                     // Wait 500 ms, then try to connect again, but do not wait
382                     // longer than 5 min (= 600 * 500 ms) total:
383                     if (i == 600) {
384                         throw new BootstrapException(ex);
385                     }
386                     Thread.sleep( 500 );
387                 }
388             }
389         } catch ( BootstrapException e ) {
390             throw e;
391         } catch ( java.lang.RuntimeException e ) {
392             throw e;
393         } catch ( java.lang.Exception e ) {
394             throw new BootstrapException( e );
395         }
396 
397         return xContext;
398     }
399 
400     private static final Random randomPipeName = new Random();
401 
pipe( final InputStream in, final PrintStream out, final String prefix )402     private static void pipe(
403         final InputStream in, final PrintStream out, final String prefix ) {
404 
405         new Thread( "Pipe: " + prefix) {
406             @Override
407             public void run() {
408                 try {
409                     BufferedReader r = new BufferedReader(
410                         new InputStreamReader(in, "UTF-8") );
411 
412                     for ( ; ; ) {
413                         String s = r.readLine();
414                         if ( s == null ) {
415                             break;
416                         }
417                         out.println( prefix + s );
418                     }
419                 } catch ( UnsupportedEncodingException e ) {
420                     e.printStackTrace( System.err );
421                 } catch ( java.io.IOException e ) {
422                     e.printStackTrace( System.err );
423                 }
424             }
425         }.start();
426     }
427 }
428 
429 // vim:set shiftwidth=4 softtabstop=4 expandtab:
430