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 test.javauno.nativethreadpool;
25 
26 import com.sun.star.bridge.BridgeExistsException;
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.comp.loader.FactoryHelper;
31 import com.sun.star.connection.AlreadyAcceptingException;
32 import com.sun.star.connection.ConnectionSetupException;
33 import com.sun.star.connection.Acceptor;
34 import com.sun.star.connection.XAcceptor;
35 import com.sun.star.connection.XConnection;
36 import com.sun.star.lang.WrappedTargetRuntimeException;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.lang.XSingleServiceFactory;
39 import com.sun.star.registry.XRegistryKey;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XComponentContext;
42 
43 public final class Relay implements XRelay, XSource {
start(XSource source)44     public void start(XSource source) {
45         this.source = source;
46         XComponentContext context;
47         try {
48             context = Bootstrap.createInitialComponentContext(null);
49         } catch (RuntimeException e) {
50             throw e;
51         } catch (com.sun.star.uno.Exception e) {
52             throw new WrappedTargetRuntimeException(e.toString(), this, e);
53         } catch (Exception e) {
54             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
55         }
56         final XAcceptor acceptor = Acceptor.create(context);
57         final XBridgeFactory factory;
58         try {
59             factory = UnoRuntime.queryInterface(
60                 XBridgeFactory.class,
61                 context.getServiceManager().createInstanceWithContext(
62                     "com.sun.star.bridge.BridgeFactory", context));
63         } catch (com.sun.star.uno.Exception e) {
64             throw new WrappedTargetRuntimeException(e.toString(), this, e);
65         }
66         new Thread() {
67             public void run() {
68                 try {
69                     // Use "127.0.0.1" instead of "localhost", see #i32281#:
70                     factory.createBridge(
71                         "", "urp",
72                         acceptor.accept("socket,host=127.0.0.1,port=3831"),
73                         new XInstanceProvider() {
74                             public Object getInstance(String instanceName) {
75                                 return Relay.this;
76                             }
77                         });
78                 } catch (AlreadyAcceptingException e) {
79                     e.printStackTrace(System.err);
80                 } catch (ConnectionSetupException e) {
81                     e.printStackTrace(System.err);
82                 } catch (BridgeExistsException e) {
83                     e.printStackTrace(System.err);
84                 } catch (com.sun.star.lang.IllegalArgumentException e) {
85                     e.printStackTrace(System.err);
86                 }
87             }
88         }.start();
89         try {
90             Thread.sleep(3000); // wait for new thread to accept connection
91         } catch (InterruptedException e) {
92             Thread.currentThread().interrupt();
93             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
94         }
95     }
96 
get()97     public int get() {
98         return source.get();
99     }
100 
__getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)101     public static XSingleServiceFactory __getServiceFactory(
102         String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
103     {
104         return implName.equals(implementationName)
105             ? FactoryHelper.getServiceFactory(
106                 Relay.class, serviceName, multiFactory, regKey)
107             : null;
108     }
109 
__writeRegistryServiceInfo(XRegistryKey regKey)110     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
111         return FactoryHelper.writeRegistryServiceInfo(
112             implementationName, serviceName, regKey);
113     }
114 
115     private static final String implementationName
116     = "test.javauno.nativethreadpool.comp.Relay";
117     private static final String serviceName
118     = "test.javauno.nativethreadpool.Relay";
119 
120     private XSource source;
121 }
122