1 /* 2 * This file is part of the LibreOffice project. 3 * 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/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 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 . 17 */ 18 19 package servicetests; 20 21 import com.sun.star.bridge.XBridgeFactory; 22 import com.sun.star.bridge.XInstanceProvider; 23 import com.sun.star.bridge.XUnoUrlResolver; 24 import com.sun.star.bridge.UnoUrlResolver; 25 import com.sun.star.comp.helper.Bootstrap; 26 import com.sun.star.connection.Acceptor; 27 import com.sun.star.connection.XConnection; 28 import com.sun.star.container.XSet; 29 import com.sun.star.lang.XMultiComponentFactory; 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; 36 37 public final class RemoteServiceTest extends TestBase { 38 @Override getTestServiceFactory()39 protected TestServiceFactory getTestServiceFactory() throws Exception { 40 final Process p = Runtime.getRuntime().exec(new String[] { 41 "java", "-classpath", System.getProperty("java.class.path"), 42 Server.class.getName() }); 43 pipe(p.getInputStream(), System.out, "CO> "); 44 pipe(p.getErrorStream(), System.err, "CE> "); 45 Thread.sleep(5000); // wait for server to start accepting 46 return new TestServiceFactory() { 47 public Object get() throws Exception { 48 XUnoUrlResolver resolver = UnoUrlResolver.create(Bootstrap.createInitialComponentContext(null)); 49 String sUrl = "uno:" + CONNECTION_DESCRIPTION + ";" 50 + PROTOCOL_DESCRIPTION 51 + ";testtools.servicetests.TestService2"; 52 return resolver.resolve(sUrl); 53 } 54 55 public void dispose() throws Exception { 56 p.waitFor(); 57 } 58 }; 59 } 60 61 public static final class Server { 62 public static void main(String[] arguments) throws Exception { 63 XComponentContext context 64 = Bootstrap.createInitialComponentContext(null); 65 XMultiComponentFactory serviceManager 66 = context.getServiceManager(); 67 UnoRuntime.queryInterface(XSet.class, serviceManager). 68 insert(new TestService()); 69 final Object instance = serviceManager.createInstanceWithContext( 70 "testtools.servicetests.TestService2", context); 71 XBridgeFactory bridgeFactory 72 = UnoRuntime.queryInterface( 73 XBridgeFactory.class, 74 serviceManager.createInstanceWithContext( 75 "com.sun.star.bridge.BridgeFactory", context)); 76 XConnection connection = Acceptor.create(context).accept( 77 CONNECTION_DESCRIPTION); 78 bridgeFactory.createBridge( 79 "", PROTOCOL_DESCRIPTION, connection, 80 new XInstanceProvider() { 81 public Object getInstance(String instanceName) { 82 return instance; 83 } 84 }); 85 } 86 } 87 88 private void pipe(final InputStream in, final PrintStream out, 89 final String prefix) { 90 new Thread("Pipe: " + prefix) { 91 @Override 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 private static final String CONNECTION_DESCRIPTION 111 = "socket,host=localhost,port=12345"; 112 private static final String PROTOCOL_DESCRIPTION = "urp"; 113 } 114