xref: /AOO41X/main/scripting/java/org/openoffice/idesupport/localoffice/LocalOfficeImpl.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.idesupport.localoffice;
29 
30 import java.net.ConnectException;
31 
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.lang.XComponent;
35 import com.sun.star.bridge.UnoUrlResolver;
36 import com.sun.star.bridge.XUnoUrlResolver;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.comp.helper.Bootstrap;
39 import com.sun.star.uno.XComponentContext;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.Exception;
42 
43 import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
44 
45 import org.openoffice.idesupport.LocalOffice;
46 
47 /**
48  * LocalOfficeImpl represents a connection to the local office.
49  *
50  * This class is an implementation of LocalOffice ane allows to
51  * get access to some scripting framework releated functionality
52  * of the locally running office. The office has to be started
53  * with options appropriate for establishing local connection.
54  *
55  * @author misha <misha@openoffice.org>
56  */
57 public final class LocalOfficeImpl
58     extends LocalOffice
59 {
60     private final static String     STORAGE_MRG_SINGLETON =
61         "/singletons/drafts.com.sun.star.script.framework.storage.theScriptStorageManager";
62 
63     private transient String                    mOfficePath;
64     private transient XMultiComponentFactory    mComponentFactory;
65     private transient XComponentContext         mComponentContext;
66     private transient XMultiServiceFactory      mServiceFactory;
67     /**
68      * Constructor.
69      */
70     public LocalOfficeImpl()
71     {
72     }
73 
74     /**
75      * Connects to the running office.
76      *
77      * @param officePath is a platform specific path string
78      *   to the office distribution.
79      * @param port is a communication port.
80      */
81     protected void connect(String officePath, int port)
82         throws ConnectException
83     {
84         mOfficePath    = officePath;
85         try {
86             bootstrap(port);
87         } catch (java.lang.Exception ex) {
88             throw new ConnectException(ex.getMessage());
89         }
90     }
91 
92     /**
93      * Refresh the script storage.
94      *
95      * @param uri is an identifier of storage has to be refreshed.
96      */
97     public void refreshStorage(String uri)
98     {
99         try {
100             Object  object = null;
101             object      = mComponentContext.getValueByName(STORAGE_MRG_SINGLETON);
102             XScriptStorageManager storageMgr;
103             storageMgr  = (XScriptStorageManager)UnoRuntime.queryInterface(
104                 XScriptStorageManager.class, object);
105             storageMgr.refreshScriptStorage(uri);
106         } catch (java.lang.Exception ex) {
107 System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getMessage());
108 System.out.println("*** LocalOfficeImpl.refreshStorage: FAILED " + ex.getClass().getName());
109         }
110 System.out.println("*** LocalOfficeImpl.refreshStorage: DONE");
111     }
112 
113     /**
114      * Closes the connection to the running office.
115      */
116     public void disconnect()
117     {
118 /*
119         if(mComponentFactory != null) {
120             XComponent  comp    = (XComponent)UnoRuntime.queryInterface(
121                 XComponent.class, mComponentFactory);
122             comp.dispose();
123         }
124 */
125     }
126 
127     /**
128      * Boot straps UNO.
129      *
130      * The office has to be started with following string:
131      * "-accept=socket,host=localhost,port=<PORT>;urp;StarOffice.ServiceManager"
132      *
133      * @param port is a communication port.
134      */
135     private void bootstrap(int port)
136         throws java.lang.Exception
137     {
138         Object          object;
139         mComponentContext   = Bootstrap.createInitialComponentContext(null);
140         XUnoUrlResolver urlresolver = UnoUrlResolver.create(mComponentContext);
141         object              = urlresolver.resolve(
142             "uno:socket,host=localhost,port=" +
143             port +
144             ";urp;StarOffice.ServiceManager");
145         mComponentFactory   = (XMultiComponentFactory)UnoRuntime.queryInterface(
146             XMultiComponentFactory.class, object);
147         XPropertySet    factoryProps;
148         factoryProps        = (XPropertySet)UnoRuntime.queryInterface(
149             XPropertySet.class, mComponentFactory);
150         object              = factoryProps.getPropertyValue("DefaultContext");
151         mComponentContext   = (XComponentContext)UnoRuntime.queryInterface(
152             XComponentContext.class, object);
153     }
154 }
155