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 com.sun.star.lib; 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.comp.helper.Bootstrap; 30 import com.sun.star.connection.Acceptor; 31 import com.sun.star.connection.Connector; 32 import com.sun.star.connection.XAcceptor; 33 import com.sun.star.connection.XConnection; 34 import com.sun.star.connection.XConnector; 35 import com.sun.star.uno.UnoRuntime; 36 import com.sun.star.uno.XComponentContext; 37 import java.io.BufferedReader; 38 import java.io.InputStream; 39 import java.io.InputStreamReader; 40 import java.io.PrintStream; 41 42 public final class TestBed { execute(XInstanceProvider provider, boolean waitForServer, Class client, long wait)43 public boolean execute(XInstanceProvider provider, boolean waitForServer, 44 Class client, long wait) throws Exception { 45 // assert client.isAssignableFrom(client) && wait >= 0; 46 synchronized (lock) { 47 server = new Server(provider); 48 server.start(); 49 server.waitAccepting(); 50 } 51 Process p = Runtime.getRuntime().exec(new String[] { 52 "java", "-classpath", System.getProperty("java.class.path"), 53 /* 54 "-Xdebug", 55 "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n", 56 */ 57 client.getName() }); 58 pipe(p.getInputStream(), System.out, "CO> "); 59 pipe(p.getErrorStream(), System.err, "CE> "); 60 boolean clientDone = false; 61 if (wait <= 0) { 62 clientDone = p.waitFor() == CLIENT_DONE; 63 } else { 64 try { 65 Thread.sleep(wait); 66 } catch (InterruptedException e) { 67 p.destroy(); 68 throw e; 69 } 70 try { 71 clientDone = p.exitValue() == CLIENT_DONE; 72 } catch (IllegalThreadStateException e) { 73 p.destroy(); 74 } 75 } 76 boolean success = clientDone; 77 if (waitForServer) { 78 success &= server.waitDone(); 79 } 80 return success; 81 } 82 serverDone(boolean success)83 public void serverDone(boolean success) { 84 synchronized (lock) { 85 server.done(success); 86 } 87 } 88 pipe(final InputStream in, final PrintStream out, final String prefix)89 private void pipe(final InputStream in, final PrintStream out, 90 final String prefix) { 91 new Thread("Pipe: " + prefix) { 92 public void run() { 93 BufferedReader r 94 = new BufferedReader(new InputStreamReader(in)); 95 try { 96 for (;;) { 97 String s = r.readLine(); 98 if (s == null) { 99 break; 100 } 101 out.println(prefix + s); 102 } 103 } catch (java.io.IOException e) { 104 e.printStackTrace(System.err); 105 } 106 } 107 }.start(); 108 } 109 110 public static abstract class Client { run(XComponentContext context)111 protected abstract boolean run(XComponentContext context) 112 throws Throwable; 113 getConnectionDescription()114 protected final String getConnectionDescription() { 115 return connectionDescription; 116 } 117 getProtocolDescription()118 protected final String getProtocolDescription() { 119 return protocolDescription; 120 } 121 getBridge(XComponentContext context)122 protected final XBridge getBridge(XComponentContext context) 123 throws com.sun.star.uno.Exception 124 { 125 XConnector connector = Connector.create(context); 126 XBridgeFactory factory = UnoRuntime.queryInterface( 127 XBridgeFactory.class, 128 context.getServiceManager().createInstanceWithContext( 129 "com.sun.star.bridge.BridgeFactory", context)); 130 System.out.println("Client: Connecting..."); 131 XConnection connection = connector.connect(connectionDescription); 132 System.out.println("Client: ...connected..."); 133 XBridge bridge = factory.createBridge( 134 "", protocolDescription, connection, null); 135 System.out.println("Client: ...bridged."); 136 return bridge; 137 } 138 execute()139 protected final void execute() { 140 int status = CLIENT_FAILED; 141 try { 142 if (run(Bootstrap.createInitialComponentContext(null))) { 143 status = CLIENT_DONE; 144 } 145 } catch (Throwable e) { 146 e.printStackTrace(System.err); 147 } 148 System.exit(status); 149 } 150 } 151 152 private static final class Server extends Thread { Server(XInstanceProvider provider)153 public Server(XInstanceProvider provider) { 154 super("Server"); 155 // assert provider != null; 156 this.provider = provider; 157 } 158 run()159 public void run() { 160 try { 161 XComponentContext context 162 = Bootstrap.createInitialComponentContext(null); 163 XAcceptor acceptor = Acceptor.create(context); 164 XBridgeFactory factory 165 = UnoRuntime.queryInterface( 166 XBridgeFactory.class, 167 context.getServiceManager().createInstanceWithContext( 168 "com.sun.star.bridge.BridgeFactory", context)); 169 System.out.println("Server: Accepting..."); 170 synchronized (this) { 171 state = ACCEPTING; 172 notifyAll(); 173 } 174 for (;;) { 175 XConnection connection = acceptor.accept( 176 connectionDescription); 177 System.out.println("Server: ...connected..."); 178 XBridge bridge = factory.createBridge( 179 "", protocolDescription, connection, provider); 180 System.out.println("Server: ...bridged."); 181 } 182 } catch (Throwable e) { 183 e.printStackTrace(System.err); 184 } 185 } 186 waitAccepting()187 public synchronized void waitAccepting() throws InterruptedException { 188 while (state < ACCEPTING) { 189 wait(); 190 } 191 } 192 waitDone()193 public synchronized boolean waitDone() throws InterruptedException { 194 while (state <= ACCEPTING) { 195 wait(); 196 } 197 return state == SUCCEEDED; 198 } 199 done(boolean success)200 public synchronized void done(boolean success) { 201 state = success ? SUCCEEDED : FAILED; 202 notifyAll(); 203 } 204 205 private static final int INITIAL = 0; 206 private static final int ACCEPTING = 1; 207 private static final int FAILED = 2; 208 private static final int SUCCEEDED = 3; 209 210 private final XInstanceProvider provider; 211 212 private int state = INITIAL; 213 } 214 215 private static final int TEST_SUCCEEDED = 0; 216 private static final int TEST_FAILED = 1; 217 private static final int TEST_ERROR = 2; 218 219 private static final int CLIENT_FAILED = 0; 220 private static final int CLIENT_DONE = 123; 221 222 private static final String connectionDescription 223 = "socket,host=localhost,port=12345"; 224 private static final String protocolDescription = "urp"; 225 226 private final Object lock = new Object(); 227 private Server server = null; 228 } 229