1*5c44d1b3SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5c44d1b3SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5c44d1b3SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5c44d1b3SAndrew Rist  * distributed with this work for additional information
6*5c44d1b3SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5c44d1b3SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5c44d1b3SAndrew Rist  * "License"); you may not use this file except in compliance
9*5c44d1b3SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5c44d1b3SAndrew Rist  *
11*5c44d1b3SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5c44d1b3SAndrew Rist  *
13*5c44d1b3SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5c44d1b3SAndrew Rist  * software distributed under the License is distributed on an
15*5c44d1b3SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5c44d1b3SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5c44d1b3SAndrew Rist  * specific language governing permissions and limitations
18*5c44d1b3SAndrew Rist  * under the License.
19*5c44d1b3SAndrew Rist  *
20*5c44d1b3SAndrew Rist  *************************************************************/
21*5c44d1b3SAndrew Rist 
22*5c44d1b3SAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.comp.bridge;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.bridge.XBridge;
26cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
27cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
30cdf0e10cSrcweir import com.sun.star.lang.EventObject;
31cdf0e10cSrcweir import com.sun.star.lang.XComponent;
32cdf0e10cSrcweir import com.sun.star.lang.XEventListener;
33cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.container.XSet;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
38cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
39cdf0e10cSrcweir import com.sun.star.connection.XConnection;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class TestComponentMain
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	static class InstanceProvider implements XInstanceProvider {
47cdf0e10cSrcweir         XComponentContext ctx;
48cdf0e10cSrcweir 
InstanceProvider( XComponentContext ctx )49cdf0e10cSrcweir         public InstanceProvider( XComponentContext ctx )
50cdf0e10cSrcweir         {
51cdf0e10cSrcweir             this.ctx = ctx;
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir 
getInstance( String sInstanceName )54cdf0e10cSrcweir 		public Object getInstance( /*IN*/String sInstanceName )
55cdf0e10cSrcweir             throws com.sun.star.container.NoSuchElementException, com.sun.star.uno.RuntimeException
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             Object o =null;
58cdf0e10cSrcweir             try
59cdf0e10cSrcweir             {
60cdf0e10cSrcweir                 o = ctx.getServiceManager().createInstanceWithContext(
61cdf0e10cSrcweir                     "com.sun.star.comp.bridge.TestComponent$_TestObject" , ctx );
62cdf0e10cSrcweir             }
63cdf0e10cSrcweir             catch( com.sun.star.uno.Exception e )
64cdf0e10cSrcweir             {
65cdf0e10cSrcweir                 System.out.println( "error during instantiation" + e );
66cdf0e10cSrcweir             }
67cdf0e10cSrcweir             return o;
68cdf0e10cSrcweir 		}
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir 
main(String args[])71cdf0e10cSrcweir 	static public void main(String args[]) throws Exception, com.sun.star.uno.Exception {
72cdf0e10cSrcweir 		if(args.length != 2)	{
73cdf0e10cSrcweir 			System.err.println("usage : com.sun.star.comp.bridge.TestComponentMain uno:connection;protocol;objectName singleaccept");
74cdf0e10cSrcweir 			System.exit(-1);
75cdf0e10cSrcweir 		}
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		String conDcp = null;
78cdf0e10cSrcweir 		String protDcp = null;
79cdf0e10cSrcweir 		String rootOid = null;
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 		String dcp = args[0];
82cdf0e10cSrcweir         boolean singleaccept = args[1].equals("singleaccept");
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         int index = dcp.indexOf(':');
85cdf0e10cSrcweir         String url = dcp.substring(0, index).trim();
86cdf0e10cSrcweir         dcp = dcp.substring(index + 1).trim();
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         index = dcp.indexOf(';');
89cdf0e10cSrcweir         conDcp = dcp.substring(0, index).trim();
90cdf0e10cSrcweir         dcp = dcp.substring(index + 1).trim();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         index = dcp.indexOf(';');
93cdf0e10cSrcweir         protDcp = dcp.substring(0, index).trim();
94cdf0e10cSrcweir         dcp = dcp.substring(index + 1).trim();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         rootOid = dcp.trim().trim();
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         XComponentContext ctx = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
99cdf0e10cSrcweir         XMultiComponentFactory smgr = ctx.getServiceManager();
100cdf0e10cSrcweir         XMultiServiceFactory oldsmgr =
101cdf0e10cSrcweir             UnoRuntime.queryInterface( XMultiServiceFactory.class, smgr );
102cdf0e10cSrcweir 
103cdf0e10cSrcweir         // prepare servicemanager
104cdf0e10cSrcweir         XSet set = UnoRuntime.queryInterface(XSet.class, smgr);
105cdf0e10cSrcweir         Object o = com.sun.star.comp.bridge.TestComponent.__getServiceFactory(
106cdf0e10cSrcweir             "com.sun.star.comp.bridge.TestComponent$_TestObject", oldsmgr,null );
107cdf0e10cSrcweir         set.insert(o);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		XAcceptor xAcceptor = Acceptor.create(ctx);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir         while( true )
112cdf0e10cSrcweir         {
113cdf0e10cSrcweir             System.err.println("waiting for connect...");
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             XConnection xConnection = xAcceptor.accept(conDcp);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             XBridgeFactory xBridgeFactory = UnoRuntime.queryInterface(
118cdf0e10cSrcweir                 XBridgeFactory.class,
119cdf0e10cSrcweir                 smgr.createInstanceWithContext("com.sun.star.bridge.BridgeFactory",ctx));
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             XBridge xBridge = xBridgeFactory.createBridge(
122cdf0e10cSrcweir                 "", protDcp, xConnection, new InstanceProvider(ctx));
123cdf0e10cSrcweir 
124cdf0e10cSrcweir             if (singleaccept) {
125cdf0e10cSrcweir                 Listener listener = new Listener();
126cdf0e10cSrcweir                 UnoRuntime.queryInterface(XComponent.class, xBridge).
127cdf0e10cSrcweir                     addEventListener(listener);
128cdf0e10cSrcweir                 listener.await();
129cdf0e10cSrcweir                 break;
130cdf0e10cSrcweir             }
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	}
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     private static final class Listener implements XEventListener {
disposing(EventObject source)136cdf0e10cSrcweir         public synchronized void disposing(EventObject source) {
137cdf0e10cSrcweir             done = true;
138cdf0e10cSrcweir             notifyAll();
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir 
await()141cdf0e10cSrcweir         public synchronized void await() {
142cdf0e10cSrcweir             while (!done) {
143cdf0e10cSrcweir                 try {
144cdf0e10cSrcweir                     wait();
145cdf0e10cSrcweir                 } catch (InterruptedException e) {
146cdf0e10cSrcweir                     Thread.currentThread().interrupt();
147cdf0e10cSrcweir                     throw new RuntimeException(e);
148cdf0e10cSrcweir                 }
149cdf0e10cSrcweir             }
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         private boolean done = false;
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir }
155