1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package test.javauno.nativethreadpool;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.bridge.BridgeExistsException;
31*cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
32*cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
33*cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
34*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
35*cdf0e10cSrcweir import com.sun.star.connection.AlreadyAcceptingException;
36*cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException;
37*cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
38*cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
39*cdf0e10cSrcweir import com.sun.star.connection.XConnection;
40*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetRuntimeException;
41*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
43*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
44*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
45*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir public final class Relay implements XRelay, XSource {
48*cdf0e10cSrcweir     public void start(XSource source) {
49*cdf0e10cSrcweir         this.source = source;
50*cdf0e10cSrcweir         XComponentContext context;
51*cdf0e10cSrcweir         try {
52*cdf0e10cSrcweir             context = Bootstrap.createInitialComponentContext(null);
53*cdf0e10cSrcweir         } catch (RuntimeException e) {
54*cdf0e10cSrcweir             throw e;
55*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
56*cdf0e10cSrcweir             throw new WrappedTargetRuntimeException(e.toString(), this, e);
57*cdf0e10cSrcweir         } catch (Exception e) {
58*cdf0e10cSrcweir             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
59*cdf0e10cSrcweir         }
60*cdf0e10cSrcweir         final XAcceptor acceptor = Acceptor.create(context);
61*cdf0e10cSrcweir         final XBridgeFactory factory;
62*cdf0e10cSrcweir         try {
63*cdf0e10cSrcweir             factory = UnoRuntime.queryInterface(
64*cdf0e10cSrcweir                 XBridgeFactory.class,
65*cdf0e10cSrcweir                 context.getServiceManager().createInstanceWithContext(
66*cdf0e10cSrcweir                     "com.sun.star.bridge.BridgeFactory", context));
67*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
68*cdf0e10cSrcweir             throw new WrappedTargetRuntimeException(e.toString(), this, e);
69*cdf0e10cSrcweir         }
70*cdf0e10cSrcweir         new Thread() {
71*cdf0e10cSrcweir             public void run() {
72*cdf0e10cSrcweir                 try {
73*cdf0e10cSrcweir                     // Use "127.0.0.1" instead of "localhost", see #i32281#:
74*cdf0e10cSrcweir                     factory.createBridge(
75*cdf0e10cSrcweir                         "", "urp",
76*cdf0e10cSrcweir                         acceptor.accept("socket,host=127.0.0.1,port=3831"),
77*cdf0e10cSrcweir                         new XInstanceProvider() {
78*cdf0e10cSrcweir                             public Object getInstance(String instanceName) {
79*cdf0e10cSrcweir                                 return Relay.this;
80*cdf0e10cSrcweir                             }
81*cdf0e10cSrcweir                         });
82*cdf0e10cSrcweir                 } catch (AlreadyAcceptingException e) {
83*cdf0e10cSrcweir                     e.printStackTrace(System.err);
84*cdf0e10cSrcweir                 } catch (ConnectionSetupException e) {
85*cdf0e10cSrcweir                     e.printStackTrace(System.err);
86*cdf0e10cSrcweir                 } catch (BridgeExistsException e) {
87*cdf0e10cSrcweir                     e.printStackTrace(System.err);
88*cdf0e10cSrcweir                 } catch (com.sun.star.lang.IllegalArgumentException e) {
89*cdf0e10cSrcweir                     e.printStackTrace(System.err);
90*cdf0e10cSrcweir                 }
91*cdf0e10cSrcweir             }
92*cdf0e10cSrcweir         }.start();
93*cdf0e10cSrcweir         try {
94*cdf0e10cSrcweir             Thread.sleep(3000); // wait for new thread to accept connection
95*cdf0e10cSrcweir         } catch (InterruptedException e) {
96*cdf0e10cSrcweir             Thread.currentThread().interrupt();
97*cdf0e10cSrcweir             throw new com.sun.star.uno.RuntimeException(e.toString(), this);
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     public int get() {
102*cdf0e10cSrcweir         return source.get();
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(
106*cdf0e10cSrcweir         String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         return implName.equals(implementationName)
109*cdf0e10cSrcweir             ? FactoryHelper.getServiceFactory(
110*cdf0e10cSrcweir                 Relay.class, serviceName, multiFactory, regKey)
111*cdf0e10cSrcweir             : null;
112*cdf0e10cSrcweir     }
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir     public static boolean __writeRegistryServiceInfo(XRegistryKey regKey) {
115*cdf0e10cSrcweir         return FactoryHelper.writeRegistryServiceInfo(
116*cdf0e10cSrcweir             implementationName, serviceName, regKey);
117*cdf0e10cSrcweir     }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     private static final String implementationName
120*cdf0e10cSrcweir     = "test.javauno.nativethreadpool.comp.Relay";
121*cdf0e10cSrcweir     private static final String serviceName
122*cdf0e10cSrcweir     = "test.javauno.nativethreadpool.Relay";
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     private XSource source;
125*cdf0e10cSrcweir }
126