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