1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._remotebridge.uno;
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.connection.XAcceptor;
34 import com.sun.star.connection.XConnection;
35 import com.sun.star.connection.XConnector;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.uno.Exception;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import java.io.PrintWriter;
42 import lib.StatusException;
43 import lib.TestCase;
44 import lib.TestEnvironment;
45 import lib.TestParameters;
46 
47 
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.bridge.Bridge</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::lang::XInitialization</code></li>
54 *  <li> <code>com::sun::star::lang::XComponent</code></li>
55 *  <li> <code>com::sun::star::bridge::XBridge</code></li>
56 * </ul>
57 * This object test <b> is NOT </b> designed to be run in several
58 * threads concurently.
59 * @see com.sun.star.lang.XInitialization
60 * @see com.sun.star.lang.XComponent
61 * @see com.sun.star.bridge.XBridge
62 * @see com.sun.star.bridge.Bridge
63 * @see ifc.lang._XInitialization
64 * @see ifc.lang._XComponent
65 * @see ifc.bridge._XBridge
66 */
67 public class various extends TestCase {
68 
69     /**
70      *  String for establishing a connection
71      */
72     protected String connectString = null ;
73 
74     /**
75     * Choose the first port after <code>basePort</code>
76     * which is free.
77     */
78     protected static final int basePort = 50000;
79     private int curPort = 50000;
80 
81     private XAcceptor xAcctr;
82     private XConnector xCntr;
83     private XBridgeFactory xBrdgFctr;
84     private AcceptorThread accThread;
85 
86     public XInterface bridge = null;
87 
88     /**
89      * Implementation of interface XInstanceProvider
90      *
91      * @see com.sun.star.bridge.XInstanceProvider
92      */
93     protected class MyInstanceProvider implements XInstanceProvider {
94         /**
95          * a MultiServiceFactory for creating instances
96          *
97          * @see com.sun.star.lang.MultiServiceFactory
98          */
99         private XMultiServiceFactory xMSF = null;
100 
101         /**
102          * Construct object with a MultiServiceFactory
103          *
104          * @see com.sun.star.lang.MultiServiceFactory
105          */
106         public MyInstanceProvider(XMultiServiceFactory xMSF) {
107             this.xMSF = xMSF;
108         }
109 
110         /**
111          * get an instance by name
112          */
113         public Object getInstance(String aInstanceName)
114                         throws com.sun.star.container.NoSuchElementException
115                         {
116             System.out.println("######## Try to get "+aInstanceName);
117             try {
118                 return xMSF.createInstance(aInstanceName);
119             }
120             catch(com.sun.star.uno.Exception e) {
121                 throw new StatusException("Unexpected exception", e);
122             }
123         }
124     }
125 
126     /**
127     * Calls <code>accept()</code> method in a separate thread.
128     * Then stores exception thrown by call if it occured, or
129     * return value.
130     */
131     protected class AcceptorThread extends Thread {
132         /**
133         * If exception occured during method call it is
134         * stored in this field.
135         */
136         public Exception ex = null ;
137         private XAcceptor acc = null ;
138         private XInstanceProvider xInstProv = null ;
139         private XBridgeFactory xBrdgFctr = null;
140         /**
141         * If method call returns some value it stores in this field.
142         */
143         public XConnection acceptedCall = null ;
144 
145         /**
146         * Creates object which can call <code>accept</code> method
147         * of the Acceptor object specified.
148         */
149         public AcceptorThread(XAcceptor acc, XInstanceProvider xInstProv,
150                 XBridgeFactory xBrdgFctr) {
151             this.acc = acc ;
152             this.xInstProv = xInstProv;
153             this.xBrdgFctr = xBrdgFctr;
154         }
155 
156         /**
157         * Call <code>accept()</code> method and establish a bridge with an
158         * instance provider
159         */
160         public void run() {
161             try {
162                 acceptedCall = acc.accept(connectString) ;
163                 xBrdgFctr.createBridge("MyBridge", "urp",
164                                             acceptedCall, xInstProv);
165             } catch (com.sun.star.lang.IllegalArgumentException e) {
166                 ex = e ;
167             } catch (com.sun.star.connection.ConnectionSetupException e) {
168                 ex = e ;
169             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
170                 ex = e ;
171             } catch (com.sun.star.bridge.BridgeExistsException e) {
172                 ex = e ;
173             }
174         }
175     }
176 
177     private final boolean[] bridgeDisposed = new boolean[1] ;
178 
179     /**
180     * Creating a Testenvironment for the interfaces to be tested.
181     * Creates an instance of the service
182     * <code>com.sun.star.bridge.Bridge</code>.
183     *     Object relations created :
184     * <ul>
185     *  <li> <code>'XInitialization.args'</code> for
186     *   {@link ifc.lang._XInitialization} and
187     *   {@link ifc.bridge._XBridge} : contains arguments
188     *   for <code>initialize()</code> method test.</li>
189     * </ul>
190     */
191     protected TestEnvironment createTestEnvironment(TestParameters tParam,
192             PrintWriter log) {
193         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
194 
195         try {
196             XInterface xInt = (XInterface)xMSF.createInstance(
197                     "com.sun.star.bridge.Bridge");
198 
199             TestEnvironment tEnv = new TestEnvironment(xInt);
200             // creating arguments for XInitialization
201             // first, creating a connection
202             // connection string
203             String cncstr = (String) tParam.get("CNCSTR") ;
204             int idx = cncstr.indexOf("host=") + 5 ;
205 
206             // select the port
207 //            curPort; //utils.getNextFreePort(basePort);
208             log.println("Choose Port nr: " + curPort);
209 
210             connectString = "socket,host=" +
211                     cncstr.substring(idx, cncstr.indexOf(",", idx)) +
212                     ",port=" + curPort;
213 
214             // create acceptor
215             XInterface oAcctr = (XInterface)xMSF.createInstance(
216                     "com.sun.star.connection.Acceptor") ;
217 
218             xAcctr = (XAcceptor)UnoRuntime.queryInterface(
219                     XAcceptor.class, oAcctr);
220             // create connector
221             XInterface oCntr = (XInterface)xMSF.createInstance(
222                     "com.sun.star.connection.Connector") ;
223             xCntr = (XConnector)UnoRuntime.queryInterface(
224                     XConnector.class, oCntr);
225 
226             // create bridge factory
227             XInterface oBrdg = (XInterface)xMSF.createInstance(
228                     "com.sun.star.bridge.BridgeFactory") ;
229             xBrdgFctr = (XBridgeFactory)
230                         UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
231 
232             // create own implementation of XInstanceProvider
233             XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
234             // create waiting acceptor thread
235             accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
236             accThread.start();
237             // let the thread sleep
238             try {
239                 Thread.sleep(500);
240             }
241             catch (java.lang.InterruptedException e) {}
242 
243             // establish the connection
244             XConnection xConnection = xCntr.connect(connectString);
245 
246             String protocol = "urp";
247             String bridgeName = protocol + ":" + connectString;
248 
249 /*            bridgeDisposed[0] = false ;
250             XComponent xComp = (XComponent)UnoRuntime.queryInterface(
251                 XComponent.class, xInt);
252             final PrintWriter logF = log;
253             xComp.addEventListener(new XEventListener() {
254                 public void disposing(EventObject ev) {
255                     bridgeDisposed[0] = true ;
256                     logF.println("The bridge Disposed.");
257                 }
258             });
259 */
260             tEnv.addObjRelation("XInitialization.args", new Object[] {
261                     bridgeName, protocol, xConnection, null});
262 
263             bridge = tEnv.getTestObject();
264 
265             return tEnv;
266         } catch (com.sun.star.uno.Exception e) {
267             e.printStackTrace(log);
268             throw new StatusException("Unexpected exception", e);
269         }
270     }
271 
272     /**
273      * Stop the acceptor thread and dispose the bridge
274      */
275     protected void cleanup(TestParameters Param, PrintWriter log) {
276 
277         System.out.println("++++++++ cleanup");
278         xAcctr.stopAccepting();
279         if (accThread.isAlive()) {
280             accThread.interrupt();
281         }
282         XComponent xComp = (XComponent)UnoRuntime.queryInterface(
283                 XComponent.class, xAcctr);
284         if (xComp != null)
285             xComp.dispose();
286         xComp = (XComponent)UnoRuntime.queryInterface(
287                 XComponent.class, xCntr);
288         if (xComp != null)
289             xComp.dispose();
290         xComp = (XComponent)UnoRuntime.queryInterface(
291                 XComponent.class, xBrdgFctr);
292         if (xComp != null)
293             xComp.dispose();
294 
295         xComp = (XComponent)UnoRuntime.queryInterface(
296                 XComponent.class, bridge);
297         if (xComp != null) {
298             System.out.println("######## Dispose bridge");
299             bridgeDisposed[0] = true;
300             xComp.dispose();
301             // wait for dispose
302             try {
303                 Thread.sleep(5000);
304             }
305             catch(java.lang.InterruptedException e) {
306             }
307         }
308     }
309 }
310