D:\DEV\PROJECTS\Harbor_Tests_Servlets\src\java\harbor\test\servlets\ContainerClassLoader.java
/*
 * ContainerClassLoader.java
 */

package harbor.test.servlets;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import kewlstuff.harbor.servlet.ExClassLoader; //Import the special extended classloader
import harbor.interfaces.I_MsgTest; //The interfaces for the class we going to run
import kewlstuff.harbor.client.Vessel;

/**
 *
 * @author Johnny Kewl
 * @version 1.0
 * This shows you how to get at container classes from any WEB-APP
 * The classes in the container are not in the ClassPath... nor should they be.
 * But sometimes after developing wonderful utilities for remote clients to use,
 * you may also want to use a few of those classes.
 * So what we do is use the very same ClassLoader Extender that Harbor uses.
 * What you see here is generic, you could do the same thing from any java application.
 * 
 * Please dont get confused... yes we could just use harbor as a remote client
 * and there is very little overhead in the protocol, but when your classes are
 * sitting right there on the same machine... why not load them as java does.
 */
public class ContainerClassLoader extends HttpServlet {
    private ClassLoader reposClassLoader = null;
    private String sRepository = null;
    
    // This function runs once when started so its a nice place
    // to extend the class loading ability
    public void init() throws ServletException{ 
          //Not using vessel for remote calls, just to find the Harbor repository so
          //we got some classes to play with, naturally this must be on the same
          //machine
          String harborUrl = "http://localhost:8080/harbor/service"; //location of container harbor
          Vessel vessel = new Vessel(harborUrl); //get a vessel from the harbor 
          
          String webBase = vessel.getProperty("harbor.webBase.location");
          sRepository = webBase + "HarborRepository";
        
          //If you familiar with the urlclassloader, this is similar
          //but this provides access to ALL jars and classes in a local folder
          //In other words even if you have a Jar 5 subfolders deep under the specified
          //folder it will put them in the class path.
          //It just means with this one call, every class in the repository is yours
          //to use... and the TRUE is important, it means even if Harbor is running, and
          //someone changes the classes, your application will work normally.
          reposClassLoader  = new ExClassLoader(sRepository,true);
    }
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        //Use the standard class loading technique to get the class
        String sMsg = "Class Did NOT LOAD";
        try{
            I_MsgTest i_MsgTest = (I_MsgTest)Class.forName("harbor.classContainer.MsgTest",true,reposClassLoader).newInstance();
            sMsg = i_MsgTest.getTheMsg();
        }catch(Exception e){sMsg = "Got a class loading error: " + e.getMessage();}  
        
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ContainerClassLoader</title>");
        out.println("</head>");
        out.println("<body>");
        
        out.println("<h1>Class Repository : " + sRepository + "</h1>");
        out.println("<h1>The Message : " + sMsg + "</h1>");
        
        out.println("</body>");
        out.println("</html>");
       
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}