1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._remotebridge;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
34cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
35cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
36cdf0e10cSrcweir import com.sun.star.connection.XConnection;
37cdf0e10cSrcweir import com.sun.star.connection.XConnector;
38cdf0e10cSrcweir import com.sun.star.lang.XComponent;
39cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
40cdf0e10cSrcweir import com.sun.star.uno.Exception;
41cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42cdf0e10cSrcweir import com.sun.star.uno.XInterface;
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.
124cdf0e10cSrcweir     * Then stores exception thrown by call if it occured, or
125cdf0e10cSrcweir     * return value.
126cdf0e10cSrcweir     */
127cdf0e10cSrcweir     protected class AcceptorThread extends Thread {
128cdf0e10cSrcweir         /**
129cdf0e10cSrcweir         * If exception occured 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             } catch (com.sun.star.lang.IllegalArgumentException e) {
160cdf0e10cSrcweir                 ex = e ;
161cdf0e10cSrcweir             } catch (com.sun.star.connection.ConnectionSetupException e) {
162cdf0e10cSrcweir                 ex = e ;
163cdf0e10cSrcweir             } catch (com.sun.star.connection.AlreadyAcceptingException e) {
164cdf0e10cSrcweir                 ex = e ;
165cdf0e10cSrcweir             }
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     private final boolean[] bridgeDisposed = new boolean[1] ;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     /**
172cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
173cdf0e10cSrcweir     * Creates an instance of the service
174cdf0e10cSrcweir     * <code>com.sun.star.bridge.Bridge</code>.
175cdf0e10cSrcweir     *     Object relations created :
176cdf0e10cSrcweir     * <ul>
177cdf0e10cSrcweir     *  <li> <code>'XInitialization.args'</code> for
178cdf0e10cSrcweir     *   {@link ifc.lang._XInitialization} and
179cdf0e10cSrcweir     *   {@link ifc.bridge._XBridge} : contains arguments
180cdf0e10cSrcweir     *   for <code>initialize()</code> method test.</li>
181cdf0e10cSrcweir     * </ul>
182cdf0e10cSrcweir     */
createTestEnvironment(TestParameters tParam, PrintWriter log)183cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam,
184cdf0e10cSrcweir             PrintWriter log) {
185cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory) tParam.getMSF();
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         try {
188cdf0e10cSrcweir             XInterface xInt = (XInterface)xMSF.createInstance(
189cdf0e10cSrcweir                     "com.sun.star.bridge.Bridge");
190cdf0e10cSrcweir 
191cdf0e10cSrcweir             TestEnvironment tEnv = new TestEnvironment(xInt);
192cdf0e10cSrcweir             // creating arguments for XInitialization
193cdf0e10cSrcweir             // first, creating a connection
194cdf0e10cSrcweir             // connection string
195cdf0e10cSrcweir             String cncstr = (String) tParam.get("CNCSTR") ;
196cdf0e10cSrcweir             int idx = cncstr.indexOf("host=") + 5 ;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir             // select the port
199cdf0e10cSrcweir //            curPort; //utils.getNextFreePort(basePort);
200cdf0e10cSrcweir             log.println("Choose Port nr: " + curPort);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir             connectString = "socket,host=" +
203cdf0e10cSrcweir                     cncstr.substring(idx, cncstr.indexOf(",", idx)) +
204cdf0e10cSrcweir                     ",port=" + curPort;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir             // create acceptor
207cdf0e10cSrcweir             XInterface oAcctr = (XInterface)xMSF.createInstance(
208cdf0e10cSrcweir                     "com.sun.star.connection.Acceptor") ;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir             xAcctr = (XAcceptor)UnoRuntime.queryInterface(
211cdf0e10cSrcweir                     XAcceptor.class, oAcctr);
212cdf0e10cSrcweir             // create connector
213cdf0e10cSrcweir             XInterface oCntr = (XInterface)xMSF.createInstance(
214cdf0e10cSrcweir                     "com.sun.star.connection.Connector") ;
215cdf0e10cSrcweir             xCntr = (XConnector)UnoRuntime.queryInterface(
216cdf0e10cSrcweir                     XConnector.class, oCntr);
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             // create bridge factory
219cdf0e10cSrcweir             XInterface oBrdg = (XInterface)xMSF.createInstance(
220cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory") ;
221cdf0e10cSrcweir             xBrdgFctr = (XBridgeFactory)
222cdf0e10cSrcweir                         UnoRuntime.queryInterface(XBridgeFactory.class, oBrdg);
223cdf0e10cSrcweir 
224cdf0e10cSrcweir             // create own implementation of XInstanceProvider
225cdf0e10cSrcweir             XInstanceProvider xInstProv = new MyInstanceProvider(xMSF);
226cdf0e10cSrcweir             // create waiting acceptor thread
227cdf0e10cSrcweir             accThread = new AcceptorThread(xAcctr, xInstProv, xBrdgFctr);
228cdf0e10cSrcweir             accThread.start();
229cdf0e10cSrcweir             // let the thread sleep
230cdf0e10cSrcweir             try {
231cdf0e10cSrcweir                 Thread.sleep(500);
232cdf0e10cSrcweir             }
233cdf0e10cSrcweir             catch (java.lang.InterruptedException e) {}
234cdf0e10cSrcweir 
235cdf0e10cSrcweir             // establish the connection
236cdf0e10cSrcweir             XConnection xConnection = xCntr.connect(connectString);
237cdf0e10cSrcweir 
238cdf0e10cSrcweir             String protocol = "urp";
239cdf0e10cSrcweir             String bridgeName = protocol + ":" + connectString;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir /*            bridgeDisposed[0] = false ;
242cdf0e10cSrcweir             XComponent xComp = (XComponent)UnoRuntime.queryInterface(
243cdf0e10cSrcweir                 XComponent.class, xInt);
244cdf0e10cSrcweir             final PrintWriter logF = log;
245cdf0e10cSrcweir             xComp.addEventListener(new XEventListener() {
246cdf0e10cSrcweir                 public void disposing(EventObject ev) {
247cdf0e10cSrcweir                     bridgeDisposed[0] = true ;
248cdf0e10cSrcweir                     logF.println("The bridge Disposed.");
249cdf0e10cSrcweir                 }
250cdf0e10cSrcweir             });
251cdf0e10cSrcweir */
252cdf0e10cSrcweir             tEnv.addObjRelation("XInitialization.args", new Object[] {
253cdf0e10cSrcweir                     bridgeName, protocol, xConnection, null});
254cdf0e10cSrcweir 
255cdf0e10cSrcweir             bridge = tEnv.getTestObject();
256cdf0e10cSrcweir 
257cdf0e10cSrcweir             return tEnv;
258cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
259cdf0e10cSrcweir             e.printStackTrace(log);
260cdf0e10cSrcweir             throw new StatusException("Unexpected exception", e);
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     /**
265cdf0e10cSrcweir      * Stop the acceptor thread and dispose the bridge
266cdf0e10cSrcweir      */
cleanup(TestParameters Param, PrintWriter log)267cdf0e10cSrcweir     protected void cleanup(TestParameters Param, PrintWriter log) {
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         System.out.println("++++++++ cleanup");
270cdf0e10cSrcweir         xAcctr.stopAccepting();
271cdf0e10cSrcweir         if (accThread.isAlive()) {
272cdf0e10cSrcweir             accThread.interrupt();
273cdf0e10cSrcweir         }
274cdf0e10cSrcweir         XComponent xComp = (XComponent)UnoRuntime.queryInterface(
275cdf0e10cSrcweir                 XComponent.class, xAcctr);
276cdf0e10cSrcweir         if (xComp != null)
277cdf0e10cSrcweir             xComp.dispose();
278cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
279cdf0e10cSrcweir                 XComponent.class, xCntr);
280cdf0e10cSrcweir         if (xComp != null)
281cdf0e10cSrcweir             xComp.dispose();
282cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
283cdf0e10cSrcweir                 XComponent.class, xBrdgFctr);
284cdf0e10cSrcweir         if (xComp != null)
285cdf0e10cSrcweir             xComp.dispose();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         xComp = (XComponent)UnoRuntime.queryInterface(
288cdf0e10cSrcweir                 XComponent.class, bridge);
289cdf0e10cSrcweir         if (xComp != null) {
290cdf0e10cSrcweir             System.out.println("######## Dispose bridge");
291cdf0e10cSrcweir             bridgeDisposed[0] = true;
292cdf0e10cSrcweir             xComp.dispose();
293cdf0e10cSrcweir             // wait for dispose
294cdf0e10cSrcweir             try {
295cdf0e10cSrcweir                 Thread.sleep(5000);
296cdf0e10cSrcweir             }
297cdf0e10cSrcweir             catch(java.lang.InterruptedException e) {
298cdf0e10cSrcweir             }
299cdf0e10cSrcweir         }
300cdf0e10cSrcweir     }
301cdf0e10cSrcweir }
302