KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > EmbeddedServletOptions


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper;
19
20 import java.io.File JavaDoc;
21 import java.util.*;
22
23 import javax.servlet.ServletConfig JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.jasper.compiler.TldLocationsCache;
29 import org.apache.jasper.compiler.JspConfig;
30 import org.apache.jasper.compiler.TagPluginManager;
31 import org.apache.jasper.compiler.Localizer;
32 import org.apache.jasper.xmlparser.ParserUtils;
33
34 /**
35  * A class to hold all init parameters specific to the JSP engine.
36  *
37  * @author Anil K. Vijendran
38  * @author Hans Bergsten
39  * @author Pierre Delisle
40  */

41 public final class EmbeddedServletOptions implements Options {
42     
43     // Logger
44
private Log log = LogFactory.getLog(EmbeddedServletOptions.class);
45     
46     private Properties settings = new Properties();
47     
48     /**
49      * Is Jasper being used in development mode?
50      */

51     private boolean development = true;
52     
53     /**
54      * Should Ant fork its java compiles of JSP pages.
55      */

56     public boolean fork = true;
57     
58     /**
59      * Do you want to keep the generated Java files around?
60      */

61     private boolean keepGenerated = true;
62     
63     /**
64      * Should white spaces between directives or actions be trimmed?
65      */

66     private boolean trimSpaces = false;
67     
68     /**
69      * Determines whether tag handler pooling is enabled.
70      */

71     private boolean isPoolingEnabled = true;
72     
73     /**
74      * Do you want support for "mapped" files? This will generate
75      * servlet that has a print statement per line of the JSP file.
76      * This seems like a really nice feature to have for debugging.
77      */

78     private boolean mappedFile = true;
79     
80     /**
81      * Do you want stack traces and such displayed in the client's
82      * browser? If this is false, such messages go to the standard
83      * error or a log file if the standard error is redirected.
84      */

85     private boolean sendErrorToClient = false;
86     
87     /**
88      * Do we want to include debugging information in the class file?
89      */

90     private boolean classDebugInfo = true;
91     
92     /**
93      * Background compile thread check interval in seconds.
94      */

95     private int checkInterval = 0;
96     
97     /**
98      * Is the generation of SMAP info for JSR45 debuggin suppressed?
99      */

100     private boolean isSmapSuppressed = false;
101     
102     /**
103      * Should SMAP info for JSR45 debugging be dumped to a file?
104      */

105     private boolean isSmapDumped = false;
106     
107     /**
108      * Are Text strings to be generated as char arrays?
109      */

110     private boolean genStringAsCharArray = false;
111     
112     private boolean errorOnUseBeanInvalidClassAttribute = true;
113     
114     /**
115      * I want to see my generated servlets. Which directory are they
116      * in?
117      */

118     private File JavaDoc scratchDir;
119     
120     /**
121      * Need to have this as is for versions 4 and 5 of IE. Can be set from
122      * the initParams so if it changes in the future all that is needed is
123      * to have a jsp initParam of type ieClassId="<value>"
124      */

125     private String JavaDoc ieClassId = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
126     
127     /**
128      * What classpath should I use while compiling generated servlets?
129      */

130     private String JavaDoc classpath = null;
131     
132     /**
133      * Compiler to use.
134      */

135     private String JavaDoc compiler = null;
136     
137     /**
138      * Compiler target VM.
139      */

140     private String JavaDoc compilerTargetVM = "1.5";
141     
142     /**
143      * The compiler source VM.
144      */

145     private String JavaDoc compilerSourceVM = "1.5";
146     
147     /**
148      * The compiler class name.
149      */

150     private String JavaDoc compilerClassName = null;
151     
152     /**
153      * Cache for the TLD locations
154      */

155     private TldLocationsCache tldLocationsCache = null;
156     
157     /**
158      * Jsp config information
159      */

160     private JspConfig jspConfig = null;
161     
162     /**
163      * TagPluginManager
164      */

165     private TagPluginManager tagPluginManager = null;
166     
167     /**
168      * Java platform encoding to generate the JSP
169      * page servlet.
170      */

171     private String JavaDoc javaEncoding = "UTF8";
172     
173     /**
174      * Modification test interval.
175      */

176     private int modificationTestInterval = 4;
177     
178     /**
179      * Is generation of X-Powered-By response header enabled/disabled?
180      */

181     private boolean xpoweredBy;
182     
183     /**
184      * Should we include a source fragment in exception messages, which could be displayed
185      * to the developer ?
186      */

187     private boolean displaySourceFragment = true;
188
189     
190     public String JavaDoc getProperty(String JavaDoc name ) {
191         return settings.getProperty( name );
192     }
193     
194     public void setProperty(String JavaDoc name, String JavaDoc value ) {
195         if (name != null && value != null){
196             settings.setProperty( name, value );
197         }
198     }
199     
200     /**
201      * Are we keeping generated code around?
202      */

203     public boolean getKeepGenerated() {
204         return keepGenerated;
205     }
206     
207     /**
208      * Should white spaces between directives or actions be trimmed?
209      */

210     public boolean getTrimSpaces() {
211         return trimSpaces;
212     }
213     
214     public boolean isPoolingEnabled() {
215         return isPoolingEnabled;
216     }
217     
218     /**
219      * Are we supporting HTML mapped servlets?
220      */

221     public boolean getMappedFile() {
222         return mappedFile;
223     }
224     
225     /**
226      * Should errors be sent to client or thrown into stderr?
227      */

228     public boolean getSendErrorToClient() {
229         return sendErrorToClient;
230     }
231     
232     /**
233      * Should class files be compiled with debug information?
234      */

235     public boolean getClassDebugInfo() {
236         return classDebugInfo;
237     }
238     
239     /**
240      * Background JSP compile thread check intervall
241      */

242     public int getCheckInterval() {
243         return checkInterval;
244     }
245     
246     /**
247      * Modification test interval.
248      */

249     public int getModificationTestInterval() {
250         return modificationTestInterval;
251     }
252     
253     /**
254      * Is Jasper being used in development mode?
255      */

256     public boolean getDevelopment() {
257         return development;
258     }
259     
260     /**
261      * Is the generation of SMAP info for JSR45 debuggin suppressed?
262      */

263     public boolean isSmapSuppressed() {
264         return isSmapSuppressed;
265     }
266     
267     /**
268      * Should SMAP info for JSR45 debugging be dumped to a file?
269      */

270     public boolean isSmapDumped() {
271         return isSmapDumped;
272     }
273     
274     /**
275      * Are Text strings to be generated as char arrays?
276      */

277     public boolean genStringAsCharArray() {
278         return this.genStringAsCharArray;
279     }
280     
281     /**
282      * Class ID for use in the plugin tag when the browser is IE.
283      */

284     public String JavaDoc getIeClassId() {
285         return ieClassId;
286     }
287     
288     /**
289      * What is my scratch dir?
290      */

291     public File JavaDoc getScratchDir() {
292         return scratchDir;
293     }
294     
295     /**
296      * What classpath should I use while compiling the servlets
297      * generated from JSP files?
298      */

299     public String JavaDoc getClassPath() {
300         return classpath;
301     }
302     
303     /**
304      * Is generation of X-Powered-By response header enabled/disabled?
305      */

306     public boolean isXpoweredBy() {
307         return xpoweredBy;
308     }
309     
310     /**
311      * Compiler to use.
312      */

313     public String JavaDoc getCompiler() {
314         return compiler;
315     }
316     
317     /**
318      * @see Options#getCompilerTargetVM
319      */

320     public String JavaDoc getCompilerTargetVM() {
321         return compilerTargetVM;
322     }
323     
324     /**
325      * @see Options#getCompilerSourceVM
326      */

327     public String JavaDoc getCompilerSourceVM() {
328         return compilerSourceVM;
329     }
330     
331     /**
332      * Java compiler class to use.
333      */

334     public String JavaDoc getCompilerClassName() {
335         return compilerClassName;
336     }
337
338     public boolean getErrorOnUseBeanInvalidClassAttribute() {
339         return errorOnUseBeanInvalidClassAttribute;
340     }
341     
342     public void setErrorOnUseBeanInvalidClassAttribute(boolean b) {
343         errorOnUseBeanInvalidClassAttribute = b;
344     }
345     
346     public TldLocationsCache getTldLocationsCache() {
347         return tldLocationsCache;
348     }
349     
350     public void setTldLocationsCache( TldLocationsCache tldC ) {
351         tldLocationsCache = tldC;
352     }
353     
354     public String JavaDoc getJavaEncoding() {
355         return javaEncoding;
356     }
357     
358     public boolean getFork() {
359         return fork;
360     }
361     
362     public JspConfig getJspConfig() {
363         return jspConfig;
364     }
365     
366     public TagPluginManager getTagPluginManager() {
367         return tagPluginManager;
368     }
369     
370     public boolean isCaching() {
371         return false;
372     }
373     
374     public Map getCache() {
375         return null;
376     }
377
378     /**
379      * Should we include a source fragment in exception messages, which could be displayed
380      * to the developer ?
381      */

382     public boolean getDisplaySourceFragment() {
383         return displaySourceFragment;
384     }
385
386     /**
387      * Create an EmbeddedServletOptions object using data available from
388      * ServletConfig and ServletContext.
389      */

390     public EmbeddedServletOptions(ServletConfig JavaDoc config,
391             ServletContext JavaDoc context) {
392         
393         // JVM version numbers
394
try {
395             if (Float.parseFloat(System.getProperty("java.specification.version")) > 1.4) {
396                 compilerSourceVM = compilerTargetVM = "1.5";
397             } else {
398                 compilerSourceVM = compilerTargetVM = "1.4";
399             }
400         } catch (NumberFormatException JavaDoc e) {
401             // Ignore
402
}
403         
404         Enumeration enumeration=config.getInitParameterNames();
405         while( enumeration.hasMoreElements() ) {
406             String JavaDoc k=(String JavaDoc)enumeration.nextElement();
407             String JavaDoc v=config.getInitParameter( k );
408             setProperty( k, v);
409         }
410         
411         // quick hack
412
String JavaDoc validating=config.getInitParameter( "validating");
413         if( "false".equals( validating )) ParserUtils.validating=false;
414         
415         String JavaDoc keepgen = config.getInitParameter("keepgenerated");
416         if (keepgen != null) {
417             if (keepgen.equalsIgnoreCase("true")) {
418                 this.keepGenerated = true;
419             } else if (keepgen.equalsIgnoreCase("false")) {
420                 this.keepGenerated = false;
421             } else {
422                 if (log.isWarnEnabled()) {
423                     log.warn(Localizer.getMessage("jsp.warning.keepgen"));
424                 }
425             }
426         }
427         
428         
429         String JavaDoc trimsp = config.getInitParameter("trimSpaces");
430         if (trimsp != null) {
431             if (trimsp.equalsIgnoreCase("true")) {
432                 trimSpaces = true;
433             } else if (trimsp.equalsIgnoreCase("false")) {
434                 trimSpaces = false;
435             } else {
436                 if (log.isWarnEnabled()) {
437                     log.warn(Localizer.getMessage("jsp.warning.trimspaces"));
438                 }
439             }
440         }
441         
442         this.isPoolingEnabled = true;
443         String JavaDoc poolingEnabledParam
444         = config.getInitParameter("enablePooling");
445         if (poolingEnabledParam != null
446                 && !poolingEnabledParam.equalsIgnoreCase("true")) {
447             if (poolingEnabledParam.equalsIgnoreCase("false")) {
448                 this.isPoolingEnabled = false;
449             } else {
450                 if (log.isWarnEnabled()) {
451                     log.warn(Localizer.getMessage("jsp.warning.enablePooling"));
452                 }
453             }
454         }
455         
456         String JavaDoc mapFile = config.getInitParameter("mappedfile");
457         if (mapFile != null) {
458             if (mapFile.equalsIgnoreCase("true")) {
459                 this.mappedFile = true;
460             } else if (mapFile.equalsIgnoreCase("false")) {
461                 this.mappedFile = false;
462             } else {
463                 if (log.isWarnEnabled()) {
464                     log.warn(Localizer.getMessage("jsp.warning.mappedFile"));
465                 }
466             }
467         }
468         
469         String JavaDoc senderr = config.getInitParameter("sendErrToClient");
470         if (senderr != null) {
471             if (senderr.equalsIgnoreCase("true")) {
472                 this.sendErrorToClient = true;
473             } else if (senderr.equalsIgnoreCase("false")) {
474                 this.sendErrorToClient = false;
475             } else {
476                 if (log.isWarnEnabled()) {
477                     log.warn(Localizer.getMessage("jsp.warning.sendErrToClient"));
478                 }
479             }
480         }
481         
482         String JavaDoc debugInfo = config.getInitParameter("classdebuginfo");
483         if (debugInfo != null) {
484             if (debugInfo.equalsIgnoreCase("true")) {
485                 this.classDebugInfo = true;
486             } else if (debugInfo.equalsIgnoreCase("false")) {
487                 this.classDebugInfo = false;
488             } else {
489                 if (log.isWarnEnabled()) {
490                     log.warn(Localizer.getMessage("jsp.warning.classDebugInfo"));
491                 }
492             }
493         }
494         
495         String JavaDoc checkInterval = config.getInitParameter("checkInterval");
496         if (checkInterval != null) {
497             try {
498                 this.checkInterval = Integer.parseInt(checkInterval);
499                 if (this.checkInterval == 0) {
500                     this.checkInterval = 300;
501                     if (log.isWarnEnabled()) {
502                         log.warn(Localizer.getMessage("jsp.warning.checkInterval"));
503                     }
504                 }
505             } catch(NumberFormatException JavaDoc ex) {
506                 if (log.isWarnEnabled()) {
507                     log.warn(Localizer.getMessage("jsp.warning.checkInterval"));
508                 }
509             }
510         }
511         
512         String JavaDoc modificationTestInterval = config.getInitParameter("modificationTestInterval");
513         if (modificationTestInterval != null) {
514             try {
515                 this.modificationTestInterval = Integer.parseInt(modificationTestInterval);
516             } catch(NumberFormatException JavaDoc ex) {
517                 if (log.isWarnEnabled()) {
518                     log.warn(Localizer.getMessage("jsp.warning.modificationTestInterval"));
519                 }
520             }
521         }
522         
523         String JavaDoc development = config.getInitParameter("development");
524         if (development != null) {
525             if (development.equalsIgnoreCase("true")) {
526                 this.development = true;
527             } else if (development.equalsIgnoreCase("false")) {
528                 this.development = false;
529             } else {
530                 if (log.isWarnEnabled()) {
531                     log.warn(Localizer.getMessage("jsp.warning.development"));
532                 }
533             }
534         }
535         
536         String JavaDoc suppressSmap = config.getInitParameter("suppressSmap");
537         if (suppressSmap != null) {
538             if (suppressSmap.equalsIgnoreCase("true")) {
539                 isSmapSuppressed = true;
540             } else if (suppressSmap.equalsIgnoreCase("false")) {
541                 isSmapSuppressed = false;
542             } else {
543                 if (log.isWarnEnabled()) {
544                     log.warn(Localizer.getMessage("jsp.warning.suppressSmap"));
545                 }
546             }
547         }
548         
549         String JavaDoc dumpSmap = config.getInitParameter("dumpSmap");
550         if (dumpSmap != null) {
551             if (dumpSmap.equalsIgnoreCase("true")) {
552                 isSmapDumped = true;
553             } else if (dumpSmap.equalsIgnoreCase("false")) {
554                 isSmapDumped = false;
555             } else {
556                 if (log.isWarnEnabled()) {
557                     log.warn(Localizer.getMessage("jsp.warning.dumpSmap"));
558                 }
559             }
560         }
561         
562         String JavaDoc genCharArray = config.getInitParameter("genStrAsCharArray");
563         if (genCharArray != null) {
564             if (genCharArray.equalsIgnoreCase("true")) {
565                 genStringAsCharArray = true;
566             } else if (genCharArray.equalsIgnoreCase("false")) {
567                 genStringAsCharArray = false;
568             } else {
569                 if (log.isWarnEnabled()) {
570                     log.warn(Localizer.getMessage("jsp.warning.genchararray"));
571                 }
572             }
573         }
574         
575         String JavaDoc errBeanClass =
576             config.getInitParameter("errorOnUseBeanInvalidClassAttribute");
577         if (errBeanClass != null) {
578             if (errBeanClass.equalsIgnoreCase("true")) {
579                 errorOnUseBeanInvalidClassAttribute = true;
580             } else if (errBeanClass.equalsIgnoreCase("false")) {
581                 errorOnUseBeanInvalidClassAttribute = false;
582             } else {
583                 if (log.isWarnEnabled()) {
584                     log.warn(Localizer.getMessage("jsp.warning.errBean"));
585                 }
586             }
587         }
588         
589         String JavaDoc ieClassId = config.getInitParameter("ieClassId");
590         if (ieClassId != null)
591             this.ieClassId = ieClassId;
592         
593         String JavaDoc classpath = config.getInitParameter("classpath");
594         if (classpath != null)
595             this.classpath = classpath;
596         
597         /*
598          * scratchdir
599          */

600         String JavaDoc dir = config.getInitParameter("scratchdir");
601         if (dir != null) {
602             scratchDir = new File JavaDoc(dir);
603         } else {
604             // First try the Servlet 2.2 javax.servlet.context.tempdir property
605
scratchDir = (File JavaDoc) context.getAttribute(Constants.TMP_DIR);
606             if (scratchDir == null) {
607                 // Not running in a Servlet 2.2 container.
608
// Try to get the JDK 1.2 java.io.tmpdir property
609
dir = System.getProperty("java.io.tmpdir");
610                 if (dir != null)
611                     scratchDir = new File JavaDoc(dir);
612             }
613         }
614         if (this.scratchDir == null) {
615             log.fatal(Localizer.getMessage("jsp.error.no.scratch.dir"));
616             return;
617         }
618         
619         if (!(scratchDir.exists() && scratchDir.canRead() &&
620                 scratchDir.canWrite() && scratchDir.isDirectory()))
621             log.fatal(Localizer.getMessage("jsp.error.bad.scratch.dir",
622                     scratchDir.getAbsolutePath()));
623         
624         this.compiler = config.getInitParameter("compiler");
625         
626         String JavaDoc compilerTargetVM = config.getInitParameter("compilerTargetVM");
627         if(compilerTargetVM != null) {
628             this.compilerTargetVM = compilerTargetVM;
629         }
630         
631         String JavaDoc compilerSourceVM = config.getInitParameter("compilerSourceVM");
632         if(compilerSourceVM != null) {
633             this.compilerSourceVM = compilerSourceVM;
634         }
635         
636         String JavaDoc javaEncoding = config.getInitParameter("javaEncoding");
637         if (javaEncoding != null) {
638             this.javaEncoding = javaEncoding;
639         }
640         
641         String JavaDoc compilerClassName = config.getInitParameter("compilerClassName");
642         if (compilerClassName != null) {
643             this.compilerClassName = compilerClassName;
644         }
645         
646         String JavaDoc fork = config.getInitParameter("fork");
647         if (fork != null) {
648             if (fork.equalsIgnoreCase("true")) {
649                 this.fork = true;
650             } else if (fork.equalsIgnoreCase("false")) {
651                 this.fork = false;
652             } else {
653                 if (log.isWarnEnabled()) {
654                     log.warn(Localizer.getMessage("jsp.warning.fork"));
655                 }
656             }
657         }
658         
659         String JavaDoc xpoweredBy = config.getInitParameter("xpoweredBy");
660         if (xpoweredBy != null) {
661             if (xpoweredBy.equalsIgnoreCase("true")) {
662                 this.xpoweredBy = true;
663             } else if (xpoweredBy.equalsIgnoreCase("false")) {
664                 this.xpoweredBy = false;
665             } else {
666                 if (log.isWarnEnabled()) {
667                     log.warn(Localizer.getMessage("jsp.warning.xpoweredBy"));
668                 }
669             }
670         }
671         
672         String JavaDoc displaySourceFragment = config.getInitParameter("displaySourceFragment");
673         if (displaySourceFragment != null) {
674             if (displaySourceFragment.equalsIgnoreCase("true")) {
675                 this.displaySourceFragment = true;
676             } else if (displaySourceFragment.equalsIgnoreCase("false")) {
677                 this.displaySourceFragment = false;
678             } else {
679                 if (log.isWarnEnabled()) {
680                     log.warn(Localizer.getMessage("jsp.warning.displaySourceFragment"));
681                 }
682             }
683         }
684         
685         // Setup the global Tag Libraries location cache for this
686
// web-application.
687
tldLocationsCache = new TldLocationsCache(context);
688         
689         // Setup the jsp config info for this web app.
690
jspConfig = new JspConfig(context);
691         
692         // Create a Tag plugin instance
693
tagPluginManager = new TagPluginManager(context);
694     }
695     
696 }
697
698
Popular Tags