1*5c44d1b3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5c44d1b3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5c44d1b3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5c44d1b3SAndrew Rist  * distributed with this work for additional information
6*5c44d1b3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5c44d1b3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5c44d1b3SAndrew Rist  * "License"); you may not use this file except in compliance
9*5c44d1b3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5c44d1b3SAndrew Rist  *
11*5c44d1b3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5c44d1b3SAndrew Rist  *
13*5c44d1b3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5c44d1b3SAndrew Rist  * software distributed under the License is distributed on an
15*5c44d1b3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5c44d1b3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5c44d1b3SAndrew Rist  * specific language governing permissions and limitations
18*5c44d1b3SAndrew Rist  * under the License.
19*5c44d1b3SAndrew Rist  *
20*5c44d1b3SAndrew Rist  *************************************************************/
21*5c44d1b3SAndrew Rist 
22*5c44d1b3SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package testtools.servicetests;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
27cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
28cdf0e10cSrcweir import com.sun.star.bridge.UnoUrlResolver;
29cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
30cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
31cdf0e10cSrcweir import com.sun.star.connection.XConnection;
32cdf0e10cSrcweir import com.sun.star.container.XSet;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
36cdf0e10cSrcweir import complexlib.ComplexTestCase;
37cdf0e10cSrcweir import java.io.BufferedReader;
38cdf0e10cSrcweir import java.io.InputStream;
39cdf0e10cSrcweir import java.io.InputStreamReader;
40cdf0e10cSrcweir import java.io.PrintStream;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir public final class RemoteServiceTest extends TestBase {
getTestServiceFactory()43cdf0e10cSrcweir     protected TestServiceFactory getTestServiceFactory() throws Exception {
44cdf0e10cSrcweir         final Process p = Runtime.getRuntime().exec(new String[] {
45cdf0e10cSrcweir             "java", "-classpath", System.getProperty("java.class.path"),
46cdf0e10cSrcweir             Server.class.getName() });
47cdf0e10cSrcweir         pipe(p.getInputStream(), System.out, "CO> ");
48cdf0e10cSrcweir         pipe(p.getErrorStream(), System.err, "CE> ");
49cdf0e10cSrcweir         Thread.sleep(5000); // wait for server to start accepting
50cdf0e10cSrcweir         return new TestServiceFactory() {
51cdf0e10cSrcweir                 public Object get() throws Exception {
52cdf0e10cSrcweir                     return (UnoUrlResolver.create(
53cdf0e10cSrcweir                                 Bootstrap.createInitialComponentContext(null))).
54cdf0e10cSrcweir                         resolve(
55cdf0e10cSrcweir                             "uno:" + CONNECTION_DESCRIPTION + ";"
56cdf0e10cSrcweir                             + PROTOCOL_DESCRIPTION
57cdf0e10cSrcweir                             + ";testtools.servicetests.TestService2");
58cdf0e10cSrcweir                 }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir                 public void dispose() throws Exception {
61cdf0e10cSrcweir                     p.waitFor();
62cdf0e10cSrcweir                 }
63cdf0e10cSrcweir             };
64cdf0e10cSrcweir     }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     public static final class Server {
67cdf0e10cSrcweir         public static void main(String[] arguments) throws Exception {
68cdf0e10cSrcweir             XComponentContext context
69cdf0e10cSrcweir                 = Bootstrap.createInitialComponentContext(null);
70cdf0e10cSrcweir             XMultiComponentFactory serviceManager
71cdf0e10cSrcweir                 = context.getServiceManager();
72cdf0e10cSrcweir             UnoRuntime.queryInterface(XSet.class, serviceManager).
73cdf0e10cSrcweir                 insert(new TestService());
74cdf0e10cSrcweir             final Object instance = serviceManager.createInstanceWithContext(
75cdf0e10cSrcweir                 "testtools.servicetests.TestService2", context);
76cdf0e10cSrcweir             XBridgeFactory bridgeFactory
77cdf0e10cSrcweir                 = UnoRuntime.queryInterface(
78cdf0e10cSrcweir                     XBridgeFactory.class,
79cdf0e10cSrcweir                     serviceManager.createInstanceWithContext(
80cdf0e10cSrcweir                         "com.sun.star.bridge.BridgeFactory", context));
81cdf0e10cSrcweir             XConnection connection = Acceptor.create(context).accept(
82cdf0e10cSrcweir                 CONNECTION_DESCRIPTION);
83cdf0e10cSrcweir             bridgeFactory.createBridge(
84cdf0e10cSrcweir                 "", PROTOCOL_DESCRIPTION, connection,
85cdf0e10cSrcweir                 new XInstanceProvider() {
86cdf0e10cSrcweir                     public Object getInstance(String instanceName) {
87cdf0e10cSrcweir                         return instance;
88cdf0e10cSrcweir                     }
89cdf0e10cSrcweir                 });
90cdf0e10cSrcweir         }
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     private void pipe(final InputStream in, final PrintStream out,
94cdf0e10cSrcweir                       final String prefix) {
95cdf0e10cSrcweir         new Thread("Pipe: " + prefix) {
96cdf0e10cSrcweir             public void run() {
97cdf0e10cSrcweir                 BufferedReader r
98cdf0e10cSrcweir                     = new BufferedReader(new InputStreamReader(in));
99cdf0e10cSrcweir                 try {
100cdf0e10cSrcweir                     for (;;) {
101cdf0e10cSrcweir                         String s = r.readLine();
102cdf0e10cSrcweir                         if (s == null) {
103cdf0e10cSrcweir                             break;
104cdf0e10cSrcweir                         }
105cdf0e10cSrcweir                         out.println(prefix + s);
106cdf0e10cSrcweir                     }
107cdf0e10cSrcweir                 } catch (java.io.IOException e) {
108cdf0e10cSrcweir                     e.printStackTrace(System.err);
109cdf0e10cSrcweir                 }
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir         }.start();
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     private static final String CONNECTION_DESCRIPTION
115cdf0e10cSrcweir     = "socket,host=localhost,port=12345";
116cdf0e10cSrcweir     private static final String PROTOCOL_DESCRIPTION = "urp";
117cdf0e10cSrcweir }
118