Update ooo320-m1
[ooovba.git] / qadevOOo / runner / basicrunner / basichelper / Connector.java
blobbdf8179395f009dd83dee59b49d7188492475c61
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Connector.java,v $
10 * $Revision: 1.3 $
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 ************************************************************************/
30 package basicrunner.basichelper;
32 import com.sun.star.lang.XInitialization;
33 import com.sun.star.lang.XServiceInfo;
34 import com.sun.star.lang.XTypeProvider;
35 import com.sun.star.uno.Type;
36 import com.sun.star.connection.XConnector;
37 import com.sun.star.connection.XConnection;
38 import com.sun.star.connection.ConnectionSetupException;
39 import com.sun.star.connection.NoConnectException;
40 import com.sun.star.container.XNameAccess;
41 import com.sun.star.container.NoSuchElementException;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.lang.XSingleServiceFactory;
45 /**
46 * This is a special service that is used in testing Acceptor
47 * component in BASIC. This componennt creates a separate thread
48 * that tries to connect to BASIC's acceptor. After successfull
49 * connection it writes a connectionString to XConnection.
51 public class Connector implements XServiceInfo, XSingleServiceFactory {
52 /** The service name of this class **/
53 static final String __serviceName = "basichelper.Connector";
54 /** The Connector implementation **/
55 static ConnectorImpl oConnector = null;
57 /** Create a connector.
58 */
59 public Connector() {
60 oConnector = new ConnectorImpl();
63 /**
64 * Returns an instance of the connector.
65 * Arguments are not supported here and will be ignored.
66 * @param args The arguments.
67 * @return The connector.
69 public Object createInstanceWithArguments(Object[] args) {
70 return oConnector;
73 /**
74 * Returns an instance of the connector.
75 * @return The connector.
77 public Object createInstance() {
78 return createInstanceWithArguments(null);
81 /**
82 * Get a unique id for this implementation.
83 * @return The id.
85 public byte[] getImplementationId() {
86 return toString().getBytes();
89 /**
90 * Return all implemented types of this class.
91 * @return The implemented UNO types.
93 public Type[] getTypes() {
94 Class interfaces[] = getClass().getInterfaces();
96 Type types[] = new Type[interfaces.length];
97 for(int i = 0; i < interfaces.length; ++ i)
98 types[i] = new Type(interfaces[i]);
100 return types;
103 /** Is this servioce supported?
104 * @param name The service name.
105 * @return True, if the service is supported.
107 public boolean supportsService(String name) {
108 return __serviceName.equals(name);
112 * Get all supported service names.
113 * @return All supported servcices.
115 public String[] getSupportedServiceNames() {
116 return new String[] {__serviceName};
120 * Get the implementation name of this class.
121 * @return The implementation name.
123 public String getImplementationName() {
124 return getClass().getName();
129 * The actual implementation of the connector
130 * @see com.sun.star.lang.XInitialization
131 * @see com.sun.star.lang.XTypeProvider
132 * @see com.sun.star.container.XNameAccess
134 class ConnectorImpl implements XInitialization, XTypeProvider, XNameAccess {
135 static String aState;
136 static Integer iTimeout;
139 * Construct a new connector.
141 public ConnectorImpl() {
142 aState = "just created";
143 iTimeout = new Integer(3000);
147 * Method initialize() creates a new thread that will try to connect to
148 * Acceptor for a few seconds. One should pass as parameters an array,
149 * where element 0 is an instance of Connector and element 1 is a
150 * connection string (the same as in Acceptor)
151 * @param parm1 An instance of XConnector.
152 * @see com.sun.star.connection.XConnector
153 * @throws Exception Is thrown, when initialize fails.
155 public void initialize(Object[] parm1) throws com.sun.star.uno.Exception {
156 aState = "just initialized";
157 XConnector cntr = (XConnector)UnoRuntime.queryInterface(
158 XConnector.class, parm1[0]);
159 ConnThread aThread = new ConnThread(cntr, (String)parm1[1]);
160 aThread.start();
164 * Get the element names
165 * @return All element names.
167 public String[] getElementNames() {
168 return new String[]{"State", "Timeout"};
172 * Does this element exist?
173 * @param name The element name.
174 * @return True, if the name exists.
176 public boolean hasByName(String name) {
177 return (name.equals("State") || name.equals("Timeout"));
181 * Get an element by its name.
182 * @param name The name of the element.
183 * @return The value of the element.
184 * @throws NoSuchElementException The element does not exist.
186 public Object getByName(String name) throws NoSuchElementException{
187 if (name.equals("State"))
188 return aState;
189 else if (name.equals("Timeout"))
190 return iTimeout;
191 else
192 throw new NoSuchElementException();
196 * Are there elements
197 * @return Always true.
199 public boolean hasElements() {
200 return true;
204 * Get element type.
205 * @return null.
207 public Type getElementType() {
208 return null;
212 * Get a unique id for this implementation.
213 * @return The id.
215 public byte[] getImplementationId() {
216 return toString().getBytes();
220 * Return all implemented types of this class.
221 * @return The implemented UNO types.
223 public Type[] getTypes() {
224 Class interfaces[] = getClass().getInterfaces();
226 Type types[] = new Type[interfaces.length];
227 for(int i = 0; i < interfaces.length; ++ i)
228 types[i] = new Type(interfaces[i]);
230 return types;
235 * A connector thread
237 class ConnThread extends Thread {
238 String connStr;
239 XConnector oConnector;
241 /**Construct the thread.
242 * @param oCntr A connector.
243 * @param cStr The conection string.
245 public ConnThread(XConnector oCntr, String cStr){
246 connStr = cStr;
247 oConnector = oCntr;
251 * Run the thread.
253 public void run(){
254 try {
255 Thread.sleep(ConnectorImpl.iTimeout.intValue());
256 ConnectorImpl.aState = "before connection";
257 XConnection oConnection = oConnector.connect(connStr);
258 if (oConnection != null) {
259 ConnectorImpl.aState = "connected";
260 oConnection.write(connStr.getBytes());
261 oConnection.write(new byte[]{0});
262 } else
263 ConnectorImpl.aState = "XConnection is null";
264 } catch (ConnectionSetupException e) {
265 ConnectorImpl.aState = "ConnectionSetupException";
266 throw new RuntimeException(e.toString());
267 } catch (NoConnectException e) {
268 ConnectorImpl.aState = "NoConnectException";
269 throw new RuntimeException(e.toString());
270 } catch (Exception e) {
271 ConnectorImpl.aState = "error";
272 throw new RuntimeException("Can't sleep exception");