1*620ba73aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*620ba73aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*620ba73aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*620ba73aSAndrew Rist  * distributed with this work for additional information
6*620ba73aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*620ba73aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*620ba73aSAndrew Rist  * "License"); you may not use this file except in compliance
9*620ba73aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*620ba73aSAndrew Rist  *
11*620ba73aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*620ba73aSAndrew Rist  *
13*620ba73aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*620ba73aSAndrew Rist  * software distributed under the License is distributed on an
15*620ba73aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*620ba73aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*620ba73aSAndrew Rist  * specific language governing permissions and limitations
18*620ba73aSAndrew Rist  * under the License.
19*620ba73aSAndrew Rist  *
20*620ba73aSAndrew Rist  *************************************************************/
21*620ba73aSAndrew Rist 
22*620ba73aSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package test.javauno.acquire;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.bridge.UnoUrlResolver;
27cdf0e10cSrcweir import com.sun.star.bridge.XBridgeFactory;
28cdf0e10cSrcweir import com.sun.star.bridge.XInstanceProvider;
29cdf0e10cSrcweir import com.sun.star.comp.helper.Bootstrap;
30cdf0e10cSrcweir import com.sun.star.connection.Acceptor;
31cdf0e10cSrcweir import com.sun.star.connection.XAcceptor;
32cdf0e10cSrcweir import com.sun.star.connection.XConnection;
33cdf0e10cSrcweir import com.sun.star.lib.uno.helper.UnoUrl;
34cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
35cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
36cdf0e10cSrcweir import com.sun.star.uno.XInterface;
37cdf0e10cSrcweir import util.WaitUnreachable;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir public final class TestAcquire {
main(String[] arguments)40cdf0e10cSrcweir     public static void main(String[] arguments) throws Exception {
41cdf0e10cSrcweir         // - arguments[0] must be "client" or "server"
42cdf0e10cSrcweir         // - arguments[1] must be the UNO URL to connect to (client) or accept
43cdf0e10cSrcweir         //   on (server)
44cdf0e10cSrcweir         XComponentContext context
45cdf0e10cSrcweir             = Bootstrap.createInitialComponentContext(null);
46cdf0e10cSrcweir         if (arguments[0].equals("client")) {
47cdf0e10cSrcweir             execClient(context, arguments[1]);
48cdf0e10cSrcweir         } else {
49cdf0e10cSrcweir             execServer(context, arguments[1]);
50cdf0e10cSrcweir         }
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
assertNotNull(Object obj)53cdf0e10cSrcweir     private static void assertNotNull(Object obj) {
54cdf0e10cSrcweir         if (obj == null) {
55cdf0e10cSrcweir             throw new RuntimeException("assertNotNull failed");
56cdf0e10cSrcweir         }
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
receive(Object obj)59cdf0e10cSrcweir     private static void receive(Object obj) {
60cdf0e10cSrcweir         assertNotNull(obj);
61cdf0e10cSrcweir         WaitUnreachable.ensureFinalization(obj);
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
execClient(XComponentContext context, String url)64cdf0e10cSrcweir     private static void execClient(XComponentContext context, String url)
65cdf0e10cSrcweir         throws Exception
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         XTest test = UnoRuntime.queryInterface(
68cdf0e10cSrcweir             XTest.class, UnoUrlResolver.create(context).resolve(url));
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         WaitUnreachable u;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         u = new WaitUnreachable(new XInterface() {});
73cdf0e10cSrcweir         test.setInterfaceToInterface((XInterface) u.get());
74cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
75cdf0e10cSrcweir         test.clearInterface();
76cdf0e10cSrcweir         u.waitUnreachable();
77cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
78cdf0e10cSrcweir         test.setInterfaceToInterface((XBase) u.get());
79cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
80cdf0e10cSrcweir         test.clearInterface();
81cdf0e10cSrcweir         u.waitUnreachable();
82cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
83cdf0e10cSrcweir         test.setInterfaceToInterface((XDerived) u.get());
84cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
85cdf0e10cSrcweir         test.clearInterface();
86cdf0e10cSrcweir         u.waitUnreachable();
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
89cdf0e10cSrcweir         test.setBaseToInterface((XBase) u.get());
90cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
91cdf0e10cSrcweir         test.clearInterface();
92cdf0e10cSrcweir         u.waitUnreachable();
93cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
94cdf0e10cSrcweir         test.setBaseToInterface((XDerived) u.get());
95cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
96cdf0e10cSrcweir         test.clearInterface();
97cdf0e10cSrcweir         u.waitUnreachable();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
100cdf0e10cSrcweir         test.setDerivedToInterface((XDerived) u.get());
101cdf0e10cSrcweir         receive(test.getInterfaceFromInterface());
102cdf0e10cSrcweir         test.clearInterface();
103cdf0e10cSrcweir         u.waitUnreachable();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
106cdf0e10cSrcweir         test.setBaseToBase((XBase) u.get());
107cdf0e10cSrcweir         receive(test.getInterfaceFromBase());
108cdf0e10cSrcweir         receive(test.getBaseFromBase());
109cdf0e10cSrcweir         test.clearBase();
110cdf0e10cSrcweir         u.waitUnreachable();
111cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
112cdf0e10cSrcweir         test.setBaseToBase((XDerived) u.get());
113cdf0e10cSrcweir         receive(test.getInterfaceFromBase());
114cdf0e10cSrcweir         receive(test.getBaseFromBase());
115cdf0e10cSrcweir         test.clearBase();
116cdf0e10cSrcweir         u.waitUnreachable();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
119cdf0e10cSrcweir         test.setDerivedToBase((XDerived) u.get());
120cdf0e10cSrcweir         receive(test.getInterfaceFromBase());
121cdf0e10cSrcweir         receive(test.getBaseFromBase());
122cdf0e10cSrcweir         test.clearBase();
123cdf0e10cSrcweir         u.waitUnreachable();
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
126cdf0e10cSrcweir         test.setDerivedToDerived((XDerived) u.get());
127cdf0e10cSrcweir         receive(test.getInterfaceFromDerived());
128cdf0e10cSrcweir         receive(test.getBaseFromDerived());
129cdf0e10cSrcweir         receive(test.getDerivedFromDerived());
130cdf0e10cSrcweir         test.clearDerived();
131cdf0e10cSrcweir         u.waitUnreachable();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         u = new WaitUnreachable(new XInterface() {});
134cdf0e10cSrcweir         receive(test.roundTripInterfaceToInterface((XInterface) u.get()));
135cdf0e10cSrcweir         u.waitUnreachable();
136cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
137cdf0e10cSrcweir         receive(test.roundTripInterfaceToInterface((XBase) u.get()));
138cdf0e10cSrcweir         u.waitUnreachable();
139cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
140cdf0e10cSrcweir         receive(test.roundTripInterfaceToInterface((XDerived) u.get()));
141cdf0e10cSrcweir         u.waitUnreachable();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
144cdf0e10cSrcweir         receive(test.roundTripBaseToInterface((XBase) u.get()));
145cdf0e10cSrcweir         u.waitUnreachable();
146cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
147cdf0e10cSrcweir         receive(test.roundTripBaseToInterface((XDerived) u.get()));
148cdf0e10cSrcweir         u.waitUnreachable();
149cdf0e10cSrcweir 
150cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
151cdf0e10cSrcweir         receive(test.roundTripDerivedToInterface((XDerived) u.get()));
152cdf0e10cSrcweir         u.waitUnreachable();
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         u = new WaitUnreachable(new XBase() {});
155cdf0e10cSrcweir         receive(test.roundTripBaseToBase((XBase) u.get()));
156cdf0e10cSrcweir         u.waitUnreachable();
157cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
158cdf0e10cSrcweir         receive(test.roundTripBaseToBase((XDerived) u.get()));
159cdf0e10cSrcweir         u.waitUnreachable();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
162cdf0e10cSrcweir         receive(test.roundTripDerivedToBase((XDerived) u.get()));
163cdf0e10cSrcweir         u.waitUnreachable();
164cdf0e10cSrcweir 
165cdf0e10cSrcweir         u = new WaitUnreachable(new XDerived() {});
166cdf0e10cSrcweir         receive(test.roundTripDerivedToDerived((XDerived) u.get()));
167cdf0e10cSrcweir         u.waitUnreachable();
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         u = new WaitUnreachable(test);
170cdf0e10cSrcweir         test = null;
171cdf0e10cSrcweir         u.waitUnreachable();
172cdf0e10cSrcweir         System.out.println(
173cdf0e10cSrcweir             "Client and server both cleanly terminate now: Success");
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
execServer(XComponentContext context, String url)176cdf0e10cSrcweir     private static void execServer(XComponentContext context, String url)
177cdf0e10cSrcweir         throws Exception
178cdf0e10cSrcweir     {
179cdf0e10cSrcweir         XAcceptor acceptor = Acceptor.create(context);
180cdf0e10cSrcweir         XBridgeFactory factory = UnoRuntime.queryInterface(
181cdf0e10cSrcweir             XBridgeFactory.class,
182cdf0e10cSrcweir             context.getServiceManager().createInstanceWithContext(
183cdf0e10cSrcweir                 "com.sun.star.bridge.BridgeFactory", context));
184cdf0e10cSrcweir         UnoUrl unoUrl = UnoUrl.parseUnoUrl(url);
185cdf0e10cSrcweir         System.out.println("Server: Accepting...");
186cdf0e10cSrcweir         XConnection connection = acceptor.accept(
187cdf0e10cSrcweir             unoUrl.getConnectionAndParametersAsString());
188cdf0e10cSrcweir         System.out.println("Server: ...connected...");
189cdf0e10cSrcweir         factory.createBridge(
190cdf0e10cSrcweir             "", unoUrl.getProtocolAndParametersAsString(), connection,
191cdf0e10cSrcweir             new Provider());
192cdf0e10cSrcweir         System.out.println("Server: ...bridged.");
193cdf0e10cSrcweir     }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     private static final class Provider implements XInstanceProvider {
getInstance(String instanceName)196cdf0e10cSrcweir         public Object getInstance(String instanceName) {
197cdf0e10cSrcweir             return new XTest() {
198cdf0e10cSrcweir                     public void setInterfaceToInterface(Object obj) {
199cdf0e10cSrcweir                         iface = obj;
200cdf0e10cSrcweir                     }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                     public void setBaseToInterface(XBase obj) {
203cdf0e10cSrcweir                         iface = obj;
204cdf0e10cSrcweir                     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                     public void setDerivedToInterface(XDerived obj) {
207cdf0e10cSrcweir                         iface = obj;
208cdf0e10cSrcweir                     }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir                     public Object getInterfaceFromInterface() {
211cdf0e10cSrcweir                         return iface;
212cdf0e10cSrcweir                     }
213cdf0e10cSrcweir 
214cdf0e10cSrcweir                     public void clearInterface() {
215cdf0e10cSrcweir                         WaitUnreachable u = new WaitUnreachable(iface);
216cdf0e10cSrcweir                         iface = null;
217cdf0e10cSrcweir                         u.waitUnreachable();
218cdf0e10cSrcweir                     }
219cdf0e10cSrcweir 
220cdf0e10cSrcweir                     public void setBaseToBase(XBase obj) {
221cdf0e10cSrcweir                         base = obj;
222cdf0e10cSrcweir                     }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                     public void setDerivedToBase(XDerived obj) {
225cdf0e10cSrcweir                         base = obj;
226cdf0e10cSrcweir                     }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir                     public Object getInterfaceFromBase() {
229cdf0e10cSrcweir                         return base;
230cdf0e10cSrcweir                     }
231cdf0e10cSrcweir 
232cdf0e10cSrcweir                     public XBase getBaseFromBase() {
233cdf0e10cSrcweir                         return base;
234cdf0e10cSrcweir                     }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir                     public void clearBase() {
237cdf0e10cSrcweir                         WaitUnreachable u = new WaitUnreachable(base);
238cdf0e10cSrcweir                         base = null;
239cdf0e10cSrcweir                         u.waitUnreachable();
240cdf0e10cSrcweir                     }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir                     public void setDerivedToDerived(XDerived obj) {
243cdf0e10cSrcweir                         derived = obj;
244cdf0e10cSrcweir                     }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir                     public Object getInterfaceFromDerived() {
247cdf0e10cSrcweir                         return derived;
248cdf0e10cSrcweir                     }
249cdf0e10cSrcweir 
250cdf0e10cSrcweir                     public XBase getBaseFromDerived() {
251cdf0e10cSrcweir                         return derived;
252cdf0e10cSrcweir                     }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir                     public XDerived getDerivedFromDerived() {
255cdf0e10cSrcweir                         return derived;
256cdf0e10cSrcweir                     }
257cdf0e10cSrcweir 
258cdf0e10cSrcweir                     public void clearDerived() {
259cdf0e10cSrcweir                         WaitUnreachable u = new WaitUnreachable(derived);
260cdf0e10cSrcweir                         derived = null;
261cdf0e10cSrcweir                         u.waitUnreachable();
262cdf0e10cSrcweir                     }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir                     public Object roundTripInterfaceToInterface(Object obj) {
265cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
266cdf0e10cSrcweir                         return obj;
267cdf0e10cSrcweir                     }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir                     public Object roundTripBaseToInterface(XBase obj) {
270cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
271cdf0e10cSrcweir                         return obj;
272cdf0e10cSrcweir                     }
273cdf0e10cSrcweir 
274cdf0e10cSrcweir                     public Object roundTripDerivedToInterface(XDerived obj) {
275cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
276cdf0e10cSrcweir                         return obj;
277cdf0e10cSrcweir                     }
278cdf0e10cSrcweir 
279cdf0e10cSrcweir                     public XBase roundTripBaseToBase(XBase obj) {
280cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
281cdf0e10cSrcweir                         return obj;
282cdf0e10cSrcweir                     }
283cdf0e10cSrcweir 
284cdf0e10cSrcweir                     public XBase roundTripDerivedToBase(XDerived obj) {
285cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
286cdf0e10cSrcweir                         return obj;
287cdf0e10cSrcweir                     }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir                     public XDerived roundTripDerivedToDerived(XDerived obj) {
290cdf0e10cSrcweir                         WaitUnreachable.ensureFinalization(obj);
291cdf0e10cSrcweir                         return obj;
292cdf0e10cSrcweir                     }
293cdf0e10cSrcweir 
294cdf0e10cSrcweir                     private Object iface;
295cdf0e10cSrcweir                     private XBase base;
296cdf0e10cSrcweir                     private XDerived derived;
297cdf0e10cSrcweir                 };
298cdf0e10cSrcweir         }
299cdf0e10cSrcweir     }
300cdf0e10cSrcweir }
301