1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.bridge; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import lib.MultiMethodTest; 27cdf0e10cSrcweir import lib.StatusException; 28cdf0e10cSrcweir import util.utils; 29cdf0e10cSrcweir 30cdf0e10cSrcweir import com.sun.star.bridge.XBridge; 31cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory; 32cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider; 33cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 34cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException; 35cdf0e10cSrcweir import com.sun.star.connection.NoConnectException; 36cdf0e10cSrcweir import com.sun.star.connection.XAcceptor; 37cdf0e10cSrcweir import com.sun.star.connection.XConnection; 38cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 39cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 41cdf0e10cSrcweir 42cdf0e10cSrcweir /** 43cdf0e10cSrcweir * Testing <code>com.sun.star.bridge.XUnoUrlResolver</code> 44cdf0e10cSrcweir * interface methods : 45cdf0e10cSrcweir * <ul> 46cdf0e10cSrcweir * <li><code> resolve()</code></li> 47cdf0e10cSrcweir * </ul> <p> 48cdf0e10cSrcweir * @see com.sun.star.bridge.XUnoUrlResolver 49cdf0e10cSrcweir */ 50cdf0e10cSrcweir public class _XUnoUrlResolver extends MultiMethodTest { 51cdf0e10cSrcweir 52cdf0e10cSrcweir // starting port and current port to choose 53cdf0e10cSrcweir static int basePort = 0; 54cdf0e10cSrcweir int curPort = 0; 55cdf0e10cSrcweir 56cdf0e10cSrcweir public XUnoUrlResolver oObj; 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** 59cdf0e10cSrcweir * Implementation for providing an instance 60cdf0e10cSrcweir * 61cdf0e10cSrcweir * @see com.sun.star.bridge.XInstanceProvider 62cdf0e10cSrcweir */ 63cdf0e10cSrcweir class MyInstanceProvider implements XInstanceProvider { 64cdf0e10cSrcweir /** 65cdf0e10cSrcweir * a MultiServiceFactory for creating instances 66cdf0e10cSrcweir * 67cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 68cdf0e10cSrcweir */ 69cdf0e10cSrcweir private XMultiServiceFactory xMSF = null; 70cdf0e10cSrcweir 71cdf0e10cSrcweir /** 72cdf0e10cSrcweir * Construct object with a MultiServiceFactory 73cdf0e10cSrcweir * 74cdf0e10cSrcweir * @see com.sun.star.lang.MultiServiceFactory 75cdf0e10cSrcweir */ MyInstanceProvider(XMultiServiceFactory xMSF)76cdf0e10cSrcweir public MyInstanceProvider(XMultiServiceFactory xMSF) { 77cdf0e10cSrcweir this.xMSF = xMSF; 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir /** 81cdf0e10cSrcweir * get an instance by name 82cdf0e10cSrcweir */ getInstance(String aInstanceName)83cdf0e10cSrcweir public Object getInstance(String aInstanceName) 84cdf0e10cSrcweir throws com.sun.star.container.NoSuchElementException 85cdf0e10cSrcweir { 86cdf0e10cSrcweir try { 87cdf0e10cSrcweir return xMSF.createInstance(aInstanceName); 88cdf0e10cSrcweir } 89cdf0e10cSrcweir catch(com.sun.star.uno.Exception e) { 90cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir } 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir /** 96cdf0e10cSrcweir * Thread for creating a bridge so the resolver can access it 97cdf0e10cSrcweir */ 98cdf0e10cSrcweir class BridgeThread extends Thread { 99cdf0e10cSrcweir private XBridgeFactory xBrdgFctr = null; 100cdf0e10cSrcweir private XInstanceProvider xInstProv = null; 101cdf0e10cSrcweir private XAcceptor xAcc = null; 102cdf0e10cSrcweir private String connectString = null; 103cdf0e10cSrcweir 104cdf0e10cSrcweir public XBridge xBridge = null; 105cdf0e10cSrcweir BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr, XInstanceProvider xInstProv, String connectString )106cdf0e10cSrcweir public BridgeThread(XAcceptor xAcc, XBridgeFactory xBrdgFctr, 107cdf0e10cSrcweir XInstanceProvider xInstProv, String connectString 108cdf0e10cSrcweir ) { 109cdf0e10cSrcweir this.xInstProv = xInstProv; 110cdf0e10cSrcweir this.xBrdgFctr = xBrdgFctr; 111cdf0e10cSrcweir this.xAcc = xAcc; 112cdf0e10cSrcweir this.connectString = connectString; 113cdf0e10cSrcweir } 114cdf0e10cSrcweir run()115cdf0e10cSrcweir public void run() { 116cdf0e10cSrcweir try { 117cdf0e10cSrcweir // create a connection 118cdf0e10cSrcweir XConnection xCon = xAcc.accept(connectString); 119cdf0e10cSrcweir // create a bridge over that conmnection 120cdf0e10cSrcweir xBridge = xBrdgFctr.createBridge( 121cdf0e10cSrcweir "MyBridge", "urp", xCon, xInstProv); 122cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 123cdf0e10cSrcweir e.printStackTrace(log); 124cdf0e10cSrcweir } catch (com.sun.star.connection.ConnectionSetupException e) { 125cdf0e10cSrcweir e.printStackTrace(log); 126cdf0e10cSrcweir } catch (com.sun.star.connection.AlreadyAcceptingException e) { 127cdf0e10cSrcweir e.printStackTrace(log); 128cdf0e10cSrcweir } catch (com.sun.star.bridge.BridgeExistsException e) { 129cdf0e10cSrcweir e.printStackTrace(log); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir } 134cdf0e10cSrcweir /** 135cdf0e10cSrcweir * Test calls the method using environment property 136cdf0e10cSrcweir * <code>'CNCSTR'</code>. <p> 137cdf0e10cSrcweir * Has <b> OK </b> status if the method successfully returns 138cdf0e10cSrcweir * object that support interface <code>XMultiServiceFactory</code> and 139cdf0e10cSrcweir * no exceptions were thrown. <p> 140cdf0e10cSrcweir * @see com.sun.star.lang.XMultiServiceFactory 141cdf0e10cSrcweir */ _resolve()142cdf0e10cSrcweir public void _resolve() { 143cdf0e10cSrcweir String connectStr = (String)tParam.get("CNCSTR"); 144cdf0e10cSrcweir int pIndex = connectStr.indexOf("port=") + 5; 145cdf0e10cSrcweir connectStr = connectStr.substring(0, pIndex); 146cdf0e10cSrcweir System.out.println("ConnectString: " + connectStr); 147cdf0e10cSrcweir 148cdf0e10cSrcweir // select the port 149cdf0e10cSrcweir basePort = ((Integer)tEnv.getObjRelation("PORT")).intValue(); 150cdf0e10cSrcweir curPort = utils.getNextFreePort(basePort); 151cdf0e10cSrcweir log.println("Choose Port nr: " + curPort); 152cdf0e10cSrcweir 153cdf0e10cSrcweir connectStr += curPort; 154cdf0e10cSrcweir 155cdf0e10cSrcweir try { 156cdf0e10cSrcweir XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF(); 157cdf0e10cSrcweir 158cdf0e10cSrcweir // get the bridge factory 159cdf0e10cSrcweir XBridgeFactory xBrdgFctr = (XBridgeFactory) 160cdf0e10cSrcweir UnoRuntime.queryInterface(XBridgeFactory.class, 161cdf0e10cSrcweir tEnv.getObjRelation("BRIDGEFACTORY")); 162cdf0e10cSrcweir 163cdf0e10cSrcweir // get the acceptor 164cdf0e10cSrcweir XAcceptor xAcc = (XAcceptor)UnoRuntime.queryInterface( 165cdf0e10cSrcweir XAcceptor.class, tEnv.getObjRelation("ACCEPTOR")); 166cdf0e10cSrcweir 167cdf0e10cSrcweir // instance provider 168cdf0e10cSrcweir XInstanceProvider xInstProv = new MyInstanceProvider(xMSF); 169cdf0e10cSrcweir // thread for providing a bridge 170cdf0e10cSrcweir BridgeThread brThread = new BridgeThread(xAcc, xBrdgFctr, 171cdf0e10cSrcweir xInstProv, connectStr); 172cdf0e10cSrcweir brThread.start(); 173cdf0e10cSrcweir 174cdf0e10cSrcweir try { 175cdf0e10cSrcweir Thread.sleep(500); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir catch(java.lang.InterruptedException e) {} 178cdf0e10cSrcweir // get an instance from the remote 179cdf0e10cSrcweir Object obj = oObj.resolve( 180cdf0e10cSrcweir "uno:" + connectStr + ";urp;com.sun.star.lang.ServiceManager"); 181cdf0e10cSrcweir // got the instance? 182cdf0e10cSrcweir XMultiServiceFactory oMSF = (XMultiServiceFactory) 183cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, obj); 184cdf0e10cSrcweir 185cdf0e10cSrcweir if (brThread.isAlive()) 186cdf0e10cSrcweir brThread.interrupt(); 187cdf0e10cSrcweir 188cdf0e10cSrcweir tRes.tested("resolve()", oMSF != null); 189cdf0e10cSrcweir } catch (NoConnectException e) { 190cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 191cdf0e10cSrcweir e.printStackTrace(log); 192cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 193cdf0e10cSrcweir } catch (ConnectionSetupException e) { 194cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 195cdf0e10cSrcweir e.printStackTrace(log); 196cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 197cdf0e10cSrcweir } catch (IllegalArgumentException e) { 198cdf0e10cSrcweir log.println("Unexpected exception thrown " + e.getMessage()); 199cdf0e10cSrcweir e.printStackTrace(log); 200cdf0e10cSrcweir throw new StatusException("Unexpected exception", e); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir } 203cdf0e10cSrcweir } 204