17736 App.java should not be trafficking in URIs
authorStephen Talley <stephen.talley@oracle.com>
Tue, 18 Jan 2011 16:43:47 -0500
changeset 637 041f2d1097a2
parent 636 c944b40f7c74
child 638 91cbfdc9db0d
17736 App.java should not be trafficking in URIs
usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/App.java
usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/resources/Resources.properties
--- a/usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/App.java	Fri Jan 14 17:50:37 2011 -0500
+++ b/usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/App.java	Tue Jan 18 16:43:47 2011 -0500
@@ -179,6 +179,55 @@
 	}
     }
 
+    private static class InstanceInfo implements Serializable {
+	//
+	// Instance data
+	//
+
+	public String address;
+	public Properties properties;
+	public String host;
+	public String user;
+	public String role;
+
+	//
+	// Constructors
+	//
+
+        public InstanceInfo(String address, Properties properties, String host,
+            String user, String role) {
+
+	    this.address = address;
+	    this.properties = properties;
+	    this.host = host;
+	    this.user = user;
+	    this.role = role;
+	}
+
+	//
+	// Object methods
+	//
+
+	@Override
+	public String toString() {
+	    return "address = " + quote(address) +
+		", host = " + quote(host) +
+		", user = " + quote(user) +
+		", role = " + quote(role);
+	}
+
+	//
+	// Private methods
+	//
+
+	private static String quote(String str) {
+	    if (str == null) {
+		return "(null)";
+	    }
+	    return "\"" + str + "\"";
+	}
+    }
+
     //
     // Static data
     //
@@ -188,10 +237,10 @@
     private static final String ARG_LONG_HOST = "host";
     private static final String ARG_SHORT_NEWJVM = "j";
     private static final String ARG_LONG_NEWJVM = "newjvm";
+    private static final String ARG_SHORT_ROLE = "r";
+    private static final String ARG_LONG_ROLE = "role";
     private static final String ARG_SHORT_USER = "u";
     private static final String ARG_LONG_USER = "user";
-    private static final String ARG_SHORT_ROLE = "r";
-    private static final String ARG_LONG_ROLE = "role";
     private static final String ARG_SHORT_VERSION = "v";
     private static final String ARG_LONG_VERSION = "version";
     private static final String ARG_SHORT_HELP = "?";
@@ -232,16 +281,13 @@
 	    ARG_LONG_HELP, false, Finder.getString("cli.arg.help"));
 
 	OptionListGroup mainGroup = new OptionListGroup(false,
-	    propOption, hostOption, userOption, roleOption, newJVMOption,
-	    addressOption);
+            propOption, hostOption, userOption, roleOption, newJVMOption,
+            addressOption);
 
 	options = new OptionChoiceGroup(true, mainGroup, versionOption,
 	    helpOption);
     }
 
-    public static final String URI_SCHEME = "vp";
-    public static final String URI_USER_ROLE_SEPARATOR = ":";
-
     public static final String VP_USER_DIR =
 	System.getProperty("user.home") + File.separator + ".vp";
 
@@ -455,37 +501,28 @@
 	}
     }
 
