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