[Prev][Next][Index][Thread]

Re: Help on compiling Alpha5



I went through the same steps. I've included the three files that I
modified to conform to the JSDK 1.0 Beta version of the api.

ciao,
Christine Tomlinson

At 04:24 PM 3/18/97 +0800, Paul Pang wrote:
>
>Sorry for my early bothering, I found the SDK from Sun's jeeves
>
>> After I tried to compile the Jigsaw Alpha5, I'd received the 
>> following error message.
>
>Well, I still got the error message about the  Interface 
>w3c.jigsaw.servlet.ServletStub of class w3c.jigsaw.servlet.ServletWrapper 
>not found ..
>
>Just wanna to know is it possible to build a runnable Jigsaw Server 
>without using the w3c.jigsaw.servlet class ?
>
>
>Cheers,
>
>**************************************************************
>*  Name : Paul Pang (^ӧ)           . +      .    o       *
>*  http://www.cs.cuhk.hk/~cwpang           .      *       .  *
>*  The Chinese University of Hong Kong     .       .       . *
>**************************************************************  
>
>
// JigsawHttpServletRequest.java
// $Id: JigsawHttpServletRequest.java,v 1.4 1997/01/24 12:03:03 abaird Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.servlet;

import java.io.*;
import java.util.*;

import java.servlet.*;
import java.servlet.http.*;

import w3c.jigsaw.http.*;
import w3c.jigsaw.forms.*;
import w3c.www.http.*;
import w3c.jigsaw.auth.AuthFilter; // for auth infos access

/**
 *  @author Alexandre Rafalovitch <alex@access.com.au>
 *  @author Anselm Baird-Smith <abaird@w3.org>
 */

public class JigsawHttpServletRequest implements HttpServletRequest {
    /**
     * The initial request.
     */
    private Request request;
    /**
     * The lazyly computed queryParameters hashtable.
     */
    private Hashtable queryParameters = null;

    private void prepareQueryParameters()
    {
	if( queryParameters != null )
	    return;
	String query = request.getQueryString();
	if (query == null)
	    return ;
	InputStream qis = new StringBufferInputStream(query);
	try {
	    queryParameters = new URLDecoder(qis).parse();
	} catch (Exception ex) {
	    throw new RuntimeException("Java implementation bug.");
	}
    }

    public int getContentLength()
    {
	return request.getContentLength();
    }

    public String getContentType()
    {
	return request.getContentType().toString();
    }

    protected ServletInputStream is = null;
    public ServletInputStream getInputStream()
    {
	// If already computed return:
	if ( is != null )
	    return is;
	// Build it:
	InputStream stream = null;
        try {
	    if ((stream = request.getInputStream()) != null)
	        return is = new JigsawServletInputStream(stream);
        } catch (IOException ex) {
        }
	return null;
    }

    public String getParameter(String name)
    {
	prepareQueryParameters();
	if ( queryParameters != null )
	    return (String) queryParameters.get(name);
	else
	    return null;
    }

    public Hashtable getParameters()
    {
	prepareQueryParameters();
	return queryParameters;
    }

    public String getProtocol()
    {
	return request.getVersion();
    }

    public String getRemoteAddr()
    {
	return request.getClient().getInetAddress().toString();
    }

    public String getRemoteHost()
    {
	return request.getClient().getInetAddress().getHostName();
    }

    public String getServerName()
    {
	return request.getClient().getServer().getHost();
    }

    public int getServerPort()
    {
	return request.getClient().getServer().getLocalPort();
    }

    /**
     * HttpServletRequest interface implementation -
     */

    /**
     * Sun JSDK 1.0 Beta ServletRequest I/F
     * To Be Implemented - C.T.
     */
    public Enumeration getParameterNames() {
      return null;
    }

    public String getAuthType()
    {
	return (String) request.getState(AuthFilter.STATE_AUTHTYPE);
    }

