1*620ba73aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*620ba73aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*620ba73aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*620ba73aSAndrew Rist  * distributed with this work for additional information
6*620ba73aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*620ba73aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*620ba73aSAndrew Rist  * "License"); you may not use this file except in compliance
9*620ba73aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*620ba73aSAndrew Rist  *
11*620ba73aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*620ba73aSAndrew Rist  *
13*620ba73aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*620ba73aSAndrew Rist  * software distributed under the License is distributed on an
15*620ba73aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*620ba73aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*620ba73aSAndrew Rist  * specific language governing permissions and limitations
18*620ba73aSAndrew Rist  * under the License.
19*620ba73aSAndrew Rist  *
20*620ba73aSAndrew Rist  *************************************************************/
21*620ba73aSAndrew Rist 
22*620ba73aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package test.javauno.nativethreadpool;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.bridge.BridgeExistsException;
27cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
28cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
29cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
30cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
31cdf0e10cSrcweir import com.sun.star.connection.AlreadyAcceptingException;
32cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException;
33cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
34cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
35cdf0e10cSrcweir import com.sun.star.connection.XConnection;
36cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetRuntimeException;
37cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
38cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
39cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public final class Relay implements XRelay, XSource {
start(XSource source)44cdf0e10cSrcweir     public void start(XSource source) {
45cdf0e10cSrcweir         this.source = source;
46cdf0e10cSrcweir         XComponentContext context;
47cdf0e10cSrcweir         try {
48cdf0e10cSrcweir             context = Bootstrap.createInitialComponentContext(null);
49cdf0e10cSrcweir         } catch (RuntimeException e) {
50cdf0e10cSrcweir             throw e;
51cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
52cdf0e10cSrcweir             throw new WrappedTargetRuntimeException(e.toString(), this, e);
53cdf0e10cSrcweir         } catch (Exception e) {
54cdf0e10cSrcweir             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
55cdf0e10cSrcweir         }
56cdf0e10cSrcweir         final XAcceptor acceptor = Acceptor.create(context);
57cdf0e10cSrcweir         final XBridgeFactory factory;
58cdf0e10cSrcweir         try {
59cdf0e10cSrcweir             factory = UnoRuntime.queryInterface(
60cdf0e10cSrcweir                 XBridgeFactory.class,
61cdf0e10cSrcweir                 context.getServiceManager().createInstanceWithContext(
62cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory", context));
63cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
64cdf0e10cSrcweir             throw new WrappedTargetRuntimeException(e.toString(), this, e);
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir         new Thread() {
67cdf0e10cSrcweir             public void run() {
68cdf0e10cSrcweir                 try {
69cdf0e10cSrcweir                     // Use "127.0.0.1" instead of "localhost", see #i32281#:
70cdf0e10cSrcweir                     factory.createBridge(
71cdf0e10cSrcweir                         "", "urp",
72cdf0e10cSrcweir                         acceptor.accept("socket,host=127.0.0.1,port=3831"),
73cdf0e10cSrcweir                         new XInstanceProvider() {
74cdf0e10cSrcweir                             public Object getInstance(String instanceName) {
75cdf0e10cSrcweir                                 return Relay.this;
76cdf0e10cSrcweir                             }
77cdf0e10cSrcweir                         });
78cdf0e10cSrcweir                 } catch (AlreadyAcceptingException e) {
79cdf0e10cSrcweir                     e.printStackTrace(System.err);
80cdf0e10cSrcweir                 } catch (ConnectionSetupException e) {
81cdf0e10cSrcweir                     e.printStackTrace(System.err);
82cdf0e10cSrcweir                 } catch (BridgeExistsException e) {
83cdf0e10cSrcweir                     e.printStackTrace(System.err);
84cdf0e10cSrcweir                 } catch (com.sun.star.lang.IllegalArgumentException e) {
85cdf0e10cSrcweir                     e.printStackTrace(System.err);
86cdf0e10cSrcweir                 }
87cdf0e10cSrcweir             }
88cdf0e10cSrcweir         }.start();
89cdf0e10cSrcweir         try {
90cdf0e10cSrcweir             Thread.sleep(3000); // wait for new thread to accept connection
91cdf0e10cSrcweir         } catch (InterruptedException e) {
92cdf0e10cSrcweir             Thread.currentThread().interrupt();
93cdf0e10cSrcweir             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
94cdf0e10cSrcweir         }
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
get()97cdf0e10cSrcweir     public int get() {
98cdf0e10cSrcweir         return source.get();
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir 
__getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)101cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
102cdf0e10cSrcweir         String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         return implName.equals(implementationName)
105cdf0e10cSrcweir             ? FactoryHelper.getServiceFactory(
106cdf0e10cSrcweir                 Relay.class, serviceName, multiFactory, regKey)
107cdf0e10cSrcweir             : null;
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
__writeRegistryServiceInfo(XRegistryKey regKey)110cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
111cdf0e10cSrcweir         return FactoryHelper.writeRegistryServiceInfo(
112cdf0e10cSrcweir             implementationName, serviceName, regKey);
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     private static final String implementationName
116cdf0e10cSrcweir     = "test.javauno.nativethreadpool.comp.Relay";
117cdf0e10cSrcweir     private static final String serviceName
118cdf0e10cSrcweir     = "test.javauno.nativethreadpool.Relay";
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     private XSource source;
121cdf0e10cSrcweir }
122