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 org.openoffice.idesupport.localoffice;
25 
26 import java.net.ConnectException;
27 
28 import com.sun.star.lang.XMultiServiceFactory;
29 import com.sun.star.lang.XMultiComponentFactory;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.bridge.UnoUrlResolver;
32 import com.sun.star.bridge.XUnoUrlResolver;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.comp.helper.Bootstrap;
35 import com.sun.star.uno.XComponentContext;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.Exception;
38 
39 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
40 
41 import org.openoffice.idesupport.LocalOffice;
42 
43 /**
44  * LocalOfficeImpl represents a connection to the local office.
45  *
46  * This class is an implementation of LocalOffice ane allows to
47  * get access to some scripting framework releated functionality
48  * of the locally running office. The office has to be started
49  * with options appropriate for establishing local connection.
50  *
51  * @author misha <misha@openoffice.org>
52  */
53 public final class LocalOfficeImpl
54     extends LocalOffice
55 {
56     private final static String     STORAGE_MRG_SINGLETON =
57         "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
58 
59     private transient String                    mOfficePath;
60     private transient XMultiComponentFactory    mComponentFactory;
61     private transient XComponentContext         mComponentContext;
62     private transient XMultiServiceFactory      mServiceFactory;
63     /**
64      * Constructor.
65      */
LocalOfficeImpl()66     public LocalOfficeImpl()
67     {
68     }
69 
70     /**
71      * Connects to the running office.
72      *
73      * @param officePath is a platform specific path string
74      *   to the office distribution.
75      * @param port is a communication port.
76      */
connect(String officePath, int port)77     protected void connect(String officePath, int port)
78         throws ConnectException
79     {
80         mOfficePath    = officePath;
81         try {
82             bootstrap(port);
83         } catch (java.lang.Exception ex) {
84             throw new ConnectException(ex.getMessage());
85         }
86     }
87 
88     /**
89      * Refresh the script storage.
90      *
91      * @param uri is an identifier of storage has to be refreshed.
92      */
refreshStorage(String uri)93     public void refreshStorage(String uri)
94     {
95         try {
96             Object  object = null;
97             object      = mComponentContext.getValueByName(STORAGE_MRG_SINGLETON);
98             XScriptStorageManager storageMgr;
99             storageMgr  = (XScriptStorageManager)UnoRuntime.queryInterface(
100                 XScriptStorageManager.class, object);
101             storageMgr.refreshScriptStorage(uri);
102         } catch (java.lang.Exception ex) {
103 System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getMessage());
104 System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getClass().getName());
105         }
106 System.out.println("*** LocalOfficeImpl.refreshStorage: DONE");
107     }
108 
109     /**
110      * Closes the connection to the running office.
111      */
disconnect()112     public void disconnect()
113     {
114 /*
115         if(mComponentFactory != null) {
116             XComponent  comp    = (XComponent)UnoRuntime.queryInterface(
117                 XComponent.class, mComponentFactory);
118             comp.dispose();
119         }
120 */
121     }
122 
123     /**
124      * Boot straps UNO.
125      *
126      * The office has to be started with following string:
127      * "-accept=socket,host=localhost,port=<PORT>;urp;StarOffice.ServiceManager"
128      *
129      * @param port is a communication port.
130      */
bootstrap(int port)131     private void bootstrap(int port)
132         throws java.lang.Exception
133     {
134         Object          object;
135         mComponentContext   = Bootstrap.createInitialComponentContext(null);
136         XUnoUrlResolver urlresolver = UnoUrlResolver.create(mComponentContext);
137         object              = urlresolver.resolve(
138             "uno:socket,host=localhost,port=" +
139             port +
140             ";urp;StarOffice.ServiceManager");
141         mComponentFactory   = (XMultiComponentFactory)UnoRuntime.queryInterface(
142             XMultiComponentFactory.class, object);
143         XPropertySet    factoryProps;
144         factoryProps        = (XPropertySet)UnoRuntime.queryInterface(
145             XPropertySet.class, mComponentFactory);
146         object              = factoryProps.getPropertyValue("DefaultContext");
147         mComponentContext   = (XComponentContext)UnoRuntime.queryInterface(
148             XComponentContext.class, object);
149     }
150 }
151