KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > jasper > JspServletOptions


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

16 package org.jboss.web.tomcat.tc6.jasper;
17
18 import java.util.Properties JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.io.File JavaDoc;
23
24 import javax.servlet.ServletConfig JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26
27 import org.apache.jasper.Options;
28 import org.apache.jasper.Constants;
29 import org.apache.jasper.xmlparser.ParserUtils;
30 import org.apache.jasper.compiler.TldLocationsCache;
31 import org.apache.jasper.compiler.JspConfig;
32 import org.apache.jasper.compiler.TagPluginManager;
33 import org.apache.jasper.compiler.Localizer;
34 import org.jboss.logging.Logger;
35
36 /**
37  * Override the default JspServletOptions to externalize the jsp layer
38  * configuration. This overrides the default TagLibCache to the jboss version.
39  *
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 58265 $
42  */

43 public class JspServletOptions
44    implements Options
45 {
46    static Logger log = Logger.getLogger(JspServletOptions.class);
47
48    private Properties JavaDoc settings = new Properties JavaDoc();
49
50    /**
51     * Is Jasper being used in development mode?
52     */

53    private boolean development = true;
54
55    /**
56     * Should we include a source fragment in exception messages, which could be displayed
57     * to the developer ?
58     */

59    private boolean displaySourceFragment = true;
60
61    /**
62     * Should Ant fork its java compiles of JSP pages.
63     */

64    public boolean fork = true;
65
66    /**
67     * Do you want to keep the generated Java files around?
68     */

69    private boolean keepGenerated = true;
70
71    /**
72     * Should white spaces between directives or actions be trimmed?
73     */

74    private boolean trimSpaces = false;
75
76    /**
77     * Determines whether tag handler pooling is enabled.
78     */

79    private boolean isPoolingEnabled = true;
80
81    /**
82     * Do you want support for "mapped" files? This will generate servlet that
83     * has a print statement per line of the JSP file. This seems like a really
84     * nice feature to have for debugging.
85     */

86    private boolean mappedFile = true;
87
88    /**
89     * Do you want stack traces and such displayed in the client's browser? If
90     * this is false, such messages go to the standard error or a log file if the
91     * standard error is redirected.
92     */

93    private boolean sendErrorToClient = false;
94
95    /**
96     * Do we want to include debugging information in the class file?
97     */

98    private boolean classDebugInfo = true;
99
100    /**
101     * Background compile thread check interval in seconds.
102     */

103    private int checkInterval = 0;
104
105    /**
106     * Is the generation of SMAP info for JSR45 debuggin suppressed?
107     */

108    private boolean isSmapSuppressed = false;
109
110    /**
111     * Should SMAP info for JSR45 debugging be dumped to a file?
112     */

113    private boolean isSmapDumped = false;
114
115    /**
116     * Are Text strings to be generated as char arrays?
117     */

118    private boolean genStringAsCharArray = false;
119
120    private boolean errorOnUseBeanInvalidClassAttribute = true;
121
122    /**
123     * I want to see my generated servlets. Which directory are they in?
124     */

125    private File JavaDoc scratchDir;
126
127    /**
128     * Need to have this as is for versions 4 and 5 of IE. Can be set from the
129     * initParams so if it changes in the future all that is needed is to have a
130     * jsp initParam of type ieClassId="<value>"
131     */

132    private String JavaDoc ieClassId = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
133
134    /**
135     * What classpath should I use while compiling generated servlets?
136     */

137    private String JavaDoc classpath = null;
138
139    /**
140     * Compiler to use.
141     */

142    private String JavaDoc compiler = null;
143
144    /**
145     * The compiler class name.
146     */

147    private String JavaDoc compilerClassName = null;
148    
149    /**
150     * Compiler target VM.
151     */

152    private String JavaDoc compilerTargetVM = "1.4";
153
154    /**
155     * The compiler source VM.
156     */

157    private String JavaDoc compilerSourceVM = "1.4";
158
159    /**
160     * Cache for the TLD locations
161     */

162    private TldLocationsCache tldLocationsCache = null;
163
164    /**
165     * Jsp config information
166     */

167    private JspConfig jspConfig = null;
168
169    /**
170     * TagPluginManager
171     */

172    private TagPluginManager tagPluginManager = null;
173
174    /**
175     * Java platform encoding to generate the JSP page servlet.
176     */

177    private String JavaDoc javaEncoding = "UTF8";
178
179    /**
180     * Modification test interval.
181     */

182    private int modificationTestInterval = 4;
183
184    private boolean ignoreAnnotations;
185
186
187
188    /**
189     * Is generation of X-Powered-By response header enabled/disabled?
190     */

191    private boolean xpoweredBy;
192
193
194    public boolean getIgnoreAnnotations()
195    {
196       return ignoreAnnotations;
197    }
198
199    public void setIgnoreAnnotations(boolean ignoreAnnotations)
200    {
201       this.ignoreAnnotations = ignoreAnnotations;
202    }
203
204    public String JavaDoc getProperty(String JavaDoc name)
205    {
206       return settings.getProperty(name);
207    }
208
209    public void setProperty(String JavaDoc name, String JavaDoc value)
210    {
211       if (name != null && value != null)
212       {
213          settings.setProperty(name, value);
214       }
215    }
216
217    /**
218     * Are we keeping generated code around?
219     */

220    public boolean getKeepGenerated()
221    {
222       return keepGenerated;
223    }
224
225    /**
226     * Should white spaces between directives or actions be trimmed?
227     */

228    public boolean getTrimSpaces()
229    {
230       return trimSpaces;
231    }
232
233    public boolean isPoolingEnabled()
234    {
235       return isPoolingEnabled;
236    }
237
238    /**
239     * Are we supporting HTML mapped servlets?
240     */

241    public boolean getMappedFile()
242    {
243       return mappedFile;
244    }
245
246    /**
247     * Should errors be sent to client or thrown into stderr?
248     */

249    public boolean getSendErrorToClient()
250    {
251       return sendErrorToClient;
252    }
253
254    /**
255     * Should class files be compiled with debug information?
256     */

257    public boolean getClassDebugInfo()
258    {
259       return classDebugInfo;
260    }
261
262    /**
263     * Background JSP compile thread check intervall
264     */

265    public int getCheckInterval()
266    {
267       return checkInterval;
268    }
269
270    /**
271     * Modification test interval.
272     */

273    public int getModificationTestInterval()
274    {
275       return modificationTestInterval;
276    }
277
278    /**
279     * Is Jasper being used in development mode?
280     */

281    public boolean getDevelopment()
282    {
283       return development;
284    }
285
286    public boolean getDisplaySourceFragment()
287    {
288       // TODO Auto-generated method stub
289
return displaySourceFragment;
290    }
291
292    /**
293     * Is the generation of SMAP info for JSR45 debuggin suppressed?
294     */

295    public boolean isSmapSuppressed()
296    {
297       return isSmapSuppressed;
298    }
299
300    /**
301     * Should SMAP info for JSR45 debugging be dumped to a file?
302     */

303    public boolean isSmapDumped()
304    {
305       return isSmapDumped;
306    }
307
308    /**
309     * Are Text strings to be generated as char arrays?
310     */

311    public boolean genStringAsCharArray()
312    {
313       return this.genStringAsCharArray;
314    }
315
316    /**
317     * Class ID for use in the plugin tag when the browser is IE.
318     */

319    public String JavaDoc getIeClassId()
320    {
321       return ieClassId;
322    }
323
324    /**
325     * What is my scratch dir?
326     */

327    public File JavaDoc getScratchDir()
328    {
329       return scratchDir;
330    }
331
332    /**
333     * What classpath should I use while compiling the servlets generated from
334     * JSP files?
335     */

336    public String JavaDoc getClassPath()
337    {
338       return classpath;
339    }
340
341    /**
342     * Is generation of X-Powered-By response header enabled/disabled?
343     */

344    public boolean isXpoweredBy()
345    {
346       return xpoweredBy;
347    }
348
349    /**
350     * Compiler to use.
351     */

352    public String JavaDoc getCompiler()
353    {
354       return compiler;
355    }
356
357    /**
358     * @see Options#getCompilerClassName
359     */

360    public String JavaDoc getCompilerClassName()
361    {
362       return compilerClassName;
363    }
364    
365    /**
366     * @see Options#getCompilerTargetVM
367     */

368    public String JavaDoc getCompilerTargetVM()
369    {
370       return compilerTargetVM;
371    }
372
373    /**
374     * @see Options#getCompilerSourceVM
375     */

376    public String JavaDoc getCompilerSourceVM()
377    {
378       return compilerSourceVM;
379    }
380
381    public boolean getErrorOnUseBeanInvalidClassAttribute()
382    {
383       return errorOnUseBeanInvalidClassAttribute;
384    }
385
386    public void setErrorOnUseBeanInvalidClassAttribute(boolean b)
387    {
388       errorOnUseBeanInvalidClassAttribute = b;
389    }
390
391    public TldLocationsCache getTldLocationsCache()
392    {
393       return tldLocationsCache;
394    }
395
396    public void setTldLocationsCache(TldLocationsCache tldC)
397    {
398       tldLocationsCache = tldC;
399    }
400
401    public String JavaDoc getJavaEncoding()
402    {
403       return javaEncoding;
404    }
405
406    public boolean getFork()
407    {
408       return fork;
409    }
410
411    public JspConfig getJspConfig()
412    {
413       return jspConfig;
414    }
415
416    public TagPluginManager getTagPluginManager()
417    {
418       return tagPluginManager;
419    }
420
421    /**
422     * Is caching enabled (used for precompilation).
423     */

424    public boolean isCaching()
425    {
426       return false;
427    }
428     
429    /**
430     * The web-application wide cache for the returned TreeNode
431     * by parseXMLDocument in TagLibraryInfoImpl.parseTLD,
432     * if isCaching returns true.
433     *
434     * @return the Map(String uri, TreeNode tld) instance.
435     */

436    public Map JavaDoc getCache()
437    {
438       return null;
439    }
440
441    /**
442     * Create an EmbeddedServletOptions object using data available from
443     * ServletConfig and ServletContext.
444     */

445    public JspServletOptions(ServletConfig JavaDoc config, ServletContext JavaDoc context)
446    {
447
448       Enumeration JavaDoc enumeration = config.getInitParameterNames();
449       while (enumeration.hasMoreElements())
450       {
451          String JavaDoc k = (String JavaDoc) enumeration.nextElement();
452          String JavaDoc v = config.getInitParameter(k);
453          setProperty(k, v);
454       }
455
456       // quick hack
457
String JavaDoc validating = config.getInitParameter("validating");
458       if ("false".equals(validating)) ParserUtils.validating = false;
459
460       String JavaDoc keepgen = config.getInitParameter("keepgenerated");
461       if (keepgen != null)
462       {
463          if (keepgen.equalsIgnoreCase("true"))
464          {
465             this.keepGenerated = true;
466          }
467          else if (keepgen.equalsIgnoreCase("false"))
468          {
469             this.keepGenerated = false;
470          }
471          else
472          {
473             log.warn(Localizer.getMessage("jsp.warning.keepgen"));
474          }
475       }
476
477
478       String JavaDoc trimsp = config.getInitParameter("trimSpaces");
479       if (trimsp != null)
480       {
481          if (trimsp.equalsIgnoreCase("true"))
482          {
483             trimSpaces = true;
484          }
485          else if (trimsp.equalsIgnoreCase("false"))
486          {
487             trimSpaces = false;
488          }
489          else
490          {
491             log.warn(Localizer.getMessage("jsp.warning.trimspaces"));
492          }
493       }
494
495       this.isPoolingEnabled = true;
496       String JavaDoc poolingEnabledParam
497          = config.getInitParameter("enablePooling");
498       if (poolingEnabledParam != null
499          && !poolingEnabledParam.equalsIgnoreCase("true"))
500       {
501          if (poolingEnabledParam.equalsIgnoreCase("false"))
502          {
503             this.isPoolingEnabled = false;
504          }
505          else
506          {
507             log.warn(Localizer.getMessage("jsp.warning.enablePooling"));
508          }
509       }
510
511       String JavaDoc mapFile = config.getInitParameter("mappedfile");
512       if (mapFile != null)
513       {
514          if (mapFile.equalsIgnoreCase("true"))
515          {
516             this.mappedFile = true;
517          }
518          else if (mapFile.equalsIgnoreCase("false"))
519          {
520             this.mappedFile = false;
521          }
522          else
523          {
524             log.warn(Localizer.getMessage("jsp.warning.mappedFile"));
525          }
526       }
527
528       String JavaDoc senderr = config.getInitParameter("sendErrToClient");
529       if (senderr != null)
530       {
531          if (senderr.equalsIgnoreCase("true"))
532          {
533             this.sendErrorToClient = true;
534          }
535          else if (senderr.equalsIgnoreCase("false"))
536          {
537             this.sendErrorToClient = false;
538          }
539          else
540          {
541             log.warn(Localizer.getMessage("jsp.warning.sendErrToClient"));
542          }
543       }
544
545       String JavaDoc debugInfo = config.getInitParameter("classdebuginfo");
546       if (debugInfo != null)
547       {
548          if (debugInfo.equalsIgnoreCase("true"))
549          {
550             this.classDebugInfo = true;
551          }
552          else if (debugInfo.equalsIgnoreCase("false"))
553          {
554             this.classDebugInfo = false;
555          }
556          else
557          {
558             log.warn(Localizer.getMessage("jsp.warning.classDebugInfo"));
559          }
560       }
561
562       String JavaDoc checkInterval = config.getInitParameter("checkInterval");
563       if (checkInterval != null)
564       {
565          try
566          {
567             this.checkInterval = Integer.parseInt(checkInterval);
568             if (this.checkInterval == 0)
569             {
570                this.checkInterval = 300;
571                log.warn(Localizer.getMessage("jsp.warning.checkInterval"));
572             }
573          }
574          catch (NumberFormatException JavaDoc ex)
575          {
576             log.warn(Localizer.getMessage("jsp.warning.checkInterval"));
577          }
578       }
579
580       String JavaDoc modificationTestInterval = config.getInitParameter("modificationTestInterval");
581       if (modificationTestInterval != null)
582       {
583          try
584          {
585             this.modificationTestInterval = Integer.parseInt(modificationTestInterval);
586          }
587          catch (NumberFormatException JavaDoc ex)
588          {
589             log.warn(Localizer.getMessage("jsp.warning.modificationTestInterval"));
590          }
591       }
592
593       String JavaDoc development = config.getInitParameter("development");
594       if (development != null)
595       {
596          if (development.equalsIgnoreCase("true"))
597          {
598             this.development = true;
599          }
600          else if (development.equalsIgnoreCase("false"))
601          {
602             this.development = false;
603          }
604          else
605          {
606             log.warn(Localizer.getMessage("jsp.warning.development"));
607          }
608       }
609
610       String JavaDoc displaySourceFragment = config.getInitParameter("displaySourceFragment");
611       if (displaySourceFragment != null)
612       {
613           if (displaySourceFragment.equalsIgnoreCase("true"))
614           {
615               this.displaySourceFragment = true;
616           }
617           else if (displaySourceFragment.equalsIgnoreCase("false"))
618           {
619               this.displaySourceFragment = false;
620           }
621           else
622           {
623               log.warn(Localizer.getMessage("jsp.warning.displaySourceFragment"));
624           }
625       }
626
627       String JavaDoc suppressSmap = config.getInitParameter("suppressSmap");
628       if (suppressSmap != null)
629       {
630          if (suppressSmap.equalsIgnoreCase("true"))
631          {
632             isSmapSuppressed = true;
633          }
634          else if (suppressSmap.equalsIgnoreCase("false"))
635          {
636             isSmapSuppressed = false;
637          }
638          else
639          {
640             log.warn(Localizer.getMessage("jsp.warning.suppressSmap"));
641          }
642       }
643
644       String JavaDoc dumpSmap = config.getInitParameter("dumpSmap");
645       if (dumpSmap != null)
646       {
647          if (dumpSmap.equalsIgnoreCase("true"))
648          {
649             isSmapDumped = true;
650          }
651          else if (dumpSmap.equalsIgnoreCase("false"))
652          {
653             isSmapDumped = false;
654          }
655          else
656          {
657             log.warn(Localizer.getMessage("jsp.warning.dumpSmap"));
658          }
659       }
660
661       String JavaDoc genCharArray = config.getInitParameter("genStrAsCharArray");
662       if (genCharArray != null)
663       {
664          if (genCharArray.equalsIgnoreCase("true"))
665          {
666             genStringAsCharArray = true;
667          }
668          else if (genCharArray.equalsIgnoreCase("false"))
669          {
670             genStringAsCharArray = false;
671          }
672          else
673          {
674             log.warn(Localizer.getMessage("jsp.warning.genchararray"));
675          }
676       }
677
678       String JavaDoc errBeanClass =
679          config.getInitParameter("errorOnUseBeanInvalidClassAttribute");
680       if (errBeanClass != null)
681       {
682          if (errBeanClass.equalsIgnoreCase("true"))
683          {
684             errorOnUseBeanInvalidClassAttribute = true;
685          }
686          else if (errBeanClass.equalsIgnoreCase("false"))
687          {
688             errorOnUseBeanInvalidClassAttribute = false;
689          }
690          else
691          {
692             log.warn(Localizer.getMessage("jsp.warning.errBean"));
693          }
694       }
695
696       String JavaDoc ieClassId = config.getInitParameter("ieClassId");
697       if (ieClassId != null)
698          this.ieClassId = ieClassId;
699
700       String JavaDoc classpath = config.getInitParameter("classpath");
701       if (classpath != null)
702          this.classpath = classpath;
703
704       /*
705        * scratchdir
706        */

707       String JavaDoc dir = config.getInitParameter("scratchdir");
708       if (dir != null)
709       {
710          scratchDir = new File JavaDoc(dir);
711       }
712       else
713       {
714          // First try the Servlet 2.2 javax.servlet.context.tempdir property
715
scratchDir = (File JavaDoc) context.getAttribute(Constants.TMP_DIR);
716          if (scratchDir == null)
717          {
718             // Not running in a Servlet 2.2 container.
719
// Try to get the JDK 1.2 java.io.tmpdir property
720
dir = System.getProperty("java.io.tmpdir");
721             if (dir != null)
722                scratchDir = new File JavaDoc(dir);
723          }
724       }
725       if (this.scratchDir == null)
726       {
727          log.fatal(Localizer.getMessage("jsp.error.no.scratch.dir"));
728          return;
729       }
730
731       if (!(scratchDir.exists() && scratchDir.canRead() &&
732          scratchDir.canWrite() && scratchDir.isDirectory()))
733          log.fatal(Localizer.getMessage("jsp.error.bad.scratch.dir",
734             scratchDir.getAbsolutePath()));
735
736       this.compiler = config.getInitParameter("compiler");
737
738       String JavaDoc compilerTargetVM = config.getInitParameter("compilerTargetVM");
739       if (compilerTargetVM != null)
740       {
741          this.compilerTargetVM = compilerTargetVM;
742       }
743
744       String JavaDoc compilerSourceVM = config.getInitParameter("compilerSourceVM");
745       if (compilerSourceVM != null)
746       {
747          this.compilerSourceVM = compilerSourceVM;
748       }
749
750       String JavaDoc javaEncoding = config.getInitParameter("javaEncoding");
751       if (javaEncoding != null)
752       {
753          this.javaEncoding = javaEncoding;
754       }
755
756       String JavaDoc compilerClassName = config.getInitParameter("compilerClassName");
757       if(compilerClassName != null)
758       {
759          this.compilerClassName = compilerClassName;
760       }
761       
762       String JavaDoc fork = config.getInitParameter("fork");
763       if (fork != null)
764       {
765          if (fork.equalsIgnoreCase("true"))
766          {
767             this.fork = true;
768          }
769          else if (fork.equalsIgnoreCase("false"))
770          {
771             this.fork = false;
772          }
773          else
774          {
775             log.warn(Localizer.getMessage("jsp.warning.fork"));
776          }
777       }
778
779       String JavaDoc xpoweredBy = config.getInitParameter("xpoweredBy");
780       if (xpoweredBy != null)
781       {
782          if (xpoweredBy.equalsIgnoreCase("true"))
783          {
784             this.xpoweredBy = true;
785          }
786          else if (xpoweredBy.equalsIgnoreCase("false"))
787          {
788             this.xpoweredBy = false;
789          }
790          else
791          {
792             log.warn(Localizer.getMessage("jsp.warning.xpoweredBy"));
793          }
794       }
795
796       /* Setup the global Tag Libraries location cache for this web app. The
797          tagLibJarN entries define the jars the cache should scan.
798       */

799       String JavaDoc base = "tagLibJar";
800       ArrayList JavaDoc tldJars = new ArrayList JavaDoc();
801       int count = 0;
802       String JavaDoc jarPath = null;
803       do
804       {
805          String JavaDoc name = base + count;
806          jarPath = config.getInitParameter(name);
807          if( jarPath != null )
808             tldJars.add(jarPath);
809          count ++;
810       } while( jarPath != null );
811       
812       tldLocationsCache = new TagLibCache(context, tldJars);
813
814       // Setup the jsp config info for this web app.
815
jspConfig = new JspConfig(context);
816
817       // Create a Tag plugin instance
818
tagPluginManager = new TagPluginManager(context);
819    }
820
821 }
822
Popular Tags