KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

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

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

49 public class JspServletOptions
50    implements Options
51 {
52    static Logger log = Logger.getLogger(JspServletOptions.class);
53
54    private Properties JavaDoc settings = new Properties JavaDoc();
55
56    /**
57     * Is Jasper being used in development mode?
58     */

59    private boolean development = 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     * Compiler target VM.
146     */

147    private String JavaDoc compilerTargetVM = "1.4";
148
149    /**
150     * The compiler source VM.
151     */

152    private String JavaDoc compilerSourceVM = "1.4";
153
154    /**
155     * Cache for the TLD locations
156     */

157    private TldLocationsCache tldLocationsCache = null;
158
159    /**
160     * Jsp config information
161     */

162    private JspConfig jspConfig = null;
163
164    /**
165     * TagPluginManager
166     */

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

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

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

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

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

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

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

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

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

245    public int getCheckInterval()
246    {
247       return checkInterval;
248    }
249
250    /**
251     * Modification test interval.
252     */

253    public int getModificationTestInterval()
254    {
255       return modificationTestInterval;
256    }
257
258    /**
259     * Is Jasper being used in development mode?
260     */

261    public boolean getDevelopment()
262    {
263       return development;
264    }
265
266    /**
267     * Is the generation of SMAP info for JSR45 debuggin suppressed?
268     */

269    public boolean isSmapSuppressed()
270    {
271       return isSmapSuppressed;
272    }
273
274    /**
275     * Should SMAP info for JSR45 debugging be dumped to a file?
276     */

277    public boolean isSmapDumped()
278    {
279       return isSmapDumped;
280    }
281
282    /**
283     * Are Text strings to be generated as char arrays?
284     */

285    public boolean genStringAsCharArray()
286    {
287       return this.genStringAsCharArray;
288    }
289
290    /**
291     * Class ID for use in the plugin tag when the browser is IE.
292     */

293    public String JavaDoc getIeClassId()
294    {
295       return ieClassId;
296    }
297
298    /**
299     * What is my scratch dir?
300     */

301    public File JavaDoc getScratchDir()
302    {
303       return scratchDir;
304    }
305
306    /**
307     * What classpath should I use while compiling the servlets generated from
308     * JSP files?
309     */

310    public String JavaDoc getClassPath()
311    {
312       return classpath;
313    }
314
315    /**
316     * Is generation of X-Powered-By response header enabled/disabled?
317     */

318    public boolean isXpoweredBy()
319    {
320       return xpoweredBy;
321    }
322
323    /**
324     * Compiler to use.
325     */

326    public String JavaDoc getCompiler()
327    {
328       return compiler;
329    }
330
331    /**
332     * @see Options#getCompilerTargetVM
333     */

334    public String JavaDoc getCompilerTargetVM()
335    {
336       return compilerTargetVM;
337    }
338
339    /**
340     * @see Options#getCompilerSourceVM
341     */

342    public String JavaDoc getCompilerSourceVM()
343    {
344       return compilerSourceVM;
345    }
346
347    public boolean getErrorOnUseBeanInvalidClassAttribute()
348    {
349       return errorOnUseBeanInvalidClassAttribute;
350    }
351
352    public void setErrorOnUseBeanInvalidClassAttribute(boolean b)
353    {
354       errorOnUseBeanInvalidClassAttribute = b;
355    }
356
357    public TldLocationsCache getTldLocationsCache()
358    {
359       return tldLocationsCache;
360    }
361
362    public void setTldLocationsCache(TldLocationsCache tldC)
363    {
364       tldLocationsCache = tldC;
365    }
366
367    public String JavaDoc getJavaEncoding()
368    {
369       return javaEncoding;
370    }
371
372    public boolean getFork()
373    {
374       return fork;
375    }
376
377    public JspConfig getJspConfig()
378    {
379       return jspConfig;
380    }
381
382    public TagPluginManager getTagPluginManager()
383    {
384       return tagPluginManager;
385    }
386
387    /**
388     * Create an EmbeddedServletOptions object using data available from
389     * ServletConfig and ServletContext.
390     */

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

636       String JavaDoc dir = config.getInitParameter("scratchdir");
637       if (dir != null)
638       {
639          scratchDir = new File JavaDoc(dir);
640       }
641       else
642       {
643          // First try the Servlet 2.2 javax.servlet.context.tempdir property
644
scratchDir = (File JavaDoc) context.getAttribute(Constants.TMP_DIR);
645          if (scratchDir == null)
646          {
647             // Not running in a Servlet 2.2 container.
648
// Try to get the JDK 1.2 java.io.tmpdir property
649
dir = System.getProperty("java.io.tmpdir");
650             if (dir != null)
651                scratchDir = new File JavaDoc(dir);
652          }
653       }
654       if (this.scratchDir == null)
655       {
656          log.fatal(Localizer.getMessage("jsp.error.no.scratch.dir"));
657          return;
658       }
659
660       if (!(scratchDir.exists() && scratchDir.canRead() &&
661          scratchDir.canWrite() && scratchDir.isDirectory()))
662          log.fatal(Localizer.getMessage("jsp.error.bad.scratch.dir",
663             scratchDir.getAbsolutePath()));
664
665       this.compiler = config.getInitParameter("compiler");
666
667       String JavaDoc compilerTargetVM = config.getInitParameter("compilerTargetVM");
668       if (compilerTargetVM != null)
669       {
670          this.compilerTargetVM = compilerTargetVM;
671       }
672
673       String JavaDoc compilerSourceVM = config.getInitParameter("compilerSourceVM");
674       if (compilerSourceVM != null)
675       {
676          this.compilerSourceVM = compilerSourceVM;
677       }
678
679       String JavaDoc javaEncoding = config.getInitParameter("javaEncoding");
680       if (javaEncoding != null)
681       {
682          this.javaEncoding = javaEncoding;
683       }
684
685       String JavaDoc fork = config.getInitParameter("fork");
686       if (fork != null)
687       {
688          if (fork.equalsIgnoreCase("true"))
689          {
690             this.fork = true;
691          }
692          else if (fork.equalsIgnoreCase("false"))
693          {
694             this.fork = false;
695          }
696          else
697          {
698             log.warn(Localizer.getMessage("jsp.warning.fork"));
699          }
700       }
701
702       String JavaDoc xpoweredBy = config.getInitParameter("xpoweredBy");
703       if (xpoweredBy != null)
704       {
705          if (xpoweredBy.equalsIgnoreCase("true"))
706          {
707             this.xpoweredBy = true;
708          }
709          else if (xpoweredBy.equalsIgnoreCase("false"))
710          {
711             this.xpoweredBy = false;
712          }
713          else
714          {
715             log.warn(Localizer.getMessage("jsp.warning.xpoweredBy"));
716          }
717       }
718
719       /* Setup the global Tag Libraries location cache for this web app. The
720          tagLibJarN entries define the jars the cache should scan.
721       */

722       String JavaDoc base = "tagLibJar";
723       ArrayList JavaDoc tldJars = new ArrayList JavaDoc();
724       int count = 0;
725       String JavaDoc jarPath = null;
726       do
727       {
728          String JavaDoc name = base + count;
729          jarPath = config.getInitParameter(name);
730          if( jarPath != null )
731             tldJars.add(jarPath);
732          count ++;
733       } while( jarPath != null );
734       
735       tldLocationsCache = new TagLibCache(context, tldJars);
736
737       // Setup the jsp config info for this web app.
738
jspConfig = new JspConfig(context);
739
740       // Create a Tag plugin instance
741
tagPluginManager = new TagPluginManager(context);
742    }
743
744 }
745
Popular Tags