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 ifc.bridge; 29 30 import lib.MultiMethodTest; 31 import lib.StatusException; 32 import util.utils; 33 34 import com.sun.star.bridge.XBridge; 35 import com.sun.star.bridge.XBridgeFactory; 36 import com.sun.star.bridge.XInstanceProvider; 37 import com.sun.star.bridge.XUnoUrlResolver; 38 import com.sun.star.connection.ConnectionSetupException; 39 import com.sun.star.connection.NoConnectException; 40 import com.sun.star.connection.XAcceptor; 41 import com.sun.star.connection.XConnection; 42 import com.sun.star.lang.IllegalArgumentException; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.uno.UnoRuntime; 45 46 /** 47 * Testing <code>com.sun.star.bridge.XUnoUrlResolver</code> 48 * interface methods : 49 * <ul> 50 * <li><code> resolve()</code></li> 51 * </ul> <p> 52 * @see com.sun.star.bridge.XUnoUrlResolver 53 */ 54 public class _XUnoUrlResolver extends MultiMethodTest { 55 56 // starting port and current port to choose 57 static int basePort = 0; 58 int curPort = 0; 59 60 public XUnoUrlResolver oObj; 61 62 /** 63 * Implementation for providing an instance 64 * 65 * @see com.sun.star.bridge.XInstanceProvider 66 */ 67 class MyInstanceProvider implements XInstanceProvider { 68 /** 69 * a MultiServiceFactory for creating instances 70 * 71 * @see com.sun.star.lang.MultiServiceFactory 72 */ 73 private XMultiServiceFactory xMSF = null; 74 75 /** 76 * Construct object with a MultiServiceFactory 77 * 78 * @see com.sun.star.lang.MultiServiceFactory 79 */ 80 public MyInstanceProvider(XMultiServiceFactory xMSF) { 81 this.xMSF = xMSF; 82 } 83 84 /** 85 * get an instance by name 86 */ 87 public Object getInstance(String aInstanceName) 88 throws com.sun.star.container.NoSuchElementException 89 { 90 try { 91 return xMSF.createInstance(aInstanceName); 92 } 93 catch(com.sun.star.uno.Exception e) { 94 throw new StatusException("Unexpected exception", e); 95 } 96 } 97 } 98 99 /** 100 * Thread for creating a bridge so the resolver can access it 101 */ 102 class BridgeThread extends Thread { 103 private XBridgeFactory xBrdgFctr = null; 104 private XInstanceProvider xInstProv = null; 105 private XAcceptor xAcc = null; 106 private String connectString = null; 107 108 public XBridge xBridge = null; 109 110 public BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr, 111 XInstanceProvider xInstProv, String connectString 112 ) { 113 this.xInstProv = xInstProv; 114 this.xBrdgFctr = xBrdgFctr; 115 this.xAcc = xAcc; 116 this.connectString = connectString; 117 } 118 119 public void run() { 120 try { 121 // create a connection 122 XConnection xCon = xAcc.accept(connectString); 123 // create a bridge over that conmnection 124 xBridge = xBrdgFctr.createBridge( 125 "MyBridge", "urp", xCon, xInstProv); 126 } catch (com.sun.star.lang.IllegalArgumentException e) { 127 e.printStackTrace(log); 128 } catch (com.sun.star.connection.ConnectionSetupException e) { 129 e.printStackTrace(log); 130 } catch (com.sun.star.connection.AlreadyAcceptingException e) { 131 e.printStackTrace(log); 132 } catch (com.sun.star.bridge.BridgeExistsException e) { 133 e.printStackTrace(log); 134 } 135 } 136 137 } 138 /** 139 * Test calls the method using environment property 140 * <code>'CNCSTR'</code>. <p> 141 * Has <b> OK </b> status if the method successfully returns 142 * object that support interface <code>XMultiServiceFactory</code> and 143 * no exceptions were thrown. <p> 144 * @see com.sun.star.lang.XMultiServiceFactory 145 */ 146 public void _resolve() { 147 String connectStr = (String)tParam.get("CNCSTR"); 148 int pIndex = connectStr.indexOf("port=") + 5; 149 connectStr = connectStr.substring(0, pIndex); 150 System.out.println("ConnectString: " + connectStr); 151 152 // select the port 153 basePort = ((Integer)tEnv.getObjRelation("PORT")).intValue(); 154 curPort = utils.getNextFreePort(basePort); 155 log.println("Choose Port nr: " + curPort); 156 157 connectStr += curPort; 158 159 try { 160 XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 161 162 // get the bridge factory 163 XBridgeFactory xBrdgFctr = (XBridgeFactory) 164 UnoRuntime.queryInterface(XBridgeFactory.class, 165 tEnv.getObjRelation("BRIDGEFACTORY")); 166 167 // get the acceptor 168 XAcceptor xAcc = (XAcceptor)UnoRuntime.queryInterface( 169 XAcceptor.class, tEnv.getObjRelation("ACCEPTOR")); 170 171 // instance provider 172 XInstanceProvider xInstProv = new MyInstanceProvider(xMSF); 173 // thread for providing a bridge 174 BridgeThread brThread = new BridgeThread(xAcc, xBrdgFctr, 175 xInstProv, connectStr); 176 brThread.start(); 177 178 try { 179 Thread.sleep(500); 180 } 181 catch(java.lang.InterruptedException e) {} 182 // get an instance from the remote 183 Object obj = oObj.resolve( 184 "uno:" + connectStr + ";urp;com.sun.star.lang.ServiceManager"); 185 // got the instance? 186 XMultiServiceFactory oMSF = (XMultiServiceFactory) 187 UnoRuntime.queryInterface(XMultiServiceFactory.class, obj); 188 189 if (brThread.isAlive()) 190 brThread.interrupt(); 191 192 tRes.tested("resolve()", oMSF != null); 193 } catch (NoConnectException e) { 194 log.println("Unexpected exception thrown " + e.getMessage()); 195 e.printStackTrace(log); 196 throw new StatusException("Unexpected exception", e); 197 } catch (ConnectionSetupException e) { 198 log.println("Unexpected exception thrown " + e.getMessage()); 199 e.printStackTrace(log); 200 throw new StatusException("Unexpected exception", e); 201 } catch (IllegalArgumentException e) { 202 log.println("Unexpected exception thrown " + e.getMessage()); 203 e.printStackTrace(log); 204 throw new StatusException("Unexpected exception", e); 205 } 206 } 207 } 208