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