1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package com.sun.star.lib;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
31*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
32*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
33*cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
34*cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
35*cdf0e10cSrcweir import com.sun.star.connection.Connector;
36*cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
37*cdf0e10cSrcweir import com.sun.star.connection.XConnection;
38*cdf0e10cSrcweir import com.sun.star.connection.XConnector;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
41*cdf0e10cSrcweir import java.io.BufferedReader;
42*cdf0e10cSrcweir import java.io.InputStream;
43*cdf0e10cSrcweir import java.io.InputStreamReader;
44*cdf0e10cSrcweir import java.io.PrintStream;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir public final class TestBed {
47*cdf0e10cSrcweir     public boolean execute(XInstanceProvider provider, boolean waitForServer,
48*cdf0e10cSrcweir                            Class client, long wait) throws Exception {
49*cdf0e10cSrcweir         // assert client.isAssignableFrom(client) && wait >= 0;
50*cdf0e10cSrcweir         synchronized (lock) {
51*cdf0e10cSrcweir             server = new Server(provider);
52*cdf0e10cSrcweir             server.start();
53*cdf0e10cSrcweir             server.waitAccepting();
54*cdf0e10cSrcweir         }
55*cdf0e10cSrcweir         Process p = Runtime.getRuntime().exec(new String[] {
56*cdf0e10cSrcweir             "java", "-classpath", System.getProperty("java.class.path"),
57*cdf0e10cSrcweir /*
58*cdf0e10cSrcweir             "-Xdebug",
59*cdf0e10cSrcweir             "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n",
60*cdf0e10cSrcweir */
61*cdf0e10cSrcweir             client.getName() });
62*cdf0e10cSrcweir         pipe(p.getInputStream(), System.out, "CO> ");
63*cdf0e10cSrcweir         pipe(p.getErrorStream(), System.err, "CE> ");
64*cdf0e10cSrcweir         boolean clientDone = false;
65*cdf0e10cSrcweir         if (wait <= 0) {
66*cdf0e10cSrcweir             clientDone = p.waitFor() == CLIENT_DONE;
67*cdf0e10cSrcweir         } else {
68*cdf0e10cSrcweir             try {
69*cdf0e10cSrcweir                 Thread.sleep(wait);
70*cdf0e10cSrcweir             } catch (InterruptedException e) {
71*cdf0e10cSrcweir                 p.destroy();
72*cdf0e10cSrcweir                 throw e;
73*cdf0e10cSrcweir             }
74*cdf0e10cSrcweir             try {
75*cdf0e10cSrcweir                 clientDone = p.exitValue() == CLIENT_DONE;
76*cdf0e10cSrcweir             } catch (IllegalThreadStateException e) {
77*cdf0e10cSrcweir                 p.destroy();
78*cdf0e10cSrcweir             }
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir         boolean success = clientDone;
81*cdf0e10cSrcweir         if (waitForServer) {
82*cdf0e10cSrcweir             success &= server.waitDone();
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         return success;
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir     public void serverDone(boolean success) {
88*cdf0e10cSrcweir         synchronized (lock) {
89*cdf0e10cSrcweir             server.done(success);
90*cdf0e10cSrcweir         }
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     private void pipe(final InputStream in, final PrintStream out,
94*cdf0e10cSrcweir                       final String prefix) {
95*cdf0e10cSrcweir         new Thread("Pipe: " + prefix) {
96*cdf0e10cSrcweir             public void run() {
97*cdf0e10cSrcweir                 BufferedReader r
98*cdf0e10cSrcweir                     = new BufferedReader(new InputStreamReader(in));
99*cdf0e10cSrcweir                 try {
100*cdf0e10cSrcweir                     for (;;) {
101*cdf0e10cSrcweir                         String s = r.readLine();
102*cdf0e10cSrcweir                         if (s == null) {
103*cdf0e10cSrcweir                             break;
104*cdf0e10cSrcweir                         }
105*cdf0e10cSrcweir                         out.println(prefix + s);
106*cdf0e10cSrcweir                     }
107*cdf0e10cSrcweir                 } catch (java.io.IOException e) {
108*cdf0e10cSrcweir                     e.printStackTrace(System.err);
109*cdf0e10cSrcweir                 }
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir         }.start();
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     public static abstract class Client {
115*cdf0e10cSrcweir         protected abstract boolean run(XComponentContext context)
116*cdf0e10cSrcweir             throws Throwable;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         protected final String getConnectionDescription() {
119*cdf0e10cSrcweir             return connectionDescription;
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         protected final String getProtocolDescription() {
123*cdf0e10cSrcweir             return protocolDescription;
124*cdf0e10cSrcweir         }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         protected final XBridge getBridge(XComponentContext context)
127*cdf0e10cSrcweir             throws com.sun.star.uno.Exception
128*cdf0e10cSrcweir         {
129*cdf0e10cSrcweir             XConnector connector = Connector.create(context);
130*cdf0e10cSrcweir             XBridgeFactory factory = UnoRuntime.queryInterface(
131*cdf0e10cSrcweir                 XBridgeFactory.class,
132*cdf0e10cSrcweir                 context.getServiceManager().createInstanceWithContext(
133*cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory", context));
134*cdf0e10cSrcweir             System.out.println("Client: Connecting...");
135*cdf0e10cSrcweir             XConnection connection = connector.connect(connectionDescription);
136*cdf0e10cSrcweir             System.out.println("Client: ...connected...");
137*cdf0e10cSrcweir             XBridge bridge = factory.createBridge(
138*cdf0e10cSrcweir                 "", protocolDescription, connection, null);
139*cdf0e10cSrcweir             System.out.println("Client: ...bridged.");
140*cdf0e10cSrcweir             return bridge;
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         protected final void execute() {
144*cdf0e10cSrcweir             int status = CLIENT_FAILED;
145*cdf0e10cSrcweir             try {
146*cdf0e10cSrcweir                 if (run(Bootstrap.createInitialComponentContext(null))) {
147*cdf0e10cSrcweir                     status = CLIENT_DONE;
148*cdf0e10cSrcweir                 }
149*cdf0e10cSrcweir             } catch (Throwable e) {
150*cdf0e10cSrcweir                 e.printStackTrace(System.err);
151*cdf0e10cSrcweir             }
152*cdf0e10cSrcweir             System.exit(status);
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     private static final class Server extends Thread {
157*cdf0e10cSrcweir         public Server(XInstanceProvider provider) {
158*cdf0e10cSrcweir             super("Server");
159*cdf0e10cSrcweir             // assert provider != null;
160*cdf0e10cSrcweir             this.provider = provider;
161*cdf0e10cSrcweir         }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         public void run() {
164*cdf0e10cSrcweir             try {
165*cdf0e10cSrcweir                 XComponentContext context
166*cdf0e10cSrcweir                     = Bootstrap.createInitialComponentContext(null);
167*cdf0e10cSrcweir                 XAcceptor acceptor = Acceptor.create(context);
168*cdf0e10cSrcweir                 XBridgeFactory factory
169*cdf0e10cSrcweir                     = UnoRuntime.queryInterface(
170*cdf0e10cSrcweir                         XBridgeFactory.class,
171*cdf0e10cSrcweir                         context.getServiceManager().createInstanceWithContext(
172*cdf0e10cSrcweir                             "com.sun.star.bridge.BridgeFactory", context));
173*cdf0e10cSrcweir                 System.out.println("Server: Accepting...");
174*cdf0e10cSrcweir                 synchronized (this) {
175*cdf0e10cSrcweir                     state = ACCEPTING;
176*cdf0e10cSrcweir                     notifyAll();
177*cdf0e10cSrcweir                 }
178*cdf0e10cSrcweir                 for (;;) {
179*cdf0e10cSrcweir                     XConnection connection = acceptor.accept(
180*cdf0e10cSrcweir                         connectionDescription);
181*cdf0e10cSrcweir                     System.out.println("Server: ...connected...");
182*cdf0e10cSrcweir                     XBridge bridge = factory.createBridge(
183*cdf0e10cSrcweir                         "", protocolDescription, connection, provider);
184*cdf0e10cSrcweir                     System.out.println("Server: ...bridged.");
185*cdf0e10cSrcweir                 }
186*cdf0e10cSrcweir             } catch (Throwable e) {
187*cdf0e10cSrcweir                 e.printStackTrace(System.err);
188*cdf0e10cSrcweir             }
189*cdf0e10cSrcweir         }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         public synchronized void waitAccepting() throws InterruptedException {
192*cdf0e10cSrcweir             while (state < ACCEPTING) {
193*cdf0e10cSrcweir                 wait();
194*cdf0e10cSrcweir             }
195*cdf0e10cSrcweir         }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir         public synchronized boolean waitDone() throws InterruptedException {
198*cdf0e10cSrcweir             while (state <= ACCEPTING) {
199*cdf0e10cSrcweir                 wait();
200*cdf0e10cSrcweir             }
201*cdf0e10cSrcweir             return state == SUCCEEDED;
202*cdf0e10cSrcweir         }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         public synchronized void done(boolean success) {
205*cdf0e10cSrcweir             state = success ? SUCCEEDED : FAILED;
206*cdf0e10cSrcweir             notifyAll();
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir         private static final int INITIAL = 0;
210*cdf0e10cSrcweir         private static final int ACCEPTING = 1;
211*cdf0e10cSrcweir         private static final int FAILED = 2;
212*cdf0e10cSrcweir         private static final int SUCCEEDED = 3;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         private final XInstanceProvider provider;
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         private int state = INITIAL;
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     private static final int TEST_SUCCEEDED = 0;
220*cdf0e10cSrcweir     private static final int TEST_FAILED = 1;
221*cdf0e10cSrcweir     private static final int TEST_ERROR = 2;
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     private static final int CLIENT_FAILED = 0;
224*cdf0e10cSrcweir     private static final int CLIENT_DONE = 123;
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     private static final String connectionDescription
227*cdf0e10cSrcweir     = "socket,host=localhost,port=12345";
228*cdf0e10cSrcweir     private static final String protocolDescription = "urp";
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir     private final Object lock = new Object();
231*cdf0e10cSrcweir     private Server server = null;
232*cdf0e10cSrcweir }
233