1 23 24 package org.enhydra.xml.xmlc.servlet; 25 26 import java.io.File ; 27 import java.lang.reflect.Constructor ; 28 import java.util.StringTokenizer ; 29 30 import javax.servlet.ServletContext ; 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 42 final class XMLCContextInit { 43 44 45 private static final Class [] LOGGER_CONSTRUCTOR_SIGNATURE = new Class [] { 46 ServletContext .class, Boolean.TYPE, Boolean.TYPE }; 47 48 49 private static final Class [] CONTEXT_CONSTRUCTOR_SIGNATURE = new Class [] { 50 ServletContext .class, XMLCFactory.class, 51 XMLCContext.SessionURLEncodingMode.class, 52 Boolean.TYPE, Integer.TYPE 53 }; 54 55 58 private static final String PARAM_XMLC_RELOADING = "xmlcReloading"; 59 private static final String PARAM_XMLC_RELOADING_OFF = "off"; 60 private static final String PARAM_XMLC_RELOADING_RELOAD = "reload"; 61 private static final String PARAM_XMLC_RELOADING_RECOMPILE = "recompile"; 62 private static final String PARAM_XMLC_DEFERRED_PARSING = "reparse"; 63 64 67 private static final String PARAM_XMLC_REPARSE_RESOURCE_DIRS = "xmlcReparseResourceDirs"; 68 69 72 private static final String PARAM_XMLC_REPARSE_PACKAGE_PREFIXES = "xmlcReparsePackagePrefixes"; 73 74 77 private static final String PARAM_XMLC_REPARSE_DEFAULT_METADATA_PATH = "xmlcReparseDefaultMetaDataPath"; 78 79 82 private static final String PARAM_XMLC_RECOMP_HANDLER = "xmlcRecompilationHandler"; 83 84 87 private static final String PARAM_XMLC_LOGGER = "xmlcLogger"; 88 89 92 private static final String PARAM_XMLC_LOGGING = "xmlcLogging"; 93 private static final String PARAM_XMLC_LOGGING_INFO = "INFO"; 94 private static final String PARAM_XMLC_LOGGING_DEBUG = "DEBUG"; 95 private static final String PARAM_XMLC_LOGGING_STATS = "STATS"; 96 97 100 private static final String PARAM_XMLC_COMPRESS = "xmlcCompression"; 101 private static final String PARAM_XMLC_COMPRESS_NONE = "none"; 102 private static final String PARAM_XMLC_COMPRESS_GZIP = "gzip"; 103 104 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 114 private static final String PARAM_XMLC_CONTEXT_IMPL = "xmlcContextImpl"; 115 116 119 private static final String PARAM_XMLC_SESSION_URL_ENCODING 120 = "xmlcSessionURLEncoding"; 121 122 127 private String parseReloadingParam(ServletContext servletContext) { 128 String value = servletContext.getInitParameter(PARAM_XMLC_RELOADING); 129 if (value == null) { 130 return PARAM_XMLC_RELOADING_OFF; } 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 153 private int parseLoggingToken(String 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 175 private int parseLoggingParam(ServletContext servletContext) { 176 String value = servletContext.getInitParameter(PARAM_XMLC_LOGGING); 177 if (value == null) { 178 return XMLC_LOG_INFO; } 180 StringTokenizer tokens = new StringTokenizer (value, " ,"); 182 int flags = 0; 183 while (tokens.hasMoreTokens()) { 184 flags |= parseLoggingToken(tokens.nextToken()); 185 } 186 return flags; 187 } 188 189 193 private int parseCompressionParam(ServletContext servletContext) { 194 String value = servletContext.getInitParameter(PARAM_XMLC_COMPRESS); 195 if (value == null) { 196 return XMLCContext.XMLC_COMPRESS_NONE; } 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; } 204 } 205 206 210 private XMLCContext.SessionURLEncodingMode parseSessionURLEncodingParam(ServletContext servletContext) { 211 String value = servletContext.getInitParameter(PARAM_XMLC_SESSION_URL_ENCODING); 212 if (value == null) { 213 return XMLCContext.URL_ENCODING_AUTO; } 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 233 private XMLCFactory createFactory(String reloadingMode, 234 ServletContext 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 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 t) { 245 throw new XMLCError("Cannot create custom document " + 246 "loader of type \"" + 247 loaderName + "\"", t); 248 } 249 250 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 resDirs = servletContext.getInitParameter(PARAM_XMLC_REPARSE_RESOURCE_DIRS); 263 if (resDirs != null) { 264 StringTokenizer st = new StringTokenizer (resDirs, File.pathSeparator); 265 while (st.hasMoreTokens()) { 266 newFactory.addResourceDir(st.nextToken()); 267 } 268 } 269 270 String pkgPredixes = servletContext.getInitParameter(PARAM_XMLC_REPARSE_PACKAGE_PREFIXES); 271 if (pkgPredixes != null) { 272 StringTokenizer st = new StringTokenizer (pkgPredixes, File.pathSeparator); 273 while (st.hasMoreTokens()) { 274 newFactory.addPackagePrefix(st.nextToken()); 275 } 276 } 277 278 String 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 292 XMLCContext createContext(ServletContext servletContext) { 293 String reloadingMode = parseReloadingParam(servletContext); 294 int loggingFlags = parseLoggingParam(servletContext); 295 XMLCContext.SessionURLEncodingMode sessionURLEncodingMode 296 = parseSessionURLEncodingParam(servletContext); 297 298 String loggerName = servletContext.getInitParameter(PARAM_XMLC_LOGGER); 299 XMLCLogger logger = null; 300 Throwable loggerError = null; 301 if (loggerName != null) { 302 try { 303 Class loggerClass = Class.forName(loggerName, true, Thread.currentThread().getContextClassLoader()); 304 Constructor c = 305 loggerClass.getConstructor(LOGGER_CONSTRUCTOR_SIGNATURE); 306 logger = (XMLCLogger)c.newInstance(new Object [] { 307 servletContext, 308 new Boolean ((loggingFlags & XMLC_LOG_INFO) != 0), 309 new Boolean ((loggingFlags & XMLC_LOG_DEBUG) != 0)}); 310 } catch (Throwable 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 contextImplName = 324 servletContext.getInitParameter(PARAM_XMLC_CONTEXT_IMPL); 325 if (contextImplName != null) { 326 try { 327 Class contextImplClass = Class.forName(contextImplName, true, Thread.currentThread().getContextClassLoader()); 328 Constructor c = 329 contextImplClass.getConstructor( 330 CONTEXT_CONSTRUCTOR_SIGNATURE); 331 return (XMLCContext)c.newInstance(new Object [] { 332 servletContext, factory, sessionURLEncodingMode, 333 new Boolean ((loggingFlags & XMLC_LOG_STATS) != 0), 334 new Integer (compressionType) }); 335 } catch (Throwable 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 |