/* 
  FILE: JLinkServletTest.java

23-Aug-05  K-03-31  JCN  $$1  Submitted.

*/
package com.ptc.jlink_servlet;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * This is the main servlet implentation for the J-Link servlet example.  
 * No J-Link code is actually included in this class; all J-Link code is
 * encapsulated in ServletDeploymentTest.java.  This class monitors the 
 * status of the J-Link operation and displays the results when completed.
 */
public class JLinkServletTest extends HttpServlet
{
    ServletDeploymentTest test = null;
    
    /**
     *  The servlet GET implementation.  Required parameters: 
     * proeCmd (the name or path to the Pro/E start command), 
     * proeLoadpoint (the name or path to the loadpoint).
     * The loadpoint is needed only so that the servlet can locate the test 
     * model from the J-Link install pfcinstalltest.
     */
    public void doGet (HttpServletRequest request, HttpServletResponse response)
    {
	try
	{
	    PrintWriter out = response.getWriter();
	    ServletContext application = getServletContext();
	    
	    out.print ("<html><head><title>J-Link servlet sample</title><link rel=\"stylesheet\" type=\"text/css\" href=\"../top.css\"></head><body>");
	    
	    /**
	     * No J-Link deployment is currently underway; create one.
	     */
	    if (test == null)
	    {
		out.print ("<H1>Beginning J-Link servlet deployment test...</H1>");
	    /**
	     *  We pass the servlet application path to the J-Link class 
	     *  so that it knows where to output the JPEG file.
	     *  By default, Pro/ENGINEEER's working directory will be the 
	     *  "working" directory of the servlet Java process;
	     *  typically c:\winnt\system32 on Windows.  Since Java does 
	     *  not support changing the "process working directory",
	     *  there is no way to control the Pro/ENGINEER startup 
	     *  directory.  
	     * 
	     *  To keep Pro/E trail files out of c:\winnt\system32, 
	     *  define HOME and place a config.pro in that directory with
	     *  the option trail_dir pointing to the desired location.
	     */
		test = 
		    new ServletDeploymentTest(request.getParameter ("proeCmd"),
					      request.getParameter ("proeLoadpoint"),
					       application.getRealPath ("/"));
	    }
	    
	    /**
	     * J-Link must be accessed by only one thread, 
	     * thus access to this object is synchronized.
	     */
	    synchronized (test)
	    {
		if (test.finished())
		{
		    if (test.getStatus ())
		    {
			out.print ("<H1>J-Link servlet test deployment succeeded.</H1>");
			out.print ("<HR>");

	     /**
	      * Write the main table: feature info on the left, 
	      *  JPEG on the right.
	      */
			out.print ("<TABLE><TR><TD>");
			out.print ("<TABLE cellpadding=\"2\" border=\"1\" style=\"border-collapse: collapse\" bordercolor=\"#111111\" cellspacing=\"0\">");
			out.print ("<THEAD><th><font color=\"#0000FF\">No.</font></th><th><font color=\"#0000FF\">Feature name</font>"+
				   "</th><th><font color=\"#0000FF\">Feature id</font></th><th><font color=\"#0000FF\">Feature type</font></th></THEAD><TBODY>");
			
	      /**
	       * Write the feature info table rows.  The row strings were 
	       * already formatted correctly during the test.
	       */
			Iterator i = test.getFeatureTableRows();
			
			while (i.hasNext())
		        {
			    out.print (i.next());
			}
			
			out.print ("</TBODY></TABLE>");
			out.print ("</TD><TD>");

			/**
			 * Write the location of the image file.
			 */
			StringBuffer url = new StringBuffer ("..");
			url.append ("/" + test.getJPEGFileName());
			out.print ("<IMG SRC=\"" + url + "\"></IMG>");
			out.print ("</TD></TR></TABLE>");
			test = null;
		    }
		    else
		    {
			out.print ("<H1>Deployment failed.</H1>");
			out.print ("<h2>Troubleshooting:</h2>");
			out.print ("Remember to check the following:");
			out.print ("<OL>");
			out.print ("<LI> Place the J-Link asynchronous jar file in the servlet engine shared application path so it can loaded by the shared class loader (see <a href=\"http://www.ptc.com/cs/tpi/129506.htm\">TPI 129506</a>)");
			out.print ("<LI> Place the example class com.ptc.jlink_loader.JLinkLoader in the servlet engine shared application path so it can loaded by the shared class loader (see <a href=\"http://www.ptc.com/cs/tpi/129506.htm\">TPI 129506</a>)");
			out.print ("<LI> Add the System environment variable PRO_COMM_MSG_EXE to be [Pro/E loadpoint]/[MACHINE TYPE]/obj/pro_comm_msg[.exe]");
			out.print ("<LI> Add the directory [Pro/E loadpoint]/[MACHINE TYPE]/lib to the system library path variable (path on Windows).");
			out.print ("<LI> When running on Windows, please remember that changes to the System variables don't take effect until after Windows is restarted.");
			out.print ("<LI> Be sure that the Pro/ENGINEER command provided can correctly start Pro/ENGINEER.");
			out.print ("<LI> Be sure that the Pro/ENGINEER loadpoint provided is correct.");
			out.print ("</OL>");
			out.print ("<hr>");
			out.print ("The following is the system library path:"); 	
			out.print ("<hr>");
			out.println (System.getProperty ("java.library.path"));
		   /**
		    * Note: java.class.path cannot be reported from here; 
		    * it reports only the default class loader classpath.  
		    * Additional classes are
		    * picked up automatically from the shared directory 
		    * and web application directory by the servlet engine.
		    */
			test = null;
		    }
		}
		else
		{
		    /**
		     * Test is not finished.  Set a Refresh header to try 
		     * again in 10 seconds.
		     */
		    out.print ("<H1>Test is in process; please wait...</H1>");
		    response.setIntHeader ("Refresh", 10);
		}
	    }
	    out.print ("</body></html>");
	}
	catch (Exception e)
	    {
		e.printStackTrace();
	    }
    }
    
    /**
     * Servlet POST implementation, same as GET for this servlet.
     */
    public void doPost (HttpServletRequest request, HttpServletResponse response)
    {
	doGet (request, response);
    }
}
