You can design your own web pages by connecting to the database from web server (RMI client)
side. To develop your own user interface, the following 3 steps must be included.
Step 1: Make client talk to server.
Once the client is connected to the server, it will be fairly easy to get CFEngine object from the remote interface. The code below is implemented in the login stage (In our sample application).
private void initCFEngine() {
System.setSecurityManager(new RMISecurityManager());
String url = "rmi://" + System.getProperty("java.rmi.server.hostname",".")+"/";
// Lookup the CF Engine server
try {
engine = (CFEngine)Naming.lookup(url + "cfengine");
System.out.println("engine is found");
} catch (Exception e) {
System.out.println("Error: Cannot locate server on " +
System.getProperty("java.rmi.server.hostname","."));
System.out.println("Error: " + e);
System.exit(0);
}
}
And pass three arguments to your webserver like follows:
-Dcf.path=[CFEngine Install path]
-Djava.security.policy=[security policy path/name]
-Djava.rmi.server.hostname=[RMI server location]
Step 2: Start a user session.
After getting the CFEngine object, we can start a user session now. Everything you want to store temporary or keep the context between several pages, session can do it. Sample code as follows:
HttpSession session = request.getSession(true);
session.setAttribute("userId", ""+userId);
session.setAttribute("engine", engine);
Or:
HttpSession session = request.getSession(true);
String func = request.getParameter("function");
CFEngine engine = (CFEngine) session.getAttribute("engine");
String user = (String) session.getAttribute("userId");
Step 3: Perform functions of CFEngine.
Every method defined in the Remote Interface CFEngine can be invoked in the client. Example, get specific item's prediction:
private float doPrediction(int userId, int itemId) {
float rate = -1;
if (itemId > 0) {
synchronized(engine) {
try{
rate = engine.getPredictedRating(userId, itemId);
} catch (java.sql.SQLException e) {
System.out.println("not valid prediction" + e);
} catch (java.rmi.RemoteException e) {
System.out.println("not valid prediction" + e);
}
}
}
return rate;
} // doPrediction()
|