1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: various.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package mod
._remotebridge
.uno
;
33 import com
.sun
.star
.bridge
.XBridge
;
34 import com
.sun
.star
.bridge
.XBridgeFactory
;
35 import com
.sun
.star
.bridge
.XInstanceProvider
;
36 import com
.sun
.star
.connection
.XAcceptor
;
37 import com
.sun
.star
.connection
.XConnection
;
38 import com
.sun
.star
.connection
.XConnector
;
39 import com
.sun
.star
.lang
.XComponent
;
40 import com
.sun
.star
.lang
.XMultiServiceFactory
;
41 import com
.sun
.star
.uno
.Exception
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.uno
.XInterface
;
44 import java
.io
.PrintWriter
;
45 import lib
.StatusException
;
47 import lib
.TestEnvironment
;
48 import lib
.TestParameters
;
52 * Test for object which is represented by service
53 * <code>com.sun.star.bridge.Bridge</code>. <p>
54 * Object implements the following interfaces :
56 * <li> <code>com::sun::star::lang::XInitialization</code></li>
57 * <li> <code>com::sun::star::lang::XComponent</code></li>
58 * <li> <code>com::sun::star::bridge::XBridge</code></li>
60 * This object test <b> is NOT </b> designed to be run in several
61 * threads concurently.
62 * @see com.sun.star.lang.XInitialization
63 * @see com.sun.star.lang.XComponent
64 * @see com.sun.star.bridge.XBridge
65 * @see com.sun.star.bridge.Bridge
66 * @see ifc.lang._XInitialization
67 * @see ifc.lang._XComponent
68 * @see ifc.bridge._XBridge
70 public class various
extends TestCase
{
73 * String for establishing a connection
75 protected String connectString
= null ;
78 * Choose the first port after <code>basePort</code>
81 protected static final int basePort
= 50000;
82 private int curPort
= 50000;
84 private XAcceptor xAcctr
;
85 private XConnector xCntr
;
86 private XBridgeFactory xBrdgFctr
;
87 private AcceptorThread accThread
;
89 public XInterface bridge
= null;
92 * Implementation of interface XInstanceProvider
94 * @see com.sun.star.bridge.XInstanceProvider
96 protected class MyInstanceProvider
implements XInstanceProvider
{
98 * a MultiServiceFactory for creating instances
100 * @see com.sun.star.lang.MultiServiceFactory
102 private XMultiServiceFactory xMSF
= null;
105 * Construct object with a MultiServiceFactory
107 * @see com.sun.star.lang.MultiServiceFactory
109 public MyInstanceProvider(XMultiServiceFactory xMSF
) {
114 * get an instance by name
116 public Object
getInstance(String aInstanceName
)
117 throws com
.sun
.star
.container
.NoSuchElementException
119 System
.out
.println("######## Try to get "+aInstanceName
);
121 return xMSF
.createInstance(aInstanceName
);
123 catch(com
.sun
.star
.uno
.Exception e
) {
124 throw new StatusException("Unexpected exception", e
);
130 * Calls <code>accept()</code> method in a separate thread.
131 * Then stores exception thrown by call if it occured, or
134 protected class AcceptorThread
extends Thread
{
136 * If exception occured during method call it is
137 * stored in this field.
139 public Exception ex
= null ;
140 private XAcceptor acc
= null ;
141 private XInstanceProvider xInstProv
= null ;
142 private XBridgeFactory xBrdgFctr
= null;
144 * If method call returns some value it stores in this field.
146 public XConnection acceptedCall
= null ;
149 * Creates object which can call <code>accept</code> method
150 * of the Acceptor object specified.
152 public AcceptorThread(XAcceptor acc
, XInstanceProvider xInstProv
,
153 XBridgeFactory xBrdgFctr
) {
155 this.xInstProv
= xInstProv
;
156 this.xBrdgFctr
= xBrdgFctr
;
160 * Call <code>accept()</code> method and establish a bridge with an
165 acceptedCall
= acc
.accept(connectString
) ;
166 XBridge xBridge
= xBrdgFctr
.createBridge("MyBridge", "urp",
167 acceptedCall
, xInstProv
);
168 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
170 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
172 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
174 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
180 private final boolean[] bridgeDisposed
= new boolean[1] ;
183 * Creating a Testenvironment for the interfaces to be tested.
184 * Creates an instance of the service
185 * <code>com.sun.star.bridge.Bridge</code>.
186 * Object relations created :
188 * <li> <code>'XInitialization.args'</code> for
189 * {@link ifc.lang._XInitialization} and
190 * {@link ifc.bridge._XBridge} : contains arguments
191 * for <code>initialize()</code> method test.</li>
194 protected TestEnvironment
createTestEnvironment(TestParameters tParam
,
196 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
199 XInterface xInt
= (XInterface
)xMSF
.createInstance(
200 "com.sun.star.bridge.Bridge");
202 TestEnvironment tEnv
= new TestEnvironment(xInt
);
203 // creating arguments for XInitialization
204 // first, creating a connection
206 String cncstr
= (String
) tParam
.get("CNCSTR") ;
207 int idx
= cncstr
.indexOf("host=") + 5 ;
210 // curPort; //utils.getNextFreePort(basePort);
211 log
.println("Choose Port nr: " + curPort
);
213 connectString
= "socket,host=" +
214 cncstr
.substring(idx
, cncstr
.indexOf(",", idx
)) +
218 XInterface oAcctr
= (XInterface
)xMSF
.createInstance(
219 "com.sun.star.connection.Acceptor") ;
221 xAcctr
= (XAcceptor
)UnoRuntime
.queryInterface(
222 XAcceptor
.class, oAcctr
);
224 XInterface oCntr
= (XInterface
)xMSF
.createInstance(
225 "com.sun.star.connection.Connector") ;
226 xCntr
= (XConnector
)UnoRuntime
.queryInterface(
227 XConnector
.class, oCntr
);
229 // create bridge factory
230 XInterface oBrdg
= (XInterface
)xMSF
.createInstance(
231 "com.sun.star.bridge.BridgeFactory") ;
232 xBrdgFctr
= (XBridgeFactory
)
233 UnoRuntime
.queryInterface(XBridgeFactory
.class, oBrdg
);
235 // create own implementation of XInstanceProvider
236 XInstanceProvider xInstProv
= new MyInstanceProvider(xMSF
);
237 // create waiting acceptor thread
238 accThread
= new AcceptorThread(xAcctr
, xInstProv
, xBrdgFctr
);
240 // let the thread sleep
244 catch (java
.lang
.InterruptedException e
) {}
246 // establish the connection
247 XConnection xConnection
= xCntr
.connect(connectString
);
249 String protocol
= "urp";
250 String bridgeName
= protocol
+ ":" + connectString
;
252 /* bridgeDisposed[0] = false ;
253 XComponent xComp = (XComponent)UnoRuntime.queryInterface(
254 XComponent.class, xInt);
255 final PrintWriter logF = log;
256 xComp.addEventListener(new XEventListener() {
257 public void disposing(EventObject ev) {
258 bridgeDisposed[0] = true ;
259 logF.println("The bridge Disposed.");
263 tEnv
.addObjRelation("XInitialization.args", new Object
[] {
264 bridgeName
, protocol
, xConnection
, null});
266 bridge
= tEnv
.getTestObject();
269 } catch (com
.sun
.star
.uno
.Exception e
) {
270 e
.printStackTrace(log
);
271 throw new StatusException("Unexpected exception", e
);
276 * Stop the acceptor thread and dispose the bridge
278 protected void cleanup(TestParameters Param
, PrintWriter log
) {
280 System
.out
.println("++++++++ cleanup");
281 xAcctr
.stopAccepting();
282 if (accThread
.isAlive()) {
283 accThread
.interrupt();
285 XComponent xComp
= (XComponent
)UnoRuntime
.queryInterface(
286 XComponent
.class, xAcctr
);
289 xComp
= (XComponent
)UnoRuntime
.queryInterface(
290 XComponent
.class, xCntr
);
293 xComp
= (XComponent
)UnoRuntime
.queryInterface(
294 XComponent
.class, xBrdgFctr
);
298 xComp
= (XComponent
)UnoRuntime
.queryInterface(
299 XComponent
.class, bridge
);
301 System
.out
.println("######## Dispose bridge");
302 bridgeDisposed
[0] = true;
308 catch(java
.lang
.InterruptedException e
) {