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 
24 package test.javauno.acquire;
25 
26 import com.sun.star.bridge.UnoUrlResolver;
27 import com.sun.star.bridge.XBridgeFactory;
28 import com.sun.star.bridge.XInstanceProvider;
29 import com.sun.star.comp.helper.Bootstrap;
30 import com.sun.star.connection.Acceptor;
31 import com.sun.star.connection.XAcceptor;
32 import com.sun.star.connection.XConnection;
33 import com.sun.star.lib.uno.helper.UnoUrl;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XComponentContext;
36 import com.sun.star.uno.XInterface;
37 import util.WaitUnreachable;
38 
39 public final class TestAcquire {
main(String[] arguments)40     public static void main(String[] arguments) throws Exception {
41         // - arguments[0] must be "client" or "server"
42         // - arguments[1] must be the UNO URL to connect to (client) or accept
43         //   on (server)
44         XComponentContext context
45             = Bootstrap.createInitialComponentContext(null);
46         if (arguments[0].equals("client")) {
47             execClient(context, arguments[1]);
48         } else {
49             execServer(context, arguments[1]);
50         }
51     }
52 
assertNotNull(Object obj)53     private static void assertNotNull(Object obj) {
54         if (obj == null) {
55             throw new RuntimeException("assertNotNull failed");
56         }
57     }
58 
receive(Object obj)59     private static void receive(Object obj) {
60         assertNotNull(obj);
61         WaitUnreachable.ensureFinalization(obj);
62     }
63 
execClient(XComponentContext context, String url)64     private static void execClient(XComponentContext context, String url)
65         throws Exception
66     {
67         XTest test = UnoRuntime.queryInterface(
68             XTest.class, UnoUrlResolver.create(context).resolve(url));
69 
70         WaitUnreachable u;
71 
72         u = new WaitUnreachable(new XInterface() {});
73         test.setInterfaceToInterface((XInterface) u.get());
74         receive(test.getInterfaceFromInterface());
75         test.clearInterface();
76         u.waitUnreachable();
77         u = new WaitUnreachable(new XBase() {});
78         test.setInterfaceToInterface((XBase) u.get());
79         receive(test.getInterfaceFromInterface());
80         test.clearInterface();
81         u.waitUnreachable();
82         u = new WaitUnreachable(new XDerived() {});
83         test.setInterfaceToInterface((XDerived) u.get());
84         receive(test.getInterfaceFromInterface());
85         test.clearInterface();
86         u.waitUnreachable();
87 
88         u = new WaitUnreachable(new XBase() {});
89         test.setBaseToInterface((XBase) u.get());
90         receive(test.getInterfaceFromInterface());
91         test.clearInterface();
92         u.waitUnreachable();
93         u = new WaitUnreachable(new XDerived() {});
94         test.setBaseToInterface((XDerived) u.get());
95         receive(test.getInterfaceFromInterface());
96         test.clearInterface();
97         u.waitUnreachable();
98 
99         u = new WaitUnreachable(new XDerived() {});
100         test.setDerivedToInterface((XDerived) u.get());
101         receive(test.getInterfaceFromInterface());
102         test.clearInterface();
103         u.waitUnreachable();
104 
105         u = new WaitUnreachable(new XBase() {});
106         test.setBaseToBase((XBase) u.get());
107         receive(test.getInterfaceFromBase());
108         receive(test.getBaseFromBase());
109         test.clearBase();
110         u.waitUnreachable();
111         u = new WaitUnreachable(new XDerived() {});
112         test.setBaseToBase((XDerived) u.get());
113         receive(test.getInterfaceFromBase());
114         receive(test.getBaseFromBase());
115         test.clearBase();
116         u.waitUnreachable();
117 
118         u = new WaitUnreachable(new XDerived() {});
119         test.setDerivedToBase((XDerived) u.get());
120         receive(test.getInterfaceFromBase());
121         receive(test.getBaseFromBase());
122         test.clearBase();
123         u.waitUnreachable();
124 
125         u = new WaitUnreachable(new XDerived() {});
126         test.setDerivedToDerived((XDerived) u.get());
127         receive(test.getInterfaceFromDerived());
128         receive(test.getBaseFromDerived());
129         receive(test.getDerivedFromDerived());
130         test.clearDerived();
131         u.waitUnreachable();
132 
133         u = new WaitUnreachable(new XInterface() {});
134         receive(test.roundTripInterfaceToInterface((XInterface) u.get()));
135         u.waitUnreachable();
136         u = new WaitUnreachable(new XBase() {});
137         receive(test.roundTripInterfaceToInterface((XBase) u.get()));
138         u.waitUnreachable();
139         u = new WaitUnreachable(new XDerived() {});
140         receive(test.roundTripInterfaceToInterface((XDerived) u.get()));
141         u.waitUnreachable();
142 
143         u = new WaitUnreachable(new XBase() {});
144         receive(test.roundTripBaseToInterface((XBase) u.get()));
145         u.waitUnreachable();
146         u = new WaitUnreachable(new XDerived() {});
147         receive(test.roundTripBaseToInterface((XDerived) u.get()));
148         u.waitUnreachable();
149 
150         u = new WaitUnreachable(new XDerived() {});
151         receive(test.roundTripDerivedToInterface((XDerived) u.get()));
152         u.waitUnreachable();
153 
154         u = new WaitUnreachable(new XBase() {});
155         receive(test.roundTripBaseToBase((XBase) u.get()));
156         u.waitUnreachable();
157         u = new WaitUnreachable(new XDerived() {});
158         receive(test.roundTripBaseToBase((XDerived) u.get()));
159         u.waitUnreachable();
160 
161         u = new WaitUnreachable(new XDerived() {});
162         receive(test.roundTripDerivedToBase((XDerived) u.get()));
163         u.waitUnreachable();
164 
165         u = new WaitUnreachable(new XDerived() {});
166         receive(test.roundTripDerivedToDerived((XDerived) u.get()));
167         u.waitUnreachable();
168 
169         u = new WaitUnreachable(test);
170         test = null;
171         u.waitUnreachable();
172         System.out.println(
173             "Client and server both cleanly terminate now: Success");
174     }
175 
execServer(XComponentContext context, String url)176     private static void execServer(XComponentContext context, String url)
177         throws Exception
178     {
179         XAcceptor acceptor = Acceptor.create(context);
180         XBridgeFactory factory = UnoRuntime.queryInterface(
181             XBridgeFactory.class,
182             context.getServiceManager().createInstanceWithContext(
183                 "com.sun.star.bridge.BridgeFactory", context));
184         UnoUrl unoUrl = UnoUrl.parseUnoUrl(url);
185         System.out.println("Server: Accepting...");
186         XConnection connection = acceptor.accept(
187             unoUrl.getConnectionAndParametersAsString());
188         System.out.println("Server: ...connected...");
189         factory.createBridge(
190             "", unoUrl.getProtocolAndParametersAsString(), connection,
191             new Provider());
192         System.out.println("Server: ...bridged.");
193     }
194 
195     private static final class Provider implements XInstanceProvider {
getInstance(String instanceName)196         public Object getInstance(String instanceName) {
197             return new XTest() {
198                     public void setInterfaceToInterface(Object obj) {
199                         iface = obj;
200                     }
201 
202                     public void setBaseToInterface(XBase obj) {
203                         iface = obj;
204                     }
205 
206                     public void setDerivedToInterface(XDerived obj) {
207                         iface = obj;
208                     }
209 
210                     public Object getInterfaceFromInterface() {
211                         return iface;
212                     }
213 
214                     public void clearInterface() {
215                         WaitUnreachable u = new WaitUnreachable(iface);
216                         iface = null;
217                         u.waitUnreachable();
218                     }
219 
220                     public void setBaseToBase(XBase obj) {
221                         base = obj;
222                     }
223 
224                     public void setDerivedToBase(XDerived obj) {
225                         base = obj;
226                     }
227 
228                     public Object getInterfaceFromBase() {
229                         return base;
230                     }
231 
232                     public XBase getBaseFromBase() {
233                         return base;
234                     }
235 
236                     public void clearBase() {
237                         WaitUnreachable u = new WaitUnreachable(base);
238                         base = null;
239                         u.waitUnreachable();
240                     }
241 
242                     public void setDerivedToDerived(XDerived obj) {
243                         derived = obj;
244                     }
245 
246                     public Object getInterfaceFromDerived() {
247                         return derived;
248                     }
249 
250                     public XBase getBaseFromDerived() {
251                         return derived;
252                     }
253 
254                     public XDerived getDerivedFromDerived() {
255                         return derived;
256                     }
257 
258                     public void clearDerived() {
259                         WaitUnreachable u = new WaitUnreachable(derived);
260                         derived = null;
261                         u.waitUnreachable();
262                     }
263 
264                     public Object roundTripInterfaceToInterface(Object obj) {
265                         WaitUnreachable.ensureFinalization(obj);
266                         return obj;
267                     }
268 
269                     public Object roundTripBaseToInterface(XBase obj) {
270                         WaitUnreachable.ensureFinalization(obj);
271                         return obj;
272                     }
273 
274                     public Object roundTripDerivedToInterface(XDerived obj) {
275                         WaitUnreachable.ensureFinalization(obj);
276                         return obj;
277                     }
278 
279                     public XBase roundTripBaseToBase(XBase obj) {
280                         WaitUnreachable.ensureFinalization(obj);
281                         return obj;
282                     }
283 
284                     public XBase roundTripDerivedToBase(XDerived obj) {
285                         WaitUnreachable.ensureFinalization(obj);
286                         return obj;
287                     }
288 
289                     public XDerived roundTripDerivedToDerived(XDerived obj) {
290                         WaitUnreachable.ensureFinalization(obj);
291                         return obj;
292                     }
293 
294                     private Object iface;
295                     private XBase base;
296                     private XDerived derived;
297                 };
298         }
299     }
300 }
301