package cylikon.YahooEnt; import javax.servlet.*; import javax.servlet.http.*; //import javax.ejb.FinderException; //import javax.ejb.CreateException; import javax.naming.*; import java.rmi.RemoteException; import java.util.Properties; import java.sql.*; import sun.jdbc.rowset.CachedRowSet; import java.io.*; public class YahooEntServlet extends HttpServlet { public static String catName[]= {"NYC","HipHop","Lesbian Gay","Technical Playground","My Resume","Site Search","Maintenance"}; public static String catNo[]={"2","3","5","52","51","8","75"}; Thread thr=null; //let's create the Entity Beans during servlet initialization... // the CachedRowSets created during ejbCreate will be bound to a JNDI namespace public void init(ServletConfig config) throws ServletException { super.init(config); int i=0; try { Context ctx=getInitialContext(); CategoryHome home = (CategoryHome) ctx.lookup("YahooEnt"); while (i< catName.length) { Category cat= home.create(catNo[i],catName[i]); System.out.println("EJB "+catName[i]+" was created."); int ready=cat.crsReady(); String crserror = cat.crsError(); if (ready==1) { System.out.println("CachedRowSet for "+catName[i]+" is ready."); CachedRowSet crs=cat.crs(); //bind the CachedRowSet to the JNDI namespace ctx.rebind(catName[i],crs); } //end if else { //report the error System.out.println("An ERROR occurred with the CachedRowSet for "+catName[i]+"."); System.out.println("The error from crsError was: "+crserror); System.out.println("CachedRowSet could not be bound to the JNDI namespace."); } //end else i++; //increment i } //end while } catch (Throwable t) { System.out.println("ERROR initializing servlet: "+t+" "+t.getMessage()); } //end try-catch // Now, start the CRUpdater thread (thr).... try { thr=new Thread(new CRUpdater()); thr.start(); } catch (Throwable t) { System.out.println("ERROR with the new Thread thr: "+t.getMessage()); } } // end init method // ********* Servlet doGet method ********************** public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { PrintWriter out =res.getWriter(); String category=req.getParameter("cat"); // if category is null, we will be listing all available CachedRowSets bound to JNDI, // otherwise, we will only list the requested category. if (category==null) { // we are list ALL available CachedRowSets.. Context ctx=getInitialContext(); int i=0; while(iCategory "+catNo[i]+": "+catName[i]+" ("+cr.size()+")
"); out.flush(); while (cr.next()) { // let's just print out links from the CachedRowSet, fields 3&4 out.print(""+cr.getString(3).trim()+"
"); } //end while } catch (Throwable t) { out.println("CachedRowSet for "+catName[i]+" not found in the JNDI namespace.
The error was: "+t.getMessage()); } //end catch out.print("

"); out.flush(); i++; } //end while } //end if else { out.print("Feature not ready yet!"); } //noting yet } //end doGet // ********* Servlet Destroy method ********************** public void destroy() { // we need to remove all EJBs created and unbind the cached rowsets from the JNDI namespaces Context ctx=null; CategoryHome home=null; int i=0; // first, let's stop the CRUpdater thread (thr)..... try { System.out.println("Attempting to stop CRUpdater thread..."); if (thr !=null) { if (thr.isAlive()) { thr.interrupt(); thr.join(); } //end if } //end if } catch (Throwable t) { System.out.println("ERROR stopping thread thr: "+t.getMessage()); } try { if (!thr.isAlive()) { System.out.println("CRUpdater thread stopped."); } else { System.out.println("CRUpdater thread is still running!"); } } catch (Throwable t) { System.out.println("Unknown ERROR: "+t.getMessage()); } try { ctx=getInitialContext(); home = (CategoryHome) ctx.lookup("YahooEnt"); } catch (Throwable t) { System.out.println("\nERROR in servlet destroy method: "+t+" "+t.getMessage()); } //end try-catch try { while (i."); }//end try-catch return c; } //end getInitialContext } //end class ////////// Runnable class for updating the cached rowsets and jndi namespaces ///////// class CRUpdater implements Runnable { String names[] = YahooEntServlet.catName; String numbers[] = YahooEntServlet.catNo; Context cx=null; int i; Category cat=null; CategoryHome home=null; CachedRowSet cr=null; public CRUpdater() { } //super class public void run() { while (true) { i=0; try{ Thread.sleep(240000); //take breaks between cached row refreshment-4 minutes here } catch (InterruptedException ie) { System.out.println("Thread interrupted: "+ie.getMessage()); break; } System.out.println("Updating and rebinding CachedRowSets for YahooEnt..."); try { cx=YahooEntServlet.getInitialContext(); home=(CategoryHome) cx.lookup("YahooEnt"); } catch (Throwable t) { System.out.println("ERROR looking up YahooEnt: "+t.getMessage()); break; } while (i