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