Database Reference
In-Depth Information
Listing 12-60. Parse Selected Application
if (0 == appClassComboBox.getSelectedIndex())
return;
String appId = (String)appClassComboBox.getSelectedItem();
int place = appId.indexOf("/");
String appClass = appId.substring(place + 1);
appId = appId.substring(0, place);
With those two data elements, the application ID and the application inner class name, we can start
a new JVM as that specific application. We plan to start a new instance of the OJSAdmin , security
administration interface application with the identity of the application we selected here, instead of the
default identity of OJSAdmin . The code we use to start the new JVM and new instance of OJSAdmin is
shown in Listing 12-61.
Notice in the last line of Listing 12-61 that we are going to run the main() method of OJSAdmin , and
we are providing the application ID and application inner class name as arguments. In our discussion of
the Login constructors in Listing 12-4 and the OJSAdmin.ojsInit() method in Listing 12-11, I mentioned
how we can take on the identity of a different application. There we saw code that uses reflection to
create an instance of the application inner class that is named in argument two, and we set the
application ID of Login to the value of argument one.
Listing 12-61. Start a New JVM and new Instance of OJSAdmin
Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec ("C:/Java/jdk1.6.0_24/bin/ javaw -classpath " +
"C:/dev/mywork/OraJavSec/Project1/classes;C:/dev/ojdbc6.jar " +
"orajavsec.OJSAdmin " + appId + " " + appClass );
BufferedReader stdError =
new BufferedReader(new InputStreamReader( proc.getErrorStream ()));
String inLine;
while ((inLine = stdError.readLine()) != null) {
System.out.println(inLine);
}
We get a new instance of Runtime and call its exec() method. The exec() method returns an instance
of Process , and we set a local member variable. Typically, you might get the output and error streams
that are coming from the Process , and you might print them or handle them. In our case, we have
intentionally requested not to have the output stream generated by our Process . We did that by running
the version of the Java runtime that starts windowless , javaw.exe . Notice the “w” at the end of the
executable name. In this way, we don't get an output stream and we don't have to deal with it. We do,
however, choose to print the error stream to the current system out of the current JVM that called the
new Runtime.exec() . We want to see any exception messages that are thrown, and if we don't deal with
the error stream, our process may lock-up while waiting for us to deal with it.
If you can, I recommend that, in your corporate environment, you give full paths to your javaw
executable and to the current application, OJSAdmin and Oracle drivers jar file in the CLASSPATH setting. If
that doesn't work for all the computers that will be running this code in your environment, you will need
to set environment variables for the Runtime . You might also consider using the ProcessBuilder class to
get more fine-grained control of the environment settings. You might also consider placing these paths
in a properties file that will accompany the code. You can read these specific values from the properties
file; however, you will need a different properties file for each different client computer environment.
 
Search WWH ::




Custom Search