|
Added
Link Here
|
| 1 |
package org.eclipse.tptp.platform.common.ui.internal.util; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.net.InetAddress; |
| 5 |
|
| 6 |
import org.eclipse.hyades.internal.execution.local.common.ControlMessage; |
| 7 |
import org.eclipse.hyades.internal.execution.local.control.CommandHandler; |
| 8 |
import org.eclipse.hyades.internal.execution.local.control.ConnectionListener; |
| 9 |
import org.eclipse.hyades.internal.execution.local.control.Node; |
| 10 |
import org.eclipse.hyades.internal.execution.security.AuthenticationListener; |
| 11 |
import org.eclipse.hyades.internal.execution.security.LoginFailedException; |
| 12 |
import org.eclipse.hyades.internal.execution.security.SecureConnectionRequiredException; |
| 13 |
import org.eclipse.hyades.internal.execution.security.UntrustedAgentControllerException; |
| 14 |
import org.eclipse.hyades.security.internal.util.ConnectUtilUI; |
| 15 |
import org.eclipse.hyades.security.internal.util.IConnectUtilUser; |
| 16 |
import org.eclipse.tptp.platform.execution.security.User; |
| 17 |
import org.eclipse.tptp.platform.execution.util.internal.AgentControllerFactory; |
| 18 |
|
| 19 |
public class UIAgentControllerFactory extends AgentControllerFactory { |
| 20 |
public User promptAuthentication(String hostName, String userName) { |
| 21 |
ConnectUtilUI cui = new ConnectUtilUI(); |
| 22 |
IConnectUtilUser cuUser = cui.promptAuthentication(hostName, userName); |
| 23 |
if(cuUser == null) return null; |
| 24 |
|
| 25 |
return new User(null, cuUser.getName(), cuUser.getPassword()); |
| 26 |
} |
| 27 |
|
| 28 |
public User findUser(String hostName) { |
| 29 |
User user = findUserForHost (hostName); |
| 30 |
if (user != null) return user; |
| 31 |
|
| 32 |
if ("localhost".equals(hostName)) |
| 33 |
return findUserForHost (null); |
| 34 |
|
| 35 |
return null; |
| 36 |
} |
| 37 |
|
| 38 |
private User findUserForHost (String hostName) { |
| 39 |
org.eclipse.hyades.internal.execution.local.control.Node node; |
| 40 |
|
| 41 |
try { |
| 42 |
if (hostName != null) |
| 43 |
node = org.eclipse.hyades.internal.execution.local.control.NodeFactory.getNode(hostName, null); |
| 44 |
else |
| 45 |
node = org.eclipse.hyades.internal.execution.local.control.NodeFactory.getNode(InetAddress.getLocalHost(), null); |
| 46 |
} catch (Exception e) { |
| 47 |
node = null; |
| 48 |
} |
| 49 |
|
| 50 |
if (node == null || !node.isConnected()) { |
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
org.eclipse.hyades.internal.execution.security.User hyadesUser = node.getUser(); |
| 55 |
if (hyadesUser == null) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
// just to extract the password, saving current hyades.User API |
| 59 |
HyadesConnection con = new HyadesConnection(); |
| 60 |
try { hyadesUser.login(con); } catch (Exception e) {} |
| 61 |
|
| 62 |
return con.getUser(); |
| 63 |
} |
| 64 |
|
| 65 |
class HyadesConnection implements org.eclipse.hyades.internal.execution.local.control.Connection { |
| 66 |
public User user = null; |
| 67 |
|
| 68 |
public void sendMessage(ControlMessage msg, CommandHandler handler) throws IOException { |
| 69 |
int count = msg.getSize(); |
| 70 |
if (count <= 1) return; |
| 71 |
|
| 72 |
org.eclipse.hyades.internal.execution.local.common.CommandElement ce = msg.getCommand(0); |
| 73 |
if (!(ce instanceof org.eclipse.hyades.internal.execution.local.common.AuthenticateCommand)) return; |
| 74 |
|
| 75 |
org.eclipse.hyades.internal.execution.local.common.AuthenticateCommand acmd = |
| 76 |
(org.eclipse.hyades.internal.execution.local.common.AuthenticateCommand) ce; |
| 77 |
|
| 78 |
byte buf[] = new byte[acmd.getSize()]; |
| 79 |
acmd.writeToBuffer(buf, 0); |
| 80 |
|
| 81 |
org.eclipse.hyades.internal.execution.local.common.RAString hyadesName = |
| 82 |
new org.eclipse.hyades.internal.execution.local.common.RAString(); |
| 83 |
|
| 84 |
org.eclipse.hyades.internal.execution.local.common.RAString hyadesPsw = |
| 85 |
new org.eclipse.hyades.internal.execution.local.common.RAString(); |
| 86 |
|
| 87 |
int offset = org.eclipse.hyades.internal.execution.local.common.Message.readRAStringFromBuffer(buf, 4, hyadesName); |
| 88 |
org.eclipse.hyades.internal.execution.local.common.Message.readRAStringFromBuffer(buf, offset, hyadesPsw); |
| 89 |
|
| 90 |
user = new User(null, hyadesName.getData(), hyadesPsw.getData()); |
| 91 |
} |
| 92 |
|
| 93 |
public void addAuthenticationListener(AuthenticationListener listener) {} |
| 94 |
public void addConnectionListener(ConnectionListener listener) {} |
| 95 |
public void connect(Node node, int port) throws IOException,SecureConnectionRequiredException, LoginFailedException, |
| 96 |
UntrustedAgentControllerException { |
| 97 |
} |
| 98 |
public void disconnect() {} |
| 99 |
public Node getNode() { return null; } |
| 100 |
public int getPort() { return 0; } |
| 101 |
public boolean isActive() { return false; } |
| 102 |
public void removeAuthenticationListener(AuthenticationListener listener) {} |
| 103 |
public void removeConnectionListener(ConnectionListener listener) {} |
| 104 |
|
| 105 |
public User getUser() { return user; } |
| 106 |
} |
| 107 |
} |