KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > WebModule


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import com.sun.enterprise.deployment.runtime.web.SunWebApp;
38 import com.sun.enterprise.deployment.runtime.web.LocaleCharsetInfo;
39 import com.sun.enterprise.deployment.runtime.web.LocaleCharsetMap;
40 import com.sun.logging.LogDomains;
41 import org.apache.catalina.LifecycleException;
42 import org.apache.catalina.Pipeline;
43 import org.apache.catalina.Request;
44 import org.apache.catalina.Response;
45 import org.apache.catalina.Valve;
46 import org.apache.catalina.Wrapper;
47 import org.apache.catalina.core.StandardWrapper;
48 import org.apache.catalina.core.StandardPipeline;
49 import org.apache.catalina.deploy.FilterMap;
50 import com.sun.enterprise.deployment.web.ServletFilterMapping;
51
52 /**
53  * Class representing a web module for use by the Application Server.
54  */

55
56 public class WebModule extends PwcWebModule {
57
58     // ----------------------------------------------------- Class Variables
59

60     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
61
62     // ----------------------------------------------------- Instance Variables
63

64     // Object containing sun-web.xml information
65
private SunWebApp iasBean = null;
66
67     //locale-charset-info tag from sun-web.xml
68
private LocaleCharsetMap[] _lcMap = null;
69     
70     /**
71      * Is the default-web.xml parsed?
72      */

73     private boolean hasBeenXmlConfigured = false;
74     
75     private WebContainer webContainer;
76
77     private HashMap JavaDoc<String JavaDoc,AdHocServletInfo> adHocPaths;
78     private boolean hasAdHocPaths;
79
80     private HashMap JavaDoc<String JavaDoc,AdHocServletInfo> adHocSubtrees;
81     private boolean hasAdHocSubtrees;
82
83     private StandardPipeline adHocPipeline;
84
85     // File encoding of static resources
86
private String JavaDoc fileEncoding;
87     
88     /**
89      * Cached findXXX results
90      */

91     protected Object JavaDoc[] cachedFinds;
92     
93
94     /**
95      * Constructor.
96      *
97      * @param webContainer Web container on which this web module is deployed
98      */

99     public WebModule(WebContainer webContainer) {
100
101         this.webContainer = webContainer;
102
103         this.adHocPaths = new HashMap JavaDoc<String JavaDoc,AdHocServletInfo>();
104         this.adHocSubtrees = new HashMap JavaDoc<String JavaDoc,AdHocServletInfo>();
105
106         this.adHocPipeline = new StandardPipeline(this);
107         this.adHocPipeline.setBasic(new AdHocContextValve(this));
108     }
109
110     
111     /**
112      * set the sun-web.xml config bean
113      */

114     public void setIasWebAppConfigBean(SunWebApp iasBean) {
115        this.iasBean = iasBean;
116     }
117
118
119     /**
120      * gets the sun-web.xml config bean
121      */

122     public SunWebApp getIasWebAppConfigBean() {
123        return iasBean;
124     }
125
126
127     /**
128      * Sets the parameter encoding (i18n) info from sun-web.xml.
129      */

130     public void setI18nInfo() {
131
132         if (iasBean == null) {
133             return;
134         }
135
136         if (iasBean.isParameterEncoding()) {
137             formHintField = (String JavaDoc) iasBean.getAttributeValue(
138                                                 SunWebApp.PARAMETER_ENCODING,
139                                                 SunWebApp.FORM_HINT_FIELD);
140             defaultCharset = (String JavaDoc) iasBean.getAttributeValue(
141                                                 SunWebApp.PARAMETER_ENCODING,
142                                                 SunWebApp.DEFAULT_CHARSET);
143         }
144
145         LocaleCharsetInfo lcinfo = iasBean.getLocaleCharsetInfo();
146         if (lcinfo != null) {
147             if (lcinfo.getAttributeValue(
148                             LocaleCharsetInfo.DEFAULT_LOCALE) != null) {
149                logger.warning("webmodule.default_locale_deprecated");
150             }
151             /*
152              * <parameter-encoding> subelem of <sun-web-app> takes precedence
153              * over that of <locale-charset-info>
154              */

155             if (lcinfo.isParameterEncoding()
156                     && !iasBean.isParameterEncoding()) {
157                 formHintField = (String JavaDoc) lcinfo.getAttributeValue(
158                                         LocaleCharsetInfo.PARAMETER_ENCODING,
159                                         LocaleCharsetInfo.FORM_HINT_FIELD);
160                 defaultCharset = (String JavaDoc) lcinfo.getAttributeValue(
161                                         LocaleCharsetInfo.PARAMETER_ENCODING,
162                                         LocaleCharsetInfo.DEFAULT_CHARSET);
163             }
164             _lcMap = lcinfo.getLocaleCharsetMap();
165         }
166     }
167
168
169     /**
170      * return locale-charset-map
171      */

172     public LocaleCharsetMap[] getLocaleCharsetMap() {
173         return _lcMap;
174     }
175
176
177     /**
178      * Returns true if this web module specifies a locale-charset-map in its
179      * sun-web.xml, false otherwise.
180      *
181      * @return true if this web module specifies a locale-charset-map in its
182      * sun-web.xml, false otherwise
183      */

184     public boolean hasLocaleToCharsetMapping() {
185         LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
186         return (locCharsetMap != null && locCharsetMap.length > 0);
187     }
188
189
190     /**
191      * Matches the given request locales against the charsets specified in
192      * the locale-charset-map of this web module's sun-web.xml, and returns
193      * the first matching charset.
194      *
195      * @param locales Request locales
196      *
197      * @return First matching charset, or null if this web module does not
198      * specify any locale-charset-map in its sun-web.xml, or no match was
199      * found
200      */

201     public String JavaDoc mapLocalesToCharset(Enumeration JavaDoc locales) {
202
203         String JavaDoc encoding = null;
204
205         LocaleCharsetMap[] locCharsetMap = getLocaleCharsetMap();
206         if (locCharsetMap != null && locCharsetMap.length > 0) {
207             /*
208              * Check to see if there is a match between the request
209              * locales (in preference order) and the locales in the
210              * locale-charset-map.
211              */

212             boolean matchFound = false;
213             while (locales.hasMoreElements() && !matchFound) {
214                 Locale JavaDoc reqLoc = (Locale JavaDoc) locales.nextElement();
215                 for (int i=0; i<locCharsetMap.length && !matchFound; i++) {
216                     String JavaDoc language = locCharsetMap[i].getAttributeValue(
217                                                 LocaleCharsetMap.LOCALE);
218                     if (language == null || language.equals("")) {
219                         continue;
220                     }
221                     String JavaDoc country = null;
222                     int index = language.indexOf('_');
223                     if (index != -1) {
224                         country = language.substring(index+1);
225                         language = language.substring(0, index);
226                     }
227                     Locale JavaDoc mapLoc = null;
228                     if (country != null) {
229                         mapLoc = new Locale JavaDoc(language, country);
230                     } else {
231                         mapLoc = new Locale JavaDoc(language);
232                     }
233                     if (mapLoc.equals(reqLoc)) {
234                         /*
235                          * Match found. Get the charset to which the
236                          * matched locale maps.
237                          */

238                         encoding = locCharsetMap[i].getAttributeValue(
239                                                     LocaleCharsetMap.CHARSET);
240                         matchFound = true;
241                     }
242                 }
243             }
244         }
245
246         return encoding;
247     }
248
249     
250     /**
251      * Set to <code>true</code> when the default-web.xml has been read for
252      * this module.
253      */

254     public void setXmlConfigured(boolean hasBeenXmlConfigured){
255         this.hasBeenXmlConfigured = hasBeenXmlConfigured;
256     }
257     
258     
259     /**
260      * Return <code>true</code> if the default=web.xml has been read for
261      * this module.
262      */

263     public boolean hasBeenXmlConfigured(){
264         return hasBeenXmlConfigured;
265     }
266     
267     
268     /**
269      * Cache the result of doing findXX on this object
270      * NOTE: this method MUST be used only when loading/using
271      * the content of default-web.xml
272      */

273     public void setCachedFindOperation(Object JavaDoc[] cachedFinds){
274         this.cachedFinds = cachedFinds;
275     }
276     
277     
278     /**
279      * Return the cached result of doing findXX on this object
280      * NOTE: this method MUST be used only when loading/using
281      * the content of default-web.xml
282      */

283     public Object JavaDoc[] getCachedFindOperation(){
284         return cachedFinds;
285     }
286
287
288     /**
289      * Starts this web module and registers it and its servlets for monitoring.
290      */

291     public synchronized void start() throws LifecycleException {
292         super.start();
293         webContainer.enableMonitoring(this,
294                                       ((VirtualServer) getParent()).getID());
295     }
296
297
298     /**
299      * Indicates whether this web module contains any ad-hoc paths.
300      *
301      * An ad-hoc path is a servlet path that is mapped to a servlet
302      * not declared in the web module's deployment descriptor.
303      *
304      * A web module all of whose mappings are for ad-hoc paths is called an
305      * ad-hoc web module.
306      *
307      * @return true if this web module contains any ad-hoc paths, false
308      * otherwise
309      */

310     public boolean hasAdHocPaths() {
311         return this.hasAdHocPaths;
312     }
313
314
315     /**
316      * Indicates whether this web module contains any ad-hoc subtrees.
317      *
318      * @return true if this web module contains any ad-hoc subtrees, false
319      * otherwise
320      */

321     public boolean hasAdHocSubtrees() {
322         return this.hasAdHocSubtrees;
323     }
324
325
326     /*
327      * Adds the given ad-hoc path and subtree, along with information about
328      * the servlet that will be responsible for servicing it, to this web
329      * module.
330      *
331      * @param path The ad-hoc path to add
332      * @param subtree The ad-hoc subtree path to add
333      * @param servletInfo Information about the servlet that is responsible
334      * for servicing the given ad-hoc path
335      */

336     void addAdHocPathAndSubtree(String JavaDoc path,
337                                 String JavaDoc subtree,
338                                 AdHocServletInfo servletInfo) {
339
340         if (path == null && subtree == null) {
341             return;
342         }
343
344         Wrapper adHocWrapper = (Wrapper)
345             findChild(servletInfo.getServletName());
346         if (adHocWrapper == null) {
347             adHocWrapper = createAdHocWrapper(servletInfo);
348             addChild(adHocWrapper);
349         }
350
351         if (path != null) {
352             adHocPaths.put(path, servletInfo);
353             hasAdHocPaths = true;
354         }
355
356         if (subtree != null) {
357             adHocSubtrees.put(subtree, servletInfo);
358             hasAdHocSubtrees = true;
359         }
360     }
361
362
363     /*
364      * Adds the given ad-hoc path to servlet mappings to this web module.
365      *
366      * @param newPaths Mappings of ad-hoc paths to the servlets responsible
367      * for servicing them
368      */

369     void addAdHocPaths(HashMap JavaDoc newPaths) {
370
371         if (newPaths == null || newPaths.isEmpty()) {
372             return;
373         }
374
375         Iterator JavaDoc<String JavaDoc> iter = newPaths.keySet().iterator();
376         while (iter.hasNext()) {
377             String JavaDoc adHocPath = iter.next();
378             AdHocServletInfo servletInfo = (AdHocServletInfo)
379                 newPaths.get(adHocPath);
380             Wrapper adHocWrapper = (Wrapper)
381                 findChild(servletInfo.getServletName());
382             if (adHocWrapper == null) {
383                 adHocWrapper = createAdHocWrapper(servletInfo);
384                 addChild(adHocWrapper);
385             }
386             adHocPaths.put(adHocPath, servletInfo);
387         }
388
389         hasAdHocPaths = true;
390     }
391
392
393     /*
394      * Adds the given ad-hoc subtree path to servlet mappings to this web
395      * module.
396      *
397      * @param newSubtrees Mappings of ad-hoc subtree paths to the servlets
398      * responsible for servicing them
399      */

400     void addAdHocSubtrees(HashMap JavaDoc newSubtrees) {
401
402         if (newSubtrees == null || newSubtrees.isEmpty()) {
403             return;
404         }
405
406         Iterator JavaDoc<String JavaDoc> iter = newSubtrees.keySet().iterator();
407         while (iter.hasNext()) {
408             String JavaDoc adHocSubtree = iter.next();
409             AdHocServletInfo servletInfo = (AdHocServletInfo)
410                 newSubtrees.get(adHocSubtree);
411             Wrapper adHocWrapper = (Wrapper)
412                 findChild(servletInfo.getServletName());
413             if (adHocWrapper == null) {
414                 adHocWrapper = createAdHocWrapper(servletInfo);
415                 addChild(adHocWrapper);
416             }
417             adHocSubtrees.put(adHocSubtree, servletInfo);
418         }
419
420         hasAdHocSubtrees = true;
421     }
422
423
424     /*
425      * Gets the ad-hoc path to servlet mappings managed by this web module.
426      *
427      * @return The ad-hoc path to servlet mappings managed by this web
428      * module.
429      */

430     HashMap JavaDoc getAdHocPaths() {
431         return adHocPaths;
432     }
433
434
435     /*
436      * Gets the ad-hoc subtree path to servlet mappings managed by this
437      * web module.
438      *
439      * @return The ad-hoc subtree path to servlet mappings managed by
440      * this web module.
441      */

442     HashMap JavaDoc getAdHocSubtrees() {
443         return adHocSubtrees;
444     }
445
446
447     /**
448      * Returns the name of the ad-hoc servlet responsible for servicing the
449      * given path.
450      *
451      * @param path The path whose associated ad-hoc servlet is needed
452      *
453      * @return The name of the ad-hoc servlet responsible for servicing the
454      * given path, or null if the given path does not represent an ad-hoc
455      * path
456      */

457     public String JavaDoc getAdHocServletName(String JavaDoc path) {
458
459         if (!hasAdHocPaths() && !hasAdHocSubtrees()) {
460             return null;
461         }
462
463         AdHocServletInfo servletInfo = null;
464
465         // Check if given path matches any of the ad-hoc paths (exact match)
466
if (path == null) {
467             servletInfo = adHocPaths.get("");
468         } else {
469             servletInfo = adHocPaths.get(path);
470         }
471
472         // Check if given path starts with any of the ad-hoc subtree paths
473
if (servletInfo == null && path != null && hasAdHocSubtrees()) {
474             Iterator JavaDoc<String JavaDoc> iter = adHocSubtrees.keySet().iterator();
475             while (iter.hasNext()) {
476                 String JavaDoc adHocSubtree = iter.next();
477                 if (path.startsWith(adHocSubtree)) {
478                     servletInfo = adHocSubtrees.get(adHocSubtree);
479                     break;
480                 }
481             }
482         }
483
484         if (servletInfo != null) {
485             return servletInfo.getServletName();
486         } else {
487             return null;
488         }
489     }
490
491
492     /*
493      * Removes the given ad-hoc path from this web module.
494      *
495      * @param path The ad-hoc path to remove
496      */

497     void removeAdHocPath(String JavaDoc path) {
498
499         if (path == null) {
500             return;
501         }
502
503         adHocPaths.remove(path);
504         if (adHocPaths.isEmpty()) {
505             this.hasAdHocPaths = false;
506         }
507     }
508
509
510     /*
511      * Removes the given ad-hoc path from this web module.
512      *
513      * @param subtree The ad-hoc subtree to remove
514      */

515     void removeAdHocSubtree(String JavaDoc subtree) {
516
517         if (subtree == null) {
518             return;
519         }
520
521         adHocSubtrees.remove(subtree);
522         if (adHocSubtrees.isEmpty()) {
523             this.hasAdHocSubtrees = false;
524         }
525     }
526
527
528     /**
529      * Adds the given valve to this web module's ad-hoc pipeline.
530      *
531      * @param valve The valve to add
532      */

533     public void addAdHocValve(Valve valve) {
534         adHocPipeline.addValve(valve);
535     }
536
537
538     /**
539      * Removes the given valve from this web module's ad-hoc pipeline.
540      *
541      * @param valve The valve to remove
542      */

543     public void removeAdHocValve(Valve valve) {
544         adHocPipeline.removeValve(valve);
545     }
546
547
548     /**
549      * Gets this web module's ad-hoc pipeline.
550      *
551      * @return This web module's ad-hoc pipeline
552      */

553     public Pipeline getAdHocPipeline() {
554         return adHocPipeline;
555     }
556
557
558     /**
559      * Sets the file encoding of all static resources of this web module.
560      *
561      * @param enc The file encoding of static resources of this web module
562      */

563     public void setFileEncoding(String JavaDoc enc) {
564         this.fileEncoding = enc;
565     }
566
567
568     /**
569      * Gets the file encoding of all static resources of this web module.
570      *
571      * @return The file encoding of static resources of this web module
572      */

573     public String JavaDoc getFileEncoding() {
574         return fileEncoding;
575     }
576
577
578     /**
579      * Configures this web module with the filter mappings specified in the
580      * deployment descriptor.
581      *
582      * @param sfm The filter mappings of this web module as specified in the
583      * deployment descriptor
584      */

585     void addFilterMap(ServletFilterMapping sfm) {
586
587         FilterMap filterMap = null;
588         Iterator JavaDoc<String JavaDoc> i1 = null;
589         Iterator JavaDoc<String JavaDoc> i2 = null;
590
591         // Create one FilterMap for each <servlet-name>
592
List JavaDoc servletNames = sfm.getServletNames();
593         if (servletNames != null) {
594             i1 = servletNames.iterator();
595             while (i1.hasNext()) {
596                 filterMap = new FilterMap();
597                 filterMap.setFilterName(sfm.getName());
598                 filterMap.setServletName(i1.next());
599                 Set JavaDoc dispatchers = sfm.getDispatchers();
600                 if (dispatchers != null) {
601                     i2 = dispatchers.iterator();
602                     while (i2.hasNext()){
603                         filterMap.setDispatcher(i2.next());
604                     }
605                 }
606                 addFilterMap(filterMap);
607         }
608         }
609
610         // Create one FilterMap for each <url-pattern>
611
List JavaDoc urlPatterns = sfm.getURLPatterns();
612         if (urlPatterns != null) {
613             i1 = urlPatterns.iterator();
614             while (i1.hasNext()) {
615                 filterMap = new FilterMap();
616                 filterMap.setFilterName(sfm.getName());
617                 filterMap.setURLPattern(i1.next());
618                 Set JavaDoc dispatchers = sfm.getDispatchers();
619                 if (dispatchers != null) {
620                     i2 = dispatchers.iterator();
621                     while (i2.hasNext()){
622                         filterMap.setDispatcher(i2.next());
623                     }
624                 }
625                 addFilterMap(filterMap);
626         }
627         }
628     }
629
630
631     /**
632      * Creates an ad-hoc servlet wrapper from the given ad-hoc servlet info.
633      *
634      * @param servletInfo Ad-hoc servlet info from which to generate
635      * ad-hoc servlet wrapper
636      *
637      * @return The generated ad-hoc servlet wrapper
638      */

639     private Wrapper createAdHocWrapper(AdHocServletInfo servletInfo) {
640
641         Wrapper adHocWrapper = new StandardWrapper();
642         adHocWrapper.setServletClass(servletInfo.getServletClass().getName());
643         adHocWrapper.setName(servletInfo.getServletName());
644         Map JavaDoc<String JavaDoc,String JavaDoc> initParams = servletInfo.getServletInitParams();
645         if (initParams != null && !initParams.isEmpty()) {
646             Iterator JavaDoc<String JavaDoc> iter = initParams.keySet().iterator();
647             while (iter.hasNext()) {
648                 String JavaDoc paramName = iter.next();
649                 adHocWrapper.addInitParameter(
650                     paramName,
651                     initParams.get(paramName));
652             }
653         }
654
655         return adHocWrapper;
656     }
657 }
658
659
660
Popular Tags