2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod
._remotebridge
.uno
;
21 import com
.sun
.star
.bridge
.XBridgeFactory
;
22 import com
.sun
.star
.bridge
.XInstanceProvider
;
23 import com
.sun
.star
.connection
.XAcceptor
;
24 import com
.sun
.star
.connection
.XConnection
;
25 import com
.sun
.star
.connection
.XConnector
;
26 import com
.sun
.star
.lang
.XComponent
;
27 import com
.sun
.star
.lang
.XMultiServiceFactory
;
28 import com
.sun
.star
.uno
.Exception
;
29 import com
.sun
.star
.uno
.UnoRuntime
;
30 import com
.sun
.star
.uno
.XInterface
;
31 import java
.io
.PrintWriter
;
32 import lib
.StatusException
;
34 import lib
.TestEnvironment
;
35 import lib
.TestParameters
;
39 * Test for object which is represented by service
40 * <code>com.sun.star.bridge.Bridge</code>. <p>
41 * Object implements the following interfaces :
43 * <li> <code>com::sun::star::lang::XInitialization</code></li>
44 * <li> <code>com::sun::star::lang::XComponent</code></li>
45 * <li> <code>com::sun::star::bridge::XBridge</code></li>
47 * This object test <b> is NOT </b> designed to be run in several
48 * threads concurently.
49 * @see com.sun.star.lang.XInitialization
50 * @see com.sun.star.lang.XComponent
51 * @see com.sun.star.bridge.XBridge
52 * @see com.sun.star.bridge.Bridge
53 * @see ifc.lang._XInitialization
54 * @see ifc.lang._XComponent
55 * @see ifc.bridge._XBridge
57 public class various
extends TestCase
{
60 * String for establishing a connection
62 protected String connectString
= null ;
65 * Choose the first port after <code>basePort</code>
68 protected static final int basePort
= 50000;
69 private int curPort
= 50000;
71 private XAcceptor xAcctr
;
72 private XConnector xCntr
;
73 private XBridgeFactory xBrdgFctr
;
74 private AcceptorThread accThread
;
76 public XInterface bridge
= null;
79 * Implementation of interface XInstanceProvider
81 * @see com.sun.star.bridge.XInstanceProvider
83 protected class MyInstanceProvider
implements XInstanceProvider
{
85 * a MultiServiceFactory for creating instances
87 * @see com.sun.star.lang.MultiServiceFactory
89 private XMultiServiceFactory xMSF
= null;
92 * Construct object with a MultiServiceFactory
94 * @see com.sun.star.lang.MultiServiceFactory
96 public MyInstanceProvider(XMultiServiceFactory xMSF
) {
101 * get an instance by name
103 public Object
getInstance(String aInstanceName
)
104 throws com
.sun
.star
.container
.NoSuchElementException
106 System
.out
.println("######## Try to get "+aInstanceName
);
108 return xMSF
.createInstance(aInstanceName
);
110 catch(com
.sun
.star
.uno
.Exception e
) {
111 throw new StatusException("Unexpected exception", e
);
117 * Calls <code>accept()</code> method in a separate thread.
118 * Then stores exception thrown by call if it occurred, or
121 protected class AcceptorThread
extends Thread
{
123 * If exception occurred during method call it is
124 * stored in this field.
126 public Exception ex
= null ;
127 private XAcceptor acc
= null ;
128 private XInstanceProvider xInstProv
= null ;
129 private XBridgeFactory xBrdgFctr
= null;
131 * If method call returns some value it stores in this field.
133 public XConnection acceptedCall
= null ;
136 * Creates object which can call <code>accept</code> method
137 * of the Acceptor object specified.
139 public AcceptorThread(XAcceptor acc
, XInstanceProvider xInstProv
,
140 XBridgeFactory xBrdgFctr
) {
142 this.xInstProv
= xInstProv
;
143 this.xBrdgFctr
= xBrdgFctr
;
147 * Call <code>accept()</code> method and establish a bridge with an
152 acceptedCall
= acc
.accept(connectString
) ;
153 xBrdgFctr
.createBridge("MyBridge", "urp",
154 acceptedCall
, xInstProv
);
155 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
157 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
159 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
165 private final boolean[] bridgeDisposed
= new boolean[1] ;
168 * Creating a Testenvironment for the interfaces to be tested.
169 * Creates an instance of the service
170 * <code>com.sun.star.bridge.Bridge</code>.
171 * Object relations created :
173 * <li> <code>'XInitialization.args'</code> for
174 * {@link ifc.lang._XInitialization} and
175 * {@link ifc.bridge._XBridge} : contains arguments
176 * for <code>initialize()</code> method test.</li>
179 protected TestEnvironment
createTestEnvironment(TestParameters tParam
,
181 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
184 XInterface xInt
= (XInterface
)xMSF
.createInstance(
185 "com.sun.star.bridge.Bridge");
187 TestEnvironment tEnv
= new TestEnvironment(xInt
);
188 // creating arguments for XInitialization
189 // first, creating a connection
191 String cncstr
= (String
) tParam
.get("CNCSTR") ;
192 int idx
= cncstr
.indexOf("host=") + 5 ;
195 // curPort; //utils.getNextFreePort(basePort);
196 log
.println("Choose Port nr: " + curPort
);
198 connectString
= "socket,host=" +
199 cncstr
.substring(idx
, cncstr
.indexOf(",", idx
)) +
203 XInterface oAcctr
= (XInterface
)xMSF
.createInstance(
204 "com.sun.star.connection.Acceptor") ;
206 xAcctr
= UnoRuntime
.queryInterface(
207 XAcceptor
.class, oAcctr
);
209 XInterface oCntr
= (XInterface
)xMSF
.createInstance(
210 "com.sun.star.connection.Connector") ;
211 xCntr
= UnoRuntime
.queryInterface(
212 XConnector
.class, oCntr
);
214 // create bridge factory
215 XInterface oBrdg
= (XInterface
)xMSF
.createInstance(
216 "com.sun.star.bridge.BridgeFactory") ;
217 xBrdgFctr
= UnoRuntime
.queryInterface(XBridgeFactory
.class, oBrdg
);
219 // create own implementation of XInstanceProvider
220 XInstanceProvider xInstProv
= new MyInstanceProvider(xMSF
);
221 // create waiting acceptor thread
222 accThread
= new AcceptorThread(xAcctr
, xInstProv
, xBrdgFctr
);
224 // let the thread sleep
228 catch (java
.lang
.InterruptedException e
) {}
230 // establish the connection
231 XConnection xConnection
= xCntr
.connect(connectString
);
233 String protocol
= "urp";
234 String bridgeName
= protocol
+ ":" + connectString
;
236 /* bridgeDisposed[0] = false ;
237 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
238 XComponent.class, xInt);
239 final PrintWriter logF = log;
240 xComp.addEventListener(new XEventListener() {
241 public void disposing(EventObject ev) {
242 bridgeDisposed[0] = true ;
243 logF.println("The bridge Disposed.");
247 tEnv
.addObjRelation("XInitialization.args", new Object
[] {
248 bridgeName
, protocol
, xConnection
, null});
250 bridge
= tEnv
.getTestObject();
253 } catch (com
.sun
.star
.uno
.Exception e
) {
254 e
.printStackTrace(log
);
255 throw new StatusException("Unexpected exception", e
);
260 * Stop the acceptor thread and dispose the bridge
262 protected void cleanup(TestParameters Param
, PrintWriter log
) {
264 System
.out
.println("++++++++ cleanup");
265 xAcctr
.stopAccepting();
266 if (accThread
.isAlive()) {
267 accThread
.interrupt();
269 XComponent xComp
= UnoRuntime
.queryInterface(
270 XComponent
.class, xAcctr
);
273 xComp
= UnoRuntime
.queryInterface(
274 XComponent
.class, xCntr
);
277 xComp
= UnoRuntime
.queryInterface(
278 XComponent
.class, xBrdgFctr
);
282 xComp
= UnoRuntime
.queryInterface(
283 XComponent
.class, bridge
);
285 System
.out
.println("######## Dispose bridge");
286 bridgeDisposed
[0] = true;
292 catch(java
.lang
.InterruptedException e
) {