xref: /trunk/test/testuno/source/org/openoffice/test/uno/UnoApp.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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 package org.openoffice.test.uno;
23 
24 import java.io.File;
25 import java.util.Timer;
26 
27 import org.openoffice.test.OpenOffice;
28 import org.openoffice.test.common.FileUtil;
29 import org.openoffice.test.common.SystemUtil;
30 
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.bridge.UnoUrlResolver;
33 import com.sun.star.bridge.XUnoUrlResolver;
34 import com.sun.star.comp.helper.Bootstrap;
35 import com.sun.star.frame.XComponentLoader;
36 import com.sun.star.frame.XDesktop;
37 import com.sun.star.frame.XStorable;
38 import com.sun.star.lang.XComponent;
39 import com.sun.star.lang.XMultiComponentFactory;
40 import com.sun.star.lang.XMultiServiceFactory;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XComponentContext;
43 import com.sun.star.util.XCloseable;
44 import com.sun.star.util.XModifiable;
45 
46 public class UnoApp {
47 
48     private OpenOffice openOffice = null;
49 
50     private String unoUrl = null;
51 
52     private XComponentContext componentContext = null;
53 
54     private XMultiComponentFactory componentFactory = null;
55 
56     private XMultiServiceFactory serviceFactory = null;
57 
58     private XDesktop desktop = null;
59 
60     private double reconnectInterval = 2;
61 
62     private int reconnectCount = 10;
63 
getDefaultOpenOffice()64     public static OpenOffice getDefaultOpenOffice() {
65         OpenOffice openOffice = new OpenOffice();
66         openOffice.addArgs("-nofirststartwizard", "-norestore", "-quickstart=no");
67         openOffice.setUnoUrl(OpenOffice.DEFAULT_UNO_URL);
68                 return openOffice;
69     }
70 
UnoApp()71     public UnoApp() {
72         this.openOffice = getDefaultOpenOffice();
73     }
74 
UnoApp(OpenOffice openOffice)75     public UnoApp(OpenOffice openOffice) {
76         this.openOffice = openOffice;
77     }
78 
UnoApp(String unoUrl)79     public UnoApp(String unoUrl) {
80         this.unoUrl = unoUrl;
81     }
82 
83     /**
84      * Start OpenOffice and connect to it
85      */
start()86     public void start() {
87         if (componentContext != null) {
88             try {
89                 componentContext.getServiceManager();
90                 return;
91             } catch (Exception e) {
92 
93             }
94         }
95 
96         if (openOffice != null) {
97             openOffice.start();
98             unoUrl = openOffice.getUnoUrl();
99         }
100 
101         for (int i = 0; i < reconnectCount; i++) {
102             try {
103                 XUnoUrlResolver resolver = UnoUrlResolver.create(Bootstrap.createInitialComponentContext(null));
104                 componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, resolver.resolve("uno:" + unoUrl + ";StarOffice.ComponentContext"));
105                 componentFactory = componentContext.getServiceManager();
106                 serviceFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, componentFactory);
107                 desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, serviceFactory.createInstance("com.sun.star.comp.framework.Desktop"));
108                 return;
109             } catch (Exception e) {
110                 // e.printStackTrace(); // for debugging
111             }
112 
113             SystemUtil.sleep(reconnectInterval);
114         }
115 
116         throw new RuntimeException("Failed to connect to uno url: " + unoUrl);
117     }
118 
119     private Timer timer = new Timer(true);
120 
121     /**
122      * Shut down the connection and close OpenOffice
123      */
close()124     public void close() {
125         try {
126             desktop.terminate();
127             if (openOffice != null) {
128                 // Wait 5 seconds for exit, checking every 500 ms:
129                 for (int i = 0; i < 10; i++) {
130                     if (!openOffice.isRunning())
131                         break;
132                     Thread.sleep(500);
133                 }
134             }
135         } catch (Exception e) {
136             // e.printStackTrace(); // for debugging
137         } finally {
138             if (openOffice != null && openOffice.isRunning())
139                 openOffice.kill();
140 
141             componentContext = null;
142             componentFactory = null;
143             serviceFactory = null;
144             desktop = null;
145         }
146     }
147 
148     /**
149      * Get the XComponentContext of the connected OpenOffice instance
150      *
151      * @return
152      */
getComponentContext()153     public XComponentContext getComponentContext() {
154         return componentContext;
155     }
156 
getComponentFactory()157     public XMultiComponentFactory getComponentFactory() {
158         return componentFactory;
159     }
160 
getServiceFactory()161     public XMultiServiceFactory getServiceFactory() {
162         return serviceFactory;
163     }
164 
getDesktop()165     public XDesktop getDesktop() {
166         return desktop;
167     }
168 
169 //  public XComponent loadDocument(String file) throws Exception {
170 //      XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
171 //      return componentLoader.loadComponentFromURL(FileUtil.getUrl(file), "_blank", 0, new PropertyValue[0]);
172 //  }
173 
loadDocument(String file, PropertyValue... propertyValue)174     public XComponent loadDocument(String file, PropertyValue... propertyValue) throws Exception {
175         XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
176         return componentLoader.loadComponentFromURL(FileUtil.getUrl(file), "_blank", 0, propertyValue);
177     }
178 
179 //  public XComponent loadDocumentFromURL(String url) throws Exception {
180 //      XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
181 //      return componentLoader.loadComponentFromURL(url, "_blank", 0, new PropertyValue[0]);
182 //  }
183 
loadDocumentFromURL(String url, PropertyValue... propertyValue)184     public XComponent loadDocumentFromURL(String url, PropertyValue... propertyValue) throws Exception {
185         XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
186         return componentLoader.loadComponentFromURL(url, "_blank", 0, propertyValue);
187     }
188 
newDocument(String type)189     public XComponent newDocument(String type) throws Exception {
190         XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop);
191         return componentLoader.loadComponentFromURL("private:factory/" + type, "_blank", 0, new PropertyValue[0]);
192     }
193 
194 //  public void saveDocument(XComponent doc, String toPath) throws Exception {
195 //      XStorable m_xstorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, doc);
196 //      String fileUrl = FileUtil.getUrl(new File(toPath));
197 //      m_xstorable.storeAsURL(fileUrl, new PropertyValue[0]);
198 //  }
199 
saveDocument(XComponent doc, String toPath, PropertyValue... propertyValue)200     public void saveDocument(XComponent doc, String toPath, PropertyValue... propertyValue) throws Exception {
201         XStorable m_xstorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, doc);
202         String fileUrl = FileUtil.getUrl(new File(toPath));
203         m_xstorable.storeToURL(fileUrl, propertyValue);
204     }
205 
saveDocumentToURL(XComponent doc, String toURL, PropertyValue... propertyValue)206     public void saveDocumentToURL(XComponent doc, String toURL, PropertyValue... propertyValue) throws Exception {
207         XStorable m_xstorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, doc);
208         m_xstorable.storeToURL(toURL, propertyValue);
209     }
210 
closeDocument(XComponent doc)211     public void closeDocument(XComponent doc) {
212         try {
213             XModifiable modified = (XModifiable) UnoRuntime.queryInterface(XModifiable.class, doc);
214             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
215             if (modified != null)
216                 modified.setModified(false);
217             closer.close(true);
218         } catch (Exception e) {
219             // ignore
220         }
221     }
222 }
223