Example SAK Application
The following example application provides a simple example that shows the use of the Server Access Kit (SAK). The example is named ExecuteTask.java and is located in the prog_examples/SAK_apps directory where Info*Engine is installed.
The example application executes a task supplied when you run the application.
Compile the Application
To compile the application:
1. Set your CLASSPATH environment variable.
Your CLASSPATH environment variable must include the following files/folders:
%JAVA_HOME%/lib/tools.jar
%<Windchill>%/lib/servlet.jar
%<Windchill>%/codebase
%<Windchill>%/codebase/WEB-INF/classes
%<Windchill>%/codebase/WEB-INF/lib/ieWeb.jar
Example:
set
CLASSPATH=C:/jdk1.3.1/lib/tools.jar;C:/ptc/Windchill/lib/servlet
.jar;C:/ptc/Windchill/codebase;C:/ptc/Windchill/codebase/WEB-INF/
classes;C:/ptc/Windchill/codebase/WEB-INF/lib/ieWeb.jar
2. Create the following directory:
%<Windchill>%/codebase/WEB-INF/classes
3. Change to the directory:
%<Windchill>>%/prog_examples/SAK_apps
and execute the following:
javac -d %<Windchill>%/codebase/WEB-INF/classes ExecuteTask.java
Run the Application
To run the application, use the following syntax:
java com.infoengine.examples.applications.ExecuteTask
-t <task_uri>
-u <username>
-props <property resource>
-n <naming service name>
where:
<task_uri> is the URI of the task to be executed
<username> associates the username specified with the task
<property resource> is the directory path to the property resource file ie.properties
<naming service name> uses the specified Naming Service
For example:
java com.infoengine.examples.applications.ExecuteTask -t
C:/ptc/Windchill/tasks/infoengine/examples/CreateGroup.xml -u
wcadimn -props C:/ptc/Windchill/codebase/WEB-INF/ie.properties
-n com.ptc.host.namingService
Application Source
The following code implements the example:
package com.infoengine.examples.applications;

import com.infoengine.au.NamingService;
import com.infoengine.object.factory.Group;
import com.infoengine.SAK.Task;
import com.infoengine.util.IEException;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Vector;

public class ExecuteTask {

/**
* Demonstrate the com.infoengine.SAK interface of Infoengine.
*
* command line: java com.infoengine.examples.applications.ExecuteTask
* -t <task uri>
* -p <processor>
* -u <username>
* -n <naming service name>
* -props <property resource>
*
* @param args
*/
public static void main ( String [] args ) {

String taskUri = null;
String processor = null;
String username = null;
String namingService = null;
String props = null;

for ( int i = 0; i < args.length; i++ ) {
if ( args[i].equals ( "-t" ) ) {
if ( i < args.length-1 )
taskUri = args[++i];
}
else if ( args[i].equals ( "-p" ) ) {
if ( i < args.length-1 )
processor = args[++i];
}
else if ( args[i].equals ( "-u" ) ) {
if ( i < args.length-1 )
username = args[++i];
}
else if ( args[i].equals ( "-n" ) ) {
if ( i < args.length-1 )
namingService = args[++i];
}
else if ( args[i].equals ( "-props" ) ) {
if ( i < args.length-1 )
props = args[++i];
}
else {
System.err.println
("unrecognized parameter \"" + args[i] + "\"");
usage ();
}
}
if ( taskUri == null ) {
System.err.println ( "missing required parameter -t <task uri>" );
usage ();
}
if ( namingService == null )
namingService = "namingService";
if ( props == null )
System.err.println
("missing required parameter -props <property resource>");
usage ();
// Initialize the naming service and read the infoengine properties
try {
NamingService ns =
NamingService.newInstance (namingService, props);
}
catch ( FileNotFoundException f ) {
System.err.println (props + ": property file not found");
System.exit (1);
}
catch ( Exception e ) {
System.err.println (props + ": unable to read properties");
e.printStackTrace (System.err);
System.exit (1);
}
// Create a Task object to execute
Task task = null;
if ( processor == null ) {
task = new Task (taskUri);
}
else {
Vector procVector = new Vector ();
procVector.addElement (processor);
task = new Task (taskUri, procVector);
}
// execute the task
try {
task.invoke ();
}
catch ( Exception e ) {
e.printStackTrace (System.err);
System.exit (1);
}
// retrieve and print the results
Enumeration groups = task.getGroupNames ();
System.out.println ("groups produced by " + taskUri + ": " + groups);
PrintWriter pw = new PrintWriter (System.out);
while ( groups.hasMoreElements () ) {
Group group = task.getGroup ((String)groups.nextElement ());
group.toXML (pw, true, true);
}
System.exit ( 0 );
}
/**
* Print the command line parameters
*/
public static void usage () {
System.err.println
("java com.infoengine.examples.applications.ExecuteTask");
System.err.println ("\t-t <task uri>");
System.err.println ("\t-p <processor>");
System.err.println ("\t-u <username>");
System.err.println ("\t-n <naming service name>");
System.err.println ("\t-props <property resource>");
System.exit (1);
}
}
Was this helpful?