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