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 package com.sun.star.comp.bridge;
28 
29 import com.sun.star.bridge.XBridge;
30 import com.sun.star.bridge.XBridgeFactory;
31 import com.sun.star.bridge.XInstanceProvider;
32 
33 import com.sun.star.uno.XComponentContext;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XComponent;
36 import com.sun.star.lang.XEventListener;
37 import com.sun.star.lang.XMultiComponentFactory;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.container.XSet;
40 
41 import com.sun.star.connection.Acceptor;
42 import com.sun.star.connection.XAcceptor;
43 import com.sun.star.connection.XConnection;
44 
45 import com.sun.star.uno.UnoRuntime;
46 
47 public class TestComponentMain
48 {
49 
50 	static class InstanceProvider implements XInstanceProvider {
51         XComponentContext ctx;
52 
53         public InstanceProvider( XComponentContext ctx )
54         {
55             this.ctx = ctx;
56         }
57 
58 		public Object getInstance( /*IN*/String sInstanceName )
59             throws com.sun.star.container.NoSuchElementException, com.sun.star.uno.RuntimeException
60         {
61             Object o =null;
62             try
63             {
64                 o = ctx.getServiceManager().createInstanceWithContext(
65                     "com.sun.star.comp.bridge.TestComponent$_TestObject" , ctx );
66             }
67             catch( com.sun.star.uno.Exception e )
68             {
69                 System.out.println( "error during instantiation" + e );
70             }
71             return o;
72 		}
73 	}
74 
75 	static public void main(String args[]) throws Exception, com.sun.star.uno.Exception {
76 		if(args.length != 2)	{
77 			System.err.println("usage : com.sun.star.comp.bridge.TestComponentMain uno:connection;protocol;objectName singleaccept");
78 			System.exit(-1);
79 		}
80 
81 		String conDcp = null;
82 		String protDcp = null;
83 		String rootOid = null;
84 
85 		String dcp = args[0];
86         boolean singleaccept = args[1].equals("singleaccept");
87 
88         int index = dcp.indexOf(':');
89         String url = dcp.substring(0, index).trim();
90         dcp = dcp.substring(index + 1).trim();
91 
92         index = dcp.indexOf(';');
93         conDcp = dcp.substring(0, index).trim();
94         dcp = dcp.substring(index + 1).trim();
95 
96         index = dcp.indexOf(';');
97         protDcp = dcp.substring(0, index).trim();
98         dcp = dcp.substring(index + 1).trim();
99 
100         rootOid = dcp.trim().trim();
101 
102         XComponentContext ctx = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
103         XMultiComponentFactory smgr = ctx.getServiceManager();
104         XMultiServiceFactory oldsmgr =
105             UnoRuntime.queryInterface( XMultiServiceFactory.class, smgr );
106 
107         // prepare servicemanager
108         XSet set = UnoRuntime.queryInterface(XSet.class, smgr);
109         Object o = com.sun.star.comp.bridge.TestComponent.__getServiceFactory(
110             "com.sun.star.comp.bridge.TestComponent$_TestObject", oldsmgr,null );
111         set.insert(o);
112 
113 		XAcceptor xAcceptor = Acceptor.create(ctx);
114 
115         while( true )
116         {
117             System.err.println("waiting for connect...");
118 
119             XConnection xConnection = xAcceptor.accept(conDcp);
120 
121             XBridgeFactory xBridgeFactory = UnoRuntime.queryInterface(
122                 XBridgeFactory.class,
123                 smgr.createInstanceWithContext("com.sun.star.bridge.BridgeFactory",ctx));
124 
125             XBridge xBridge = xBridgeFactory.createBridge(
126                 "", protDcp, xConnection, new InstanceProvider(ctx));
127 
128             if (singleaccept) {
129                 Listener listener = new Listener();
130                 UnoRuntime.queryInterface(XComponent.class, xBridge).
131                     addEventListener(listener);
132                 listener.await();
133                 break;
134             }
135         }
136 
137 	}
138 
139     private static final class Listener implements XEventListener {
140         public synchronized void disposing(EventObject source) {
141             done = true;
142             notifyAll();
143         }
144 
145         public synchronized void await() {
146             while (!done) {
147                 try {
148                     wait();
149                 } catch (InterruptedException e) {
150                     Thread.currentThread().interrupt();
151                     throw new RuntimeException(e);
152                 }
153             }
154         }
155 
156         private boolean done = false;
157     }
158 }
159