Annotation of java/classes/org/w3c/jigsaw/servlet/ServletIndexer.java, revision 1.6
1.1 bmahe 1: // ServletIndexer.java
1.6 ! ylafon 2: // $Id: ServletIndexer.java,v 1.5 1999/01/07 10:52:33 bmahe Exp $
1.1 bmahe 3: // (c) COPYRIGHT MIT and INRIA, 1998.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package org.w3c.jigsaw.servlet;
7:
8: import java.io.*;
9: import java.util.*;
10:
11: import javax.servlet.*;
12:
13: import org.w3c.tools.resources.*;
14: import org.w3c.tools.resources.indexer.*;
15:
16: /**
1.6 ! ylafon 17: * @version $Revision: 1.5 $
1.1 bmahe 18: * @author Benoît Mahé (bmahe@w3.org)
19: */
20: public class ServletIndexer extends SampleResourceIndexer {
21:
22: /**
23: * Copy one hastable in another one.
24: * @param fromdefs The source
25: * @param todefs The destination
26: */
27: protected void copyDefs(Hashtable fromdefs, Hashtable toDefs) {
28: Enumeration keys = fromdefs.keys();
29: while(keys.hasMoreElements()) {
30: Object key = keys.nextElement();
31: toDefs.put(keys, fromdefs.get(key));
32: }
33: }
34:
35: /**
36: * Create a default file resource for this file (that exists).
37: * @param directory The directory of the file.
38: * @param name The name of the file.
39: * @param defs A set of default attribute values.
40: * @return An instance of Resource, or <strong>null</strong> if
41: * we were unable to create it.
42: */
43:
44: protected Resource createFileResource(File directory,
45: RequestInterface req,
46: String name,
47: Hashtable defs)
48: {
49: if (! name.endsWith(".class"))
50: return super.createFileResource(directory, req, name, defs);
1.4 bmahe 51: System.out.println("Create Servlet : "+name);
1.1 bmahe 52: ResourceReference rr = null;
53: FramedResource template = null;
54:
55: // Check that at least one class is defined for all the extensions:
56: String exts[] = getFileExtensions(name) ;
57: if ( exts == null )
58: return null ;
59: for (int i = exts.length-1 ; i >= 0 ; i--) {
60: rr = getTemplateFor(exts[i]) ;
61: if ( rr != null )
62: break ;
63: }
64: if ( rr == null ) {
65: // Look for a default template:
66: if ((rr = loadExtension(defname)) == null)
67: return null ;
68: return super.createFileResource(directory, req, name, defs);
69: } else {
70: //this could become a servlet
71: Hashtable tempdefs = null;
72: if (defs != null)
73: tempdefs = (Hashtable) defs.clone();
74: else
75: tempdefs = new Hashtable(5) ;
76: if ( tempdefs.get("directory") == null )
77: tempdefs.put("directory", directory) ;
78: if ( tempdefs.get("context") == null )
79: tempdefs.put("context", getContext());
80: try {
81: template = (FramedResource) rr.lock();
82: if (template instanceof ServletWrapper) {
83: if (tempdefs.get("servlet-class") == null)
84: tempdefs.put("servlet-class", name);
1.4 bmahe 85: String id = getIndexedFileName(name);
1.1 bmahe 86: tempdefs.put("identifier", id) ;
87: String url = (String) tempdefs.get("url");
88: if ((url != null) && (url.endsWith(".class"))) {
1.4 bmahe 89: int idx = url.lastIndexOf(".class");
1.1 bmahe 90: tempdefs.put("url", url.substring(0, idx));
91: }
92: } else {
93: if ( tempdefs.get("identifier") == null )
94: tempdefs.put("identifier", name);
95: }
96: if (exts != null) {
97: // Merge with values defined by the extension:
98: for (int i = exts.length ; --i >= 0 ; )
99: mergeDefaultAttributes(template, exts[i], tempdefs) ;
100: }
1.2 bmahe 101: // Create, initialize and return the new resource
1.1 bmahe 102: try {
103: FramedResource cloned =
104: (FramedResource) template.getClone(tempdefs);
105: if (cloned instanceof ServletWrapper) {
106: ServletWrapper wrapper = (ServletWrapper) cloned;
107: // check the servlet class
108: if (! wrapper.isWrappingAServlet())
109: return null;
110: }
1.2 bmahe 111: //ok, the defs are good.
1.1 bmahe 112: copyDefs(tempdefs, defs);
113: return cloned;
114: } catch (Exception ex) {
115: ex.printStackTrace() ;
116: return null ;
117: }
118: } catch (InvalidResourceException ex) {
119: ex.printStackTrace();
120: return null;
121: } finally {
122: rr.unlock();
123: }
124: }
1.3 bmahe 125: }
126:
127: /**
128: * Try to create a virtual resource if the real (physical) resource
129: * is not there.
130: * @param directory The directory the file is in.
131: * @param name The name of the file.
132: * @param defs Any default attribute values that should be provided
133: * to the created resource at initialization time.
134: * @return A Resource instance, or <strong>null</strong> if the given
135: * file can't be truned into a resource given our configuration
136: * database.
137: */
138:
139: protected Resource createVirtualResource( File directory,
1.6 ! ylafon 140: RequestInterface req,
1.3 bmahe 141: String name,
142: Hashtable defs)
143: {
1.6 ! ylafon 144: Resource res = super.createVirtualResource(directory, req, name, defs);
1.3 bmahe 145: if (res != null)
146: return res;
147: //could be a servlet
1.5 bmahe 148: char fileSeparatorChar = File.separatorChar;
149: String sname = name.replace('.', fileSeparatorChar)+".class";
1.3 bmahe 150: File servletfile = new File(directory, sname);
151: if (servletfile.exists())
152: return createFileResource(directory, null, sname, defs);
153: else
154: return null;
1.4 bmahe 155: }
156:
157: protected String getIndexedFileName(String name) {
158: String indexed = name;
159: int idx = name.lastIndexOf(".class");
160: if (idx != -1)
161: indexed = name.substring(0,idx);
162: return indexed;
1.1 bmahe 163: }
164: }
Webmaster