    /**
     * Sun JSDK 1.0 Beta prototype - C.T.
     */
    public long getDateHeader(String name) {
      return getDateHeader(name, -1);
    }

    public long getDateHeader(String name, long i)
    {
	String value = getHeader(name);
	long longVal = i;

	if (value != null) {
	    try {
		return longVal = Date.parse(value);
	    } catch(Exception e) {
	    }
	}
	return longVal;
    }

    public String getHeader(int i) {
	return null;
    }

    public String getHeader(String name)
    {
	return request.getValue(name);
    }

    public String getHeaderName(int i)
    {
	return null;
    }

    /**
     * Sun JSDK 1.0 Beta HttpServletRequest I/F
     * To Be Implemented - C.T.
     */
    public Enumeration getHeaderNames() {
      return null;
    }

    /**
     * Sun JSDK 1.0 Beta prototype - C.T.
     */
    public int getIntHeader(String name) {
      return getIntHeader(name, -1);
    }

    public int getIntHeader(String name, int i)
    {
	String value = getHeader(name);
	int intVal = i;

	if (value != null) {
	    try {
		intVal = Integer.parseInt(value);
	    } catch(NumberFormatException eX) {
	    }
	}
	return intVal;
    }

    public  String getMethod()
    {
	return request.getMethod();
    }

    public  String getPathInfo()
    {
	return (String) request.getState(ServletWrapper.STATE_EXTRA_PATH);
    }

    /**
     * JSDK 1.0 Beta ServletRequest I/F - C.T.
     * To Be Implemented - null ==> conversion cannot be performed
     */
    public String getRealPath(String virtualPath) {
      return null;
    }

    public  String getPathTranslated()
    {
	return null;
    }

    public  String getQueryString()
    {
	// FIXME make sure this gets untranslated
	return request.getQueryString();
    }

    public String getRemoteUser()
    {
	return (String) request.getState(AuthFilter.STATE_AUTHUSER);
    }

    public  String getRequestURI()
    {
	return request.getURL().toExternalForm();
    }

    public  String getRequestPath()
    {
	return request.getURLPath();
    }

    public  String getServletPath()
    {
	return request.getTargetResource().getURLPath();
    }

    /**
     * Sun JSDK 1.0 Beta ServletRequest I/F - C.T.
     * To Be Implemented - null ==> attrName undefined
     */
    public Object getAttribute(String attrName) {
      return null;
    }

    JigsawHttpServletRequest(Request request)
    {
	this.request = request;
    }


}
// ServletDirectory.java
// $Id: ServletDirectory.java,v 1.4 1997/01/20 16:14:08 abaird Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.servlet;

import java.net.*;
import java.util.*;

import java.servlet.*;

import w3c.tools.store.*;
import w3c.jigsaw.resources.*;

/**
 *  @author Alexandre Rafalovitch <alex@access.com.au>
 *  @author Anselm Baird-Smith <abaird@w3.org>
 */

