Annotation of java/classes/org/w3c/jigsaw/servlet/ServletWrapper.java, revision 1.20
1.1 abaird 1: // ServletWrapper.java
1.20 ! bmahe 2: // $Id: ServletWrapper.java,v 1.19 1998/02/26 13:20:10 bmahe Exp $
1.1 abaird 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
1.18 bmahe 6: package org.w3c.jigsaw.servlet;
1.1 abaird 7:
8: import java.io.* ;
9: import java.util.*;
10:
1.9 bmahe 11: import javax.servlet.*;
1.1 abaird 12:
1.18 bmahe 13: import org.w3c.tools.resources.store.*;
14: import org.w3c.util.*;
15: import org.w3c.www.mime.*;
16: import org.w3c.www.http.*;
17: import org.w3c.jigsaw.http.*;
18: import org.w3c.tools.resources.*;
1.1 abaird 19:
1.2 abaird 20: /**
21: * @author Alexandre Rafalovitch <alex@access.com.au>
22: * @author Anselm Baird-Smith <abaird@w3.org>
1.18 bmahe 23: * @author Benoit Mahe <bmahe@w3.org>
1.2 abaird 24: */
25:
1.18 bmahe 26: public class ServletWrapper extends FramedResource implements ServletConfig
1.1 abaird 27: {
28:
1.20 ! bmahe 29: private LocalServletLoader loader = null;
1.1 abaird 30:
1.20 ! bmahe 31: protected final static boolean debug = false;
1.9 bmahe 32:
1.20 ! bmahe 33: /**
! 34: * Attributes index - The servlet class name.
! 35: */
! 36: protected static int ATTR_SERVLET_CLASS = -1 ;
! 37: /**
! 38: * Attribute index - The init parameters for that servlet.
! 39: */
! 40: protected static int ATTR_PARAMETERS = 1;
! 41: /**
! 42: * Attribute index - Our parent-inherited servlet context.
! 43: */
! 44: protected static int ATTR_SERVLET_CONTEXT = -1;
! 45: /**
! 46: * Attribute index - use auto-reload?
! 47: */
! 48: protected static int ATTR_AUTO_RELOAD = -1;
! 49:
! 50: static {
! 51: Attribute a = null ;
! 52: Class cls = null ;
! 53: try {
! 54: cls = Class.forName("org.w3c.jigsaw.servlet.ServletWrapper") ;
! 55: } catch (Exception ex) {
! 56: ex.printStackTrace();
! 57: System.exit(0);
! 58: }
! 59: // The servlet class attribute.
! 60: a = new StringAttribute("servlet-class"
1.1 abaird 61: , null
1.20 ! bmahe 62: , Attribute.EDITABLE | Attribute.MANDATORY) ;
! 63: ATTR_SERVLET_CLASS = AttributeRegistry.registerAttribute(cls, a) ;
! 64: // This servlet init parameters
! 65: a = new PropertiesAttribute("servlet-parameters"
! 66: , null
! 67: , Attribute.EDITABLE);
! 68: ATTR_PARAMETERS = AttributeRegistry.registerAttribute(cls, a);
! 69: // Our servlet context:
! 70: a = new ObjectAttribute("servlet-context",
! 71: "org.w3c.jigsaw.servlet.ServletDirectoryFrame",
! 72: null,
! 73: Attribute.DONTSAVE);
! 74: ATTR_SERVLET_CONTEXT = AttributeRegistry.registerAttribute(cls, a);
! 75: // Use auto-reload?
! 76: a = new BooleanAttribute("auto-reload",
! 77: Boolean.TRUE,
! 78: Attribute.EDITABLE);
! 79: ATTR_AUTO_RELOAD = AttributeRegistry.registerAttribute(cls, a);
! 80: }
1.1 abaird 81:
1.20 ! bmahe 82: /**
! 83: * Should we use LocalClassLoader? (auto-reload)
! 84: */
! 85: protected boolean getAutoReloadFlag() {
! 86: return getBoolean(ATTR_AUTO_RELOAD, true);
! 87: }
! 88:
! 89: /**
! 90: * The servlet wrapped within that Jigsaw resource.
! 91: */
! 92: protected Servlet servlet = null;
! 93: /**
! 94: * Is out servler initialized ?
! 95: */
! 96: protected boolean inited = false;
! 97:
! 98: /**
! 99: * The Path where we can find the servlet class file.
! 100: */
! 101: public File getServletDirectory() {
! 102: ServletDirectoryFrame dir =
! 103: (ServletDirectoryFrame) getValue(ATTR_SERVLET_CONTEXT, null);
! 104: if (dir == null)
! 105: return null;
! 106: else return dir.getDirectory();
! 107: }
! 108:
! 109: /**
! 110: * Servlet stub implementation - Get an init parameter value.
! 111: */
! 112:
! 113: public synchronized String getInitParameter(String string) {
! 114: ArrayDictionary d = getServletParameters();
! 115: String v = (d != null) ? (String) d.get(string) : null;
! 116: return v;
! 117: }
! 118:
! 119: /**
! 120: * Servlet stub implementation - Get all init parameters.
! 121: */
! 122:
! 123: public synchronized Enumeration getInitParameterNames() {
! 124: // Convert init parameters to hashtable:
! 125: ArrayDictionary d = getServletParameters();
! 126: return (d != null) ? d.keys() : new EmptyEnumeration();
! 127: }
! 128:
! 129: /**
! 130: * Servlet stub implementation - Get that servlet context.
! 131: */
! 132:
! 133: public ServletContext getServletContext() {
! 134: return (ServletContext) getValue(ATTR_SERVLET_CONTEXT, null);
! 135: }
! 136:
! 137: protected void checkServletClass() {
! 138: if (getAutoReloadFlag()) {
! 139: if (loader.classChanged(getServletClass())) {
! 140: inited = launchServlet();
! 141: }
! 142: } else {
! 143: if (! inited)
! 144: inited = launchServlet();
! 145: }
! 146: }
! 147:
! 148: protected boolean isInited() {
! 149: return inited;
! 150: }
! 151:
! 152: protected void service(Request request, Reply reply)
! 153: throws ServletException, IOException
! 154: {
! 155: servlet.service(new JigsawHttpServletRequest(servlet, request),
! 156: new JigsawHttpServletResponse(request, reply));
! 157: }
! 158:
! 159: /**
! 160: * Get the class name of the wrapped servlet.
! 161: * @return The class name for the servlet if attribute is defined.
! 162: * Otherwise the class name is deduced from the resource identifier.
! 163: */
! 164:
! 165: public String getServletClass()
! 166: {
! 167: String sclass = getString(ATTR_SERVLET_CLASS, null);
! 168: if (sclass == null) {
! 169: String ident = getIdentifier();
! 170: if (ident.endsWith(".class"))
! 171: sclass = ident;
! 172: }
! 173: return sclass;
! 174: }
! 175:
! 176: /**
! 177: * Get the init parameters for our wrapped servlet.
! 178: * @return An ArrayDictionary instance if the attribute is defined,
! 179: * <strong>false</strong> otherwise.
! 180: */
! 181:
! 182: public ArrayDictionary getServletParameters() {
! 183: return (ArrayDictionary) getValue(ATTR_PARAMETERS, null);
! 184: }
! 185:
! 186: /**
! 187: * Catch assignements to the servlet class name attribute.
! 188: * <p>When a change to that attribute is detected, the servlet is
! 189: * automatically reinitialized.
! 190: */
! 191:
! 192: public void setValue(int idx, Object value) {
! 193: super.setValue(idx, value);
! 194: if ((idx == ATTR_SERVLET_CLASS) && (value != null))
! 195: inited = launchServlet();
! 196: if (idx == ATTR_AUTO_RELOAD) {
! 197: if (getAutoReloadFlag())
! 198: loader = new LocalServletLoader(this);
! 199: else
! 200: loader = null;
! 201: inited = launchServlet();
! 202: }
! 203: }
! 204:
! 205: /**
! 206: * Destroy the servlet we are wrapping.
! 207: */
! 208:
! 209: protected synchronized void destroyServlet() {
! 210: if (servlet != null) {
! 211: servlet.destroy();
! 212: servlet = null;
! 213: }
! 214: }
! 215:
! 216: /**
! 217: * Get the servlet we are wrapping.
! 218: * @return A servlet instance, if the servlet is alredy running,
! 219: * <strong>null</strong> otherwise.
! 220: */
! 221:
! 222: public Servlet getServlet() {
! 223: return servlet;
! 224: }
! 225:
! 226: /**
! 227: * Initialize our servlet from the given (loaded) class.
! 228: * @param cls The servlet loaded main class.
! 229: * @return A boolean, <strong>true</strong> if servlet was successfully
! 230: * initialised, <strong>false</strong> otherwise.
! 231: */
! 232:
! 233: protected boolean launchServlet(Class cls) {
! 234: try {
! 235: servlet = (Servlet) cls.newInstance();
! 236: servlet.init((ServletConfig) this);
! 237: } catch (Exception ex) {
! 238: String msg = ("unable to initialize servlet, exception: "+
! 239: ex.getMessage());
! 240: if ( debug )
! 241: ex.printStackTrace();
! 242: getServer().errlog(this, msg);
! 243: return false;
! 244: }
! 245: return (servlet != null) ;
! 246: }
! 247:
! 248: /**
! 249: * Launch the servlet we are wrapping.
! 250: * <p>This method either succeed, or the wrapper resource itself will fail
! 251: * to initialize, acting as transparently as possible (in some sense).
! 252: * @return A boolean, <strong>true</strong> if servlet launched.
! 253: */
! 254:
! 255: protected boolean launchServlet() {
! 256: // Get and check the servlet class:
! 257: if ( servlet != null )
! 258: destroyServlet();
! 259: String clsname = getServletClass();
! 260: if ( clsname == null ) {
! 261: getServer().errlog(this, "no servlet class attribute defined.");
! 262: return false;
! 263: } else {
! 264: Class c = null;
! 265: try {
! 266: if (getAutoReloadFlag()) {
! 267: if (loader.classChanged(clsname))
! 268: loader = new LocalServletLoader(this.loader);
! 269: c = loader.loadServletClass(clsname, true);
! 270: } else {
! 271: c = Class.forName(clsname);
! 272: }
! 273: } catch (ClassNotFoundException ex) {
! 274: if ( debug )
! 275: ex.printStackTrace();
! 276: String msg = ("unable to load servlet class \""+
! 277: getServletClass()+
! 278: "\" : "+
! 279: ex.getMessage());
! 280: getServer().errlog(msg);
! 281: }
! 282: return (c != null) ? launchServlet(c) : false;
! 283: }
! 284: }
! 285:
! 286: public void notifyUnload() {
! 287: destroyServlet();
! 288: }
! 289:
! 290: /**
! 291: * Initialize this servlet wrapper resource.
! 292: * After the wrapper itself is inited, it performs the servlet
! 293: * initialzation.
! 294: * @param values The default attribute values.
! 295: */
! 296:
! 297: public void initialize(Object values[]) {
! 298: super.initialize(values);
! 299: loader = new LocalServletLoader(this);
! 300: // Initialize the wrapped servlet (is now the rigt time ?)
! 301: if ( getServletClass() != null )
! 302: inited = launchServlet();
! 303: try {
! 304: registerFrameIfNone("org.w3c.jigsaw.servlet.ServletWrapperFrame",
! 305: "servlet-wrapper-frame");
! 306: } catch (Exception ex) {
! 307: ex.printStackTrace();
! 308: }
1.18 bmahe 309: }
1.1 abaird 310:
311: }
312:
313:
314:
315:
316:
317:
318:
319:
Webmaster