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