001: // RemoteServletWrapper.java
002: // $Id: RemoteServletWrapper.java,v 1.13 2001/11/12 14:02:05 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.servlet;
007:
008: import javax.servlet.ServletException;
009:
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.AttributeHolder;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.Resource;
014: import org.w3c.tools.resources.ServerInterface;
015: import org.w3c.tools.resources.StringAttribute;
016:
017: /**
018: * @author Alexandre Rafalovitch <alex@access.com.au>
019: * @author Anselm Baird-Smith <abaird@w3.org>
020: */
021:
022: public class RemoteServletWrapper extends ServletWrapper {
023: private static final boolean debug = false;
024:
025: /**
026: * Attribute index - The servlet content base.
027: */
028: protected static int ATTR_SERVLET_BASE = -1;
029:
030: static {
031: Class c = null;
032: Attribute a = null;
033: try {
034: c = Class
035: .forName("org.w3c.jigsaw.servlet.RemoteServletWrapper");
036: } catch (Exception ex) {
037: ex.printStackTrace();
038: System.exit(1);
039: }
040: // Register the servlet base URL:
041: a = new StringAttribute("servlet-base", null,
042: Attribute.EDITABLE | Attribute.MANDATORY);
043: ATTR_SERVLET_BASE = AttributeRegistry.registerAttribute(c, a);
044: }
045:
046: /**
047: * The ServletLoader instance for loading that servlet.
048: */
049: protected ServletLoader loader = null;
050:
051: /**
052: * Check the servlet class, ans try to initialize it.
053: * @exception ClassNotFoundException if servlet class can't be found.
054: * @exception ServletException if servlet can't be initialized.
055: */
056: protected void checkServlet() throws ClassNotFoundException,
057: ServletException {
058: synchronized (servletPool) {
059: // synchronization for pool access added, tk, 21.10.2001
060: if (!inited) {
061: inited = launchServlet();
062: }
063: }
064: }
065:
066: /**
067: * Get or create a suitable ServletLoader instance to load that servlet.
068: * @return A ServletLoader instance.
069: */
070:
071: protected synchronized ServletLoader getServletLoader() {
072: if (loader == null) {
073: loader = new ServletLoader(this );
074: }
075: return loader;
076: }
077:
078: /**
079: * Get the remote servlet URL base.
080: * @return The String encoded base URL for that servlet, or <strong>null
081: * </strong> if undefined.
082: */
083:
084: public String getServletBase() {
085: return getString(ATTR_SERVLET_BASE, null);
086: }
087:
088: public void setValue(int idx, Object value) {
089: super .setValueOfSuperClass(idx, value);
090: try {
091: // synchronization for pool access added, tk, 21.10.2001
092: synchronized (servletPool) {
093: if ((idx == ATTR_SERVLET_CLASS) && (value != null)) {
094: inited = launchServlet();
095: }
096: if ((idx == ATTR_SERVLET_BASE) && (value != null)) {
097: inited = launchServlet();
098: }
099: }
100: } catch (Exception ex) {
101: String msg = ("unable to set servlet class \""
102: + getServletClass() + "\" : " + ex.getMessage());
103: getServer().errlog(msg);
104: }
105: }
106:
107: /**
108: * Initialize the servlet.
109: * @exception ClassNotFoundException if servlet class can't be found.
110: * @exception ServletException if servlet can't be initialized.
111: */
112: protected boolean launchServlet() throws ClassNotFoundException,
113: ServletException {
114: if (debug) {
115: System.out.println("Launching servlet: "
116: + getServletClass());
117: }
118: // Get and check the servlet class:
119: // if ( servlet != null )
120: destroyServlet();
121: if (inited) {
122: String msg = "relaunching servlet failed due to incomplete \""
123: + getServletClass() + "\" cleanup.";
124: getServer().errlog(this , msg);
125: return false;
126: } else {
127: // Load appropriate servlet class:
128: Class c = null;
129: try {
130: // Load the servlet class through the loader:
131: c = getServletLoader().loadClass(getServletClass(),
132: true);
133: } catch (ClassFormatError er) {
134: String msg = ("class \"" + getServletClass()
135: + "\" loaded from " + getServletBase() + ", invalid format.");
136: if (debug) {
137: er.printStackTrace();
138: }
139: getServer().errlog(this , msg);
140: } catch (ClassNotFoundException ex) {
141: String msg = ("class \"" + getServletClass()
142: + "\" loaded from " + getServletBase() + ", not found.");
143: if (debug) {
144: ex.printStackTrace();
145: }
146: getServer().errlog(this , msg);
147: }
148: return (c != null) ? launchServlet(c) : false;
149: }
150: }
151: }
|