1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.comp.urlresolver;
25 
26 import com.sun.star.uno.XComponentContext;
27 import com.sun.star.comp.helper.Bootstrap;
28 import com.sun.star.lang.XMultiComponentFactory;
29 import com.sun.star.bridge.UnoUrlResolver;
30 import com.sun.star.bridge.XUnoUrlResolver;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.uno.UnoRuntime;
33 //import com.sun.star.connection.NoConnectionException;
34 
35 /** start the office with these options <br>
36     soffice -accept=socket,host=localhost,port=8100;urp;
37 */
38 public class UrlResolver_Test
39 {
main(String[] args)40     public static void main(String[] args) {
41         try {
42             XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext( null );
43 
44             // initial serviceManager
45             XMultiComponentFactory xLocalServiceManager = xcomponentcontext.getServiceManager();
46 
47             // create a connector, so that it can contact the office
48             XUnoUrlResolver urlResolver
49                 = UnoUrlResolver.create( xcomponentcontext );
50 
51             Object initialObject = urlResolver.resolve(
52                 "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" );
53 
54             XMultiComponentFactory  xOfficeFactory= UnoRuntime.queryInterface(
55                 XMultiComponentFactory.class, initialObject );
56 
57             // retrieve the component context (it's not yet exported from the office)
58             // Query for the XPropertySet interface.
59             XPropertySet xProperySet = UnoRuntime.queryInterface(
60                 XPropertySet.class, xOfficeFactory);
61 
62             // Get the default context from the office server.
63             Object oDefaultContext = xProperySet.getPropertyValue( "DefaultContext" );
64 
65             // Query for the interface XComponentContext.
66             XComponentContext xOfficeComponentContext = UnoRuntime.queryInterface(
67                 XComponentContext.class, oDefaultContext );
68 
69             // now create the desktop service
70             // NOTE: use the office component context here !
71             Object oDesktop = xOfficeFactory.createInstanceWithContext("com.sun.star.frame.Desktop",
72                                                                        xOfficeComponentContext );
73         } catch(com.sun.star.connection.NoConnectException e) {
74             System.out.println(e.getMessage());
75             e.printStackTrace();
76         } catch(com.sun.star.connection.ConnectionSetupException ce) {
77             System.out.println(ce.getMessage());
78             ce.printStackTrace();
79         } catch(com.sun.star.lang.IllegalArgumentException ie) {
80             System.out.println(ie.getMessage());
81             ie.printStackTrace();
82         } catch(com.sun.star.beans.UnknownPropertyException ue) {
83             System.out.println(ue.getMessage());
84             ue.printStackTrace();
85         } catch(java.lang.Exception ee) {
86             System.out.println(ee.getMessage());
87             ee.printStackTrace();
88         }
89 
90         }
91 }
92