-    public void newInstance(Properties properties, URI uri) {
-	String hostVal = uri.getHost();
-	String roleVal = null;
-	String userVal = uri.getUserInfo();
-	int i = userVal.indexOf(URI_USER_ROLE_SEPARATOR);
-	if (i != -1) {
-	    roleVal = userVal.substring(i + 1);
-	    userVal = userVal.substring(0, i);
-	}
-
+    public void newInstance(InstanceInfo info) {
 	AppInstance instance = null;
 	boolean success = false;
 
 	try {
 	    LoginProperty<String> host =
-		new LoginProperty<String>(hostVal, false);
+		new LoginProperty<String>(info.host, false);
 	    host.setEditableOnError(true);
 
 	    LoginProperty<String> user =
-		new LoginProperty<String>(userVal, false);
+		new LoginProperty<String>(info.user, false);
 	    user.setEditableOnError(true);
 
 	    LoginProperty<String> role =
-		new LoginProperty<String>(roleVal, false);
-//	    role.setEditableOnError(true);
+		new LoginProperty<String>(info.role, false);
+	    role.setEditableOnError(true);
 
-	    LoginRequest request = new LoginRequest(host, user, role);
+            LoginRequest request = new LoginRequest(host, user, role);
 
-	    instance = new AppInstance(this, properties, request);
+	    instance = new AppInstance(this, info.properties, request);
 
-	    SimpleNavigable[] path = Navigator.toArray(uri.getPath());
+	    SimpleNavigable[] path = Navigator.toArray(info.address);
 	    instance.getNavigator().goToAsyncAndWait(true, null, path);
 	    success = true;
 
@@ -501,12 +538,12 @@
 	// Unexpected error - write to log
 	} catch (RuntimeException e) {
 	    Logger log = Logger.getLogger(getClass().getName());
-	    log.log(Level.SEVERE, "could not launch: " + uri, e);
+	    log.log(Level.SEVERE, "could not launch: " + info, e);
 
 	// Unexpected error - write to log
 	} catch (Error e) {
 	    Logger log = Logger.getLogger(getClass().getName());
-	    log.log(Level.SEVERE, "could not launch: " + uri, e);
+	    log.log(Level.SEVERE, "could not launch: " + info, e);
 
 	} finally {
 	    if (!success && instance != null &&
@@ -519,44 +556,22 @@
     }
 
     public void readFully(UDSocket socket) {
-	BufferedReader reader = null;
+	ObjectInputStream in = null;
         Logger log = Logger.getLogger(getClass().getName());
 
 	try {
-	    reader = new BufferedReader(
-		new InputStreamReader(socket.getInputStream()));
-
-	    String line;
-	    while ((line = reader.readLine()) != null) {
-
-		// Format of each line is "<uri>[ <properties>]"
-		String[] parts = line.split("\\s+");
-		URI uri = new URI(parts[0]);
-
-		Properties properties = null;
-		if (parts.length > 1) {
-		    String decodedProps = URLDecoder.decode(parts[1],
-			Control.ENCODING);
-
-		    StringReader propReader = new StringReader(decodedProps);
-		    properties = new Properties();
-		    properties.load(propReader);
-		}
-
-		newInstance(properties, uri);
-	    }
-
-	} catch (IllegalArgumentException e) {
-	    log.log(Level.SEVERE, "malformed Unicode escape sequence", e);
+	    in = new ObjectInputStream(socket.getInputStream());
+	    InstanceInfo info = (InstanceInfo)in.readObject();
+	    newInstance(info);
+	} catch (ClassNotFoundException e) {
+            log.log(Level.SEVERE, "unexpected or invalid object read from UDS",
+		e);
 
 	} catch (IOException e) {
 	    log.log(Level.SEVERE, "I/O error", e);
 
-	} catch (URISyntaxException e) {
-	    log.log(Level.WARNING, "remote request with invalid URI", e);
-
 	} finally {
-	    IOUtil.closeIgnore(reader);
+	    IOUtil.closeIgnore(in);
 	}
     }
 
@@ -576,16 +591,6 @@
 	System.out.println(Finder.getString("cli.error.version", VERSION));
     }
 
-    public static URI createURI(String host, String user, String role,
-	String address) throws URISyntaxException {
-
-	if (role != null) {
-	    user += URI_USER_ROLE_SEPARATOR + role;
-	}
-
-	return new URI(URI_SCHEME, user, host, -1, address, null, null);
-    }
-
     public static void main(String args[]) {
 	addVersionToThreadName(Thread.currentThread());
 
@@ -608,43 +613,20 @@
 	    CommandUtil.exit(e, usage);
 	}
 
-	String address = bean.getAddress();
-	URI uri = null;
-	try {
-	    uri = createURI(bean.getHost(), bean.getUser(), bean.getRole(),
-		address);
-	} catch (URISyntaxException e) {
-	    System.err.println(Finder.getString("cli.error.uri", e.getInput()));
-	    System.exit(1);
-	}
+        InstanceInfo info = new InstanceInfo(bean.getAddress(),
+            bean.getProperties(), bean.getHost(), bean.getUser(),
+            bean.getRole());
 
-	Properties properties = bean.getProperties();
 	if (!bean.getNewjvm()) {
 	    // Check for running instance
 	    try {
 		UDSocket socket = UnixDomainSocket.connect(VP_UDS);
 
 		// Send arguments to running instance...
-		PrintWriter out = new PrintWriter(socket.getOutputStream());
-
-		// Format of each line is "<uri>[ <properties>]"
-		out.print(uri);
+		ObjectOutputStream out = new ObjectOutputStream(
+		    socket.getOutputStream());
 
-		try {
-		    if (properties != null) {
-			StringWriter propWriter = new StringWriter();
-			properties.store(propWriter, null);
-			String props = propWriter.toString();
-			String encodedProps = URLEncoder.encode(props,
-			    Control.ENCODING);
-			out.print(' ');
-			out.print(encodedProps);
-		    }
-		} catch (IOException e) {
-		    // Unlikely
-		}
-
-		out.println();
+		out.writeObject(info);
 		out.close();
 		System.exit(0);
 	    } catch (UDSNotSupportedException e) {
@@ -677,7 +659,7 @@
 	System.setSecurityManager(new SecurityManager());
 
 	App app = new App();
-	app.newInstance(properties, uri);
+	app.newInstance(info);
 	app.exitIfNoIntances(1);
 
 	if (!bean.getNewjvm()) {
--- a/usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/resources/Resources.properties	Fri Jan 14 17:50:37 2011 -0500
+++ b/usr/src/java/vpanels/client/org/opensolaris/os/vp/client/swing/resources/Resources.properties	Tue Jan 18 16:43:47 2011 -0500
@@ -20,7 +20,7 @@
 #
 
 #
-# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
 #
 
 init.error.security.io = unable to read panel security policy, refusing to continue
@@ -54,16 +54,15 @@
 error.details.hide = Hide details
 
 cli.description = Launch Solaris system management panels.
-cli.arg.address = Specify the URI identifying the management panel to display.
-cli.arg.host = Specify an optional host to log into.  The default is the local machine.
-cli.arg.user = Specify an optional user to log in as.  The default is the current user.
-cli.arg.role = Specify an optional role to use when logging in.
+cli.arg.address = Specify the address identifying the management panel to display.
+cli.arg.host = Specify a host to log into.  The default is the local machine.
+cli.arg.user = Specify a user to log in as.  The default is the current user.
+cli.arg.role = Specify a role to assume when logging in.
 cli.arg.newjvm = Start a new JVM for this invocation instead of using an existing JVM.
 cli.arg.help = Show this message and exit.
 cli.arg.version = Show the version and exit.
 
 cli.error.version = vp {0}
-cli.error.uri = specified arguments resulted in an invalid URI: {0}
 
 errorpanel.tabs.name = Errors
 errorpanel.tab.name = Custom Panel "{0}:{1}"