public class ServletDirectory extends DirectoryResource
    implements ServletContext
{
    /**
     * A useful utility routine that tries to guess the content-type
     * of an object based upon its extension.
     */
    protected static String guessContentTypeFromName(String fname) {
        String ext = "";
        int i = fname.lastIndexOf('#');

        if (i != -1)
            fname = fname.substring(0, i - 1);
        i = fname.lastIndexOf('.');
        i = Math.max(i, fname.lastIndexOf('/'));
        i = Math.max(i, fname.lastIndexOf('?'));

        if (i != -1 && fname.charAt(i) == '.') {
            ext = fname.substring(i).toLowerCase();
        }
        return (String) extension_map.get(ext);
    }

    static Hashtable extension_map = new Hashtable();

    static {
        setSuffix("", "content/unknown");
        setSuffix(".uu", "application/octet-stream");
        setSuffix(".saveme", "application/octet-stream");
        setSuffix(".dump", "application/octet-stream");
        setSuffix(".hqx", "application/octet-stream");
        setSuffix(".arc", "application/octet-stream");
        setSuffix(".o", "application/octet-stream");
        setSuffix(".a", "application/octet-stream");
        setSuffix(".bin", "application/octet-stream");
        setSuffix(".exe", "application/octet-stream");
        /* Temporary only. */
        setSuffix(".z", "application/octet-stream");
        setSuffix(".gz", "application/octet-stream");

        setSuffix(".oda", "application/oda");
        setSuffix(".pdf", "application/pdf");
        setSuffix(".eps", "application/postscript");
        setSuffix(".ai", "application/postscript");
        setSuffix(".ps", "application/postscript");
        setSuffix(".rtf", "application/rtf");
        setSuffix(".dvi", "application/x-dvi");
        setSuffix(".hdf", "application/x-hdf");
        setSuffix(".latex", "application/x-latex");
        setSuffix(".cdf", "application/x-netcdf");
        setSuffix(".nc", "application/x-netcdf");
        setSuffix(".tex", "application/x-tex");
        setSuffix(".texinfo", "application/x-texinfo");
        setSuffix(".texi", "application/x-texinfo");
        setSuffix(".t", "application/x-troff");
        setSuffix(".tr", "application/x-troff");
        setSuffix(".roff", "application/x-troff");
        setSuffix(".man", "application/x-troff-man");
        setSuffix(".me", "application/x-troff-me");
        setSuffix(".ms", "application/x-troff-ms");
        setSuffix(".src", "application/x-wais-source");
        setSuffix(".wsrc", "application/x-wais-source");
        setSuffix(".zip", "application/zip");
        setSuffix(".bcpio", "application/x-bcpio");
        setSuffix(".cpio", "application/x-cpio");
        setSuffix(".gtar", "application/x-gtar");
        setSuffix(".shar", "application/x-shar");
        setSuffix(".sh", "application/x-shar");
        setSuffix(".sv4cpio", "application/x-sv4cpio");
        setSuffix(".sv4crc", "application/x-sv4crc");
        setSuffix(".tar", "application/x-tar");
        setSuffix(".ustar", "application/x-ustar");
        setSuffix(".snd", "audio/basic");
        setSuffix(".au", "audio/basic");
        setSuffix(".aifc", "audio/x-aiff");
        setSuffix(".aif", "audio/x-aiff");
        setSuffix(".aiff", "audio/x-aiff");
        setSuffix(".wav", "audio/x-wav");
        setSuffix(".gif", "image/gif");
        setSuffix(".ief", "image/ief");
        setSuffix(".jfif", "image/jpeg");
        setSuffix(".jfif-tbnl", "image/jpeg");
        setSuffix(".jpe", "image/jpeg");
        setSuffix(".jpg", "image/jpeg");
        setSuffix(".jpeg", "image/jpeg");
        setSuffix(".tif", "image/tiff");
        setSuffix(".tiff", "image/tiff");
        setSuffix(".ras", "image/x-cmu-rast");
        setSuffix(".pnm", "image/x-portable-anymap");
        setSuffix(".pbm", "image/x-portable-bitmap");
        setSuffix(".pgm", "image/x-portable-graymap");
        setSuffix(".ppm", "image/x-portable-pixmap");
        setSuffix(".rgb", "image/x-rgb");
        setSuffix(".xbm", "image/x-xbitmap");
        setSuffix(".xpm", "image/x-xpixmap");
        setSuffix(".xwd", "image/x-xwindowdump");
        setSuffix(".htm", "text/html");
        setSuffix(".html", "text/html");
        setSuffix(".text", "text/plain");
        setSuffix(".c", "text/plain");
        setSuffix(".cc", "text/plain");
        setSuffix(".c++", "text/plain");
        setSuffix(".h", "text/plain");
        setSuffix(".pl", "text/plain");
        setSuffix(".txt", "text/plain");
        setSuffix(".java", "text/plain");
        setSuffix(".rtx", "application/rtf");
        setSuffix(".tsv", "text/tab-separated-values");
        setSuffix(".etx", "text/x-setext");
        setSuffix(".mpg", "video/mpeg");
        setSuffix(".mpe", "video/mpeg");
        setSuffix(".mpeg", "video/mpeg");
        setSuffix(".mov", "video/quicktime");
        setSuffix(".qt", "video/quicktime");
        setSuffix(".avi", "application/x-troff-msvideo");
        setSuffix(".movie", "video/x-sgi-movie");
        setSuffix(".mv", "video/x-sgi-movie");
        setSuffix(".mime", "message/rfc822");
    }

    static private void setSuffix(String ext, String ct) {
        extension_map.put(ext, ct);
    }

    /**
     * ServletContext implementation - Get a named attribute.
     * To Be Implemented - null ==> attrName undefined
     */

    public Object getAttribute(String attrName) {
        return null;
    }

    /**
     * ServletContext implementation - Get the MIME type for given file.
     */

    public String getMimeType(String filename) {
	return guessContentTypeFromName(filename);
    }

    /**
     * ServletContext implementation - Lookup a given servlet.
     */

    public Servlet getServlet(String name) {
	try {
	    Resource resource = lookup(name);
	    return ((resource instanceof ServletWrapper)
		    ? ((ServletWrapper) resource).getServlet()
		    : null);
	} catch (InvalidResourceException ex) {
	    return null;
	}
    }

    /**
     * ServletContext implementation - Enumerate all servlets within context.
     */

    public Enumeration getServlets() {
	return new ServletEnumeration(this, enumerateResourceIdentifiers());
    }

    /**
     * ServletContext implementation - Log a message.
     * Jeeves alpha version - C.T.
     */

    public void log(Servlet servlet, String msg) {
        log(getClass().getName() + "for " + servlet.getClass().getName() + ": " + msg);
    }

    /**
     * ServletContext implementation - Log a message.
     * Sun JSDK 1.0 Beta - C.T.
     */

    public void log(String msg) {
        System.out.println(msg);
    }

   /**
     * ServletContext implementation - Translate a piece of path.
     */

    public String getRealPath(String path) {
	return path;
    }

    /**
     * ServletContext implementation - Get server informations.
     */

    public String getServerInfo() {
	return getServer().getSoftware();
    }

    /**
     * We add a <em>context</em> attribute to all our children.
     * The <em>context</em> attribute is any object implementing the
     * ServletContext interface.
     */

    protected void updateDefaultChildAttributes(Hashtable attrs) {
	super.updateDefaultChildAttributes(attrs);
	attrs.put("servlet-context", this);
    }

}
// ServletWrapper.java
// $Id: ServletWrapper.java,v 1.5 1997/01/17 10:41:30 abaird Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.servlet;

