The example provided in this document is provided “as-is” to the user and assumes that the user:
PREREQUISITES:
For the sample to work, the Strategy Web JAR files must be accessible by the Java Runtime Environment. The Strategy Web JAR files can be found under:
Note: More elaborate programs will require additional library files and are out of the scope of this document.
SUMMARY
Using the Strategy Java Web SDK, it is possible to extend an existing task to include custom processing. In this example, the Out-of-the-box Login task was extended to dynamically set the Intelligence server name instead of hardcoding the value in the task API call:
CODE SNIPPET
package customclasses.tasks;
import com.Strategy.web.beans.MarkupOutput;
import com.Strategy.web.beans.RequestKeys;
import com.Strategy.web.objects.WebCluster;
import com.Strategy.web.objects.WebClusterMembership;
import com.Strategy.web.objects.WebClusterSource;
import com.Strategy.web.objects.WebClusters;
import com.Strategy.web.objects.WebClusterAdmin;
import com.Strategy.web.objects.WebIServerSession;
import com.Strategy.web.objects.WebObjectsException;
import com.Strategy.web.objects.WebObjectsFactory;
import com.Strategy.web.tasks.LoginTask;
import com.Strategy.web.tasks.TaskException;
import com.Strategy.web.tasks.TaskOutput;
import com.Strategy.web.tasks.TaskRequestContext;
import com.Strategy.web.tasks.TaskRequestMalformedException;
import java.lang.String;
import java.util.Enumeration;
public class customLogin extends LoginTask {
public customLogin() {
super("dynamically picks a server to create session");
}
protected void checkAuthParameters(String server, int port, String project, String login, String password, int authMode) {
try {
out("Server: " + server);
out("port: " + port);
out("project: " + project);
out("login: " + login);
out("password: " + password);
out("authMode: " + authMode);
super.checkAuthParameters(server, port, project, login, password, authMode);
} catch (TaskException e) {
e.printStackTrace();
}
}
public void processRequest(TaskRequestContext context, TaskOutput out) throws TaskException {
try {
out("entering processRequest");
com.Strategy.web.beans.RequestKeys requestkeys = context.getRequestKeys();
String s = serverTaskParam.getValue(requestkeys);
if (s.equalsIgnoreCase("cluster")) {
//read from the webclusteradmin
// Lookup the available I-Server name (clustered environment)
//I-server name
String serverName = "";
WebClusterAdmin admin = WebObjectsFactory.getInstance().getClusterAdmin();
admin.refreshAllClusters();
Enumeration < WebClusters > clusters = admin.getClusters().elements();
if (clusters.hasMoreElements()) {
WebCluster cluster = (WebCluster) clusters.nextElement();
out("cluster size: " + cluster.size());
for (int i = 0; i < cluster.size(); i++) {
out("Name of Node " + i + ": " + cluster.get(i).getNodeName());
}
serverName = cluster.get(0).getNodeName();
}
//serverName
context.getRequestKeys().setValue("server", serverName, 0);
out("testing webclustersource");
WebClusterSource wcs = WebObjectsFactory.getInstance().getClusterSource();
WebClusterMembership wcm = wcs.getConnectedClusterMembership();
out("WebClusterMembership size: " + wcm.size());
}
super.processRequest(context, out);
} catch (TaskException e) {
e.printStackTrace();
} catch (WebObjectsException e) {
e.printStackTrace();
super.processRequest(context, out);
}
}
public void out(String msg) {
System.out.println(msg);
}
}