1*620ba73aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*620ba73aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*620ba73aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*620ba73aSAndrew Rist  * distributed with this work for additional information
6*620ba73aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*620ba73aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*620ba73aSAndrew Rist  * "License"); you may not use this file except in compliance
9*620ba73aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*620ba73aSAndrew Rist  *
11*620ba73aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*620ba73aSAndrew Rist  *
13*620ba73aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*620ba73aSAndrew Rist  * software distributed under the License is distributed on an
15*620ba73aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*620ba73aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*620ba73aSAndrew Rist  * specific language governing permissions and limitations
18*620ba73aSAndrew Rist  * under the License.
19*620ba73aSAndrew Rist  *
20*620ba73aSAndrew Rist  *************************************************************/
21*620ba73aSAndrew Rist 
22*620ba73aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.lib;
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.comp.helper.Bootstrap;
30cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
31cdf0e10cSrcweir import com.sun.star.connection.Connector;
32cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
33cdf0e10cSrcweir import com.sun.star.connection.XConnection;
34cdf0e10cSrcweir import com.sun.star.connection.XConnector;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
37cdf0e10cSrcweir import java.io.BufferedReader;
38cdf0e10cSrcweir import java.io.InputStream;
39cdf0e10cSrcweir import java.io.InputStreamReader;
40cdf0e10cSrcweir import java.io.PrintStream;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir public final class TestBed {
execute(XInstanceProvider provider, boolean waitForServer, Class client, long wait)43cdf0e10cSrcweir     public boolean execute(XInstanceProvider provider, boolean waitForServer,
44cdf0e10cSrcweir                            Class client, long wait) throws Exception {
45cdf0e10cSrcweir         // assert client.isAssignableFrom(client) && wait >= 0;
46cdf0e10cSrcweir         synchronized (lock) {
47cdf0e10cSrcweir             server = new Server(provider);
48cdf0e10cSrcweir             server.start();
49cdf0e10cSrcweir             server.waitAccepting();
50cdf0e10cSrcweir         }
51cdf0e10cSrcweir         Process p = Runtime.getRuntime().exec(new String[] {
52cdf0e10cSrcweir             "java", "-classpath", System.getProperty("java.class.path"),
53cdf0e10cSrcweir /*
54cdf0e10cSrcweir             "-Xdebug",
55cdf0e10cSrcweir             "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n",
56cdf0e10cSrcweir */
57cdf0e10cSrcweir             client.getName() });
58cdf0e10cSrcweir         pipe(p.getInputStream(), System.out, "CO> ");
59cdf0e10cSrcweir         pipe(p.getErrorStream(), System.err, "CE> ");
60cdf0e10cSrcweir         boolean clientDone = false;
61cdf0e10cSrcweir         if (wait <= 0) {
62cdf0e10cSrcweir             clientDone = p.waitFor() == CLIENT_DONE;
63cdf0e10cSrcweir         } else {
64cdf0e10cSrcweir             try {
65cdf0e10cSrcweir                 Thread.sleep(wait);
66cdf0e10cSrcweir             } catch (InterruptedException e) {
67cdf0e10cSrcweir                 p.destroy();
68cdf0e10cSrcweir                 throw e;
69cdf0e10cSrcweir             }
70cdf0e10cSrcweir             try {
71cdf0e10cSrcweir                 clientDone = p.exitValue() == CLIENT_DONE;
72cdf0e10cSrcweir             } catch (IllegalThreadStateException e) {
73cdf0e10cSrcweir                 p.destroy();
74cdf0e10cSrcweir             }
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir         boolean success = clientDone;
77cdf0e10cSrcweir         if (waitForServer) {
78cdf0e10cSrcweir             success &= server.waitDone();
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         return success;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
serverDone(boolean success)83cdf0e10cSrcweir     public void serverDone(boolean success) {
84cdf0e10cSrcweir         synchronized (lock) {
85cdf0e10cSrcweir             server.done(success);
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
pipe(final InputStream in, final PrintStream out, final String prefix)89cdf0e10cSrcweir     private void pipe(final InputStream in, final PrintStream out,
90cdf0e10cSrcweir                       final String prefix) {
91cdf0e10cSrcweir         new Thread("Pipe: " + prefix) {
92cdf0e10cSrcweir             public void run() {
93cdf0e10cSrcweir                 BufferedReader r
94cdf0e10cSrcweir                     = new BufferedReader(new InputStreamReader(in));
95cdf0e10cSrcweir                 try {
96cdf0e10cSrcweir                     for (;;) {
97cdf0e10cSrcweir                         String s = r.readLine();
98cdf0e10cSrcweir                         if (s == null) {
99cdf0e10cSrcweir                             break;
100cdf0e10cSrcweir                         }
101cdf0e10cSrcweir                         out.println(prefix + s);
102cdf0e10cSrcweir                     }
103cdf0e10cSrcweir                 } catch (java.io.IOException e) {
104cdf0e10cSrcweir                     e.printStackTrace(System.err);
105cdf0e10cSrcweir                 }
106cdf0e10cSrcweir             }
107cdf0e10cSrcweir         }.start();
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     public static abstract class Client {
run(XComponentContext context)111cdf0e10cSrcweir         protected abstract boolean run(XComponentContext context)
112cdf0e10cSrcweir             throws Throwable;
113cdf0e10cSrcweir 
getConnectionDescription()114cdf0e10cSrcweir         protected final String getConnectionDescription() {
115cdf0e10cSrcweir             return connectionDescription;
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
getProtocolDescription()118cdf0e10cSrcweir         protected final String getProtocolDescription() {
119cdf0e10cSrcweir             return protocolDescription;
120cdf0e10cSrcweir         }
121cdf0e10cSrcweir 
getBridge(XComponentContext context)122cdf0e10cSrcweir         protected final XBridge getBridge(XComponentContext context)
123cdf0e10cSrcweir             throws com.sun.star.uno.Exception
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             XConnector connector = Connector.create(context);
126cdf0e10cSrcweir             XBridgeFactory factory = UnoRuntime.queryInterface(
127cdf0e10cSrcweir                 XBridgeFactory.class,
128cdf0e10cSrcweir                 context.getServiceManager().createInstanceWithContext(
129cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory", context));
130cdf0e10cSrcweir             System.out.println("Client: Connecting...");
131cdf0e10cSrcweir             XConnection connection = connector.connect(connectionDescription);
132cdf0e10cSrcweir             System.out.println("Client: ...connected...");
133cdf0e10cSrcweir             XBridge bridge = factory.createBridge(
134cdf0e10cSrcweir                 "", protocolDescription, connection, null);
135cdf0e10cSrcweir             System.out.println("Client: ...bridged.");
136cdf0e10cSrcweir             return bridge;
137cdf0e10cSrcweir         }
138cdf0e10cSrcweir 
execute()139cdf0e10cSrcweir         protected final void execute() {
140cdf0e10cSrcweir             int status = CLIENT_FAILED;
141cdf0e10cSrcweir             try {
142cdf0e10cSrcweir                 if (run(Bootstrap.createInitialComponentContext(null))) {
143cdf0e10cSrcweir                     status = CLIENT_DONE;
144cdf0e10cSrcweir                 }
145cdf0e10cSrcweir             } catch (Throwable e) {
146cdf0e10cSrcweir                 e.printStackTrace(System.err);
147cdf0e10cSrcweir             }
148cdf0e10cSrcweir             System.exit(status);
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir     }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     private static final class Server extends Thread {
Server(XInstanceProvider provider)153cdf0e10cSrcweir         public Server(XInstanceProvider provider) {
154cdf0e10cSrcweir             super("Server");
155cdf0e10cSrcweir             // assert provider != null;
156cdf0e10cSrcweir             this.provider = provider;
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir 
run()159cdf0e10cSrcweir         public void run() {
160cdf0e10cSrcweir             try {
161cdf0e10cSrcweir                 XComponentContext context
162cdf0e10cSrcweir                     = Bootstrap.createInitialComponentContext(null);
163cdf0e10cSrcweir                 XAcceptor acceptor = Acceptor.create(context);
164cdf0e10cSrcweir                 XBridgeFactory factory
165cdf0e10cSrcweir                     = UnoRuntime.queryInterface(
166cdf0e10cSrcweir                         XBridgeFactory.class,
167cdf0e10cSrcweir                         context.getServiceManager().createInstanceWithContext(
168cdf0e10cSrcweir                             "com.sun.star.bridge.BridgeFactory", context));
169cdf0e10cSrcweir                 System.out.println("Server: Accepting...");
170cdf0e10cSrcweir                 synchronized (this) {
171cdf0e10cSrcweir                     state = ACCEPTING;
172cdf0e10cSrcweir                     notifyAll();
173cdf0e10cSrcweir                 }
174cdf0e10cSrcweir                 for (;;) {
175cdf0e10cSrcweir                     XConnection connection = acceptor.accept(
176cdf0e10cSrcweir                         connectionDescription);
177cdf0e10cSrcweir                     System.out.println("Server: ...connected...");
178cdf0e10cSrcweir                     XBridge bridge = factory.createBridge(
179cdf0e10cSrcweir                         "", protocolDescription, connection, provider);
180cdf0e10cSrcweir                     System.out.println("Server: ...bridged.");
181cdf0e10cSrcweir                 }
182cdf0e10cSrcweir             } catch (Throwable e) {
183cdf0e10cSrcweir                 e.printStackTrace(System.err);
184cdf0e10cSrcweir             }
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
waitAccepting()187cdf0e10cSrcweir         public synchronized void waitAccepting() throws InterruptedException {
188cdf0e10cSrcweir             while (state < ACCEPTING) {
189cdf0e10cSrcweir                 wait();
190cdf0e10cSrcweir             }
191cdf0e10cSrcweir         }
192cdf0e10cSrcweir 
waitDone()193cdf0e10cSrcweir         public synchronized boolean waitDone() throws InterruptedException {
194cdf0e10cSrcweir             while (state <= ACCEPTING) {
195cdf0e10cSrcweir                 wait();
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir             return state == SUCCEEDED;
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir 
done(boolean success)200cdf0e10cSrcweir         public synchronized void done(boolean success) {
201cdf0e10cSrcweir             state = success ? SUCCEEDED : FAILED;
202cdf0e10cSrcweir             notifyAll();
203cdf0e10cSrcweir         }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir         private static final int INITIAL = 0;
206cdf0e10cSrcweir         private static final int ACCEPTING = 1;
207cdf0e10cSrcweir         private static final int FAILED = 2;
208cdf0e10cSrcweir         private static final int SUCCEEDED = 3;
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         private final XInstanceProvider provider;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         private int state = INITIAL;
213cdf0e10cSrcweir     }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     private static final int TEST_SUCCEEDED = 0;
216cdf0e10cSrcweir     private static final int TEST_FAILED = 1;
217cdf0e10cSrcweir     private static final int TEST_ERROR = 2;
218cdf0e10cSrcweir 
219cdf0e10cSrcweir     private static final int CLIENT_FAILED = 0;
220cdf0e10cSrcweir     private static final int CLIENT_DONE = 123;
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     private static final String connectionDescription
223cdf0e10cSrcweir     = "socket,host=localhost,port=12345";
224cdf0e10cSrcweir     private static final String protocolDescription = "urp";
225cdf0e10cSrcweir 
226cdf0e10cSrcweir     private final Object lock = new Object();
227cdf0e10cSrcweir     private Server server = null;
228cdf0e10cSrcweir }
229