2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.bridge
.XBridge
;
22 import com
.sun
.star
.bridge
.XBridgeFactory
;
23 import com
.sun
.star
.bridge
.XInstanceProvider
;
24 import com
.sun
.star
.comp
.helper
.Bootstrap
;
25 import com
.sun
.star
.connection
.Acceptor
;
26 import com
.sun
.star
.connection
.Connector
;
27 import com
.sun
.star
.connection
.XAcceptor
;
28 import com
.sun
.star
.connection
.XConnection
;
29 import com
.sun
.star
.connection
.XConnector
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
31 import com
.sun
.star
.uno
.XComponentContext
;
32 import java
.io
.BufferedReader
;
33 import java
.io
.InputStream
;
34 import java
.io
.InputStreamReader
;
35 import java
.io
.PrintStream
;
37 public final class TestBed
{
38 public boolean execute(XInstanceProvider provider
, boolean waitForServer
,
39 Class client
, long wait
) throws Exception
{
40 // assert client.isAssignableFrom(client) && wait >= 0;
42 server
= new Server(provider
);
44 server
.waitAccepting();
46 Process p
= Runtime
.getRuntime().exec(new String
[] {
47 "java", "-classpath", System
.getProperty("java.class.path"),
50 "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n",
53 pipe(p
.getInputStream(), System
.out
, "CO> ");
54 pipe(p
.getErrorStream(), System
.err
, "CE> ");
55 boolean clientDone
= false;
57 clientDone
= p
.waitFor() == CLIENT_DONE
;
61 } catch (InterruptedException e
) {
66 clientDone
= p
.exitValue() == CLIENT_DONE
;
67 } catch (IllegalThreadStateException e
) {
71 boolean success
= clientDone
;
73 success
&= server
.waitDone();
78 public void serverDone(boolean success
) {
84 private void pipe(final InputStream in
, final PrintStream out
,
85 final String prefix
) {
86 new Thread("Pipe: " + prefix
) {
89 = new BufferedReader(new InputStreamReader(in
));
92 String s
= r
.readLine();
96 out
.println(prefix
+ s
);
98 } catch (java
.io
.IOException e
) {
99 e
.printStackTrace(System
.err
);
105 public static abstract class Client
{
106 protected abstract boolean run(XComponentContext context
)
109 protected final String
getConnectionDescription() {
110 return connectionDescription
;
113 protected final String
getProtocolDescription() {
114 return protocolDescription
;
117 protected final XBridge
getBridge(XComponentContext context
)
118 throws com
.sun
.star
.uno
.Exception
120 XConnector connector
= Connector
.create(context
);
121 XBridgeFactory factory
= UnoRuntime
.queryInterface(
122 XBridgeFactory
.class,
123 context
.getServiceManager().createInstanceWithContext(
124 "com.sun.star.bridge.BridgeFactory", context
));
125 System
.out
.println("Client: Connecting...");
126 XConnection connection
= connector
.connect(connectionDescription
);
127 System
.out
.println("Client: ...connected...");
128 XBridge bridge
= factory
.createBridge(
129 "", protocolDescription
, connection
, null);
130 System
.out
.println("Client: ...bridged.");
134 protected final void execute() {
135 int status
= CLIENT_FAILED
;
137 if (run(Bootstrap
.createInitialComponentContext(null))) {
138 status
= CLIENT_DONE
;
140 } catch (Throwable e
) {
141 e
.printStackTrace(System
.err
);
147 private static final class Server
extends Thread
{
148 public Server(XInstanceProvider provider
) {
150 // assert provider != null;
151 this.provider
= provider
;
156 XComponentContext context
157 = Bootstrap
.createInitialComponentContext(null);
158 XAcceptor acceptor
= Acceptor
.create(context
);
159 XBridgeFactory factory
160 = UnoRuntime
.queryInterface(
161 XBridgeFactory
.class,
162 context
.getServiceManager().createInstanceWithContext(
163 "com.sun.star.bridge.BridgeFactory", context
));
164 System
.out
.println("Server: Accepting...");
165 synchronized (this) {
170 XConnection connection
= acceptor
.accept(
171 connectionDescription
);
172 System
.out
.println("Server: ...connected...");
173 XBridge bridge
= factory
.createBridge(
174 "", protocolDescription
, connection
, provider
);
175 System
.out
.println("Server: ...bridged.");
177 } catch (Throwable e
) {
178 e
.printStackTrace(System
.err
);
182 public synchronized void waitAccepting() throws InterruptedException
{
183 while (state
< ACCEPTING
) {
188 public synchronized boolean waitDone() throws InterruptedException
{
189 while (state
<= ACCEPTING
) {
192 return state
== SUCCEEDED
;
195 public synchronized void done(boolean success
) {
196 state
= success ? SUCCEEDED
: FAILED
;
200 private static final int INITIAL
= 0;
201 private static final int ACCEPTING
= 1;
202 private static final int FAILED
= 2;
203 private static final int SUCCEEDED
= 3;
205 private final XInstanceProvider provider
;
207 private int state
= INITIAL
;
210 private static final int TEST_SUCCEEDED
= 0;
211 private static final int TEST_FAILED
= 1;
212 private static final int TEST_ERROR
= 2;
214 private static final int CLIENT_FAILED
= 0;
215 private static final int CLIENT_DONE
= 123;
217 private static final String connectionDescription
218 = "socket,host=localhost,port=12345";
219 private static final String protocolDescription
= "urp";
221 private final Object lock
= new Object();
222 private Server server
= null;