import java.io.* ;
import java.util.*;

import java.servlet.*;

import w3c.tools.store.*;
import w3c.util.*;
import w3c.www.mime.*;
import w3c.www.http.*;
import w3c.jigsaw.http.*;
import w3c.jigsaw.resources.*;

/**
 * @author Alexandre Rafalovitch <alex@access.com.au>
 * @author Anselm Baird-Smith <abaird@w3.org>
 *
 * interface ServletStub changed to ServletConfig by Sun in JSDK 1.0 Beta.
 */

public class ServletWrapper extends FilteredResource implements ServletConfig
{
    private final static boolean debug = true;

    public static final
    String STATE_EXTRA_PATH = "w3c.jigsaw.servlet.extraPath";

    /**
     * Attributes index - The servlet class name.
     */
    protected static int ATTR_SERVLET_CLASS = -1 ;
    /**
     * Attribute index - The init parameters for that servlet.
     */
    protected static int ATTR_PARAMETERS = 1;
    /**
     * Attribute index - Our parent-inherited servlet context.
     */
    protected static int ATTR_CONTEXT = -1;

    static {
	Attribute a   = null ;
	Class     cls = null ;
	try {
	    cls = Class.forName("w3c.jigsaw.servlet.ServletWrapper") ;
	} catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(0);
	}
	// The servlet class attribute.
	a = new StringAttribute("servlet-class"
				, null
				, Attribute.EDITABLE | Attribute.MANDATORY) ;
	ATTR_SERVLET_CLASS = AttributeRegistry.registerAttribute(cls, a) ;
	// This servlet init parameters
	a = new PropertiesAttribute("servlet-parameters"
 				    , null
				    , Attribute.EDITABLE);
	ATTR_PARAMETERS = AttributeRegistry.registerAttribute(cls, a);
	// Our servlet context:
	a = new ObjectAttribute("servlet-context"
				, "w3c.jigsaw.servlet.ServletDirectory"
				, null
				, Attribute.DONTSAVE);
	ATTR_CONTEXT = AttributeRegistry.registerAttribute(cls, a);
    }

    /**
     * The servlet wrapped within that Jigsaw resource.
     */
    protected Servlet servlet = null;
    /**
     * Is out servler initialized ?
     */
    protected boolean inited = false;

    /**
     * Servlet config implementation - Get an init parameter value.
     */

    public synchronized String getInitParameter(String string) {
	ArrayDictionary d = getServletParameters();
	String          v = (d != null) ? (String) d.get(string) : null;
	if ( debug )
	    System.out.println("getInitParameter "+string+"="+v);
	return v;
    }

    /**
     * Servlet config implementation - Get all init parameters.
     */

    protected Hashtable hparams = null;
    public synchronized Hashtable getInitParameters() {
	// Convert init parameters to hashtable:
	ArrayDictionary d = getServletParameters();
	if ( d != null ) {
	    Enumeration keys = d.keys();
	    hparams = new Hashtable(d.size());
	    while (keys.hasMoreElements()) {
		Object key = keys.nextElement();
		hparams.put(key, d.get(key));
	    }
	}
	return hparams;
    }

    /**
     * Sun JSDK 1.0 Beta ServletConfig I/F - C.T.
     */

    public Enumeration getInitParameterNames() {
      ArrayDictionary d = getServletParameters();
      if ( d != null ) {
	return d.keys();
      } else
	return null;
    }

    /**
     * Servlet config implementation - Get the servlet context.
     */

    public ServletContext getServletContext() {
	return (ServletContext) getValue(ATTR_CONTEXT, null);
    }

    /**
     * Get the class name of the wrapped servlet.
     * @return The class name for the servlet if attribute is defined,
     * <strong>null</strong> otherwise.
     */

    public String getServletClass()
    {
	return getString(ATTR_SERVLET_CLASS, null);
    }

    /**
     * Get the init parameters for our wrapped servlet.
     * @return An ArrayDictionary instance if the attribute is defined,
     * <strong>false</strong> otherwise.
     */

    public ArrayDictionary getServletParameters() {
	return (ArrayDictionary) getValue(ATTR_PARAMETERS, null);
    }

    /**
     * Catch assignements to the servlet class name attribute.
     * <p>When a change to that attribute is detected, the servlet is
     * automatically reinitialized.
     */

    public void setValue(int idx, Object value) {
	super.setValue(idx, value);
	if ((idx == ATTR_SERVLET_CLASS) && (value != null))
	    inited = launchServlet();
    }

    /**
     * Destroy the servlet we are wrapping.
     */

    protected synchronized void destroyServlet() {
	if (servlet != null) {
	    servlet.destroy();
	    servlet = null;
	}
    }

    /**
     * Get the servlet we are wrapping.
     * @return A servlet instance, if the servlet is alredy running,
     * <strong>null</strong> otherwise.
     */

    public Servlet getServlet() {
	return servlet;
    }

    /**
     * Initialize our servlet from the given (loaded) class.
     * @param cls The servlet loaded main class.
     * @return A boolean, <strong>true</strong> if servlet was successfully
     * initialised, <strong>false</strong> otherwise.
     */

    protected boolean launchServlet(Class cls) {
	try {
	    servlet = (Servlet) cls.newInstance();
	    servlet.init((ServletConfig) this);
	} catch (Exception ex) {
	    String msg = ("unable to initialize servlet, exception: "
                          + ex.getMessage());
	    if ( debug )
		ex.printStackTrace();
	    getServer().errlog(this, msg);
	    return false;
	}
	return (servlet != null) ;
    }

    /**
     * Launch the servlet we are wrapping.
     * <p>This method either succeed, or the wrapper resource itself will fail
     * to initialize, acting as transparently as possible (in some sense).
     * @return A boolean, <strong>true</strong> if servlet launched.
     */

    protected boolean launchServlet() {
	// Get and check the servlet class:
	if ( servlet != null )
	    destroyServlet();
	String clsname = getServletClass();
	if ( clsname == null ) {
	    getServer().errlog(this, "no servlet class attribute defined.");
	    return false;
	} else {
	    // Load teh appropriate servlet class:
	    Class c = null;
	    try {
		c = Class.forName(clsname);
	    } catch (ClassFormatError er) {
		String msg = ("unable to load servlet class \""
			      + getServletClass()
			      + "\" (invalid class format).");
		if ( debug )
		    er.printStackTrace();
		getServer().errlog(msg);
	    } catch (ClassNotFoundException ex) {
		String msg = ("unable to load servlet class \""
			      + getServletClass()
			      + "\" (class not found).");
		if ( debug )
		    ex.printStackTrace();
		getServer().errlog(this, msg);
	    }
	    return (c != null) ? launchServlet(c) : false;
	}
    }

    /**
     * Dispatch the give request to our servlet.
     * <p>If the servlet cannot be inititalized, we just throw an error message
     * otherwise, we just delegate that request processing to the underlying
     * servlet instance.
     * @param request The request to be processed.
     * @exception HTTPException If the wrapped servlet is not initialized.
     */

    public Reply dispatch(Request request)
	throws HTTPException
    {
	// Check that the servlet has been initialized properly:
	if ( ! inited ) {
	    Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
	    reply.setContent("Servlet not configured properly");
	    throw new HTTPException(reply);
	}
	// Dispatch the request:
	Reply reply = createDefaultReply(request, HTTP.OK);
	try {
	    servlet.service(new JigsawHttpServletRequest(request)
			    , new JigsawHttpServletResponse(request, reply));
	} catch (Exception ex) {
	    if ( debug )
		ex.printStackTrace();
	    reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
	    reply.setContent("Servlet has thrown exception:" + ex.toString());
	}
	return reply;
    }

    /**
     * Jigsaw's lookup on servlets.
     * Once here, we have reached a leaf servlet (or at least the remaining
     * lookup is to be done at the servlet itself). We keep track of the
     * <em>path info</em> and mark that servlet as the target of request.
     * @param ls The lookup state.
     * @param lr The lookup result.
     * @exception HTTPException If some error occurs.
     */

    public boolean lookup(LookupState ls, LookupResult lr)
	throws HTTPException
    {
	// Get the extra path information:
	String extraPath = ls.getRemainingPath(true);
	if ((extraPath == null) || extraPath.equals(""))
	    extraPath = "/";
	// Keep this path info into the request, if possible:
	Request request = ls.getRequest();
	if ( request != null )
	    request.setState(STATE_EXTRA_PATH, extraPath);
	lr.setTarget(this);
	return super.lookup(ls, lr);
    }

    public void notifyUnload() {
	destroyServlet();
    }

    /**
     * Initialize this servlet wrapper resource.
     * After the wrapper itself is inited, it performs the servlet
     * initialzation.
     * @param values The default attribute values.
     */

    public void initialize(Object values[]) {
	super.initialize(values);
	// Initialize the wrapped servlet (is now the rigt time ?)
	if ( getServletClass() != null )
	    inited = launchServlet();
    }
}