1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package ifc.bridge; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir import lib.StatusException; 32*cdf0e10cSrcweir import util.utils; 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir import com.sun.star.bridge.XBridge; 35*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory; 36*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider; 37*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 38*cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException; 39*cdf0e10cSrcweir import com.sun.star.connection.NoConnectException; 40*cdf0e10cSrcweir import com.sun.star.connection.XAcceptor; 41*cdf0e10cSrcweir import com.sun.star.connection.XConnection; 42*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 43*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir /** 47*cdf0e10cSrcweir * Testing <code>com.sun.star.bridge.XUnoUrlResolver</code> 48*cdf0e10cSrcweir * interface methods : 49*cdf0e10cSrcweir * <ul> 50*cdf0e10cSrcweir * <li><code> resolve()</code></li> 51*cdf0e10cSrcweir * </ul> <p> 52*cdf0e10cSrcweir * @see com.sun.star.bridge.XUnoUrlResolver 53*cdf0e10cSrcweir */ 54*cdf0e10cSrcweir public class _XUnoUrlResolver extends MultiMethodTest { 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir // starting port and current port to choose 57*cdf0e10cSrcweir static int basePort = 0; 58*cdf0e10cSrcweir int curPort = 0; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir public XUnoUrlResolver oObj; 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir /** 63*cdf0e10cSrcweir * Implementation for providing an instance 64*cdf0e10cSrcweir * 65*cdf0e10cSrcweir * @see com.sun.star.bridge.XInstanceProvider 66*cdf0e10cSrcweir */ 67*cdf0e10cSrcweir class MyInstanceProvider implements XInstanceProvider { 68*cdf0e10cSrcweir /** 69*cdf0e10cSrcweir * a MultiServiceFactory for creating instances 70*cdf0e10cSrcweir * 71*cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir private XMultiServiceFactory xMSF = null; 74*cdf0e10cSrcweir 75*cdf0e10cSrcweir /** 76*cdf0e10cSrcweir * Construct object with a MultiServiceFactory 77*cdf0e10cSrcweir * 78*cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 79*cdf0e10cSrcweir */ 80*cdf0e10cSrcweir public MyInstanceProvider(XMultiServiceFactory xMSF) { 81*cdf0e10cSrcweir this.xMSF = xMSF; 82*cdf0e10cSrcweir } 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir /** 85*cdf0e10cSrcweir * get an instance by name 86*cdf0e10cSrcweir */ 87*cdf0e10cSrcweir public Object getInstance(String aInstanceName) 88*cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir try { 91*cdf0e10cSrcweir return xMSF.createInstance(aInstanceName); 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 94*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir } 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir /** 100*cdf0e10cSrcweir * Thread for creating a bridge so the resolver can access it 101*cdf0e10cSrcweir */ 102*cdf0e10cSrcweir class BridgeThread extends Thread { 103*cdf0e10cSrcweir private XBridgeFactory xBrdgFctr = null; 104*cdf0e10cSrcweir private XInstanceProvider xInstProv = null; 105*cdf0e10cSrcweir private XAcceptor xAcc = null; 106*cdf0e10cSrcweir private String connectString = null; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir public XBridge xBridge = null; 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir public BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr, 111*cdf0e10cSrcweir XInstanceProvider xInstProv, String connectString 112*cdf0e10cSrcweir ) { 113*cdf0e10cSrcweir this.xInstProv = xInstProv; 114*cdf0e10cSrcweir this.xBrdgFctr = xBrdgFctr; 115*cdf0e10cSrcweir this.xAcc = xAcc; 116*cdf0e10cSrcweir this.connectString = connectString; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir public void run() { 120*cdf0e10cSrcweir try { 121*cdf0e10cSrcweir // create a connection 122*cdf0e10cSrcweir XConnection xCon = xAcc.accept(connectString); 123*cdf0e10cSrcweir // create a bridge over that conmnection 124*cdf0e10cSrcweir xBridge = xBrdgFctr.createBridge( 125*cdf0e10cSrcweir "MyBridge", "urp", xCon, xInstProv); 126*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 127*cdf0e10cSrcweir e.printStackTrace(log); 128*cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 129*cdf0e10cSrcweir e.printStackTrace(log); 130*cdf0e10cSrcweir } catch (com.sun.star.connection.AlreadyAcceptingException e) { 131*cdf0e10cSrcweir e.printStackTrace(log); 132*cdf0e10cSrcweir } catch (com.sun.star.bridge.BridgeExistsException e) { 133*cdf0e10cSrcweir e.printStackTrace(log); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir /** 139*cdf0e10cSrcweir * Test calls the method using environment property 140*cdf0e10cSrcweir * <code>'CNCSTR'</code>. <p> 141*cdf0e10cSrcweir * Has <b> OK </b> status if the method successfully returns 142*cdf0e10cSrcweir * object that support interface <code>XMultiServiceFactory</code> and 143*cdf0e10cSrcweir * no exceptions were thrown. <p> 144*cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 145*cdf0e10cSrcweir */ 146*cdf0e10cSrcweir public void _resolve() { 147*cdf0e10cSrcweir String connectStr = (String)tParam.get("CNCSTR"); 148*cdf0e10cSrcweir int pIndex = connectStr.indexOf("port=") + 5; 149*cdf0e10cSrcweir connectStr = connectStr.substring(0, pIndex); 150*cdf0e10cSrcweir System.out.println("ConnectString: " + connectStr); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir // select the port 153*cdf0e10cSrcweir basePort = ((Integer)tEnv.getObjRelation("PORT")).intValue(); 154*cdf0e10cSrcweir curPort = utils.getNextFreePort(basePort); 155*cdf0e10cSrcweir log.println("Choose Port nr: " + curPort); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir connectStr += curPort; 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir try { 160*cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // get the bridge factory 163*cdf0e10cSrcweir XBridgeFactory xBrdgFctr = (XBridgeFactory) 164*cdf0e10cSrcweir UnoRuntime.queryInterface(XBridgeFactory.class, 165*cdf0e10cSrcweir tEnv.getObjRelation("BRIDGEFACTORY")); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir // get the acceptor 168*cdf0e10cSrcweir XAcceptor xAcc = (XAcceptor)UnoRuntime.queryInterface( 169*cdf0e10cSrcweir XAcceptor.class, tEnv.getObjRelation("ACCEPTOR")); 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir // instance provider 172*cdf0e10cSrcweir XInstanceProvider xInstProv = new MyInstanceProvider(xMSF); 173*cdf0e10cSrcweir // thread for providing a bridge 174*cdf0e10cSrcweir BridgeThread brThread = new BridgeThread(xAcc, xBrdgFctr, 175*cdf0e10cSrcweir xInstProv, connectStr); 176*cdf0e10cSrcweir brThread.start(); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir try { 179*cdf0e10cSrcweir Thread.sleep(500); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir catch(java.lang.InterruptedException e) {} 182*cdf0e10cSrcweir // get an instance from the remote 183*cdf0e10cSrcweir Object obj = oObj.resolve( 184*cdf0e10cSrcweir "uno:" + connectStr + ";urp;com.sun.star.lang.ServiceManager"); 185*cdf0e10cSrcweir // got the instance? 186*cdf0e10cSrcweir XMultiServiceFactory oMSF = (XMultiServiceFactory) 187*cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, obj); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir if (brThread.isAlive()) 190*cdf0e10cSrcweir brThread.interrupt(); 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir tRes.tested("resolve()", oMSF != null); 193*cdf0e10cSrcweir } catch (NoConnectException e) { 194*cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 195*cdf0e10cSrcweir e.printStackTrace(log); 196*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 197*cdf0e10cSrcweir } catch (ConnectionSetupException e) { 198*cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 199*cdf0e10cSrcweir e.printStackTrace(log); 200*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 201*cdf0e10cSrcweir } catch (IllegalArgumentException e) { 202*cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 203*cdf0e10cSrcweir e.printStackTrace(log); 204*cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir } 208