KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > servlet > XMLCContextInit


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XMLCContextInit.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.servlet;
25
26 import java.io.File JavaDoc;
27 import java.lang.reflect.Constructor JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31
32 import org.enhydra.xml.xmlc.XMLCError;
33 import org.enhydra.xml.xmlc.XMLCFactory;
34 import org.enhydra.xml.xmlc.XMLCLogger;
35 import org.enhydra.xml.xmlc.XMLCStdFactory;
36 import org.enhydra.xml.xmlc.deferredparsing.DocumentLoader;
37 import org.enhydra.xml.xmlc.deferredparsing.XMLCDeferredParsingFactory;
38
39 /**
40  * Create a new XMLCContext from init parameters.
41  */

42 final class XMLCContextInit {
43
44     /** Signature declaration for the XMLCLogger constructor */
45     private static final Class JavaDoc[] LOGGER_CONSTRUCTOR_SIGNATURE = new Class JavaDoc[] {
46     ServletContext JavaDoc.class, Boolean.TYPE, Boolean.TYPE };
47
48     /** Signature declaration for the XMLCContext constructor */
49     private static final Class JavaDoc[] CONTEXT_CONSTRUCTOR_SIGNATURE = new Class JavaDoc[] {
50     ServletContext JavaDoc.class, XMLCFactory.class,
51     XMLCContext.SessionURLEncodingMode.class,
52     Boolean.TYPE, Integer.TYPE
53     };
54     
55     /**
56      * Definition of xmlcReloading parameter.
57      */

58     private static final String JavaDoc PARAM_XMLC_RELOADING = "xmlcReloading";
59     private static final String JavaDoc PARAM_XMLC_RELOADING_OFF = "off";
60     private static final String JavaDoc PARAM_XMLC_RELOADING_RELOAD = "reload";
61     private static final String JavaDoc PARAM_XMLC_RELOADING_RECOMPILE = "recompile";
62     private static final String JavaDoc PARAM_XMLC_DEFERRED_PARSING = "reparse";
63
64     /**
65      * Definition of xmlcReparseResourceDirs parameter
66      */

67     private static final String JavaDoc PARAM_XMLC_REPARSE_RESOURCE_DIRS = "xmlcReparseResourceDirs";
68
69     /**
70      * Definition of xmlcReparsePackagePrefixes parameter
71      */

72     private static final String JavaDoc PARAM_XMLC_REPARSE_PACKAGE_PREFIXES = "xmlcReparsePackagePrefixes";
73     
74     /**
75      * Definition of xmlcReparseDefaultMetaDataPath poarameter
76      */

77     private static final String JavaDoc PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH = "xmlcReparseDefaultMetaDataPath";
78
79     /**
80      * Definition of xmlcRecompilationHandler parameter.
81      */

82     private static final String JavaDoc PARAM_XMLC_RECOMP_HANDLER = "xmlcRecompilationHandler";
83
84     /**
85      * Definition of xmlcLogger parameter.
86      */

87     private static final String JavaDoc PARAM_XMLC_LOGGER = "xmlcLogger";
88
89     /**
90      * Definition of xmlcLogging parameter.
91      */

92     private static final String JavaDoc PARAM_XMLC_LOGGING = "xmlcLogging";
93     private static final String JavaDoc PARAM_XMLC_LOGGING_INFO = "INFO";
94     private static final String JavaDoc PARAM_XMLC_LOGGING_DEBUG = "DEBUG";
95     private static final String JavaDoc PARAM_XMLC_LOGGING_STATS = "STATS";
96
97     /**
98      * Definition of xmlcCompression parameter.
99      */

100     private static final String JavaDoc PARAM_XMLC_COMPRESS = "xmlcCompression";
101     private static final String JavaDoc PARAM_XMLC_COMPRESS_NONE = "none";
102     private static final String JavaDoc PARAM_XMLC_COMPRESS_GZIP = "gzip";
103
104     /**
105      * Definition of xmlcLogging parameter values as a bit set.
106      */

107     private static final int XMLC_LOG_INFO = 0x01;
108     private static final int XMLC_LOG_DEBUG = 0x02;
109     private static final int XMLC_LOG_STATS = 0x04;
110
111     /**
112      * Definition of xmlcContextImpl parameter.
113      */

114     private static final String JavaDoc PARAM_XMLC_CONTEXT_IMPL = "xmlcContextImpl";
115
116     /**
117      * Definition of xmlcSessionURLEncoding parameter.
118      */

119     private static final String JavaDoc PARAM_XMLC_SESSION_URL_ENCODING
120         = "xmlcSessionURLEncoding";
121
122     /**
123      * Parse the xmlcReloading parameter.
124      * @return The string constant defined in this class (refs can be
125      * compared).
126      */

127     private String JavaDoc parseReloadingParam(ServletContext JavaDoc servletContext) {
128         String JavaDoc value = servletContext.getInitParameter(PARAM_XMLC_RELOADING);
129         if (value == null) {
130             return PARAM_XMLC_RELOADING_OFF; // default
131
} else if (value.equals(PARAM_XMLC_RELOADING_OFF)) {
132             return PARAM_XMLC_RELOADING_OFF;
133         } else if (value.equals(PARAM_XMLC_RELOADING_RELOAD)) {
134             return PARAM_XMLC_RELOADING_RELOAD;
135         } else if (value.equals(PARAM_XMLC_RELOADING_RECOMPILE)) {
136             return PARAM_XMLC_RELOADING_RECOMPILE;
137         } else if (value.equals(PARAM_XMLC_DEFERRED_PARSING)) {
138             return PARAM_XMLC_DEFERRED_PARSING;
139         } else {
140             throw new XMLCError("Invalid value for " + PARAM_XMLC_RELOADING
141                                 + " servlet parameter: \""
142                                 + value + "\"; expected one of " +
143                                 PARAM_XMLC_RELOADING_OFF + ", " +
144                                 PARAM_XMLC_RELOADING_RELOAD + ", " +
145                                 PARAM_XMLC_RELOADING_RECOMPILE + ", or " +
146                 PARAM_XMLC_DEFERRED_PARSING);
147         }
148     }
149
150     /**
151      * Parse one of the tokens from the logging config parameter.
152      */

153     private int parseLoggingToken(String JavaDoc token) {
154         if (token.equals(PARAM_XMLC_LOGGING_INFO)) {
155             return XMLC_LOG_INFO;
156         } else if (token.equals(PARAM_XMLC_LOGGING_DEBUG)) {
157             return XMLC_LOG_DEBUG;
158         } else if (token.equals(PARAM_XMLC_LOGGING_STATS)) {
159             return XMLC_LOG_STATS;
160         } else {
161             throw new XMLCError("Invalid value for argument to "
162                                 + PARAM_XMLC_LOGGING
163                                 + " servlet parameter: \""
164                                 + token + "\"; expected one of " +
165                                 PARAM_XMLC_LOGGING_INFO + ", " +
166                                 PARAM_XMLC_LOGGING_DEBUG + ", or " +
167                                 PARAM_XMLC_LOGGING_STATS);
168         }
169     }
170
171     /**
172      * Parse the xmlcLogging parameter.
173      * @return Bit set representing the logging flags.
174      */

175     private int parseLoggingParam(ServletContext JavaDoc servletContext) {
176         String JavaDoc value = servletContext.getInitParameter(PARAM_XMLC_LOGGING);
177         if (value == null) {
178             return XMLC_LOG_INFO; // default
179
}
180         // Parse the string.
181
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(value, " ,");
182         int flags = 0;
183         while (tokens.hasMoreTokens()) {
184             flags |= parseLoggingToken(tokens.nextToken());
185         }
186         return flags;
187     }
188
189     /**
190      * Parse the xmlcCompression parameter.
191      * @return Bit set representing the compression flags.
192      */

193     private int parseCompressionParam(ServletContext JavaDoc servletContext) {
194         String JavaDoc value = servletContext.getInitParameter(PARAM_XMLC_COMPRESS);
195         if (value == null) {
196             return XMLCContext.XMLC_COMPRESS_NONE; // default
197
} else if (value.equals(PARAM_XMLC_COMPRESS_NONE)) {
198             return XMLCContext.XMLC_COMPRESS_NONE;
199         } else if (value.equals(PARAM_XMLC_COMPRESS_GZIP)) {
200             return XMLCContext.XMLC_COMPRESS_GZIP;
201     } else {
202             return XMLCContext.XMLC_COMPRESS_NONE; // default
203
}
204     }
205     
206     /**
207      * Parse the PARAM_XMLC_SESSION_URL_ENCODING parameter.
208      * @return The object constant defining the encoding mode
209      */

210     private XMLCContext.SessionURLEncodingMode parseSessionURLEncodingParam(ServletContext JavaDoc servletContext) {
211         String JavaDoc value = servletContext.getInitParameter(PARAM_XMLC_SESSION_URL_ENCODING);
212         if (value == null) {
213             return XMLCContext.URL_ENCODING_AUTO; // default
214
} else if (value.equals(XMLCContext.URL_ENCODING_AUTO.toString())) {
215             return XMLCContext.URL_ENCODING_AUTO;
216         } else if (value.equals(XMLCContext.URL_ENCODING_ALWAYS.toString())) {
217             return XMLCContext.URL_ENCODING_ALWAYS;
218         } else if (value.equals(XMLCContext.URL_ENCODING_NEVER.toString())) {
219             return XMLCContext.URL_ENCODING_NEVER;
220         } else {
221             throw new XMLCError("Invalid value for " + PARAM_XMLC_RELOADING
222                                 + " servlet parameter: \""
223                                 + value + "\"; expected one of " +
224                                 XMLCContext.URL_ENCODING_AUTO + ", " +
225                                 XMLCContext.URL_ENCODING_ALWAYS + ", or " +
226                                 XMLCContext.URL_ENCODING_NEVER);
227         }
228     }
229         
230     /**
231      * Create the XMLCFactory.
232      */

233     private XMLCFactory createFactory(String JavaDoc reloadingMode,
234                                       ServletContext JavaDoc servletContext,
235                                       XMLCLogger logger) {
236         if (reloadingMode == PARAM_XMLC_RELOADING_OFF) {
237             return new XMLCStdFactory(Thread.currentThread().getContextClassLoader(), logger);
238         } else if (reloadingMode == PARAM_XMLC_DEFERRED_PARSING) {
239         DocumentLoader loader = null;
240         String JavaDoc loaderName = servletContext.getInitParameter(PARAM_XMLC_RECOMP_HANDLER);
241         if (loaderName!=null) {
242         try {
243             loader = (DocumentLoader)Class.forName(loaderName, true, Thread.currentThread().getContextClassLoader()).newInstance();
244         } catch (Throwable JavaDoc t) {
245             throw new XMLCError("Cannot create custom document " +
246                     "loader of type \"" +
247                     loaderName + "\"", t);
248         }
249         
250         // If we have a servlet aware document
251
// loader, pass it the servlet context.
252
if (loader instanceof ServletDocumentLoader) {
253             ((ServletDocumentLoader)loader)
254             .setServletContext(servletContext);
255         }
256         }
257             XMLCDeferredParsingFactory newFactory
258         = new XMLCDeferredParsingFactory(loader,
259                          Thread.currentThread().getContextClassLoader(),
260                          logger);
261
262         String JavaDoc resDirs = servletContext.getInitParameter(PARAM_XMLC_REPARSE_RESOURCE_DIRS);
263         if (resDirs != null) {
264         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (resDirs, File.pathSeparator);
265         while (st.hasMoreTokens()) {
266             newFactory.addResourceDir(st.nextToken());
267         }
268         }
269
270         String JavaDoc pkgPredixes = servletContext.getInitParameter(PARAM_XMLC_REPARSE_PACKAGE_PREFIXES);
271         if (pkgPredixes != null) {
272         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (pkgPredixes, File.pathSeparator);
273         while (st.hasMoreTokens()) {
274             newFactory.addPackagePrefix(st.nextToken());
275         }
276         }
277
278         String JavaDoc defaultMetaDataPath = servletContext.getInitParameter(PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH);
279         if (defaultMetaDataPath != null) {
280         newFactory.setDefaultMetaDataPath(defaultMetaDataPath);
281         }
282         
283         return newFactory;
284     } else {
285         throw new XMLCError("Unknown reloading mode: " + reloadingMode);
286     }
287     }
288
289     /**
290      * Create a new context.
291      */

292     XMLCContext createContext(ServletContext JavaDoc servletContext) {
293         String JavaDoc reloadingMode = parseReloadingParam(servletContext);
294         int loggingFlags = parseLoggingParam(servletContext);
295         XMLCContext.SessionURLEncodingMode sessionURLEncodingMode
296             = parseSessionURLEncodingParam(servletContext);
297
298     String JavaDoc loggerName = servletContext.getInitParameter(PARAM_XMLC_LOGGER);
299         XMLCLogger logger = null;
300     Throwable JavaDoc loggerError = null;
301     if (loggerName != null) {
302         try {
303         Class JavaDoc loggerClass = Class.forName(loggerName, true, Thread.currentThread().getContextClassLoader());
304         Constructor JavaDoc c =
305             loggerClass.getConstructor(LOGGER_CONSTRUCTOR_SIGNATURE);
306         logger = (XMLCLogger)c.newInstance(new Object JavaDoc[] {
307            servletContext,
308            new Boolean JavaDoc((loggingFlags & XMLC_LOG_INFO) != 0),
309            new Boolean JavaDoc((loggingFlags & XMLC_LOG_DEBUG) != 0)});
310         } catch (Throwable JavaDoc t) {
311         throw new XMLCError("Cannot create custom logger of type \"" +
312                     loggerName + "\"", t);
313         }
314     }
315     if (logger == null) {
316         logger = new ServletXMLCLogger(servletContext,
317                        ((loggingFlags & XMLC_LOG_INFO) != 0),
318                        ((loggingFlags & XMLC_LOG_DEBUG) != 0));
319     }
320         XMLCFactory factory = createFactory(reloadingMode, servletContext, logger);
321     int compressionType = parseCompressionParam(servletContext);
322
323     String JavaDoc contextImplName =
324         servletContext.getInitParameter(PARAM_XMLC_CONTEXT_IMPL);
325     if (contextImplName != null) {
326         try {
327         Class JavaDoc contextImplClass = Class.forName(contextImplName, true, Thread.currentThread().getContextClassLoader());
328         Constructor JavaDoc c =
329             contextImplClass.getConstructor(
330                           CONTEXT_CONSTRUCTOR_SIGNATURE);
331         return (XMLCContext)c.newInstance(new Object JavaDoc[] {
332             servletContext, factory, sessionURLEncodingMode,
333             new Boolean JavaDoc((loggingFlags & XMLC_LOG_STATS) != 0),
334             new Integer JavaDoc(compressionType) });
335         } catch (Throwable JavaDoc t) {
336         throw new XMLCError(
337                 "Cannot create custom XMLC context of type \"" +
338                 contextImplName + "\"", t);
339         }
340     } else {
341         return new
342         XMLCContext(servletContext, factory, sessionURLEncodingMode,
343                 ((loggingFlags & XMLC_LOG_STATS) != 0),
344                 compressionType);
345     }
346     }
347 }
348
Popular Tags