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 testtools.servicetests;
25 
26 import com.sun.star.bridge.XBridgeFactory;
27 import com.sun.star.bridge.XInstanceProvider;
28 import com.sun.star.bridge.UnoUrlResolver;
29 import com.sun.star.comp.helper.Bootstrap;
30 import com.sun.star.connection.Acceptor;
31 import com.sun.star.connection.XConnection;
32 import com.sun.star.container.XSet;
33 import com.sun.star.lang.XMultiComponentFactory;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XComponentContext;
36 import complexlib.ComplexTestCase;
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 RemoteServiceTest extends TestBase {
getTestServiceFactory()43     protected TestServiceFactory getTestServiceFactory() throws Exception {
44         final Process p = Runtime.getRuntime().exec(new String[] {
45             "java", "-classpath", System.getProperty("java.class.path"),
46             Server.class.getName() });
47         pipe(p.getInputStream(), System.out, "CO> ");
48         pipe(p.getErrorStream(), System.err, "CE> ");
49         Thread.sleep(5000); // wait for server to start accepting
50         return new TestServiceFactory() {
51                 public Object get() throws Exception {
52                     return (UnoUrlResolver.create(
53                                 Bootstrap.createInitialComponentContext(null))).
54                         resolve(
55                             "uno:" + CONNECTION_DESCRIPTION + ";"
56                             + PROTOCOL_DESCRIPTION
57                             + ";testtools.servicetests.TestService2");
58                 }
59 
60                 public void dispose() throws Exception {
61                     p.waitFor();
62                 }
63             };
64     }
65 
66     public static final class Server {
67         public static void main(String[] arguments) throws Exception {
68             XComponentContext context
69                 = Bootstrap.createInitialComponentContext(null);
70             XMultiComponentFactory serviceManager
71                 = context.getServiceManager();
72             UnoRuntime.queryInterface(XSet.class, serviceManager).
73                 insert(new TestService());
74             final Object instance = serviceManager.createInstanceWithContext(
75                 "testtools.servicetests.TestService2", context);
76             XBridgeFactory bridgeFactory
77                 = UnoRuntime.queryInterface(
78                     XBridgeFactory.class,
79                     serviceManager.createInstanceWithContext(
80                         "com.sun.star.bridge.BridgeFactory", context));
81             XConnection connection = Acceptor.create(context).accept(
82                 CONNECTION_DESCRIPTION);
83             bridgeFactory.createBridge(
84                 "", PROTOCOL_DESCRIPTION, connection,
85                 new XInstanceProvider() {
86                     public Object getInstance(String instanceName) {
87                         return instance;
88                     }
89                 });
90         }
91     }
92 
93     private void pipe(final InputStream in, final PrintStream out,
94                       final String prefix) {
95         new Thread("Pipe: " + prefix) {
96             public void run() {
97                 BufferedReader r
98                     = new BufferedReader(new InputStreamReader(in));
99                 try {
100                     for (;;) {
101                         String s = r.readLine();
102                         if (s == null) {
103                             break;
104                         }
105                         out.println(prefix + s);
106                     }
107                 } catch (java.io.IOException e) {
108                     e.printStackTrace(System.err);
109                 }
110             }
111         }.start();
112     }
113 
114     private static final String CONNECTION_DESCRIPTION
115     = "socket,host=localhost,port=12345";
116     private static final String PROTOCOL_DESCRIPTION = "urp";
117 }
118