diff --git a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIp.java b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIp.java index eaa3120d43..699a543071 100644 --- a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIp.java +++ b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIp.java @@ -183,17 +183,14 @@ public int write(final ByteBuffer b) throws IOException public final static class Provider { - // Must ensure host field is never null private static final String DEFAULT_LOCAL = "local"; - private static final int DEFAULT_PORT = 8000; - private static final String DEFAULT_USER = System.getProperty("user.name"); - private static final String PREFIX_LOCAL = "local://"; - private static final String PREFIX_SSH = "ssh://"; - private static final String PREFIX_FILE = "file://"; + private static final String PREFIX_LOCAL = "local"; + private static final String PREFIX_FILE = "file"; + private static final String PREFIX_SSH = "ssh"; + private static final String PREFIX_TCP = "tcp"; + private static final String DEFAULT_PREFIX = Provider.PREFIX_TCP; private final String prefix; - private final String host; - private final int port; - private final String user; + private final String suffix; public Provider() { @@ -201,89 +198,23 @@ public Provider() } public Provider(final String provider) - { - this(provider, false); - } - - public Provider(String provider, final boolean use_ssh) { if (provider == null || provider.isEmpty()) { this.prefix = Provider.PREFIX_LOCAL; - this.user = null; - this.host = null; - this.port = 0; - } - else if (provider.toLowerCase().startsWith(Provider.PREFIX_LOCAL)) - { - this.prefix = Provider.PREFIX_LOCAL; - this.user = null; - this.host = provider.substring(Provider.PREFIX_LOCAL.length()); - this.port = 0; + this.suffix = Provider.DEFAULT_LOCAL; + return; } - else if (provider.toLowerCase().startsWith(Provider.PREFIX_FILE)) + final String pre_post[] = provider.split("://", 2); + if (pre_post.length > 1) { - this.prefix = Provider.PREFIX_FILE; - this.user = null; - this.host = provider.substring(Provider.PREFIX_FILE.length()); - this.port = 0; + this.prefix = pre_post[0]; + this.suffix = pre_post[1]; } else { - final boolean sshstr = provider.toLowerCase().startsWith(Provider.PREFIX_SSH); - if (sshstr) - { - provider = provider.substring(Provider.PREFIX_SSH.length()); - } - this.prefix = sshstr || use_ssh ? Provider.PREFIX_SSH : ""; - final int at = provider.indexOf("@"); - final int cn = provider.indexOf(":"); - this.user = at < 0 ? null : provider.substring(0, at); - this.host = cn < 0 ? provider.substring(at + 1).toLowerCase() - : provider.substring(at + 1, cn).toLowerCase(); - this.port = cn < 0 ? 0 : Short.parseShort(provider.substring(cn + 1)); - } - } - - public Provider(final String host, final int port) - { - this(host, port, null, false); - } - - public Provider(final String host, final int port, final String user) - { - this(host, port, user, user != null); - } - - public Provider(String host, final int port, final String user, final boolean use_ssh) - { - if (host == null) - { - this.prefix = Provider.PREFIX_LOCAL; - this.user = null; - this.host = null; - this.port = 0; - } - else - { - this.user = user; - host = host.toLowerCase(); - if (host.startsWith(Provider.PREFIX_LOCAL)) - { - host = host.substring(Provider.PREFIX_LOCAL.length()); - this.prefix = Provider.PREFIX_LOCAL; - } - else - { - final boolean sshstr = host.startsWith(Provider.PREFIX_SSH); - if (sshstr) - { - host = host.substring(Provider.PREFIX_SSH.length()); - } - this.prefix = sshstr || use_ssh ? Provider.PREFIX_SSH : ""; - } - this.host = host; - this.port = port; + this.prefix = Provider.DEFAULT_PREFIX; + this.suffix = pre_post[0]; } } @@ -295,52 +226,19 @@ public final boolean equals(final Object obj) if (obj == null || !(obj instanceof Provider)) return false; final Provider provider = (Provider) obj; - return this.getPrefix().equals(provider.getPrefix()) && this.getHost().equals(provider.getHost()) - && this.getPort() == provider.getPort() && this.getUser().equals(provider.getUser()); - } - - public final MdsIp getConnection() - { - return new MdsIp(this); - } - - public String getHost() - { - return this.host == null || this.host.isEmpty() ? Provider.DEFAULT_LOCAL : this.host; - } - - public int getPort() - { - return this.port != 0 ? this.port : ("".equals(this.prefix) ? 0 : Provider.DEFAULT_PORT); - } - - public String getPrefix() - { - return this.prefix; - } - - public final String getUser() - { - return this.user == null || this.user.isEmpty() ? Provider.DEFAULT_USER : this.user; + return this.prefix.equals(provider.prefix) && this.suffix.equals(provider.suffix); } @Override public int hashCode() { - return this.getPrefix().hashCode() ^ this.getUser().hashCode() ^ this.getHost().hashCode() ^ this.getPort(); + return this.prefix.hashCode() ^ this.suffix.hashCode(); } @Override public final String toString() { - final StringBuilder sb = new StringBuilder(); - sb.append(this.getPrefix()); - if (this.user != null) - sb.append(this.user).append('@'); - sb.append(this.getHost()); - if (this.port != 0) - sb.append(':').append(this.port); - return sb.toString(); + return this.prefix + "://" + this.suffix; } } @@ -673,27 +571,42 @@ synchronized private final void connectToServer() throws IOException return; // connect to server this.defined_funs.clear(); - final String prefix = this.provider.getPrefix(); + final String prefix = this.provider.prefix; + String user = System.getProperty("user.name"); if (Provider.PREFIX_LOCAL.equals(prefix)) { this.connection = new MdsIpFile("mdsip", "-P", "tunnel"); } else if (Provider.PREFIX_SSH.equals(prefix)) { - this.connection = new MdsIpJsch(this.provider.user, this.provider.host, this.provider.getPort()); - } - else if (Provider.PREFIX_FILE.equals(prefix)) - { - this.connection = MdsIpFile.fromURI(this.provider.host); + this.connection = MdsIpJsch.fromString(this.provider.suffix); } else { - this.connection = new MdsIpTcp(this.provider.host, this.provider.getPort()); + String host; + final String parts[] = this.provider.suffix.split("@", 2); + if (parts.length > 1) + { + user = parts[0]; + host = parts[1]; + } + else + { + host = parts[0]; + } + if (Provider.PREFIX_FILE.equals(prefix)) + { + this.connection = MdsIpFile.fromString(host); + } + else + { + this.connection = MdsIpTcp.fromString(host); + } } if (DEBUG.D) System.out.println(this.connection.toString()); // connect to mdsip - final Message message = new Message(this.provider.getUser(), this.getMsgId()); + final Message message = new Message(user, this.getMsgId()); message.useCompression(this.use_compression); long tictoc = -System.nanoTime(); message.send(this.connection); @@ -814,7 +727,7 @@ private final Message getMessage(final Request req, final boolean serialize) this.dispatchTransferEvent("waiting for server", 0, 0); msg = this.getAnswer(); if (msg == null) - throw new MdsException("Could not get IO for " + this.provider.host, 0); + throw new MdsException("Could not get IO for " + this.provider, 0); } finally { @@ -866,7 +779,7 @@ public final String isReady() private void lostConnection() { this.disconnectFromServer(); - this.dispatchContextEvent(this.provider.host, false); + this.dispatchContextEvent(this.provider.toString(), false); } synchronized public final void mdsRemoveEvent(final UpdateEventListener l, final String event) @@ -882,7 +795,7 @@ synchronized public final void mdsRemoveEvent(final UpdateEventListener l, final } catch (final IOException e) { - System.err.print("Could not get IO for " + this.provider.host + ":\n" + e.getMessage()); + System.err.print("Could not get IO for " + this.provider + ":\n" + e.getMessage()); } } @@ -898,7 +811,7 @@ protected synchronized final void mdsSetEvent(final String event, final int even } catch (final IOException e) { - System.err.print("Could not get IO for " + this.provider.host + ":\n" + e.getMessage()); + System.err.print("Could not get IO for " + this.provider + ":\n" + e.getMessage()); } } @@ -973,9 +886,9 @@ private final void setup() throws MdsException @Override public final String toString() { - if (Provider.PREFIX_LOCAL.equals(this.provider.getPrefix())) + if (Provider.PREFIX_LOCAL.equals(this.provider.prefix)) { - return this.provider.getHost(); + return this.provider.suffix; } return this.provider.toString(); } diff --git a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpFile.java b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpFile.java index 0daad028db..2e2e472a50 100644 --- a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpFile.java +++ b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpFile.java @@ -9,9 +9,9 @@ public class MdsIpFile extends MdsIpIOStream { - static final MdsIpFile fromURI(final String uri) throws IOException + static final MdsIpFile fromString(final String string) throws IOException { - final String args[] = uri.split(" "); + final String args[] = string.split(" "); for (int i = 0; i < args.length; i++) { try diff --git a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpJsch.java b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpJsch.java index 6088867e5a..6432c0ce0d 100644 --- a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpJsch.java +++ b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpJsch.java @@ -14,6 +14,7 @@ import com.jcraft.jsch.*; import com.jcraft.jsch.ConfigRepository.Config; +import mds.mdsip.MdsIp.Connection; import mds.mdsip.MdsIp.MdsIpIOStream; public final class MdsIpJsch extends MdsIpIOStream @@ -320,6 +321,16 @@ public final void showMessage(final String message) userinfo = _userinfo; } + public static Connection fromString(final String string) throws IOException + { + final String usersplit[] = string.split("@", 2); + final String user = usersplit.length == 1 ? null : usersplit[1]; + final String rest = usersplit.length == 1 ? usersplit[0] : usersplit[1]; + final String portsplit[] = rest.split(":", 2); + final int port = portsplit.length == 1 ? 22 : Integer.parseInt(portsplit[1]); + return new MdsIpJsch(user, portsplit[0], port); + } + private static final ConfigRepository getConfigRepository() { final File config = new File(MdsIpJsch.dotssh, "config"); @@ -399,6 +410,11 @@ synchronized public void close() throws IOException } } + public String getUser() + { + return this.sessions.firstElement().getUserName(); + } + @Override public boolean isOpen() { diff --git a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpTcp.java b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpTcp.java index 882afa9ee9..c8662c917f 100644 --- a/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpTcp.java +++ b/java/mdsplus-api/src/main/java/mds/mdsip/MdsIpTcp.java @@ -10,6 +10,15 @@ public class MdsIpTcp extends Connection { + private static final int DEFAULT_PORT = 8000; + + static public Connection fromString(final String string) throws IOException + { + final String host_port[] = string.split(":", 2); + final int port = (host_port.length > 1) ? Integer.parseInt(host_port[1]) : MdsIpTcp.DEFAULT_PORT; + return new MdsIpTcp(host_port[0], port); + } + private final SelectionKey select_out; private final SelectionKey select_in; private final SocketChannel socket; diff --git a/java/mdsplus-api/src/test/java/mds/AllTests.java b/java/mdsplus-api/src/test/java/mds/AllTests.java index dc888bf87a..6b7bf1b12e 100644 --- a/java/mdsplus-api/src/test/java/mds/AllTests.java +++ b/java/mdsplus-api/src/test/java/mds/AllTests.java @@ -32,12 +32,10 @@ public static Mds setUpBeforeClass() throws Exception { final boolean local_win = System.getProperty("os.name").startsWith("Win"); final String host = System.getenv("MDSIP_SERVER"); - final String user = System.getProperty("user.name"); AllTests.local = AllTests.local || (!AllTests.mdsip) || host == null || host.length() == 0; final String treepath = (AllTests.local ? local_win : AllTests.remote_win) ? "C:\\Temp" : "/tmp"; - final String hostinfo = AllTests.local ? "local://" : host; - final boolean use_ssh = AllTests.local ? false : AllTests.ssh; - final int use_port = use_ssh ? 22 : AllTests.port; + final String prefix = AllTests.local ? "local://" : (AllTests.ssh ? "ssh://" : null); + final String command = AllTests.local ? "local" : (AllTests.ssh ? host : host + ":" + AllTests.port); final Mds mdslocal = Mds.getLocal(); if (!AllTests.use_local && mdslocal != null) mdslocal.close(); @@ -48,7 +46,7 @@ public static Mds setUpBeforeClass() throws Exception } else { - final MdsIp tmds = MdsIp.sharedConnection(new Provider(hostinfo, use_port, user, use_ssh)); + final MdsIp tmds = MdsIp.sharedConnection(new Provider(prefix + command)); if (tmds.isConnected()) mds = tmds; }