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