Solved

dynamic class loading from web server

Posted on 2004-10-22
269 Views
Last Modified: 2013-11-24
My program uses some classes which are not beloned its virtual machine,
in order to make it distributed, it think it is necessary broadcast some classes via web server.
URLClassLoader is useful for this purpose. but i don't know how can i deploy jar file of this classes in to tomcat server. is it is necessary writing a servlet or not. how can i?
0
Question by:ozan_aksit
    4 Comments
     
    LVL 35

    Accepted Solution

    by:
    You can just use the standard applet classloader (so you shouldn't need to sign your applet), and write a servlet that does this:

    --------------------

    import java.io.BufferedInputStream ;
    import java.io.BufferedOutputStream ;
    import java.io.IOException ;
    import javax.servlet.ServletException ;
    import javax.servlet.http.HttpServlet ;
    import javax.servlet.http.HttpServletRequest ;
    import javax.servlet.http.HttpServletResponse ;

    public class ClassLoaderServlet extends HttpServlet
    {
      //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
        String uri = request.getRequestURI().substring( request.getContextPath().length() ) ; ;
        String classname = uri.substring( 1 ) ;
        classname = classname.replace( '/', '.' ) ;
        classname = classname.substring( 0, classname.length() - 6 ) ;

        BufferedOutputStream bos = null ;
        BufferedInputStream bis = null ;
        try
        {
          Class c = Class.forName( classname ) ;
          if( c != null )
          {
            bos = new BufferedOutputStream( response.getOutputStream() ) ;
            bis = new BufferedInputStream( this.getClass().getResourceAsStream( uri ) ) ;
            byte[] buffer = new byte[ 64000 ] ;
            int cnt = 0 ;
            int tot = 0 ;
            while( ( cnt = bis.read( buffer, 0, 64000 ) ) != -1 )
            {
              bos.write( buffer, 0, cnt ) ;
              tot += cnt ;
            }
            System.out.println( "Loaded " + uri + " (" + tot + "bytes)" );
          }
          else
          {
            response.sendError( HttpServletResponse.SC_NOT_FOUND );
            System.err.println( "Trying to load " + classname + " which doesn't implement ServletFetchable" ) ;
          }
        }
        catch( Exception ex )
        {
          ex.printStackTrace() ;
          response.sendError( HttpServletResponse.SC_NOT_FOUND );
        }
        finally
        {
          try { if( bis != null ) bis.close() ; } catch( IOException ex ) { }
          try { if( bos != null ) bos.close() ; } catch( IOException ex ) { }
        }
      }
    }

    Then, assign this servlet (in web.xml) to match to all urls that match *.class

    Hope this helps :-)

    Tim
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    B grade?

    Bah!  What was wrong with it?
    0
     

    Author Comment

    by:ozan_aksit
    thank you i will try to send jar file by this way.
    because my classes import other clasess, and also sending them in one shot as jar file boost performance
    0
     
    LVL 35

    Expert Comment

    by:TimYates
    Good luck with it :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now