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