1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package testtools.servicetests;
29 
30 import com.sun.star.bridge.XBridgeFactory;
31 import com.sun.star.bridge.XInstanceProvider;
32 import com.sun.star.bridge.UnoUrlResolver;
33 import com.sun.star.comp.helper.Bootstrap;
34 import com.sun.star.connection.Acceptor;
35 import com.sun.star.connection.XConnection;
36 import com.sun.star.container.XSet;
37 import com.sun.star.lang.XMultiComponentFactory;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.uno.XComponentContext;
40 import complexlib.ComplexTestCase;
41 import java.io.BufferedReader;
42 import java.io.InputStream;
43 import java.io.InputStreamReader;
44 import java.io.PrintStream;
45 
46 public final class RemoteServiceTest extends TestBase {
47     protected TestServiceFactory getTestServiceFactory() throws Exception {
48         final Process p = Runtime.getRuntime().exec(new String[] {
49             "java", "-classpath", System.getProperty("java.class.path"),
50             Server.class.getName() });
51         pipe(p.getInputStream(), System.out, "CO> ");
52         pipe(p.getErrorStream(), System.err, "CE> ");
53         Thread.sleep(5000); // wait for server to start accepting
54         return new TestServiceFactory() {
55                 public Object get() throws Exception {
56                     return (UnoUrlResolver.create(
57                                 Bootstrap.createInitialComponentContext(null))).
58                         resolve(
59                             "uno:" + CONNECTION_DESCRIPTION + ";"
60                             + PROTOCOL_DESCRIPTION
61                             + ";testtools.servicetests.TestService2");
62                 }
63 
64                 public void dispose() throws Exception {
65                     p.waitFor();
66                 }
67             };
68     }
69 
70     public static final class Server {
71         public static void main(String[] arguments) throws Exception {
72             XComponentContext context
73                 = Bootstrap.createInitialComponentContext(null);
74             XMultiComponentFactory serviceManager
75                 = context.getServiceManager();
76             UnoRuntime.queryInterface(XSet.class, serviceManager).
77                 insert(new TestService());
78             final Object instance = serviceManager.createInstanceWithContext(
79                 "testtools.servicetests.TestService2", context);
80             XBridgeFactory bridgeFactory
81                 = UnoRuntime.queryInterface(
82                     XBridgeFactory.class,
83                     serviceManager.createInstanceWithContext(
84                         "com.sun.star.bridge.BridgeFactory", context));
85             XConnection connection = Acceptor.create(context).accept(
86                 CONNECTION_DESCRIPTION);
87             bridgeFactory.createBridge(
88                 "", PROTOCOL_DESCRIPTION, connection,
89                 new XInstanceProvider() {
90                     public Object getInstance(String instanceName) {
91                         return instance;
92                     }
93                 });
94         }
95     }
96 
97     private void pipe(final InputStream in, final PrintStream out,
98                       final String prefix) {
99         new Thread("Pipe: " + prefix) {
100             public void run() {
101                 BufferedReader r
102                     = new BufferedReader(new InputStreamReader(in));
103                 try {
104                     for (;;) {
105                         String s = r.readLine();
106                         if (s == null) {
107                             break;
108                         }
109                         out.println(prefix + s);
110                     }
111                 } catch (java.io.IOException e) {
112                     e.printStackTrace(System.err);
113                 }
114             }
115         }.start();
116     }
117 
118     private static final String CONNECTION_DESCRIPTION
119     = "socket,host=localhost,port=12345";
120     private static final String PROTOCOL_DESCRIPTION = "urp";
121 }
122