1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XUnoUrlResolver.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import lib
.MultiMethodTest
;
34 import lib
.StatusException
;
37 import com
.sun
.star
.bridge
.XBridge
;
38 import com
.sun
.star
.bridge
.XBridgeFactory
;
39 import com
.sun
.star
.bridge
.XInstanceProvider
;
40 import com
.sun
.star
.bridge
.XUnoUrlResolver
;
41 import com
.sun
.star
.connection
.ConnectionSetupException
;
42 import com
.sun
.star
.connection
.NoConnectException
;
43 import com
.sun
.star
.connection
.XAcceptor
;
44 import com
.sun
.star
.connection
.XConnection
;
45 import com
.sun
.star
.lang
.IllegalArgumentException
;
46 import com
.sun
.star
.lang
.XMultiServiceFactory
;
47 import com
.sun
.star
.uno
.UnoRuntime
;
50 * Testing <code>com.sun.star.bridge.XUnoUrlResolver</code>
53 * <li><code> resolve()</code></li>
55 * @see com.sun.star.bridge.XUnoUrlResolver
57 public class _XUnoUrlResolver
extends MultiMethodTest
{
59 // starting port and current port to choose
60 static int basePort
= 0;
63 public XUnoUrlResolver oObj
;
66 * Implementation for providing an instance
68 * @see com.sun.star.bridge.XInstanceProvider
70 class MyInstanceProvider
implements XInstanceProvider
{
72 * a MultiServiceFactory for creating instances
74 * @see com.sun.star.lang.MultiServiceFactory
76 private XMultiServiceFactory xMSF
= null;
79 * Construct object with a MultiServiceFactory
81 * @see com.sun.star.lang.MultiServiceFactory
83 public MyInstanceProvider(XMultiServiceFactory xMSF
) {
88 * get an instance by name
90 public Object
getInstance(String aInstanceName
)
91 throws com
.sun
.star
.container
.NoSuchElementException
94 return xMSF
.createInstance(aInstanceName
);
96 catch(com
.sun
.star
.uno
.Exception e
) {
97 throw new StatusException("Unexpected exception", e
);
103 * Thread for creating a bridge so the resolver can access it
105 class BridgeThread
extends Thread
{
106 private XBridgeFactory xBrdgFctr
= null;
107 private XInstanceProvider xInstProv
= null;
108 private XAcceptor xAcc
= null;
109 private String connectString
= null;
111 public XBridge xBridge
= null;
113 public BridgeThread(XAcceptor xAcc
, XBridgeFactory xBrdgFctr
,
114 XInstanceProvider xInstProv
, String connectString
116 this.xInstProv
= xInstProv
;
117 this.xBrdgFctr
= xBrdgFctr
;
119 this.connectString
= connectString
;
124 // create a connection
125 XConnection xCon
= xAcc
.accept(connectString
);
126 // create a bridge over that conmnection
127 xBridge
= xBrdgFctr
.createBridge(
128 "MyBridge", "urp", xCon
, xInstProv
);
129 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
130 e
.printStackTrace(log
);
131 } catch (com
.sun
.star
.connection
.ConnectionSetupException e
) {
132 e
.printStackTrace(log
);
133 } catch (com
.sun
.star
.connection
.AlreadyAcceptingException e
) {
134 e
.printStackTrace(log
);
135 } catch (com
.sun
.star
.bridge
.BridgeExistsException e
) {
136 e
.printStackTrace(log
);
142 * Test calls the method using environment property
143 * <code>'CNCSTR'</code>. <p>
144 * Has <b> OK </b> status if the method successfully returns
145 * object that support interface <code>XMultiServiceFactory</code> and
146 * no exceptions were thrown. <p>
147 * @see com.sun.star.lang.XMultiServiceFactory
149 public void _resolve() {
150 String connectStr
= (String
)tParam
.get("CNCSTR");
151 int pIndex
= connectStr
.indexOf("port=") + 5;
152 connectStr
= connectStr
.substring(0, pIndex
);
153 System
.out
.println("ConnectString: " + connectStr
);
156 basePort
= ((Integer
)tEnv
.getObjRelation("PORT")).intValue();
157 curPort
= utils
.getNextFreePort(basePort
);
158 log
.println("Choose Port nr: " + curPort
);
160 connectStr
+= curPort
;
163 XMultiServiceFactory xMSF
= (XMultiServiceFactory
)tParam
.getMSF();
165 // get the bridge factory
166 XBridgeFactory xBrdgFctr
= (XBridgeFactory
)
167 UnoRuntime
.queryInterface(XBridgeFactory
.class,
168 tEnv
.getObjRelation("BRIDGEFACTORY"));
171 XAcceptor xAcc
= (XAcceptor
)UnoRuntime
.queryInterface(
172 XAcceptor
.class, tEnv
.getObjRelation("ACCEPTOR"));
175 XInstanceProvider xInstProv
= new MyInstanceProvider(xMSF
);
176 // thread for providing a bridge
177 BridgeThread brThread
= new BridgeThread(xAcc
, xBrdgFctr
,
178 xInstProv
, connectStr
);
184 catch(java
.lang
.InterruptedException e
) {}
185 // get an instance from the remote
186 Object obj
= oObj
.resolve(
187 "uno:" + connectStr
+ ";urp;com.sun.star.lang.ServiceManager");
189 XMultiServiceFactory oMSF
= (XMultiServiceFactory
)
190 UnoRuntime
.queryInterface(XMultiServiceFactory
.class, obj
);
192 if (brThread
.isAlive())
193 brThread
.interrupt();
195 tRes
.tested("resolve()", oMSF
!= null);
196 } catch (NoConnectException e
) {
197 log
.println("Unexpected exception thrown " + e
.getMessage());
198 e
.printStackTrace(log
);
199 throw new StatusException("Unexpected exception", e
);
200 } catch (ConnectionSetupException e
) {
201 log
.println("Unexpected exception thrown " + e
.getMessage());
202 e
.printStackTrace(log
);
203 throw new StatusException("Unexpected exception", e
);
204 } catch (IllegalArgumentException e
) {
205 log
.println("Unexpected exception thrown " + e
.getMessage());
206 e
.printStackTrace(log
);
207 throw new StatusException("Unexpected exception", e
);