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
{
41 server
= new Server(provider
);
43 server
.waitAccepting();
45 Process p
= Runtime
.getRuntime().exec(new String
[] {
46 "java", "-classpath", System
.getProperty("java.class.path"),
48 pipe(p
.getInputStream(), System
.out
, "CO> ");
49 pipe(p
.getErrorStream(), System
.err
, "CE> ");
50 boolean clientDone
= false;
52 clientDone
= p
.waitFor() == CLIENT_DONE
;
56 } catch (InterruptedException e
) {
61 clientDone
= p
.exitValue() == CLIENT_DONE
;
62 } catch (IllegalThreadStateException e
) {
66 boolean success
= clientDone
;
68 success
&= server
.waitDone();
73 public void serverDone(boolean success
) {
79 private void pipe(final InputStream in
, final PrintStream out
,
80 final String prefix
) {
81 new Thread("Pipe: " + prefix
) {
85 = new BufferedReader(new InputStreamReader(in
));
88 String s
= r
.readLine();
92 out
.println(prefix
+ s
);
94 } catch (java
.io
.IOException e
) {
95 e
.printStackTrace(System
.err
);
101 public static abstract class Client
{
102 protected abstract boolean run(XComponentContext context
)
105 protected final String
getConnectionDescription() {
106 return connectionDescription
;
109 protected final String
getProtocolDescription() {
110 return protocolDescription
;
113 protected final XBridge
getBridge(XComponentContext context
)
114 throws com
.sun
.star
.uno
.Exception
116 XConnector connector
= Connector
.create(context
);
117 XBridgeFactory factory
= UnoRuntime
.queryInterface(
118 XBridgeFactory
.class,
119 context
.getServiceManager().createInstanceWithContext(
120 "com.sun.star.bridge.BridgeFactory", context
));
121 System
.out
.println("Client: Connecting...");
122 XConnection connection
= connector
.connect(connectionDescription
);
123 System
.out
.println("Client: ...connected...");
124 XBridge bridge
= factory
.createBridge(
125 "", protocolDescription
, connection
, null);
126 System
.out
.println("Client: ...bridged.");
130 protected final void execute() {
131 int status
= CLIENT_FAILED
;
133 if (run(Bootstrap
.createInitialComponentContext(null))) {
134 status
= CLIENT_DONE
;
136 } catch (Throwable e
) {
137 e
.printStackTrace(System
.err
);
143 private static final class Server
extends Thread
{
144 public Server(XInstanceProvider provider
) {
146 // assert provider != null;
147 this.provider
= provider
;
153 XComponentContext context
154 = Bootstrap
.createInitialComponentContext(null);
155 XAcceptor acceptor
= Acceptor
.create(context
);
156 XBridgeFactory factory
157 = UnoRuntime
.queryInterface(
158 XBridgeFactory
.class,
159 context
.getServiceManager().createInstanceWithContext(
160 "com.sun.star.bridge.BridgeFactory", context
));
161 System
.out
.println("Server: Accepting...");
162 synchronized (this) {
167 XConnection connection
= acceptor
.accept(
168 connectionDescription
);
169 System
.out
.println("Server: ...connected...");
170 factory
.createBridge(
171 "", protocolDescription
, connection
, provider
);
172 System
.out
.println("Server: ...bridged.");
174 } catch (Throwable e
) {
175 e
.printStackTrace(System
.err
);
179 public synchronized void waitAccepting() throws InterruptedException
{
180 while (state
< ACCEPTING
) {
185 public synchronized boolean waitDone() throws InterruptedException
{
186 while (state
<= ACCEPTING
) {
189 return state
== SUCCEEDED
;
192 public synchronized void done(boolean success
) {
193 state
= success ? SUCCEEDED
: FAILED
;
197 private static final int INITIAL
= 0;
198 private static final int ACCEPTING
= 1;
199 private static final int FAILED
= 2;
200 private static final int SUCCEEDED
= 3;
202 private final XInstanceProvider provider
;
204 private int state
= INITIAL
;
207 private static final int CLIENT_FAILED
= 0;
208 private static final int CLIENT_DONE
= 123;
210 private static final String connectionDescription
211 = "socket,host=localhost,port=12345";
212 private static final String protocolDescription
= "urp";
214 private final Object lock
= new Object();
215 private Server server
= null;