Annotation of java/classes/org/w3c/jigsaw/servlet/ServletWrapper.java, revision 1.25
1.1 abaird 1: // ServletWrapper.java
1.25 ! bmahe 2: // $Id: ServletWrapper.java,v 1.24 1998/05/25 15:05:45 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.22 bmahe 29: protected 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",
1.24 bmahe 71: "org.w3c.jigsaw.servlet.JigsawServletContext",
1.20 bmahe 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() {
1.25 ! bmahe 102: ResourceReference rr = getParent();
! 103: if (rr != null) {
! 104: try {
! 105: Resource parent = rr.lock();
! 106: if (parent.definesAttribute("directory"))
! 107: return (File) parent.getValue("directory", null);
! 108: } catch(InvalidResourceException ex) {
! 109: ex.printStackTrace();
! 110: } finally {
! 111: rr.unlock();
! 112: }
! 113: }
! 114: return null;
1.20 bmahe 115: }
116:
117: /**
118: * Servlet stub implementation - Get an init parameter value.
119: */
120:
121: public synchronized String getInitParameter(String string) {
122: ArrayDictionary d = getServletParameters();
123: String v = (d != null) ? (String) d.get(string) : null;
124: return v;
125: }
126:
127: /**
128: * Servlet stub implementation - Get all init parameters.
129: */
130:
131: public synchronized Enumeration getInitParameterNames() {
132: // Convert init parameters to hashtable:
133: ArrayDictionary d = getServletParameters();
134: return (d != null) ? d.keys() : new EmptyEnumeration();
135: }
136:
137: /**
138: * Servlet stub implementation - Get that servlet context.
139: */
140:
141: public ServletContext getServletContext() {
142: return (ServletContext) getValue(ATTR_SERVLET_CONTEXT, null);
143: }
144:
145: protected void checkServletClass() {
146: if (getAutoReloadFlag()) {
147: if (loader.classChanged(getServletClass())) {
148: inited = launchServlet();
149: }
150: } else {
151: if (! inited)
152: inited = launchServlet();
153: }
154: }
155:
156: protected boolean isInited() {
157: return inited;
158: }
159:
160: protected void service(Request request, Reply reply)
161: throws ServletException, IOException
162: {
1.23 bmahe 163: JigsawHttpServletResponse jRes = null;
164: JigsawHttpServletRequest jReq = null;
165: if (servlet instanceof SingleThreadModel) {
166: synchronized (this) {
167: jRes = new JigsawHttpServletResponse(request, reply);
168: jReq = new JigsawHttpServletRequest(servlet, request, jRes);
169: servlet.service(jReq , jRes);
170: }
171: } else {
172: jRes = new JigsawHttpServletResponse(request, reply);
173: jReq = new JigsawHttpServletRequest(servlet, request, jRes);
174: servlet.service(jReq , jRes);
175: }
1.20 bmahe 176: }
177:
178: /**
179: * Get the class name of the wrapped servlet.
180: * @return The class name for the servlet if attribute is defined.
181: * Otherwise the class name is deduced from the resource identifier.
182: */
183:
184: public String getServletClass()
185: {
186: String sclass = getString(ATTR_SERVLET_CLASS, null);
187: if (sclass == null) {
188: String ident = getIdentifier();
189: if (ident.endsWith(".class"))
190: sclass = ident;
191: }
192: return sclass;
193: }
194:
195: /**
196: * Get the init parameters for our wrapped servlet.
197: * @return An ArrayDictionary instance if the attribute is defined,
198: * <strong>false</strong> otherwise.
199: */
200:
201: public ArrayDictionary getServletParameters() {
202: return (ArrayDictionary) getValue(ATTR_PARAMETERS, null);
203: }
204:
205: /**
206: * Catch assignements to the servlet class name attribute.
207: * <p>When a change to that attribute is detected, the servlet is
208: * automatically reinitialized.
209: */
210:
211: public void setValue(int idx, Object value) {
212: super.setValue(idx, value);
213: if ((idx == ATTR_SERVLET_CLASS) && (value != null))
214: inited = launchServlet();
215: if (idx == ATTR_AUTO_RELOAD) {
216: if (getAutoReloadFlag())
217: loader = new LocalServletLoader(this);
218: else
219: loader = null;
220: inited = launchServlet();
221: }
222: }
223:
224: /**
225: * Destroy the servlet we are wrapping.
226: */
227:
228: protected synchronized void destroyServlet() {
229: if (servlet != null) {
230: servlet.destroy();
231: servlet = null;
232: }
233: }
234:
235: /**
236: * Get the servlet we are wrapping.
237: * @return A servlet instance, if the servlet is alredy running,
238: * <strong>null</strong> otherwise.
239: */
240:
241: public Servlet getServlet() {
242: return servlet;
243: }
244:
245: /**
246: * Initialize our servlet from the given (loaded) class.
247: * @param cls The servlet loaded main class.
248: * @return A boolean, <strong>true</strong> if servlet was successfully
249: * initialised, <strong>false</strong> otherwise.
250: */
251:
252: protected boolean launchServlet(Class cls) {
253: try {
254: servlet = (Servlet) cls.newInstance();
255: servlet.init((ServletConfig) this);
256: } catch (Exception ex) {
257: String msg = ("unable to initialize servlet, exception: "+
258: ex.getMessage());
259: if ( debug )
260: ex.printStackTrace();
261: getServer().errlog(this, msg);
262: return false;
263: }
264: return (servlet != null) ;
265: }
266:
267: /**
268: * Launch the servlet we are wrapping.
269: * <p>This method either succeed, or the wrapper resource itself will fail
270: * to initialize, acting as transparently as possible (in some sense).
271: * @return A boolean, <strong>true</strong> if servlet launched.
272: */
273:
274: protected boolean launchServlet() {
275: // Get and check the servlet class:
276: if ( servlet != null )
277: destroyServlet();
278: String clsname = getServletClass();
279: if ( clsname == null ) {
280: getServer().errlog(this, "no servlet class attribute defined.");
281: return false;
282: } else {
283: Class c = null;
284: try {
285: if (getAutoReloadFlag()) {
286: if (loader.classChanged(clsname))
287: loader = new LocalServletLoader(this.loader);
288: c = loader.loadServletClass(clsname, true);
289: } else {
290: c = Class.forName(clsname);
291: }
292: } catch (ClassNotFoundException ex) {
293: if ( debug )
294: ex.printStackTrace();
295: String msg = ("unable to load servlet class \""+
296: getServletClass()+
297: "\" : "+
298: ex.getMessage());
299: getServer().errlog(msg);
300: }
301: return (c != null) ? launchServlet(c) : false;
302: }
303: }
304:
305: public void notifyUnload() {
306: destroyServlet();
307: }
308:
309: /**
310: * Initialize this servlet wrapper resource.
311: * After the wrapper itself is inited, it performs the servlet
312: * initialzation.
313: * @param values The default attribute values.
314: */
315:
316: public void initialize(Object values[]) {
317: super.initialize(values);
318: loader = new LocalServletLoader(this);
319: // Initialize the wrapped servlet (is now the rigt time ?)
320: if ( getServletClass() != null )
321: inited = launchServlet();
322: try {
323: registerFrameIfNone("org.w3c.jigsaw.servlet.ServletWrapperFrame",
324: "servlet-wrapper-frame");
325: } catch (Exception ex) {
326: ex.printStackTrace();
327: }
1.18 bmahe 328: }
1.1 abaird 329:
330: }
331:
332:
333:
334:
335:
336:
337:
338:
Webmaster