1*cd519653SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22*cd519653SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.idesupport.localoffice;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.net.ConnectException;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
30cdf0e10cSrcweir import com.sun.star.lang.XComponent;
31cdf0e10cSrcweir import com.sun.star.bridge.UnoUrlResolver;
32cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
34cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
35cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37cdf0e10cSrcweir import com.sun.star.uno.Exception;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import org.openoffice.idesupport.LocalOffice;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir /**
44cdf0e10cSrcweir  * LocalOfficeImpl represents a connection to the local office.
45cdf0e10cSrcweir  *
46cdf0e10cSrcweir  * This class is an implementation of LocalOffice ane allows to
47cdf0e10cSrcweir  * get access to some scripting framework releated functionality
48cdf0e10cSrcweir  * of the locally running office. The office has to be started
49cdf0e10cSrcweir  * with options appropriate for establishing local connection.
50cdf0e10cSrcweir  *
51cdf0e10cSrcweir  * @author misha <misha@openoffice.org>
52cdf0e10cSrcweir  */
53cdf0e10cSrcweir public final class LocalOfficeImpl
54cdf0e10cSrcweir     extends LocalOffice
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     private final static String     STORAGE_MRG_SINGLETON =
57cdf0e10cSrcweir         "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     private transient String                    mOfficePath;
60cdf0e10cSrcweir     private transient XMultiComponentFactory    mComponentFactory;
61cdf0e10cSrcweir     private transient XComponentContext         mComponentContext;
62cdf0e10cSrcweir     private transient XMultiServiceFactory      mServiceFactory;
63cdf0e10cSrcweir     /**
64cdf0e10cSrcweir      * Constructor.
65cdf0e10cSrcweir      */
LocalOfficeImpl()66cdf0e10cSrcweir     public LocalOfficeImpl()
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     /**
71cdf0e10cSrcweir      * Connects to the running office.
72cdf0e10cSrcweir      *
73cdf0e10cSrcweir      * @param officePath is a platform specific path string
74cdf0e10cSrcweir      *   to the office distribution.
75cdf0e10cSrcweir      * @param port is a communication port.
76cdf0e10cSrcweir      */
connect(String officePath, int port)77cdf0e10cSrcweir     protected void connect(String officePath, int port)
78cdf0e10cSrcweir         throws ConnectException
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         mOfficePath    = officePath;
81cdf0e10cSrcweir         try {
82cdf0e10cSrcweir             bootstrap(port);
83cdf0e10cSrcweir         } catch (java.lang.Exception ex) {
84cdf0e10cSrcweir             throw new ConnectException(ex.getMessage());
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * Refresh the script storage.
90cdf0e10cSrcweir      *
91cdf0e10cSrcweir      * @param uri is an identifier of storage has to be refreshed.
92cdf0e10cSrcweir      */
refreshStorage(String uri)93cdf0e10cSrcweir     public void refreshStorage(String uri)
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         try {
96cdf0e10cSrcweir             Object  object = null;
97cdf0e10cSrcweir             object      = mComponentContext.getValueByName(STORAGE_MRG_SINGLETON);
98cdf0e10cSrcweir             XScriptStorageManager storageMgr;
99cdf0e10cSrcweir             storageMgr  = (XScriptStorageManager)UnoRuntime.queryInterface(
100cdf0e10cSrcweir                 XScriptStorageManager.class, object);
101cdf0e10cSrcweir             storageMgr.refreshScriptStorage(uri);
102cdf0e10cSrcweir         } catch (java.lang.Exception ex) {
103cdf0e10cSrcweir System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getMessage());
104cdf0e10cSrcweir System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getClass().getName());
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir System.out.println("*** LocalOfficeImpl.refreshStorage: DONE");
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     /**
110cdf0e10cSrcweir      * Closes the connection to the running office.
111cdf0e10cSrcweir      */
disconnect()112cdf0e10cSrcweir     public void disconnect()
113cdf0e10cSrcweir     {
114cdf0e10cSrcweir /*
115cdf0e10cSrcweir         if(mComponentFactory != null) {
116cdf0e10cSrcweir             XComponent  comp    = (XComponent)UnoRuntime.queryInterface(
117cdf0e10cSrcweir                 XComponent.class, mComponentFactory);
118cdf0e10cSrcweir             comp.dispose();
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir */
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
123cdf0e10cSrcweir     /**
124cdf0e10cSrcweir      * Boot straps UNO.
125cdf0e10cSrcweir      *
126cdf0e10cSrcweir      * The office has to be started with following string:
127cdf0e10cSrcweir      * "-accept=socket,host=localhost,port=<PORT>;urp;StarOffice.ServiceManager"
128cdf0e10cSrcweir      *
129cdf0e10cSrcweir      * @param port is a communication port.
130cdf0e10cSrcweir      */
bootstrap(int port)131cdf0e10cSrcweir     private void bootstrap(int port)
132cdf0e10cSrcweir         throws java.lang.Exception
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         Object          object;
135cdf0e10cSrcweir         mComponentContext   = Bootstrap.createInitialComponentContext(null);
136cdf0e10cSrcweir         XUnoUrlResolver urlresolver = UnoUrlResolver.create(mComponentContext);
137cdf0e10cSrcweir         object              = urlresolver.resolve(
138cdf0e10cSrcweir             "uno:socket,host=localhost,port=" +
139cdf0e10cSrcweir             port +
140cdf0e10cSrcweir             ";urp;StarOffice.ServiceManager");
141cdf0e10cSrcweir         mComponentFactory   = (XMultiComponentFactory)UnoRuntime.queryInterface(
142cdf0e10cSrcweir             XMultiComponentFactory.class, object);
143cdf0e10cSrcweir         XPropertySet    factoryProps;
144cdf0e10cSrcweir         factoryProps        = (XPropertySet)UnoRuntime.queryInterface(
145cdf0e10cSrcweir             XPropertySet.class, mComponentFactory);
146cdf0e10cSrcweir         object              = factoryProps.getPropertyValue("DefaultContext");
147cdf0e10cSrcweir         mComponentContext   = (XComponentContext)UnoRuntime.queryInterface(
148cdf0e10cSrcweir             XComponentContext.class, object);
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir }
151