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