Use Sample Servlet and Make Your Own

Use Sample Servlet

Step 1:
After setting up and configure your web server, compile all the source code in /src and output all the servlet class according to your web server. As Tomcat 4.0, the default class output path is Tomcat 4.0/webapp/ROOT/WEB-INF/classes.

Step 2:
Put the jsp page folder /JSPCFEngine with your web server. As Tomcat 4.0, copy the folder into Tomcat 4.0/webapp/ROOT.

Step 3:
Define and in your CFEngine Server, and start it. Once your CFEngine Server is started, pass three arguments to your webserver and run the webserver.

    -Dcf.path=[CFEngine Install path]
    -Djava.security.policy=[policy path/name]
    -Djava.rmi.server.hostname=[RMI server location]
Step 4:
Once your web server is running, open your web browser and go to http://[web server running hostname]/JSPCFEngine/Login.jsp

Write Java Security File

Since RMI does not use a naming service, a security policy file must be provided. How to give permissions and form a security policy file must refer to your web server's documentation and carefully considered.


Develop Your Own Servlet

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()