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 mod._remotebridge; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 37 import com.sun.star.bridge.XBridgeFactory; 38 import com.sun.star.bridge.XInstanceProvider; 39 import com.sun.star.connection.XAcceptor; 40 import com.sun.star.connection.XConnection; 41 import com.sun.star.connection.XConnector; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.uno.Exception; 45 import com.sun.star.uno.UnoRuntime; 46 import com.sun.star.uno.XInterface; 47 48 /** 49 * Test for object which is represented by service 50 * <code>com.sun.star.bridge.Bridge</code>. <p> 51 * Object implements the following interfaces : 52 * <ul> 53 * <li> <code>com::sun::star::lang::XInitialization</code></li> 54 * <li> <code>com::sun::star::lang::XComponent</code></li> 55 * <li> <code>com::sun::star::bridge::XBridge</code></li> 56 * </ul> 57 * This object test <b> is NOT </b> designed to be run in several 58 * threads concurently. 59 * @see com.sun.star.lang.XInitialization 60 * @see com.sun.star.lang.XComponent 61 * @see com.sun.star.bridge.XBridge 62 * @see com.sun.star.bridge.Bridge 63 * @see ifc.lang._XInitialization 64 * @see ifc.lang._XComponent 65 * @see ifc.bridge._XBridge 66 */ 67 public class various extends TestCase { 68 69 /** 70 * String for establishing a connection 71 */ 72 protected String connectString = null ; 73 74 /** 75 * Choose the first port after <code>basePort</code> 76 * which is free. 77 */ 78 protected static final int basePort = 50000; 79 private int curPort = 50000; 80 81 private XAcceptor xAcctr; 82 private XConnector xCntr; 83 private XBridgeFactory xBrdgFctr; 84 private AcceptorThread accThread; 85 86 public XInterface bridge = null; 87 88 /** 89 * Implementation of interface XInstanceProvider 90 * 91 * @see com.sun.star.bridge.XInstanceProvider 92 */ 93 protected class MyInstanceProvider implements XInstanceProvider { 94 /** 95 * a MultiServiceFactory for creating instances 96 * 97 * @see com.sun.star.lang.MultiServiceFactory 98 */ 99 private XMultiServiceFactory xMSF = null; 100 101 /** 102 * Construct object with a MultiServiceFactory 103 * 104 * @see com.sun.star.lang.MultiServiceFactory 105 */ 106 public MyInstanceProvider(XMultiServiceFactory xMSF) { 107 this.xMSF = xMSF; 108 } 109 110 /** 111 * get an instance by name 112 */ 113 public Object getInstance(String aInstanceName) 114 throws com.sun.star.container.NoSuchElementException 115 { 116 System.out.println("######## Try to get "+aInstanceName); 117 try { 118 return xMSF.createInstance(aInstanceName); 119 } 120 catch(com.sun.star.uno.Exception e) { 121 throw new StatusException("Unexpected exception", e); 122 } 123 } 124 } 125 126 /** 127 * Calls <code>accept()</code> method in a separate thread. 128 * Then stores exception thrown by call if it occured, or 129 * return value. 130 */ 131 protected class AcceptorThread extends Thread { 132 /** 133 * If exception occured during method call it is 134 * stored in this field. 135 */ 136 public Exception ex = null ; 137 private XAcceptor acc = null ; 138 private XInstanceProvider xInstProv = null ; 139 private XBridgeFactory xBrdgFctr = null; 140 /** 141 * If method call returns some value it stores in this field. 142 */ 143 public XConnection acceptedCall = null ; 144 145 /** 146 * Creates object which can call <code>accept</code> method 147 * of the Acceptor object specified. 148 */ 149 public AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv, 150 XBridgeFactory xBrdgFctr) { 151 this.acc = acc ; 152 this.xInstProv = xInstProv; 153 this.xBrdgFctr = xBrdgFctr; 154 } 155 156 /** 157 * Call <code>accept()</code> method and establish a bridge with an 158 * instance provider 159 */ 160 public void run() { 161 try { 162 acceptedCall = acc.accept(connectString) ; 163 } catch (com.sun.star.lang.IllegalArgumentException e) { 164 ex = e ; 165 } catch (com.sun.star.connection.ConnectionSetupException e) { 166 ex = e ; 167 } catch (com.sun.star.connection.AlreadyAcceptingException e) { 168 ex = e ; 169 } 170 } 171 } 172 173 private final boolean[] bridgeDisposed = new boolean[1] ; 174 175 /** 176 * Creating a Testenvironment for the interfaces to be tested. 177 * Creates an instance of the service 178 * <code>com.sun.star.bridge.Bridge</code>. 179 * Object relations created : 180 * <ul> 181 * <li> <code>'XInitialization.args'</code> for 182 * {@link ifc.lang._XInitialization} and 183 * {@link ifc.bridge._XBridge} : contains arguments 184 * for <code>initialize()</code> method test.</li> 185 * </ul> 186 */ 187 protected TestEnvironment createTestEnvironment(TestParameters tParam, 188 PrintWriter log) { 189 XMultiServiceFactory xMSF = (XMultiServiceFactory) tParam.getMSF(); 190 191 try { 192 XInterface xInt = (XInterface)xMSF.createInstance( 193 "com.sun.star.bridge.Bridge"); 194 195 TestEnvironment tEnv = new TestEnvironment(xInt); 196 // creating arguments for XInitialization 197 // first, creating a connection 198 // connection string 199 String cncstr = (String) tParam.get("CNCSTR") ; 200 int idx = cncstr.indexOf("host=") + 5 ; 201 202 // select the port 203 // curPort; //utils.getNextFreePort(basePort); 204 log.println("Choose Port nr: " + curPort); 205 206 connectString = "socket,host=" + 207 cncstr.substring(idx, cncstr.indexOf(",", idx)) + 208 ",port=" + curPort; 209 210 // create acceptor 211 XInterface oAcctr = (XInterface)xMSF.createInstance( 212 "com.sun.star.connection.Acceptor") ; 213 214 xAcctr = (XAcceptor)UnoRuntime.queryInterface( 215 XAcceptor.class, oAcctr); 216 // create connector 217 XInterface oCntr = (XInterface)xMSF.createInstance( 218 "com.sun.star.connection.Connector") ; 219 xCntr = (XConnector)UnoRuntime.queryInterface( 220 XConnector.class, oCntr); 221 222 // create bridge factory 223 XInterface oBrdg = (XInterface)xMSF.createInstance( 224 "com.sun.star.bridge.BridgeFactory") ; 225 xBrdgFctr = (XBridgeFactory) 226 UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg); 227 228 // create own implementation of XInstanceProvider 229 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF); 230 // create waiting acceptor thread 231 accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr); 232 accThread.start(); 233 // let the thread sleep 234 try { 235 Thread.sleep(500); 236 } 237 catch (java.lang.InterruptedException e) {} 238 239 // establish the connection 240 XConnection xConnection = xCntr.connect(connectString); 241 242 String protocol = "urp"; 243 String bridgeName = protocol + ":" + connectString; 244 245 /* bridgeDisposed[0] = false ; 246 XComponent xComp = (XComponent)UnoRuntime.queryInterface( 247 XComponent.class, xInt); 248 final PrintWriter logF = log; 249 xComp.addEventListener(new XEventListener() { 250 public void disposing(EventObject ev) { 251 bridgeDisposed[0] = true ; 252 logF.println("The bridge Disposed."); 253 } 254 }); 255 */ 256 tEnv.addObjRelation("XInitialization.args", new Object[] { 257 bridgeName, protocol, xConnection, null}); 258 259 bridge = tEnv.getTestObject(); 260 261 return tEnv; 262 } catch (com.sun.star.uno.Exception e) { 263 e.printStackTrace(log); 264 throw new StatusException("Unexpected exception", e); 265 } 266 } 267 268 /** 269 * Stop the acceptor thread and dispose the bridge 270 */ 271 protected void cleanup(TestParameters Param, PrintWriter log) { 272 273 System.out.println("++++++++ cleanup"); 274 xAcctr.stopAccepting(); 275 if (accThread.isAlive()) { 276 accThread.interrupt(); 277 } 278 XComponent xComp = (XComponent)UnoRuntime.queryInterface( 279 XComponent.class, xAcctr); 280 if (xComp != null) 281 xComp.dispose(); 282 xComp = (XComponent)UnoRuntime.queryInterface( 283 XComponent.class, xCntr); 284 if (xComp != null) 285 xComp.dispose(); 286 xComp = (XComponent)UnoRuntime.queryInterface( 287 XComponent.class, xBrdgFctr); 288 if (xComp != null) 289 xComp.dispose(); 290 291 xComp = (XComponent)UnoRuntime.queryInterface( 292 XComponent.class, bridge); 293 if (xComp != null) { 294 System.out.println("######## Dispose bridge"); 295 bridgeDisposed[0] = true; 296 xComp.dispose(); 297 // wait for dispose 298 try { 299 Thread.sleep(5000); 300 } 301 catch(java.lang.InterruptedException e) { 302 } 303 } 304 } 305 } 306