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 package basicrunner.basichelper;
28 
29 import com.sun.star.lang.XInitialization;
30 import com.sun.star.lang.XServiceInfo;
31 import com.sun.star.lang.XTypeProvider;
32 import com.sun.star.uno.Type;
33 import com.sun.star.connection.XConnector;
34 import com.sun.star.connection.XConnection;
35 import com.sun.star.connection.ConnectionSetupException;
36 import com.sun.star.connection.NoConnectException;
37 import com.sun.star.container.XNameAccess;
38 import com.sun.star.container.NoSuchElementException;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.lang.XSingleServiceFactory;
41 
42 /**
43  * This is a special service that is used in testing Acceptor
44  * component in BASIC. This componennt creates a separate thread
45  * that tries to connect to BASIC's acceptor. After successfull
46  * connection it writes a connectionString to XConnection.
47  */
48  public class Connector implements XServiceInfo, XSingleServiceFactory {
49     /** The service name of this class **/
50     static final String __serviceName = "basichelper.Connector";
51     /** The Connector implementation **/
52     static ConnectorImpl oConnector = null;
53 
54    /** Create a connector.
55     */
56    public Connector() {
57         oConnector = new ConnectorImpl();
58     }
59 
60     /**
61      * Returns an instance of the connector.
62      * Arguments are not supported here and will be ignored.
63      * @param args The arguments.
64      * @return The connector.
65      */
66     public Object createInstanceWithArguments(Object[] args) {
67         return oConnector;
68     }
69 
70     /**
71      * Returns an instance of the connector.
72      * @return The connector.
73      */
74     public Object createInstance() {
75         return createInstanceWithArguments(null);
76     }
77 
78     /**
79      * Get a unique id for this implementation.
80      * @return The id.
81      */
82     public byte[] getImplementationId() {
83         return toString().getBytes();
84     }
85 
86     /**
87      * Return all implemented types of this class.
88      * @return The implemented UNO types.
89      */
90     public Type[] getTypes() {
91         Class interfaces[] = getClass().getInterfaces();
92 
93         Type types[] = new Type[interfaces.length];
94         for(int i = 0; i < interfaces.length; ++ i)
95             types[i] = new Type(interfaces[i]);
96 
97         return types;
98     }
99 
100     /** Is this servioce supported?
101      * @param name The service name.
102      * @return True, if the service is supported.
103      */
104     public boolean supportsService(String name) {
105         return __serviceName.equals(name);
106     }
107 
108     /**
109      * Get all supported service names.
110      * @return All supported servcices.
111      */
112     public String[] getSupportedServiceNames() {
113         return new String[] {__serviceName};
114     }
115 
116     /**
117      * Get the implementation name of this class.
118      * @return The implementation name.
119      */
120     public String getImplementationName() {
121         return getClass().getName();
122     }
123 }
124 
125 /**
126  * The actual implementation of the connector
127  * @see com.sun.star.lang.XInitialization
128  * @see com.sun.star.lang.XTypeProvider
129  * @see com.sun.star.container.XNameAccess
130  */
131 class ConnectorImpl implements XInitialization, XTypeProvider, XNameAccess {
132     static String aState;
133     static Integer iTimeout;
134 
135     /**
136      * Construct a new connector.
137      */
138     public ConnectorImpl() {
139         aState = "just created";
140         iTimeout = new Integer(3000);
141     }
142 
143     /**
144      * Method initialize() creates a new thread that will try to connect to
145      * Acceptor for a few seconds. One should pass as parameters an array,
146      * where element 0 is an instance of Connector and element 1 is a
147      * connection string (the same as in Acceptor)
148      * @param parm1 An instance of XConnector.
149      * @see com.sun.star.connection.XConnector
150      * @throws Exception Is thrown, when initialize fails.
151      */
152     public void initialize(Object[] parm1) throws com.sun.star.uno.Exception {
153         aState = "just initialized";
154         XConnector cntr = (XConnector)UnoRuntime.queryInterface(
155                                                 XConnector.class, parm1[0]);
156         ConnThread aThread = new ConnThread(cntr, (String)parm1[1]);
157         aThread.start();
158     }
159 
160     /**
161      * Get the element names
162      * @return All element names.
163      */
164     public String[] getElementNames() {
165         return new String[]{"State", "Timeout"};
166     }
167 
168     /**
169      * Does this element exist?
170      * @param name The element name.
171      * @return True, if the name exists.
172      */
173     public boolean hasByName(String name) {
174         return (name.equals("State") || name.equals("Timeout"));
175     }
176 
177     /**
178      * Get an element by its name.
179      * @param name The name of the element.
180      * @return The value of the element.
181      * @throws NoSuchElementException The element does not exist.
182      */
183     public Object getByName(String name) throws NoSuchElementException{
184         if (name.equals("State"))
185             return aState;
186         else if (name.equals("Timeout"))
187             return iTimeout;
188         else
189             throw new NoSuchElementException();
190     }
191 
192     /**
193      * Are there elements
194      * @return Always true.
195      */
196     public boolean hasElements() {
197         return true;
198     }
199 
200     /**
201      * Get element type.
202      * @return null.
203      */
204     public Type getElementType() {
205         return null;
206     }
207 
208     /**
209      * Get a unique id for this implementation.
210      * @return The id.
211      */
212     public byte[] getImplementationId() {
213         return toString().getBytes();
214     }
215 
216     /**
217      * Return all implemented types of this class.
218      * @return The implemented UNO types.
219      */
220     public Type[] getTypes() {
221         Class interfaces[] = getClass().getInterfaces();
222 
223         Type types[] = new Type[interfaces.length];
224         for(int i = 0; i < interfaces.length; ++ i)
225             types[i] = new Type(interfaces[i]);
226 
227         return types;
228     }
229 }
230 
231 /**
232  * A connector thread
233  */
234 class ConnThread extends Thread {
235     String connStr;
236     XConnector oConnector;
237 
238     /**Construct the thread.
239      * @param oCntr A connector.
240      * @param cStr The conection string.
241      */
242     public ConnThread(XConnector oCntr, String cStr){
243         connStr = cStr;
244         oConnector = oCntr;
245     }
246 
247     /**
248      * Run the thread.
249      */
250     public void run(){
251         try {
252             Thread.sleep(ConnectorImpl.iTimeout.intValue());
253             ConnectorImpl.aState = "before connection";
254             XConnection oConnection = oConnector.connect(connStr);
255             if (oConnection != null) {
256                 ConnectorImpl.aState = "connected";
257                 oConnection.write(connStr.getBytes());
258                 oConnection.write(new byte[]{0});
259             } else
260                 ConnectorImpl.aState = "XConnection is null";
261         } catch (ConnectionSetupException e) {
262             ConnectorImpl.aState = "ConnectionSetupException";
263             throw new RuntimeException(e.toString());
264         } catch (NoConnectException e) {
265             ConnectorImpl.aState = "NoConnectException";
266             throw new RuntimeException(e.toString());
267         } catch (Exception e) {
268             ConnectorImpl.aState = "error";
269             throw new RuntimeException("Can't sleep exception");
270         }
271     }
272 }
273