Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 77241 Details for
Bug 195644
Add security support to New Agent Controller
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
updated platform.execution patch
execution.txt (text/plain), 98.61 KB, created by
Igor Alelekov
on 2007-08-29 08:25:15 EDT
(
hide
)
Description:
updated platform.execution patch
Filename:
MIME Type:
Creator:
Igor Alelekov
Created:
2007-08-29 08:25:15 EDT
Size:
98.61 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.tptp.platform.execution >Index: src/org/eclipse/tptp/platform/execution/util/internal/Constants.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/util/internal/Constants.java,v >retrieving revision 1.15 >diff -u -r1.15 Constants.java >--- src/org/eclipse/tptp/platform/execution/util/internal/Constants.java 3 May 2007 21:38:02 -0000 1.15 >+++ src/org/eclipse/tptp/platform/execution/util/internal/Constants.java 29 Aug 2007 11:00:04 -0000 >@@ -103,6 +103,12 @@ > public static final long CONNECT_DATA =0x10000000; > public static final long DATA_CONNECTION_COMPLETE =0x20000000; > public static final long DATA_CONNECTION_REFUSED =0x40000000; >+ >+ public static final long SECURITY_REQUIRED =0x00010000; >+ public static final long AUTHENTICATE =0x00020000; >+ public static final long AUTHENTICATION_FAILED =0x00040000; >+ public static final long AUTHENTICATION_SUCCESSFUL =0x00080000; >+ > public static final long DATA_PATH_SEND =0x00000001; > public static final long DATA_PATH_RECEIVE =0x00000002; > public static final long DATA_PATH_TWO_WAY = DATA_PATH_SEND | DATA_PATH_RECEIVE; >@@ -204,6 +210,7 @@ > public static final int PROCESS_LAUNCH_TIMEOUT_TRY_COUNT = 50; > public static final int DSSRVR_LAUNCH_TIMEOUT_TRY_COUNT = 1000; > public static final int CONNECT_TIMEOUT_TRY_COUNT = 32; >+ public static final int WAIT_RESPONCE_TIMEOUT = 10000; > > public static final boolean TPTP_AC = true; > >Index: src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerPool.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerPool.java,v >retrieving revision 1.2 >diff -u -r1.2 AgentControllerPool.java >--- src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerPool.java 7 Aug 2007 14:30:57 -0000 1.2 >+++ src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerPool.java 29 Aug 2007 11:00:04 -0000 >@@ -17,9 +17,12 @@ > import org.eclipse.tptp.platform.execution.client.core.IAgentController; > import org.eclipse.tptp.platform.execution.client.core.INode; > import org.eclipse.tptp.platform.execution.client.core.NodeFactory; >-import org.eclipse.tptp.platform.execution.client.core.internal.AgentController; > import org.eclipse.tptp.platform.execution.exceptions.AgentControllerUnavailableException; >+import org.eclipse.tptp.platform.execution.exceptions.LoginFailedException; > import org.eclipse.tptp.platform.execution.exceptions.NotConnectedException; >+import org.eclipse.tptp.platform.execution.client.core.internal.AgentController; >+import org.eclipse.tptp.platform.execution.exceptions.SecureConnectionRequiredException; >+import org.eclipse.tptp.platform.execution.security.User; > > /** > * This delegate is used to keep track of connections with Agent Controller. >@@ -29,7 +32,7 @@ > * > * @author Ali Mehregani > */ >-public class AgentControllerPool { >+public class AgentControllerPool { // may be inherited to provide UI > /** > * The incremental wait times > */ >@@ -41,8 +44,9 @@ > public static final int TOTAL_WAIT = 100; > > /* The instance of this class */ >- private static AgentControllerPool instance = new AgentControllerPool(); >+ protected static AgentControllerPool instance; > >+ private IAgentControllerFactory acFactory = null; > /* > * Keeps track of the connections to Agent Controller KEY = hostname VALUE = > * The connection >@@ -61,15 +65,16 @@ > /** > * Hide the construtor > */ >- private AgentControllerPool() { >+ protected AgentControllerPool() { > } > > /** >- * Return the instance of this singleton class >+ * Return the instance of this singleton class > * > * @return The instance of this class > */ > public static AgentControllerPool getInstance() { >+ if (instance == null) instance = new AgentControllerPool(); > return instance; > } > >@@ -94,12 +99,11 @@ > */ > public IAgentController getConnection(String hostName, int portNumber, > boolean reuseExistingConnection) throws Exception { >- >- String hostKeyName = AgentStatePool.getUnifiedHostName(hostName); > >+ String hostKeyName = AgentStatePool.getUnifiedHostName(hostName); >+ > /* Attempt to lookup the connection */ > IAgentController ac = (IAgentController) connectionPool.get(hostKeyName); >- > if (ac != null && ac instanceof AgentController) { > if (!((AgentController)ac).isConnected()) { > connectionPool.remove(hostKeyName); >@@ -115,7 +119,7 @@ > if (portNumber == connectionInfo.getPort()) { > /* Poke the connection to make sure that it's still alive */ > isConnectionAlive = false; >- >+ > try { > final IAgentController agentController = ac; > Thread thread = new Thread(new Runnable() { >@@ -128,7 +132,7 @@ > } > } > }); >- >+ > thread.start(); > thread.join(TOTAL_WAIT*INCREMENTAL_WAIT); > } catch (Exception e) {} >@@ -138,100 +142,25 @@ > /* Connection is dead - remove it from the connection pool. */ > connectionPool.remove(hostKeyName); > } >- > } > > /* > * A match was not found in our connection pool. Establish a new > * connection > */ >- final INode node = NodeFactory.createNode(hostName); >- final ConnectionInfo connInfo = new ConnectionInfo(); >- >- connInfo.setHostName(hostName); >- connInfo.setPort(portNumber); >- >- /* >- * The establishment of the connection should be done in a separate >- * thread. >- */ >- class EstablishConnection implements Runnable { >- private Exception e; >- private IAgentController ac; >- >- public void run() { >- try { >- ac = node.connect(connInfo); >- >- /* Notify that the connection is done */ >- synchronized (AgentControllerPool.this) { >- AgentControllerPool.this.notify(); >- } >- } catch (Exception e) { >- /* >- * For some reason Sun JDK 1.5_06 has problems resolving the >- * hostname "localhost". As a workaround change it to >- * 127.0.0.1 >- */ >- if ("localhost".equals(connInfo.getHostName())) { >- connInfo.setHostName("127.0.0.1"); >- } >- try { >- ac = node.connect(connInfo); >- >- /* Notify that the connection is done */ >- synchronized (AgentControllerPool.this) { >- AgentControllerPool.this.notify(); >- } >- } catch (Exception ex) { >- } >- this.e = e; >- } >- } >- >- public Exception getException() { >- return e; >- } > >- public IAgentController getConnection() { >- return ac; >- } >- } >- ; >- >- EstablishConnection establishConnection = new EstablishConnection(); >- int totalWait = 0; >- new Thread(establishConnection).start(); >- >- /* Wait for a maximum of 10 seconds before bailing out */ >- while (establishConnection.getConnection() == null >- && totalWait < TOTAL_WAIT >- && establishConnection.getException() == null) { >- totalWait++; >- synchronized (this) { >- try { >- wait(INCREMENTAL_WAIT); >- } catch (InterruptedException e) { >- /* Bail out */ >- break; >- } >- } >- } >- >- /* If any errors occurred, then return it */ >- Exception error = establishConnection.getException(); >- if (error != null) >- throw error; >- ac = establishConnection.getConnection(); >- if (ac == null) >+ ac = createConnection(hostName, portNumber); >+ if (ac == null) { > throw new AgentControllerUnavailableException(); >- >+ } >+ > /* Store and return the connection */ > connectionPool.put(hostKeyName, ac); > setPortMapping(hostKeyName, portNumber, ac.getConnectionInfo().getPort()); >+ > return ac; > } >- >+ > private int getPortMapping(String host, int port) { > String key = host + ":" + port; > if (portMappings.containsKey(key)) { >@@ -245,4 +174,19 @@ > String key = host + ":" + port; > portMappings.put(key, new Integer(newPort)); > } >+ >+ public void setACFactory(IAgentControllerFactory acFactory) { >+ this.acFactory = acFactory; >+ } >+ >+ public static void setAgentControllerFactory(IAgentControllerFactory acFactory) { >+ getInstance().setACFactory(acFactory); >+ } >+ >+ private IAgentController createConnection (String hostName, int portNumber) >+ throws Exception { >+ >+ if (acFactory == null) acFactory = new AgentControllerFactory(); >+ return acFactory.createConnection(hostName, portNumber); >+ } > } >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/SecureConnectionImpl.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/internal/SecureConnectionImpl.java,v >retrieving revision 1.2 >diff -u -r1.2 SecureConnectionImpl.java >--- src/org/eclipse/tptp/platform/execution/client/core/internal/SecureConnectionImpl.java 30 Aug 2006 17:38:58 -0000 1.2 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/SecureConnectionImpl.java 29 Aug 2007 11:00:04 -0000 >@@ -1,145 +1,94 @@ >-/******************************************************************************* >- * Copyright (c) 2005, 2006 IBM Corporation, Intel Corporation. >- * All rights reserved. This program and the accompanying materials >- * are made available under the terms of the Eclipse Public License v1.0 >- * which accompanies this distribution, and is available at >- * http://www.eclipse.org/legal/epl-v10.html >- * >- * Contributors: >- * IBM Corporation - Initial API and implementation >- * Vishnu K Naikawadi, Intel - Initial API and implementation >- * >- * $Id$ >- *******************************************************************************/ >- > package org.eclipse.tptp.platform.execution.client.core.internal; > >- > import java.io.IOException; >-import java.net.ConnectException; >-import java.net.InetAddress; >-import java.security.KeyManagementException; >-import java.security.NoSuchAlgorithmException; >-import java.security.Security; >- >-import javax.net.ssl.HandshakeCompletedEvent; >-import javax.net.ssl.HandshakeCompletedListener; >-import javax.net.ssl.SSLSession; >-import javax.net.ssl.SSLSocket; >-import javax.net.ssl.SSLSocketFactory; >- >-import org.eclipse.tptp.platform.execution.client.core.*; >-import org.eclipse.tptp.platform.execution.security.*; >-import org.eclipse.tptp.platform.execution.exceptions.*; >-import org.eclipse.tptp.platform.execution.util.internal.*; >- >+import java.net.Socket; > > import javax.net.ssl.SSLContext; >+import javax.net.ssl.SSLSocket; >+import javax.net.ssl.TrustManager; >+ >+import org.eclipse.tptp.platform.execution.client.core.INode; >+import org.eclipse.tptp.platform.execution.exceptions.LoginFailedException; >+import org.eclipse.tptp.platform.execution.exceptions.ReconnectRequestedException; >+import org.eclipse.tptp.platform.execution.exceptions.SecureConnectionRequiredException; >+import org.eclipse.tptp.platform.execution.exceptions.UntrustedAgentControllerException; > > public class SecureConnectionImpl extends ConnectionImpl { >- >- private boolean handshakeSuccess; >+ private SSLSocket initSSL (Socket socket) throws IOException { >+ if (socket == null || !socket.isConnected()) return null; >+ >+ TrustManager tms[] = null; >+ try { >+ X509TrustManagerImpl tm = new X509TrustManagerImpl(); >+ tms = new TrustManager[1]; >+ tms[0] = tm; >+ } catch (Exception e) {} >+ >+ SSLContext sslCtx; >+ try { >+ sslCtx = SSLContext.getInstance("SSL"); >+ sslCtx.init(null, tms, null); >+ } >+ catch(Exception e) { >+ sslCtx = null; >+ } >+ >+ if (sslCtx == null) return null; >+ >+ String host = socket.getInetAddress().getHostAddress(); >+ int port = socket.getPort(); >+ >+ SSLSocket sslSocket = (SSLSocket) sslCtx.getSocketFactory().createSocket(socket, host, port, true); >+ sslSocket.setUseClientMode(true); > >- public SecureConnectionImpl() { >- super(); >- } >+ sslSocket.startHandshake(); > >- public void connect(INode node, SecureConnectionInfo connInfo) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException { >- _port = connInfo.getPort(); >- int offset = 0; >- InetAddress[] addrs = node.getAllInetAddresses(); >- int protocolOffset = 0; >- ISecureClientParameters ClientSecureParams = connInfo.getSecureClientParameters(); >- >- /* Determine our acceptable protocols */ >- String[] sslProtocols = ClientSecureParams.getEnabledProtocols(); >- if(sslProtocols==null) { >- sslProtocols=new String[] {"SSL"}; >- } >+ if (sslSocket.getSession() == null) return null; > >- do { >- /* Connect to the remote machine */ >- try { >- /* Attach using SSL and initiate a handshake */ >- SSLContext sslContext = SSLContext.getInstance(sslProtocols[protocolOffset]/*,node.getSecurityParameters().getSecurityProvider()*/); >- //Commented the line below to support security for the RAC since it is not part of the new API >- //sslContext.init(ClientSecureParams.getKeystoreManager().getKeyManagers(), node.getAgentController(_port).getSecurityParameters().getKeystoreManager().getTrustManagers(), null); >- >- if (sslContext == null) { >- Security.addProvider(ClientSecureParams.getSecurityProvider()); >- >- _socket = SSLSocketFactory.getDefault().createSocket(addrs[offset], _port); >- ((SSLSocket) _socket).addHandshakeCompletedListener(new HandshakeCompletedListener() { >- public void handshakeCompleted(HandshakeCompletedEvent e) { >- } >- }); >- >- ((SSLSocket) _socket).startHandshake(); >- } >- else { >- _socket = sslContext.getSocketFactory().createSocket(addrs[offset], _port); >- ((SSLSocket) _socket).addHandshakeCompletedListener(new HandshakeCompletedListener() { >- public void handshakeCompleted(HandshakeCompletedEvent e) { >- } >- }); >- } >- >- String[] cyphers=ClientSecureParams.getEnabledCipherSuites(); >- if(cyphers!=null) { >- ((SSLSocket) _socket).setEnabledCipherSuites(ClientSecureParams.getEnabledCipherSuites()); >- } >- else { >- ((SSLSocket) _socket).setEnabledCipherSuites(((SSLSocket)_socket).getSupportedCipherSuites()); >- } >- ((SSLSocket) _socket).setUseClientMode(true); >- >- //((SSLSocket) _socket).startHandshake(); >- >- /* The underlying JSSE code returns the SSL socket before all >- * the SSL handshakes, including client authentication, are >- * completed. The handshake carries on, asynchronously, in >- * the background. If the handshake fails then the socket is >- * not usable. We synchronize things by calling getSession() >- * on the SSL socket. The thread calling getSession() is >- * forced to wait until the underlying SSL handshake is completed, >- * and if the handshake fails then getSession() returns a null. >- */ >- SSLSession session = ((SSLSocket) _socket).getSession(); >- >- /* If we could not establish a session we should throw an exception */ >- if (session==null) { >- throw new UntrustedAgentControllerException(Constants.TPTP_PLATFORM_EXEC_MSG39); >- } >- if(session.getCipherSuite().equals("SSL_NULL_WITH_NULL_NULL")) { >- throw new UntrustedAgentControllerException(Constants.TPTP_PLATFORM_EXEC_MSG39); >- } >- break; >- } >- catch (ConnectException e) { >- offset++; >- if (offset == addrs.length) { >- throw e; >- } >- /* Reset protocol offset */ >- protocolOffset = 0; >- } >- catch (NoSuchAlgorithmException e) { >- /* Try another protocol if there are any left */ >- protocolOffset++; >- if (protocolOffset == sslProtocols.length) { >- throw new ConnectException(Constants.TPTP_PLATFORM_EXEC_MSG40); >- } >- } >- //Commented the exception block below to allow for security - not implemented in the AC >-// catch (KeyManagementException e) { >-// /* We need to handle this */ >-// } >- } >- while (offset < addrs.length); >- >- sendConnectCommand(); >- _node = node; >- this.init(); >+ return sslSocket; > } >+ >+ public boolean connect(INode node, ConnectionImpl con) throws IOException, UntrustedAgentControllerException, >+ ReconnectRequestedException, SecureConnectionRequiredException, LoginFailedException { >+ >+ if (con == null) return false; >+ Socket socket = con.getSocket(); >+ if (socket == null || !socket.isConnected()) return false; >+ >+ con.setSocket(null); // to save socket from garbage collecting >+ >+ setSoTimeout(socket.getSoTimeout()); >+ _node = node; >+ _port = socket.getPort(); >+ inetAddress = socket.getInetAddress(); >+ >+ SSLSocket sslSocket = initSSL(socket); >+ if (sslSocket == null) return false; >+ >+ setSocket (sslSocket); > >+ init(); // to complete connection establishment >+ >+ return true; >+ } >+ >+ public int createDataConnection(int direction) throws IOException, SecureConnectionRequiredException { >+ Socket dataSock = connectSocket(); >+ if (dataSock == null) throw new IOException(); >+ >+ int dataConnectionId = -1; >+ boolean securityRequired = false; >+ >+ try { >+ dataConnectionId = initDataConnection(dataSock, direction); >+ } catch (SecureConnectionRequiredException e) { >+ securityRequired = true; >+ } >+ >+ if (!securityRequired) return dataConnectionId; >+ >+ SSLSocket sslDataSock = initSSL(dataSock); >+ >+ return initDataConnection(sslDataSock, direction); >+ } > } >\ No newline at end of file >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/NodeImpl.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/internal/NodeImpl.java,v >retrieving revision 1.6 >diff -u -r1.6 NodeImpl.java >--- src/org/eclipse/tptp/platform/execution/client/core/internal/NodeImpl.java 25 Apr 2007 15:33:58 -0000 1.6 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/NodeImpl.java 29 Aug 2007 11:00:04 -0000 >@@ -14,28 +14,20 @@ > > package org.eclipse.tptp.platform.execution.client.core.internal; > >-import java.io.IOException; > import java.net.InetAddress; > import java.net.UnknownHostException; >-import java.util.Enumeration; > import java.util.Hashtable; > import java.util.Vector; > > import org.eclipse.core.runtime.Platform; > import org.eclipse.tptp.platform.execution.client.core.*; >-import org.eclipse.tptp.platform.execution.util.*; > import org.eclipse.tptp.platform.execution.util.internal.*; > import org.eclipse.tptp.platform.execution.security.*; > import org.eclipse.tptp.platform.execution.exceptions.*; > import org.eclipse.tptp.platform.iac.administrator.internal.startstop.AutoStartStop; > import org.osgi.framework.Bundle; > >- >- > public class NodeImpl implements INode { >- >- private static final int DEFAULT_LIST_PROCESS_TIMEOUT = 7000; >- > protected String _name; > protected InetAddress[] _addr; > protected Vector _listeners=new Vector(10); >@@ -53,10 +45,7 @@ > try { > _addr=InetAddress.getAllByName(addr.getHostName()); > } >- catch(Exception e) { >- >- } >- >+ catch(Exception e) {} > } > > public NodeImpl(String name, InetAddress[] addr) { >@@ -95,11 +84,7 @@ > * @see Node#isConnected() > */ > public boolean isConnected() { >- if(_ac!=null) { >- //return _ac.isActive(); >- return true; >- } >- return false; >+ return _ac != null; > } > > /** >@@ -120,72 +105,58 @@ > > IConnection _connection = null; > int tryCount = 0; >- int ret = -1; >- if(_ac==null) >- { >- do >- { >- try >- { >- _connection=new ConnectionImpl(); >- _connection.connect(this, connInfo); >- if (tryCount == Constants.CONNECT_TIMEOUT_TRY_COUNT) >- { >+ if(_ac == null) { >+ do { >+ try { >+ _connection = ConnectionFactory.createConnection(this, connInfo); >+ if (tryCount == Constants.CONNECT_TIMEOUT_TRY_COUNT) { > throw new TimeoutException(Constants.TPTP_PLATFORM_EXEC_MSG21); > } > tryCount++; > } >+ catch (LoginFailedException e) { >+ throw e; >+ } > catch (ReconnectRequestedException e) { > connInfo.setPort(e.getReconnectPort()); > tryCount = 0; > _connection=null; > } > catch(Exception e) { >- _connection=null; >+ _connection = null; > if (Constants.TPTP_DEBUG) System.out.println("The NodeIml connect exception - " + e); > throw new AgentControllerUnavailableException(Constants.TPTP_PLATFORM_EXEC_MSG22 + e.getMessage()); > } > } > while(_connection == null); > } >- if(_connection != null) >- { >+ >+ if(_connection != null) { > _ac = new AgentController(this, connInfo); > ((AgentController)_ac).setConnection(_connection); >- return _ac; >- } >+ } > > return _ac; > } > >- public synchronized IAgentController connect(ConnectionInfo connInfo, User user) throws AgentControllerUnavailableException, SecureConnectionRequiredException, UntrustedAgentControllerException, LoginFailedException >- { >- boolean userAuthenticated = false; >- connect(connInfo); >- >- try >- { >- if (_ac != null) >- { >- userAuthenticated = _ac.authenticateUser(user); >- } >- >- if (userAuthenticated) >- { >- return _ac; >- } >+ public synchronized IAgentController connect(ConnectionInfo connInfo, User user) throws AgentControllerUnavailableException, SecureConnectionRequiredException, UntrustedAgentControllerException, LoginFailedException { >+ if (!isConnected()) { >+ connect(connInfo); > } >- catch(Exception e) >- { > >- throw new AgentControllerUnavailableException(Constants.TPTP_PLATFORM_EXEC_MSG22); >- } >+ if (_ac == null) return null; > >- return null; >+ try { >+ _ac.authenticateUser(user); >+ } catch (Exception e) { >+ e.printStackTrace(); >+ throw new AgentControllerUnavailableException(); >+ } >+ >+ return _ac; > } > >- public IAgentController getAgentController(int port) >- { >+ public IAgentController getAgentController(int port) { > return _ac; > } > >@@ -204,5 +175,11 @@ > > return isLocal; > } >+ >+ public void disconnect() { >+ if (_ac != null) { >+ _ac.disconnect(); >+ _ac = null; >+ } >+ } > } >- >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/AgentController.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/internal/AgentController.java,v >retrieving revision 1.31 >diff -u -r1.31 AgentController.java >--- src/org/eclipse/tptp/platform/execution/client/core/internal/AgentController.java 22 Aug 2007 16:47:03 -0000 1.31 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/AgentController.java 29 Aug 2007 11:00:04 -0000 >@@ -57,31 +57,19 @@ > private INode _node = null; > private ISecureClientParameters _secureClientParameters = null; > private User _user = null; >- private String Version = null; > > public Hashtable _processList = new Hashtable(); > public Hashtable _agentList = new Hashtable(); > > private boolean TPTP_AC = true; >- private int _destID = Constants.AC_DEST_ID; > private IConnection _connection = null; > private int _sourceID = 0; >- private IProcess _process = null; >- private IAgent _agent = null; >- //The recvd variable names used for the workaround for the innerclasses - Has to be replaced >- private String receivedAgentName = null; >- private String receivedAgentMetaData = null; >- private int _procCntlr_DestId = -1; > private int _consoleDataConnID = -1; > private ConsoleDataProcessor _consoleMapper = null; >- //listprocessWaitingCommandCount is for RAC >- //Do we need it for AC/ >- private long _listProcessesWaitingCommandCount = 0; > private String DELIMIT_TOKEN = " "; > private FileTransferManagerImpl _fileTransferManager = null; > final private static String FILE_TRANSFER_AGENT = "org.eclipse.tptp.FileTransferAgent"; > //generic context used for ac events that do not have a ctxt to respond to >- private int _genericcontext = -1; > > public AgentController(INode node,ConnectionInfo conninfo) > { >@@ -121,95 +109,38 @@ > return _connection.getNextContextId(); > } > >- public void disconnect() >- { >- _connection.disconnect(); >+ public void disconnect() { >+ if (_connection != null) { >+ _connection.disconnect(); >+ _connection = null; >+ } > } > > /* > * Authenticate the User Credentials with AC > * > */ >- public boolean authenticateUser(User user) throws NotConnectedException >- { >- boolean userAuthenticated = false; >- final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG6); >- IAgent[] retagent = null; >- final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); >+ public boolean authenticateUser(User user) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG6); > >- synchronized(_launcherLock) >- { >- _requireEclipseLock=false; >- StringBuffer AuthenticateUserCommand = new StringBuffer(""); >- try >- { >- AuthenticateUserCommand.append("<authenticateUser iid=\"org.eclipse.tptp.agentManager\"><userName>"); >- AuthenticateUserCommand.append(user.getName()); >- AuthenticateUserCommand.append("</userName><password>"); >- AuthenticateUserCommand.append(user.getPassword()); >- AuthenticateUserCommand.append("</password></authenticateUser>"); >- this.sendCommand(AuthenticateUserCommand.toString(), Constants.AC_DEST_ID, new ICommandHandler() >- { >- public void incomingCommand(INode node, ICommandElement command) >- { >- genCmdHandler.setCommandElement(command); >- } >- }); >- _requireEclipseLock=true; >- _launcherLock.wait(Constants.TIMEOUT_PERIOD); >- } >- catch(InterruptedException e) >- { >- e.printStackTrace(); >- } >- catch(Exception e) >- { >- e.printStackTrace(); >- } >- } >- if (genCmdHandler.getCommandElement() != null) >- { >- String commandStr = ((CommandFragment)genCmdHandler.getCommandElement()).getCommandData(); >- TPTPXMLParse ParseObj = new TPTPXMLParse(); >- ParseObj.setParser(commandStr); >- Hashtable CommandHash = ParseObj.getHashTable(); >- if (CommandHash.containsKey("userAuthenticated")) >- { >- userAuthenticated = true; >- } >- else if (CommandHash.containsKey("authenticationFailed")) >- { >- userAuthenticated = false; >- } >- } >- return userAuthenticated; >+ return ((ConnectionImpl)_connection).authenticateUser(user); > } > > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#getAgent(java.lang.String, java.lang.String) > */ > public IAgent getAgent(String agentName, String agentClassName) >- throws NotConnectedException >- { >- //if the agent is not created by the AC then the default is to create one >- boolean createNew = false; >+ throws NotConnectedException { > return getAgent(agentName, agentClassName, TPTPAgentAccess.TPTP_CONTROLLER_ACCESS); > } >+ > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#getAgent(java.lang.String, java.lang.String, boolean) > */ >- public IAgent getAgent(String agentName, String agentClassName, TPTPAgentAccess accessMode) throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG7); >- final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; >+ public IAgent getAgent(String agentName, String agentClassName, TPTPAgentAccess accessMode) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG7); > >- int receivedAgentID = -1; >- final InnerUtil innerutil = new InnerUtil(); >+ final Object _launcherLock = new Object(); > > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); > IAgent agent = null; >@@ -358,12 +289,10 @@ > * Clients should call this method to get the list of deployed agents > * returns the array of agent names > */ >- public IAgent[] queryDeployedAgents(String interfaceID) throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG8); >+ public IAgent[] queryDeployedAgents(String interfaceID) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG8); >+ > final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; > > IAgent[] retagent = null; > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); >@@ -466,12 +395,10 @@ > * TPTP_OBSERVER_ACCESS - Request Observer Access on the Agent > * TPTP_LOCK_AGENT - Request exclusive usage and others can't share this agent > */ >- private int queryAgentByProcessID(String agentName, long pid) throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG9); >+ private int queryAgentByProcessID(String agentName, long pid) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG9); >+ > final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; > > int token =-1; > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); >@@ -484,10 +411,8 @@ > try > { > org.eclipse.tptp.platform.execution.client.core.internal.commands.QueryRunningAgentsCommand command= new org.eclipse.tptp.platform.execution.client.core.internal.commands.QueryRunningAgentsCommand(); >- this.sendCommand(command.buildCommand(), Constants.AC_DEST_ID, new ICommandHandler() >- { >- public void incomingCommand(INode node, ICommandElement command) >- { >+ sendCommand(command.buildCommand(), Constants.AC_DEST_ID, new ICommandHandler() { >+ public void incomingCommand(INode node, ICommandElement command) { > genCmdHandler.setCommandElement(command); > } > }); >@@ -545,9 +470,9 @@ > } > return token; > } >- public void releaseAgent(int agentID)throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG10); >+ public void releaseAgent(int agentID)throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG10); >+ > if(TPTP_AC) > { > try >@@ -570,12 +495,11 @@ > return; > } > >- public boolean requestAgentControl(int agentID, int accessMode)throws NotConnectedException >- { >+ public boolean requestAgentControl(int agentID, int accessMode)throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG11); >+ > final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG11); >+ > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); > boolean controlgranted = false; > if(TPTP_AC) >@@ -647,13 +571,11 @@ > } > } > >- public void releaseAgentControl(int agentID)throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG12); >- if(TPTP_AC) >- { >- try >- { >+ public void releaseAgentControl(int agentID)throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG12); >+ >+ if(TPTP_AC) { >+ try { > org.eclipse.tptp.platform.execution.client.core.internal.commands.DetachFromAgentCommand command= new org.eclipse.tptp.platform.execution.client.core.internal.commands.DetachFromAgentCommand(Constants.releaseAgentControl_Cmd, agentID); > this.sendCommand(command.buildCommand(), Constants.AC_DEST_ID, new ICommandHandler() > { >@@ -843,18 +765,33 @@ > { > return getAgent(agentName, null, processId); > } >- public IAgent getAgent(String agentName, String agentClassName, long processId) throws NotConnectedException >- { >- if(!isConnected()){throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG7);} >+ >+ private IAgent cachedAgent=null; >+ >+ public IAgent getAgent(String agentName, String agentClassName, long processId) throws NotConnectedException { >+ if(!this.isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG7); >+ > IAgent agent = null; >- if (agentClassName != null && agentClassName.equals("org.eclipse.tptp.platform.execution.client.agent.ICollector")) >- { >+ >+ if (cachedAgent != null) { >+ try { >+ if (cachedAgent.getProcess().getProcessId() == processId && cachedAgent.getName().equals(agentName)) { >+ agent = cachedAgent; >+ } >+ } catch (Exception e) {} >+ >+ if (agent != null) { >+ return agent; >+ } >+ } >+ >+ if (agentClassName != null && agentClassName.equals("org.eclipse.tptp.platform.execution.client.agent.ICollector")) { > agent = new CollectorImpl(agentName); > } >- else >- { >+ else { > agent = new AgentImpl(agentName); > } >+ > IProcess process = this.createProcess(); > process.setProcessId(processId); > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); >@@ -901,6 +838,9 @@ > } > ((AgentImpl)agent).setProcess(process); > process.setAgent(agent); >+ >+ cachedAgent = agent; >+ > return agent; > } > catch(Exception e){e.printStackTrace(); return null;} >@@ -911,18 +851,16 @@ > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#listProcesses() > */ >- public Enumeration listAgents() throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG13); >+ public Enumeration listAgents() throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG13); > return _agentList.elements(); > } > > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#listProcesses() > */ >- public Enumeration listProcesses() throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG14); >+ public Enumeration listProcesses() throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG14); > return _processList.elements(); > } > >@@ -930,8 +868,7 @@ > * @see org.eclipse.tptp.platform.execution.core.IAgentController#getProcess(java.lang.String) > */ > public IProcess getProcess(long processId) throws NotConnectedException { >- if (!isConnected()) >- throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG15); >+ if (!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG15); > > String pkey = String.valueOf(processId); > >@@ -953,9 +890,9 @@ > */ > public String[] queryAvailableAgents(String[] interfaceID) throws NotConnectedException { > if(!TPTP_AC) return null; >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG16); >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG16); > >- final Object responceLock = new Object(); >+ final Object responseLock = new Object(); > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); > > QueryAvailableAgntCommand command= new QueryAvailableAgntCommand(); >@@ -966,7 +903,7 @@ > sendCommand(command.buildCommand(),Constants.AC_DEST_ID,new ICommandHandler() { > public void incomingCommand(INode node, ICommandElement command) { > genCmdHandler.setCommandElement(command); >- synchronized(responceLock) { responceLock.notifyAll(); } >+ synchronized(responseLock) { responseLock.notifyAll(); } > } > }); > } catch(Exception e) { >@@ -975,10 +912,10 @@ > > if (sendMsgEx != null) throw new NotConnectedException(sendMsgEx.getMessage()); > >- synchronized(responceLock) { >+ synchronized(responseLock) { > if (genCmdHandler.getCommandElement() == null) { > try { >- responceLock.wait(Constants.PROCESS_LAUNCH_TIMEOUT_TRY_COUNT*Constants.TIMEOUT_PERIOD); >+ responseLock.wait(Constants.PROCESS_LAUNCH_TIMEOUT_TRY_COUNT*Constants.TIMEOUT_PERIOD); > } catch (Exception e) {} > } > } >@@ -990,37 +927,30 @@ > String commandStr = ((CommandFragment)genCmdHandler.getCommandElement()).getCommandData(); > TPTPXMLParse ParseObj = new TPTPXMLParse(); > ParseObj.setParser(commandStr); >- Vector PVector = ParseObj.getVector(); >- Vector tempvector = new Vector(); >- >- for(int i=0; i < PVector.size(); i++) { >- if((((Variable)(PVector.elementAt(i))).getName()).equals("agentNames")) { >- tempvector.add((String)((Variable)(PVector.elementAt(i))).getValue()); >- } >- } >- >- String[] retStr = null; >- if (tempvector.size() == 1) { >- String temp = (String)tempvector.elementAt(0); >- if(temp != null) { >- retStr = new String[tempvector.size()]; >- retStr = TPTPString.tokenizeString(DELIMIT_TOKEN, temp); >+ Vector pVector = ParseObj.getVector(); >+ if (pVector == null) return null; >+ >+ String response = null; >+ Iterator it = pVector.iterator(); >+ while (it.hasNext()) { >+ Variable var = (Variable) it.next(); >+ if ("agentNames".equals(var.getName())) { >+ response = (String) var.getValue(); >+ break; > } > } >- >- return retStr; >+ >+ return (response == null) ? null : TPTPString.tokenizeString(DELIMIT_TOKEN, response); > } >- >- public String[] queryAvailableAgents() throws NotConnectedException >- { >+ >+ public String[] queryAvailableAgents() throws NotConnectedException { > return queryAvailableAgents(null); > } >- public IAgent[] queryRunningAgents(String agentName, String[] interfaceID, long pid) throws NotConnectedException >- { >- final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- boolean _requireEclipseLock = false; >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG17); >+ >+ public IAgent[] queryRunningAgents(String agentName, String[] interfaceID, long pid) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG17); >+ >+ final Object _launcherLock = new Object(); > IAgent[] retagent = null; > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); > if(TPTP_AC) >@@ -1035,7 +965,7 @@ > if(interfaceID != null )command.setAgentInterfaceID(interfaceID.length, interfaceID); > if(agentName != null )command.setAgentName(agentName); > if(pid != 0 )command.setProcessId(pid); >- >+ > this.sendCommand(command.buildCommand(), Constants.AC_DEST_ID, new ICommandHandler() > { > public void incomingCommand(INode node, ICommandElement command) >@@ -1185,21 +1115,17 @@ > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#queryRunningAgents() > */ >- public IAgent[] queryRunningAgents() throws NotConnectedException >- { >+ public IAgent[] queryRunningAgents() throws NotConnectedException { > return queryRunningAgents(null); > } >+ > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#getAgentMetadata(java.lang.String) > */ >- public String getAgentMetadata(String agentName) >- throws NotConnectedException >- { >- if(!isConnected())throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG18); >+ public String getAgentMetadata(String agentName) throws NotConnectedException { >+ if(!isConnected()) throw new NotConnectedException(Constants.TPTP_PLATFORM_EXEC_MSG18); >+ > final Object _launcherLock = new Object(); >- final Object _eclipseLock = new Object(); >- final InnerUtil datarecvd = new InnerUtil(); >- boolean _requireEclipseLock = false; > String retagentMetadata = ""; > > final GenericCommandHandler genCmdHandler = new GenericCommandHandler(); >@@ -1306,21 +1232,19 @@ > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#removeEventListener(java.lang.String, org.eclipse.tptp.platform.execution.core.ICommandHandler) > */ >- public void removeEventListener(String interfaceID, >- ICommandHandler eventHandler) >- { >+ public void removeEventListener(String interfaceID, ICommandHandler eventHandler) { > // TODO Auto-generated method stub > } > > /* (non-Javadoc) > * @see org.eclipse.tptp.platform.execution.core.IAgentController#sendCommand(java.lang.String, int, org.eclipse.tptp.platform.execution.core.ICommandHandler) > */ >- public void sendCommand(String cmd, int destID, ICommandHandler handler) throws IOException >- { >+ public void sendCommand(String cmd, int destID, ICommandHandler handler) throws IOException { > ControlMessage message = new ControlMessage(); > message.setMessageType(Constants.TPTP_AC_MESSAGE); > message.setMagicNumber(Constants.AC_MAGIC_NUMBER); > message.setFlags(0); >+ > CommandFragment cmdFrag = new CommandFragment(); > cmdFrag.setDestination(destID); > cmdFrag.setSource(this._sourceID); >@@ -1328,16 +1252,15 @@ > cmdFrag.setCommand(cmd); > cmdFrag.buildCommand();//Build the command in ConnectionImpl::sendMessage as we don't know the context at this place > message.appendCommand(cmdFrag); >- >- if (handler == null) >- { >+ >+ if (handler == null) { > //this.getConnection().sendMessage(message, this.acCommandHandler); > } >- else >- { >- this.getConnection().sendMessage(message, handler); >+ else { >+ getConnection().sendMessage(message, handler); > } > } >+ > /** > * Sends a command to the specified destination, the command requires a control message object > * and a commandhandler >@@ -1480,8 +1403,7 @@ > > } > >- public void incomingCommand(INode node, ICommandElement command) >- { >+ public void incomingCommand(INode node, ICommandElement command) { > } > > /* >@@ -1559,18 +1481,6 @@ > //return _consoleMapper; > return (new ConsoleDataProcessor()); > } >- class InnerUtil >- { >- private Object value; >- public Object getValue() >- { >- return this.value; >- } >- public void setValue(Object value) >- { >- this.value = value; >- } >- } > > public boolean isConnected() { > return _connection != null && _connection.isActive(); >@@ -1583,4 +1493,8 @@ > > return -1; > } >+ >+ public boolean isAuthenticated() { >+ return _connection != null && ((ConnectionImpl)_connection).isAuthenticated(); >+ } > } >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionImpl.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionImpl.java,v >retrieving revision 1.21 >diff -u -r1.21 ConnectionImpl.java >--- src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionImpl.java 22 Aug 2007 16:47:03 -0000 1.21 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionImpl.java 29 Aug 2007 11:00:04 -0000 >@@ -28,6 +28,8 @@ > import java.util.Hashtable; > > import org.eclipse.tptp.platform.execution.client.core.*; >+import org.eclipse.tptp.platform.execution.client.core.internal.commands.AuthenticateCommand; >+import org.eclipse.tptp.platform.execution.security.User; > import org.eclipse.tptp.platform.execution.util.*; > import org.eclipse.tptp.platform.execution.util.internal.CommandFragment; > import org.eclipse.tptp.platform.execution.util.internal.Constants; >@@ -37,10 +39,10 @@ > import org.eclipse.tptp.platform.execution.exceptions.*; > > public class ConnectionImpl implements IConnection { >- > protected Socket _socket; > protected INode _node; > protected int _port; >+ protected InetAddress inetAddress = null; > > private int _connectionId = 0; > private int protoVersion = 0; >@@ -52,203 +54,258 @@ > private boolean _isComplete = false; > > private final Vector _listeners = new Vector(); >- private final Object _connectionLock = new Object(); >- private final Object _loginLock = new Object(); > >+ private static final Object writeLock = new Object(); >+ private static final Object writeDataLock = new Object(); > private static final Object contextLock = new Object(); >- private boolean _isInitialized = false; >- private boolean _loginPending = false; >- private ReconnectRequestedException _reconnectRequestedException=null; >- private SecureConnectionRequiredException _secureConnectionRequiredException=null; >- private LoginFailedException _loginFailed=null; >+ private static final Object authLock = new Object(); >+ private boolean _isInitialized = false; >+ private boolean isAuthenticated = false; > > private int _soTimeout = Constants.TIMEOUT_PERIOD; > >- class DataConnection >- { >+ class DataConnection { // it is better to move all processing of data connections to AC or Node > private Socket _dataSocket; > private ACTCPDataServer _dataServer; >+ private int dataConnectionId; > >- public void setDataSocket(Socket dataSocket){_dataSocket = dataSocket;} >- public void setDataServer(ACTCPDataServer dataServer){_dataServer = dataServer;} >- public Socket getDataSocket(){return _dataSocket;} >- public ACTCPDataServer getDataServer(){return _dataServer;} >+ public void setDataSocket(Socket dataSocket) { _dataSocket = dataSocket; } >+ public void setDataServer(ACTCPDataServer dataServer) { _dataServer = dataServer; } >+ public Socket getDataSocket() { return _dataSocket; } >+ public ACTCPDataServer getDataServer() { return _dataServer; } >+ public int getDataConnectionId() { return dataConnectionId; } >+ public void setDataconnectionId(int dataConnectionId) { this.dataConnectionId = dataConnectionId; } > } > > public ConnectionImpl() { >- super(); > } >- >- public int getConnectionId() >- { >- return this._connectionId; >+ >+ public int getConnectionId() { >+ return _connectionId; > } > >- public boolean isNewAC() >- { >+ public boolean isNewAC() { > return true; > } > >- public long getNextContextId() >- { >+ public long getNextContextId() { > synchronized(contextLock){ > return _context++; > } >- > } > >- public void addContext(long contextID, ICommandHandler handler) >- { >+ public void addContext(long contextID, ICommandHandler handler) { > synchronized(contextLock){ > this._contextMapper.addContext(contextID, handler); > } > } > >- public void connect(INode node, ConnectionInfo connInfo) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException >- { >- _port=connInfo.getPort(); >- _soTimeout = connInfo.getSoTimeout(); >- int offset=0; >- InetAddress[] addrs=node.getAllInetAddresses(); >+ protected Socket connectSocket () throws IOException { >+ if (_node == null) return null; >+ return connectSocket(_node.getAllInetAddresses(), _port); >+ } >+ >+ protected Socket connectSocket (InetAddress addrs[], int port) throws IOException { >+ if (addrs == null || addrs.length <= 0) return null; > >- do { >- /* Connect to the remote machine */ >- try { >- if(Constants.TPTP_DEBUG)System.out.println("Connecting to " + addrs[offset] + " at port " + _port); >- _socket=new Socket(addrs[offset], _port); >- >- sendConnectCommand(); >- break; >- } >- catch(IOException e) { >- offset++; >- if (Constants.TPTP_DEBUG)System.out.println(e.getMessage()); >- if(offset==addrs.length) { >- if (Constants.TPTP_DEBUG)System.out.println("Error while connecting to the Agent Controller on " + addrs[offset] + " running at port " + _port); >- throw e; >- } >- } >- }while(offset<addrs.length); >- _node=node; >+ Socket socket = null; > >- this.init(); >+ for (int i=0; i<addrs.length; i++) { >+ if(Constants.TPTP_DEBUG) System.out.println("Connecting to " + addrs[i] + " at port " + _port); >+ socket = new Socket(addrs[i], port); >+ socket.setSoTimeout(_soTimeout); >+ socket.setTcpNoDelay(true); >+ inetAddress = addrs[i]; >+ break; >+ } >+ >+ return socket; >+ } >+ >+ public void connect(INode node, ConnectionInfo connInfo) throws IOException, SecureConnectionRequiredException, >+ LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException { >+ >+ _soTimeout = connInfo.getSoTimeout(); >+ if (_soTimeout > 0) _soTimeout *= Constants.CONNECT_TIMEOUT_TRY_COUNT; >+ >+ _node = node; >+ _port = connInfo.getPort(); >+ >+ _socket = connectSocket(node.getAllInetAddresses(), _port); >+ if (_socket == null) throw new IOException(); >+ >+ init(); > > // We expected to have a connectionID by now. If we don't, it's because the wait above timed out >- if ( _connectionId == 0 ) { >+ if (_connectionId == 0) { > throw new LoginFailedException("Timeout while waiting for connection to complete"); > } > } >- public void connect(INode node, int port) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException >- { >+ >+ public void connect(INode node, int port) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException { > ConnectionInfo connInfo = new ConnectionInfo(); > connInfo.setPort(port); >- connect( node, connInfo ); >- } >- >- protected void sendConnectCommand() throws IOException >- { >- if(Constants.TPTP_DEBUG)System.out.println("The AC is New"); >- sendControlCommand(Constants.CONNECT); >+ connect(node, connInfo); > } > >- >- /* Init */ >- protected void init() throws SecureConnectionRequiredException, LoginFailedException, IOException, ReconnectRequestedException >- { >- try >- { >- _socket.setSoTimeout(_soTimeout); >- _socket.setTcpNoDelay(true); >- } >- catch(SocketException e) { >- /* We can ignore this */ >- } >- >- _contextMapper=new ContextMapper(); >+ protected void init() throws SecureConnectionRequiredException, LoginFailedException, IOException, ReconnectRequestedException { >+ _contextMapper = new ContextMapper(); > _dataConnectionMap = new Hashtable(); >- _cmdHandler=new ICommandHandler() >- { >- public void incomingCommand(INode node, ICommandElement command) >- { >+ >+ _cmdHandler = new ICommandHandler() { >+ public void incomingCommand(INode node, ICommandElement command) { > handleACCommands(command); > } > }; > >- /* RKD: With the V5 RAC it first accepts the socket connection and then determines is the connection >- * is allowed. If the connection is not allowed the the connection is cut. We need to first connect >- * and then determine if we have been cut off. The connection has already occurred above us in the >- * stack. We then will wait until either a read timeout has occured on the reader thread or the >- * connection gets cut by the other side. >- */ >- >- synchronized(_connectionLock) { >- SocketReaderThread reader=new SocketReaderThread(); >- reader.setName(_node.getName()+"_connection"); >- reader.setDaemon(true); >- reader.start(); >- try >- { >- _connectionLock.wait(); >- } >- catch(InterruptedException e){e.printStackTrace();} >- >- _isInitialized=true; >- >- if (_reconnectRequestedException!=null) { >- ReconnectRequestedException temp=_reconnectRequestedException; >- _reconnectRequestedException = null; >- throw temp; >+ sendControlCommand(Constants.CONNECT); >+ >+ byte rbuf[] = new byte[512]; >+ BufferedInputStream inStream = new BufferedInputStream(_socket.getInputStream()); >+ int offset = 0; >+ >+ while (true) { >+ int bytesRead = inStream.read(rbuf, offset, rbuf.length-offset); >+ >+ if(bytesRead < 0) break; >+ >+ if (offset > 0) { >+ bytesRead += offset; >+ offset = 0; >+ } >+ >+ // If we've tried to connect to a backward compatibility >+ // layer of the AC, we'll immediately get a response in >+ // RAC format. If we ignore this, the BC layer will >+ // respond to our first message by giving us the port >+ // to connect to the regular socket TL. >+ // >+ >+ int v = checkForRACMessage(rbuf, offset); >+ if(v >= 0) { >+ // We read the header, the next four bytes will be the length >+ // (including the header) of the RAC message >+ int len = (int) TPTPMessageUtil.readTPTPLongFromBuffer(rbuf, v); >+ if (len == bytesRead) { >+ offset = 0; >+ continue; >+ } >+ >+ if (len > bytesRead) { >+ inStream.skip(len - bytesRead); >+ offset = 0; >+ continue; >+ } >+ >+ offset = len; >+ } >+ >+ // First we make sure we have at least enough read for the message header. >+ // If not, compress and re-read. >+ // >+ if (bytesRead-offset < Constants.AC_MESSAGE_HEADER_SIZE) { >+ System.arraycopy(rbuf, offset, rbuf, 0, bytesRead-offset); >+ offset = bytesRead - offset; >+ continue; > } > >- /* If this connection has a pending exception because the server is secure we need to throw the >- * exception so that the caller can create a new connection with the proper security settings. >- */ >- if(_secureConnectionRequiredException!=null) { >- SecureConnectionRequiredException temp=_secureConnectionRequiredException; >- _secureConnectionRequiredException=null; >- _loginPending=false; >- throw temp; >+ // We have the header information, now lets try and get the entire message. Once >+ // again the same error conditions can occur. >+ // >+ //System.out.println("Offset xx::"+new String(buffer)); >+ >+ ControlMessage msg=new ControlMessage(); >+ msg.setMessageType(Constants.TPTP_AC_MESSAGE); >+ int current; >+ >+ try { >+ if (rbuf.length == bytesRead + offset) { >+ current = msg.readFromBuffer(rbuf, offset); >+ } >+ else { >+ // length is not passed to readFromBuffer so we should ensure >+ // that readFromBuffer will not read trash from the end of the buffer >+ // and no exception will be thrown. >+ // ControlMessage.readFromBuffer(buffer, offset, length) method >+ // is needed in order to prevent extra memory allocations in this case. >+ byte[] buffer2 = new byte[bytesRead]; >+ System.arraycopy(rbuf, offset, buffer2, 0, bytesRead); >+ current = msg.readFromBuffer(buffer2, 0); >+ } >+ } catch (Exception e) { >+ throw new IOException(); > } >+ >+ long flags = msg.getFlags(); >+ if ((flags & Constants.CONNECTION_COMPLETE) != 0) { >+ isAuthenticated = (flags & Constants.AUTHENTICATION_FAILED) == 0; >+ >+ int cmdOffset = 0; >+ _connectionId = extractConnectionId(rbuf, current+cmdOffset); >+ cmdOffset += 4; > >- synchronized(_loginLock) { >- if(_loginPending) { >- try { >- _loginLock.wait(); >+ int uuidLen = (int) TPTPMessageUtil.readTPTPLongFromBuffer(rbuf, current+cmdOffset); >+ cmdOffset += 4 + uuidLen + 1; // including trailing null >+ >+ if ((cmdOffset + 4) <= msg.getLength()) { // check if protoVersion is available >+ protoVersion = (int) TPTPMessageUtil.readTPTPLongFromBuffer(rbuf, current+cmdOffset); >+ } >+ >+ if (Constants.TPTP_DEBUG) System.out.println("Connection complete response - " + _connectionId + ", protoVer=" + protoVersion); >+ >+ break; >+ } // equals - since CONNECTION_RECONNECT_REQUEST has two bits set >+ else if ((flags & Constants.CONNECTION_REFUSED) != 0) { >+ if ((flags & Constants.SECURITY_REQUIRED) != 0) { >+ throw new SecureConnectionRequiredException(); >+ } >+ else if((flags & Constants.CONNECTION_RECONNECT_REQUEST) == Constants.CONNECTION_RECONNECT_REQUEST) { >+ int portreq = -1; >+ >+ try { >+ close(); >+ CommandFragment cmd = (CommandFragment)msg.getCommand(0); >+ String strToParse = cmd.getCommandData(); >+ TPTPXMLParse ParseObj = new TPTPXMLParse(); >+ ParseObj.setParser(strToParse.trim()); >+ Hashtable CommandHash = ParseObj.getHashTable(); >+ if(CommandHash.containsKey("port")) { >+ portreq = Integer.parseInt((String)ParseObj.getHashTable().get("port")); >+ } > } >- catch(InterruptedException e) { >- /* We should ignore this */ >+ catch(Exception e){} >+ >+ if(portreq == -1) { >+ // This is a bad situation. The AC should have given us a port >+ // Since it didn't (unexpectedly), let's try a default >+ portreq = 10006; > } >+ >+ // Tell the caller how to reconnect >+ throw new ReconnectRequestedException(portreq); > } >+ >+ throw new IOException("Connection Refused"); > } >- >- if(_loginFailed!=null) { >- LoginFailedException temp=_loginFailed; >- _loginFailed=null; >- _loginPending=false; >- throw temp; >- } >- if(_isComplete) { >- throw new IOException(); >- } >+ >+ throw new IOException("Unknown command"); > } >+ >+ (new SocketReaderThread()).start(); >+ _isInitialized = true; > } >- >- private void handleACCommands(ICommandElement command) >- { >+ >+ private void handleACCommands(ICommandElement command) { > long contextId=command.getContext(); > if(Constants.TPTP_DEBUG)System.out.println("The context of the returned command:" + contextId); > > //Find the command handler associated with this contextId and > // forward the message appropriately. > ICommandHandler ch=_contextMapper.getHandler(contextId); >- if(ch != null) >- { >+ if(ch != null) { > if(Constants.TPTP_DEBUG)System.out.println("Forwarding to command handler"); > ch.incomingCommand(_node, command); > } >- else { >- if(Constants.TPTP_DEBUG)System.out.println("Could not find command handler"); >- } > } > > private int extractConnectionId(byte[] buffer, int offset) { >@@ -266,8 +323,7 @@ > * nonzero if error > * > *********************************************************/ >- private void sendCONNECT_DATACommand(Socket datasock, int direction) throws IOException >- { >+ private void sendCONNECT_DATACommand(Socket datasock, int direction) throws IOException { > long flags = 0 ; > > /* build the CONNECT command string */ >@@ -284,7 +340,7 @@ > long dataPathType = direction; > offset = TPTPMessageUtil.writeTPTPLongToBuffer(buffer, offset, dataPathType); > >- OutputStream stream=datasock.getOutputStream(); >+ OutputStream stream = datasock.getOutputStream(); > stream.write(buffer); > stream.flush(); > } >@@ -301,8 +357,7 @@ > * nonzero if error > * > *********************************************************/ >- private void sendControlCommand(long commandFlags) throws IOException >- { >+ private void sendControlCommand(long commandFlags) throws IOException { > long flags = 0 ; > > /* build the CONNECT command string */ >@@ -317,104 +372,82 @@ > sendMessage(connectMessage, _cmdHandler); > } > >+ int createDataConnection(int direction) throws IOException, SecureConnectionRequiredException { >+ Socket datasock = connectSocket(); >+ if (datasock == null) throw new IOException(); > >- int createDataConnection(int direction) throws IOException >- { >- Socket datasock = null; >- int dataConnectionId = -1; >- byte[] buffer=new byte[Constants.MAX_MESSAGE_LENGTH]; >- int masterOffset=0; >+ return initDataConnection(datasock, direction); >+ } >+ >+ protected int initDataConnection(Socket datasock, int direction) throws IOException, SecureConnectionRequiredException { >+ if (datasock == null || !datasock.isConnected()) return -1; > >- int offset=0; >- InetAddress[] addrs=_node.getAllInetAddresses(); >- >- do { >- /* Connect to the remote machine */ >- try { >- datasock=new Socket(addrs[offset], _port); >- datasock.setTcpNoDelay(true); >- datasock.setSoTimeout(_soTimeout); >- break; >- } >- catch(IOException exp) >- { >- offset++; >- //System.out.println(e.getMessage()); >- if(offset==addrs.length) { >- throw exp; >- } >- } >- }while(offset<addrs.length); >- >- /* CONNECT_DATA command */ >+ // CONNECT_DATA command > sendCONNECT_DATACommand(datasock, direction); >- >- /* Get the InputStream for this socket so we can read some data */ >- InputStream inStream=datasock.getInputStream(); >- int bytesRead=inStream.read(buffer, masterOffset, buffer.length-masterOffset); >+ >+ // Get the InputStream for this socket so we can read some data >+ byte[] buffer = new byte[256]; >+ InputStream inStream = datasock.getInputStream(); >+ int d = inStream.read(buffer, 0, buffer.length); > > ControlMessage DataConnectionResponseMessage = new ControlMessage(); > DataConnectionResponseMessage.setMessageType(Constants.TPTP_AC_MESSAGE); >- masterOffset = DataConnectionResponseMessage.readFromBuffer(buffer, masterOffset); >+ int masterOffset = DataConnectionResponseMessage.readFromBuffer(buffer, 0); > long flags = DataConnectionResponseMessage.getFlags(); >- if ((flags & Constants.DATA_CONNECTION_COMPLETE) != 0) >- { >- dataConnectionId = extractConnectionId(buffer, masterOffset); >- } >+ if ((flags & Constants.CONNECTION_REFUSED) != 0) { >+ if ((flags & Constants.SECURITY_REQUIRED) != 0) { >+ throw new SecureConnectionRequiredException(); >+ } >+ >+ throw new IOException("Connection Refused"); >+ } >+ >+ if ((flags & Constants.DATA_CONNECTION_COMPLETE) <= 0) return -1; >+ >+ int dataConnectionId = extractConnectionId(buffer, masterOffset); >+ if (dataConnectionId <= 0) return -1; > > DataConnection dataConnectionInfo = new DataConnection(); > ACTCPDataServer dataServer = new ACTCPDataServer(); >- if (dataConnectionId > 0) >- { > //dataConnectionId = ::getConnectionId(pMsg) ; >- if(Constants.TPTP_DEBUG)System.out.println("The Data Channel ConnectionID: " + dataConnectionId); >- dataConnectionInfo.setDataSocket(datasock); >- dataConnectionInfo.setDataServer(dataServer); >- _dataConnectionMap.put(new Integer(dataConnectionId), dataConnectionInfo); >- >- try >- { >- dataServer.startServer(null, datasock); >- } >- catch(SocketException sockExp) >- { >- System.out.println("Error starting the TCPDataServer"); >- } >- catch(IOException Exp) >- { >- System.out.println("Error starting the TCPDataServer"); >- } >+ if(Constants.TPTP_DEBUG)System.out.println("The Data Channel ConnectionID: " + dataConnectionId); >+ dataConnectionInfo.setDataSocket(datasock); >+ dataConnectionInfo.setDataServer(dataServer); >+ dataConnectionInfo.setDataconnectionId(dataConnectionId); >+ _dataConnectionMap.put(new Integer(dataConnectionId), dataConnectionInfo); >+ >+ try { >+ dataServer.startServer(null, datasock); >+ } >+ catch(SocketException sockExp) { >+ } >+ catch(IOException Exp) { > } > > return dataConnectionId; > } > >- >- public int addDataListener(int dataConnectionId, IDataProcessor dataProcessor) >- { >- if(dataConnectionId == -1){ return -1;} >+ public int addDataListener(int dataConnectionId, IDataProcessor dataProcessor) { >+ if(dataConnectionId == -1) return -1; >+ > DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); > ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); >- if(dataServer != null) >- { >+ if(dataServer != null) { > dataServer.addDataprocessor(dataProcessor); > return 0; > } >+ > return -1; > } > >- public int removeDataListener(int dataConnectionId, IDataProcessor dataProcessor) >- { >- try >- { >+ public int removeDataListener(int dataConnectionId, IDataProcessor dataProcessor) { >+ try { > DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); > ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); > dataServer.removeDataprocessor(dataProcessor); > dataServer.shutdownServer(); >- }catch(Exception e) >- { >- ///e.printStackTrace(); >- } >+ } catch(Exception e) {} >+ > return 0; > } > >@@ -428,14 +461,11 @@ > _dataConnectionMap.remove(new Integer(dataConnID)); > } > >- private void closeDataConnection(int dataConnID) >- { >+ private void closeDataConnection(int dataConnID) { > DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnID)); > >- try >- { >- if (dataConnectionInfo != null) >- { >+ try { >+ if (dataConnectionInfo != null) { > // Send DISCONNECT command for data channel > sendControlCommand(Constants.DISCONNECT); > >@@ -445,71 +475,58 @@ > dataConnectionInfo.getDataSocket().close(); > } > } >- catch(Exception exp) >- { >+ catch(Exception exp) { > if (Constants.TPTP_DEBUG) System.out.println("Error destroying the data channel - " + exp.getMessage()); > } > } > >- public void sendData(int dataConnectionId, byte[] buffer, int bufferLength) >- { >- try >- { >+ public void sendData(int dataConnectionId, byte[] buffer, int bufferLength) { >+ try { > DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); >- OutputStream stream=dataConnectionInfo._dataSocket.getOutputStream(); >- stream.write(buffer); >- stream.flush(); >- } >- catch(IOException exp) >- { >- System.out.println("Error writing the data to the Socket - " + exp); >- } >- catch(Exception e) >- { >- System.out.println("Error sending the data - " + e); >+ >+ synchronized (writeDataLock) { >+ OutputStream stream=dataConnectionInfo._dataSocket.getOutputStream(); >+ stream.write(buffer); >+ stream.flush(); >+ } >+ } >+ catch(IOException exp) { >+ } >+ catch(Exception e) { > } > } > >- public void sendMessage(IControlMessage msg, ICommandHandler handler) throws IOException >- { >- int commandCount = msg.getCommandCount(); >- >- try >- { >- for (int i=0; i < commandCount; i++) { >- //System.out.println("Adding the handler to the context ..."); >- //System.out.println("Context:" + msg.getCommand(i).getContext()); >- //System.out.println("Handler:" + handler); >- this._contextMapper.addContext(msg.getCommand(i).getContext(), handler); >+ public void sendMessage(IControlMessage msg, ICommandHandler handler) throws IOException { >+ try { >+ if (handler != null) { >+ int commandCount = msg.getCommandCount(); >+ for (int i=0; i < commandCount; i++) { >+ _contextMapper.addContext(msg.getCommand(i).getContext(), handler); >+ } > } > >- int count=msg.getSize(); >- //System.out.println("message size " + count); >- byte[] buffer=new byte[count]; >+ byte[] buffer=new byte[msg.getSize()]; > msg.writeToBuffer(buffer, 0); > >- OutputStream stream=_socket.getOutputStream(); >- stream.write(buffer); >- stream.flush(); >+ synchronized (writeLock) { >+ OutputStream stream=_socket.getOutputStream(); >+ stream.write(buffer); >+ stream.flush(); >+ } > } >- catch(IOException exp) >- { >+ catch(IOException exp) { > if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); > throw exp; > } >- catch(Exception exp) >- { >+ catch(Exception exp) { > if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); >- //throw exp; > } >- > } > > public void disconnect() { >- synchronized(_connectionLock) { > if(!_isComplete) { > _isComplete=true; >- _connectionLock.notifyAll(); >+ > try > { > // Destroy Data Connections >@@ -526,11 +543,6 @@ > // Send DISCONNECT command > sendControlCommand(Constants.DISCONNECT); > >- // Close the SocketReaderThread >- _isComplete = true; >- >- // Close the Socket >- _socket.close(); > if (Constants.TPTP_DEBUG) System.out.println("Successfully disconnected."); > } > catch(IOException e) { >@@ -538,6 +550,8 @@ > if (Constants.TPTP_DEBUG) System.out.println("Exception while disconnecting ... " + e); > } > >+ close(); >+ > /* If this is a nodeimpl clear the keystore spec so it tries an insecure connect next time */ > //Commented by GN since security not supported yet in the new > // if(_node instanceof INode) { >@@ -555,25 +569,30 @@ > } > */ > } >- } > } > >+ public void close () { >+ if (_socket != null) { >+ try { >+ _socket.close(); >+ } catch (Exception e) {} >+ } >+ >+ _isComplete = true; >+ } >+ > public INode getNode() { > return _node; > } > > public boolean isActive() { >- if(_isInitialized) { >- return !_isComplete; >- } >- return false; >+ return _isInitialized && !_isComplete; > } >- public static int checkForRACMessage(byte[] buffer, int offset) >- { >+ >+ public static int checkForRACMessage(byte[] buffer, int offset) { > Message tempMessage=new Message(); > tempMessage.readFromBuffer(buffer, offset); >- if(tempMessage.getMagicNumber() == Constants.RA_MAGIC) >- { >+ if(tempMessage.getMagicNumber() == Constants.RA_MAGIC) { > return Constants.RAC_MESSAGE_HEADER_SIZE; > } > return -1; >@@ -582,127 +601,86 @@ > public int getPort() { > return _port; > } >+ >+ public InetAddress getInetAddress() { >+ return inetAddress; >+ } >+ > protected ContextMapper getContextMapper() > { > return this._contextMapper; > } > > protected int processControlMessage(byte[] buffer, int offset, int length) { >- if(_cmdHandler != null) { >- ControlMessage msg=new ControlMessage(); >- msg.setMessageType(Constants.TPTP_AC_MESSAGE); >+ if(_cmdHandler == null) return -1; >+ >+ ControlMessage msg=new ControlMessage(); >+ msg.setMessageType(Constants.TPTP_AC_MESSAGE); > >- int current=-1; >- try { >- if(Constants.TPTP_DEBUG)System.out.println("Processing the Message."); >- if (buffer.length == length + offset) { >- current = msg.readFromBuffer(buffer, offset); >- } >- else { >- // length is not passed to readFromBuffer so we should ensure >- // that readFromBuffer will not read trash from the end of the buffer >- // and no exception will be thrown. >- // ControlMessage.readFromBuffer(buffer, offset, length) method >- // is needed in order to prevent extra memory allocations in this case. >- byte[] buffer2 = new byte[length]; >- System.arraycopy(buffer, offset, buffer2, 0, length); >- current = msg.readFromBuffer(buffer2, 0); >- if (current >= 0) >- current += offset; >- } >- /* If we read more bytes then were valid, return -1 */ >- if(current>offset+length) >- { >- return -1; >- } >+ int current=-1; >+ try { >+ if(Constants.TPTP_DEBUG)System.out.println("Processing the Message."); >+ if (buffer.length == length + offset) { >+ current = msg.readFromBuffer(buffer, offset); >+ } >+ else { >+ // length is not passed to readFromBuffer so we should ensure >+ // that readFromBuffer will not read trash from the end of the buffer >+ // and no exception will be thrown. >+ // ControlMessage.readFromBuffer(buffer, offset, length) method >+ // is needed in order to prevent extra memory allocations in this case. >+ byte[] buffer2 = new byte[length]; >+ System.arraycopy(buffer, offset, buffer2, 0, length); >+ current = msg.readFromBuffer(buffer2, 0); >+ if (current >= 0) >+ current += offset; > } >- catch(IndexOutOfBoundsException e) >- { >+ /* If we read more bytes then were valid, return -1 */ >+ if(current>offset+length) { > return -1; > } >- catch(Exception e) >- { >- System.out.println("Exception while processing the message" + e); >- return -1; >- } >+ } >+ catch(IndexOutOfBoundsException e) { >+ return -1; >+ } >+ catch(Exception e) { >+ System.out.println("Exception while processing the message" + e); >+ return -1; >+ } > >- /* If we have parsed the message successfully Then process it */ >- int len = msg.getSize(); >+ /* If we have parsed the message successfully Then process it */ >+ int len = msg.getSize(); > >- if (current == len+offset) { >- /* Valid pass on each command */ >- if(Constants.TPTP_DEBUG)System.out.println("Checking for CONNECTION_COMPLETE response"); >- long flags = msg.getFlags(); >- >- if ((flags & Constants.CONNECTION_COMPLETE) != 0) { >- int cmdOffset = 0; >- _connectionId = extractConnectionId(buffer, current+cmdOffset); >- cmdOffset += 4; >- >- int uuidLen = (int) TPTPMessageUtil.readTPTPLongFromBuffer(buffer, current+cmdOffset); >- cmdOffset += 4 + uuidLen + 1; // including trailing null >- >- if ((cmdOffset + 4) <= msg.getLength()) { // check if protoVersion is available >- protoVersion = (int) TPTPMessageUtil.readTPTPLongFromBuffer(buffer, current+cmdOffset); >- } >- >- current += msg.getLength(); >- if(Constants.TPTP_DEBUG)System.out.println("Connection complete response - " + _connectionId + ", protoVer=" + protoVersion); >- synchronized (_connectionLock) { >- _connectionLock.notifyAll(); >- } >- } >- if((flags & Constants.CONNECTION_RECONNECT_REQUEST) != 0) >- { >- int portreq = -1; >- synchronized(_socket) >- { >- try >- { >- _isComplete=true; >- _dataConnectionMap.clear(); >- _socket.close(); >- CommandFragment cmd=(CommandFragment)msg.getCommand(0); >- String strToParse = cmd.getCommandData();//new String(buffer,Constants.ACK_HDR_LEN,buffer.length-12); >- TPTPXMLParse ParseObj = new TPTPXMLParse(); >- ParseObj.setParser(strToParse.trim()); >- Hashtable CommandHash = ParseObj.getHashTable(); >- if(CommandHash.containsKey("port")) >- { >- portreq = Integer.parseInt((String)ParseObj.getHashTable().get("port")); >- } >- } >- catch(Exception e){} >- } >- >- if(portreq == -1) { >- // This is a bad situation. The AC should have given us a port >- // Since it didn't (unexpectedly), let's try a default >- portreq = 10006; >- } >- >- // Tell the caller how to reconnect >- _reconnectRequestedException = new ReconnectRequestedException( portreq ); >- synchronized (_connectionLock) { >- // This notify will cause init to throw the above request >- // and cause the read thread to bail >- _connectionLock.notifyAll(); >- } >- } >- int count=msg.getCommandCount(); >+ if (current == len+offset) { >+ /* Valid pass on each command */ >+// if(Constants.TPTP_DEBUG)System.out.println("Checking for CONNECTION_COMPLETE response"); >+ long flags = msg.getFlags(); >+ >+ if ((flags & Constants.AUTHENTICATION_FAILED) != 0) { >+ isAuthenticated = false; >+ current += msg.getLength(); >+ synchronized (authLock) { >+ authLock.notifyAll(); >+ } >+ } >+ else if ((flags & Constants.AUTHENTICATION_SUCCESSFUL) != 0) { >+ isAuthenticated = true; >+ current += msg.getLength(); >+ synchronized (authLock) { >+ authLock.notifyAll(); >+ } >+ } >+ else { >+ int count=msg.getCommandCount(); >+ for(int i=0; i<count; i++) { >+ _cmdHandler.incomingCommand(_node, msg.getCommand(i)); >+ } >+ } >+ } > >- for(int i=0; i<count; i++) >- { >- _cmdHandler.incomingCommand(_node, msg.getCommand(i)); >- } >- } >- return current; >- } >- return -1; >+ return current; > } > >- >- > /** > * @see Connection#addConnectionListener(ConnectionListener) > */ >@@ -713,7 +691,6 @@ > } > } > } >- > > /** > * @see Connection#removeConnectionListener(ConnectionListener) >@@ -729,23 +706,29 @@ > public void setSoTimeout(int timeout) { > _soTimeout = timeout; > } >- >+ > public int getProtocolVersion() { > return protoVersion; > } > >- class SocketReaderThread extends Thread implements Constants >- { >- public void run() >- { >- /* Run forever */ >- byte[] buffer=new byte[MAX_MESSAGE_LENGTH]; >+ public Socket getSocket() { >+ return _socket; >+ } >+ >+ public void setSocket(Socket socket) { >+ _socket = socket; >+ _isComplete = true; >+ } >+ >+ class SocketReaderThread extends Thread implements Constants { >+ public void run() { >+ byte[] buffer = new byte[MAX_MESSAGE_LENGTH]; > int masterOffset=0; > boolean incompleteMsg; >- int timeoutCount=0; >+ > _isComplete = false; > >- /* Get the InputStream for this socket so we can read some data */ >+ // Get the InputStream for this socket so we can read some data > BufferedInputStream inStream; > try { > inStream = new BufferedInputStream(_socket.getInputStream()); >@@ -756,71 +739,64 @@ > return; > } > >- while(!_isComplete) >- { >- incompleteMsg = false; /* 185463 */ >- try >- { >- int bytesRead=inStream.read(buffer, masterOffset, buffer.length-masterOffset); >- >- if(bytesRead==-1) {break;} >+ while(!_isComplete) { >+ incompleteMsg = false; // 185463 >+ try { >+ int bytesRead = inStream.read(buffer, masterOffset, buffer.length-masterOffset); >+ if(bytesRead == -1) break; > >- if (masterOffset > 0) >- { >+ if (masterOffset > 0) { > bytesRead += masterOffset; > masterOffset = 0; > } > >- /* >- * If we've tried to connect to a backward compatibility >- * layer of the AC, we'll immediately get a response in >- * RAC format. If we ignore this, the BC layer will >- * respond to our first message by giving us the port >- * to connect to the regular socket TL. >- */ >+ // >+ // If we've tried to connect to a backward compatibility >+ // layer of the AC, we'll immediately get a response in >+ // RAC format. If we ignore this, the BC layer will >+ // respond to our first message by giving us the port >+ // to connect to the regular socket TL. >+ // > int vret = checkForRACMessage(buffer, masterOffset); >- if(vret != -1) >- { >+ if (vret != -1) { > // We read the header, the next four bytes will be the length > // (including the header) of the RAC message >- long length=TPTPMessageUtil.readTPTPLongFromBuffer(buffer, vret); >+ long length = TPTPMessageUtil.readTPTPLongFromBuffer(buffer, vret); > masterOffset = (int)length; > } > >- while(masterOffset<bytesRead) >- { >+ while(masterOffset<bytesRead) { > int newOffset=0; >- /* First we make sure we have at least enough read for the message header. >- If not, compress and re-read. >- */ >+ // First we make sure we have at least enough read for the message header. >+ // If not, compress and re-read. >+ // > if ( bytesRead-masterOffset < AC_MESSAGE_HEADER_SIZE ) { > System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); > masterOffset=bytesRead-masterOffset; > incompleteMsg = true; > break; > } >- /* We have the header information, now lets try and get the entire message. Once >- again the same error conditions can occur. >- */ >- if(Constants.TPTP_DEBUG)System.out.println("Received a non-acknowledgement message"); >- //System.out.println("Offset xx::"+new String(buffer)); >- newOffset=processControlMessage(buffer, masterOffset, bytesRead); >- if(Constants.TPTP_DEBUG)System.out.println("Recvd Message : " + buffer.toString()); >- if ( newOffset == -1 ) >- { >+ >+ // We have the header information, now lets try and get the entire message. Once >+ // again the same error conditions can occur. >+ // >+ newOffset = processControlMessage(buffer, masterOffset, bytesRead); >+ >+ if(Constants.TPTP_DEBUG)System.out.println("Recvd Message: " + buffer.toString()); >+ >+ if (newOffset == -1) { > // newOffset of -1 indicates the message is bigger than what's left in the buffer > // If the masterOffset is zero, this means the message is bigger than the buffer > // itself, so we need to grow the buffer. Otherwise, we'll slide the message > // to the beginning of the buffer and try to read some more >- if(masterOffset>0) >- { >+ if(masterOffset>0) { > System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); > } > else if (bytesRead == buffer.length) > { > int len = buffer.length * 2; > byte[] tmpbuffer=new byte[len]; >- /* Copy necessary data over to new buffer */ >+ // Copy necessary data over to new buffer > System.arraycopy(buffer, masterOffset, tmpbuffer, 0, bytesRead-masterOffset); > buffer = tmpbuffer; > } >@@ -829,22 +805,14 @@ > break; > } > masterOffset=newOffset; >- } /* end of inner while */ >+ } // end of inner while > > if (!incompleteMsg) > { > masterOffset=0; > } > } >- catch(InterruptedIOException e) >- { >- if(timeoutCount > Constants.CONNECT_TIMEOUT_TRY_COUNT && !_isInitialized) >- { >- synchronized(_connectionLock) { >- _connectionLock.notifyAll(); >- } >- } >- timeoutCount++; >+ catch(InterruptedIOException e) { > } > catch(SocketException e) > { >@@ -856,9 +824,40 @@ > //e.printStackTrace(); > break; > } >- } /* end of outer while */ >- /* The server is now stopping */ >+ } // end of outer while >+ // The server is now stopping >+ > disconnect(); > } > } >+ >+ public boolean isAuthenticated() { >+ return isAuthenticated; >+ } >+ >+ protected boolean authenticateUser(User user) { >+ if (user == null || user.getName() == null || user.getPassword() == null) return false; >+ >+ ControlMessage authMsg = new ControlMessage(); >+ authMsg.setMessageType(Constants.TPTP_AC_MESSAGE); >+ authMsg.setMagicNumber(Constants.AC_MAGIC_NUMBER); >+ authMsg.setFlags(Constants.AUTHENTICATE); >+ >+ AuthenticateCommand authCmd = new AuthenticateCommand(user.getName(), user.getPassword()); >+ authMsg.appendCommand(authCmd); >+ >+ try { >+ sendMessage(authMsg, new ICommandHandler() { >+ public void incomingCommand(INode node, ICommandElement command) {} >+ }); >+ } catch (Exception e) { >+ return false; >+ } >+ >+ synchronized (authLock) { >+ try { authLock.wait(Constants.WAIT_RESPONCE_TIMEOUT); } catch (Exception e) {} >+ } >+ >+ return isAuthenticated; >+ } > } >Index: src/org/eclipse/tptp/platform/execution/exceptions/LoginFailedException.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/exceptions/LoginFailedException.java,v >retrieving revision 1.3 >diff -u -r1.3 LoginFailedException.java >--- src/org/eclipse/tptp/platform/execution/exceptions/LoginFailedException.java 10 Feb 2006 21:10:24 -0000 1.3 >+++ src/org/eclipse/tptp/platform/execution/exceptions/LoginFailedException.java 29 Aug 2007 11:00:04 -0000 >@@ -28,6 +28,10 @@ > private long _port; > > >+ public LoginFailedException() { >+ _port = 0; >+ } >+ > public LoginFailedException(long port) { > _port=port; > } >Index: src/org/eclipse/tptp/platform/execution/client/core/IAgentController.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/IAgentController.java,v >retrieving revision 1.17 >diff -u -r1.17 IAgentController.java >--- src/org/eclipse/tptp/platform/execution/client/core/IAgentController.java 26 Apr 2006 04:02:51 -0000 1.17 >+++ src/org/eclipse/tptp/platform/execution/client/core/IAgentController.java 29 Aug 2007 11:00:04 -0000 >@@ -283,6 +283,8 @@ > */ > boolean authenticateUser(User user) throws NotConnectedException; > >+ boolean isAuthenticated(); >+ > /** > * Get a Agent Reference to communicate with the agent > * @param agentName >Index: src/org/eclipse/tptp/platform/execution/client/core/NodeFactory.java >=================================================================== >RCS file: /cvsroot/tptp/platform/org.eclipse.tptp.platform.execution/src/org/eclipse/tptp/platform/execution/client/core/NodeFactory.java,v >retrieving revision 1.4 >diff -u -r1.4 NodeFactory.java >--- src/org/eclipse/tptp/platform/execution/client/core/NodeFactory.java 24 Apr 2006 20:31:26 -0000 1.4 >+++ src/org/eclipse/tptp/platform/execution/client/core/NodeFactory.java 29 Aug 2007 11:00:04 -0000 >@@ -87,8 +87,11 @@ > * if the Node exists it will be returned. > */ > public static INode createNode(InetAddress address) throws UnknownHostException { >- //return createNode(address, null); >- return createNode(address); >+ String hostname = address.getHostName(); >+ INode node = containsNode(hostname); >+ if (node != null) return node; >+ >+ return addNode(hostname, address, null); > } > > >@@ -148,10 +151,6 @@ > } > } > >- >- >- >- > private static INode containsNode(String name) { > synchronized(_servers) { > // If this is "localhost" try and resolve its real name first >@@ -199,7 +198,6 @@ > return null; > } > >- > /** > * Searches the Node list based upon the InetAddress. > * @return the Node if it exists, null otherwise. >@@ -208,7 +206,4 @@ > //return getNode(addr, null); > return containsNode(addr.getHostName()); > } >- >- > } >- >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/X509TrustManagerImpl.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/client/core/internal/X509TrustManagerImpl.java >diff -N src/org/eclipse/tptp/platform/execution/client/core/internal/X509TrustManagerImpl.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/X509TrustManagerImpl.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,47 @@ >+package org.eclipse.tptp.platform.execution.client.core.internal; >+ >+import java.security.KeyStore; >+import java.security.cert.CertificateException; >+import java.security.cert.X509Certificate; >+ >+import javax.net.ssl.TrustManager; >+import javax.net.ssl.TrustManagerFactory; >+import javax.net.ssl.X509TrustManager; >+ >+public class X509TrustManagerImpl implements X509TrustManager { >+ private X509TrustManager defaultX509tm; >+ >+ X509TrustManagerImpl () throws Exception { >+ TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509"); >+ tmf.init((KeyStore) null); >+ >+ TrustManager tms[] = tmf.getTrustManagers(); >+ for (int i=0; i<tms.length; i++) { >+ if (tms[i] instanceof X509TrustManager) { >+ defaultX509tm = (X509TrustManager) tms[i]; >+ break; >+ } >+ } >+ } >+ >+ public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { >+ if (defaultX509tm == null) throw new CertificateException ("Trust manager not found"); >+ >+ defaultX509tm.checkClientTrusted(certs, authType); >+ } >+ >+ public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { >+ if (defaultX509tm != null) >+ try { >+ defaultX509tm.checkServerTrusted(certs, authType); >+ return; >+ } catch (Exception e) {} >+ >+// TODO: ask user if certificate is trusted, save it >+ } >+ >+ public X509Certificate[] getAcceptedIssuers() { >+ if (defaultX509tm != null) return defaultX509tm.getAcceptedIssuers(); >+ return null; >+ } >+} >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/commands/AuthenticateCommand.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/client/core/internal/commands/AuthenticateCommand.java >diff -N src/org/eclipse/tptp/platform/execution/client/core/internal/commands/AuthenticateCommand.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/commands/AuthenticateCommand.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,28 @@ >+package org.eclipse.tptp.platform.execution.client.core.internal.commands; >+ >+import org.eclipse.tptp.platform.execution.util.internal.CommandElement; >+import org.eclipse.tptp.platform.execution.util.internal.TPTPMessageUtil; >+import org.eclipse.tptp.platform.execution.util.internal.TPTPString; >+ >+public class AuthenticateCommand extends CommandElement { >+ private TPTPString name; >+ private TPTPString password; >+ >+ public AuthenticateCommand(String name, String password) { >+ this.name = new TPTPString(name); >+ this.password = new TPTPString(password); >+ } >+ >+ public int getSize() { >+ return name.getSize() + password.getSize(); >+ } >+ >+ public int readFromBuffer(byte[] buffer, int offset) { >+ return 0; >+ } >+ >+ public int writeToBuffer(byte[] buffer, int offset) { >+ int current = TPTPMessageUtil.writeTPTPStringToBuffer(buffer, offset, name); >+ return TPTPMessageUtil.writeTPTPStringToBuffer(buffer, current, password); >+ } >+} >Index: src/org/eclipse/tptp/platform/execution/util/internal/IAgentControllerFactory.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/util/internal/IAgentControllerFactory.java >diff -N src/org/eclipse/tptp/platform/execution/util/internal/IAgentControllerFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/util/internal/IAgentControllerFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,7 @@ >+package org.eclipse.tptp.platform.execution.util.internal; >+ >+import org.eclipse.tptp.platform.execution.client.core.IAgentController; >+ >+public interface IAgentControllerFactory { >+ public IAgentController createConnection(String hostName, int portNumber) throws Exception; >+} >Index: src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionFactory.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionFactory.java >diff -N src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/client/core/internal/ConnectionFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,39 @@ >+package org.eclipse.tptp.platform.execution.client.core.internal; >+ >+import java.io.IOException; >+ >+import org.eclipse.tptp.platform.execution.client.core.ConnectionInfo; >+import org.eclipse.tptp.platform.execution.client.core.INode; >+import org.eclipse.tptp.platform.execution.exceptions.LoginFailedException; >+import org.eclipse.tptp.platform.execution.exceptions.NotConnectedException; >+import org.eclipse.tptp.platform.execution.exceptions.ReconnectRequestedException; >+import org.eclipse.tptp.platform.execution.exceptions.SecureConnectionRequiredException; >+import org.eclipse.tptp.platform.execution.exceptions.UntrustedAgentControllerException; >+ >+public class ConnectionFactory { >+ public static IConnection createConnection (INode node, ConnectionInfo conInfo) throws LoginFailedException, >+ UntrustedAgentControllerException, IOException, ReconnectRequestedException, SecureConnectionRequiredException, >+ NotConnectedException { >+ >+ ConnectionImpl con = null; >+ boolean securityRequired = false; >+ >+ try { >+ con = new ConnectionImpl(); >+ con.connect(node, conInfo); >+ } catch (SecureConnectionRequiredException e) { >+ securityRequired = true; >+ } >+ >+ if (!securityRequired) return con; // insecure connection >+ >+ SecureConnectionImpl secCon = new SecureConnectionImpl (); >+ >+ if (!secCon.connect(node, con)) { >+ secCon.close(); >+ secCon = null; >+ } >+ >+ return secCon; >+ } >+} >Index: src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerFactory.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerFactory.java >diff -N src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerFactory.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/util/internal/AgentControllerFactory.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,161 @@ >+package org.eclipse.tptp.platform.execution.util.internal; >+ >+import org.eclipse.tptp.platform.execution.client.core.ConnectionInfo; >+import org.eclipse.tptp.platform.execution.client.core.IAgentController; >+import org.eclipse.tptp.platform.execution.client.core.INode; >+import org.eclipse.tptp.platform.execution.client.core.NodeFactory; >+import org.eclipse.tptp.platform.execution.exceptions.AgentControllerUnavailableException; >+import org.eclipse.tptp.platform.execution.exceptions.LoginFailedException; >+import org.eclipse.tptp.platform.execution.exceptions.SecureConnectionRequiredException; >+import org.eclipse.tptp.platform.execution.security.User; >+ >+public class AgentControllerFactory implements IAgentControllerFactory { >+ /** >+ * The incremental wait times >+ */ >+ public static final int INCREMENTAL_WAIT = 100; >+ >+ /** >+ * The total incremental wait >+ */ >+ public static final int TOTAL_WAIT = 100; >+ >+ public IAgentController createConnection(String hostName, int portNumber) throws Exception { >+ IAgentController ac = null; >+ EstablishConnection establishConnection = new EstablishConnection(hostName, portNumber); >+ String userName = null; >+ boolean lookingForUser = true; >+ int retry = 0; >+ >+ do { >+ Thread ect = new Thread(establishConnection); >+ ect.start(); >+ /* Wait for a maximum of 10 seconds before bailing out */ >+ ect.join(TOTAL_WAIT*INCREMENTAL_WAIT); >+ if (ect.isAlive()) ect.interrupt(); >+ >+ Exception error = establishConnection.getException(); >+ if (error != null) throw error; >+ >+ ac = establishConnection.getConnection(); >+ if (ac == null) { >+ throw new AgentControllerUnavailableException(""); >+ } >+ >+ if (ac.isAuthenticated()) break; >+ >+ User user = null; >+ if (lookingForUser) { >+ user = findUser(hostName); >+ lookingForUser = false; >+ if (user != null) { >+ userName = user.getName(); >+ establishConnection.setUser(user); >+ continue; // try it >+ } >+ } >+ >+ user = promptAuthentication(hostName, userName); >+ if(user == null) break; >+ >+ userName = user.getName(); >+ establishConnection.setUser(user); >+ } while (retry++ < 3); >+ >+ if (!ac.isAuthenticated()) { >+ try { ac.disconnect(); } catch (Exception e) {} >+ throw new LoginFailedException("Authentication failed"); >+ } >+ >+ return ac; >+ } >+ >+ /* >+ * The establishment of the connection should be done in a separate >+ * thread. >+ */ >+ class EstablishConnection implements Runnable { >+ private String host; >+ private int port; >+ private Exception e; >+ private IAgentController ac; >+ private User user; >+ private INode node; >+ >+ public EstablishConnection (String host, int port) { >+ this.host = host; >+ this.port = port; >+ >+ node = null; >+ } >+ >+ public void run() { >+ ac = null; >+ e = null; >+ >+ if (node == null) { >+ try { >+ node = NodeFactory.createNode(host); >+ } catch (Exception ex) { >+ node = null; >+ } >+ >+ if (node == null) return; >+ } >+ >+ ConnectionInfo connInfo = new ConnectionInfo(); >+ >+ connInfo.setHostName(host); >+ connInfo.setPort(port); >+ >+ try { >+ if (user == null) >+ ac = node.connect(connInfo); >+ else >+ ac = node.connect(connInfo, user); >+ >+ /* Notify that the connection is done */ >+ } catch (LoginFailedException e) { >+ } catch (SecureConnectionRequiredException e) { >+ } catch (Exception e) { >+ /* >+ * For some reason Sun JDK 1.5_06 has problems resolving the >+ * hostname "localhost". As a workaround change it to >+ * 127.0.0.1 >+ */ >+ >+ this.e = e; >+ >+ if ("localhost".equals(connInfo.getHostName())) { >+ connInfo.setHostName("127.0.0.1"); >+ >+ try { >+ ac = node.connect(connInfo); >+ this.e = null; >+ } catch (Exception ex) {} >+ } >+ } >+ } >+ >+ public Exception getException() { >+ return e; >+ } >+ >+ public IAgentController getConnection() { >+ return ac; >+ } >+ >+ public void setUser(User user) { >+ this.user = user; >+ } >+ } >+ >+ // to prevent multiple User registration, to be substituted by descendants >+ public User findUser(String hostName) { >+ return null; >+ } >+ >+ public User promptAuthentication(String hostName, String userName) { >+ return null; >+ } >+} >Index: src/org/eclipse/tptp/platform/execution/samples/SecureClientAC.java >=================================================================== >RCS file: src/org/eclipse/tptp/platform/execution/samples/SecureClientAC.java >diff -N src/org/eclipse/tptp/platform/execution/samples/SecureClientAC.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/tptp/platform/execution/samples/SecureClientAC.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,118 @@ >+package org.eclipse.tptp.platform.execution.samples; >+ >+import org.eclipse.tptp.platform.execution.client.agent.*; >+import org.eclipse.tptp.platform.execution.client.core.*; >+import org.eclipse.tptp.platform.execution.security.User; >+import org.eclipse.tptp.platform.execution.util.*; >+ >+public class SecureClientAC { >+ public final static String HOST = "localhost"; >+ public final static int PORT = 10006; >+ >+ public static void main(String[] args) { >+ ConnectionInfo connInfo = null; >+ IAgentController ac = null; >+ INode node = null; >+ >+ System.out.println("Connecting to Agent Controller\n"); >+ >+ try { >+ node = NodeFactory.createNode(HOST); >+ //Set the connection parameters required >+ connInfo = new ConnectionInfo(); >+ connInfo.setHostName(HOST); >+ connInfo.setPort(PORT); >+ >+ ac = node.connect(connInfo); >+ } catch (Exception e) { >+ System.out.println("Error occurred while connecting to " + HOST + ":" + e); >+ } >+ >+ if (ac == null) return; >+ >+ System.out.println("Connected to " + HOST + ":" + PORT); >+ >+ while (!ac.isAuthenticated()) { >+ User user = getUser(); >+ if (user == null) { >+ System.out.println("\nAuthentication failed"); >+ break; >+ } >+ >+ try { >+ ac = node.connect(connInfo, user); >+ } catch (Exception e) { >+ System.out.println("ex " + e); >+ } >+ } >+ >+ if (!ac.isAuthenticated()) { >+ ac.disconnect(); >+ return; >+ } >+ >+ try { >+ ICollector timeCollector = (ICollector) ac.getAgent("org.eclipse.tptp.TimeCollector", "org.eclipse.tptp.platform.execution.client.agent.ICollector"); >+ if(timeCollector == null) { >+ System.out.println("Agent not available, configure the agent and retry"); >+ ac.disconnect(); >+ return; >+ } >+ >+ //Create the Data Connection required to recieve response from the collector; >+ //The input to startMonitoring is the to establish the data connection between the client and >+ //the agent controller for the direction of data flow. >+ //See constants for additional values. >+ timeCollector.startMonitoring(TPTPDataPath.DATA_PATH_TWO_WAY); >+ timeCollector.addDataListener(new IDataProcessor() { >+ public void incomingData(byte[] buffer, int length, java.net.InetAddress peer) { >+ String data = new String(buffer, 0, length); >+ System.out.println("The Data received from TimeCollector - " + data.trim()); >+ } >+ >+ public void incomingData(char[] buffer, int length, java.net.InetAddress peer) {} >+ public void invalidDataType(byte[] data, int length, java.net.InetAddress peer) {} >+ public void waitingForData() {} >+ }); >+ >+ //Send a message to Agent >+ >+ timeCollector.run(); >+ timeCollector.sendData("HELLO from org.eclipse.tptp.platform.execution plugin".getBytes()); >+ //Wait for the response to arrive >+ Thread.sleep(2000); >+ >+ timeCollector.stop(); >+ >+ Thread.sleep(1000); >+ // Disconnect from AC >+ ac.disconnect(); >+ System.out.println("disconnected"); >+ } >+ catch(Exception exp) { >+ System.out.println("Error occurred while connecting to " + HOST + ":" + exp); >+ } >+ } >+ >+ private static User getUser() { >+ byte buf[] = new byte[256]; >+ int n; >+ >+ System.out.println("\nUser authentication."); >+ System.out.print("Login: "); >+ try { n = System.in.read(buf); } catch (Exception e) { n = 0; } >+ if (n <= 0) return null; >+ >+ String login = new String(buf, 0, n).trim(); >+ if (login.length() == 0) return null; >+ >+ System.out.print("Password: "); >+ try { n = System.in.read(buf); } catch (Exception e) { n = 0; } >+ if (n <= 0) return null; >+ >+ String psw = new String(buf, 0, n).trim(); >+ if (psw.length() == 0) return null; >+ >+ return new User(null, login, psw); >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 195644
:
73419
|
73421
|
73422
|
73423
|
73425
|
73429
| 77241 |
77242
|
77243
|
80051
|
80132
|
80870
|
84612
|
84631