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