KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > core > ApplicationContext


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
19 package org.apache.catalina.core;
20
21
22 import java.io.File JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.concurrent.ConcurrentHashMap JavaDoc;
32
33 import javax.naming.Binding JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.naming.directory.DirContext JavaDoc;
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.Servlet JavaDoc;
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletContextAttributeEvent JavaDoc;
40 import javax.servlet.ServletContextAttributeListener JavaDoc;
41
42 import org.apache.catalina.Context;
43 import org.apache.catalina.Host;
44 import org.apache.catalina.Wrapper;
45 import org.apache.catalina.deploy.ApplicationParameter;
46 import org.apache.catalina.util.Enumerator;
47 import org.apache.catalina.util.ResourceSet;
48 import org.apache.catalina.util.ServerInfo;
49 import org.apache.catalina.util.StringManager;
50 import org.apache.naming.resources.DirContextURLStreamHandler;
51 import org.apache.naming.resources.Resource;
52 import org.apache.tomcat.util.buf.CharChunk;
53 import org.apache.tomcat.util.buf.MessageBytes;
54 import org.apache.tomcat.util.http.mapper.MappingData;
55
56
57 /**
58  * Standard implementation of <code>ServletContext</code> that represents
59  * a web application's execution environment. An instance of this class is
60  * associated with each instance of <code>StandardContext</code>.
61  *
62  * @author Craig R. McClanahan
63  * @author Remy Maucherat
64  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
65  */

66
67 public class ApplicationContext
68     implements ServletContext JavaDoc {
69
70     // ----------------------------------------------------------- Constructors
71

72
73     /**
74      * Construct a new instance of this class, associated with the specified
75      * Context instance.
76      *
77      * @param context The associated Context instance
78      */

79     public ApplicationContext(String JavaDoc basePath, StandardContext context) {
80         super();
81         this.context = context;
82         this.basePath = basePath;
83     }
84
85
86     // ----------------------------------------------------- Instance Variables
87

88
89     /**
90      * The context attributes for this context.
91      */

92     protected Map JavaDoc attributes = new ConcurrentHashMap JavaDoc();
93
94
95     /**
96      * List of read only attributes for this context.
97      */

98     private Map JavaDoc readOnlyAttributes = new ConcurrentHashMap JavaDoc();
99
100
101     /**
102      * The Context instance with which we are associated.
103      */

104     private StandardContext context = null;
105
106
107     /**
108      * Empty collection to serve as the basis for empty enumerations.
109      * <strong>DO NOT ADD ANY ELEMENTS TO THIS COLLECTION!</strong>
110      */

111     private static final ArrayList JavaDoc empty = new ArrayList JavaDoc();
112
113
114     /**
115      * The facade around this object.
116      */

117     private ServletContext JavaDoc facade = new ApplicationContextFacade(this);
118
119
120     /**
121      * The merged context initialization parameters for this Context.
122      */

123     private Map JavaDoc parameters = null;
124
125
126     /**
127      * The string manager for this package.
128      */

129     private static final StringManager sm =
130       StringManager.getManager(Constants.Package);
131
132
133     /**
134      * Base path.
135      */

136     private String JavaDoc basePath = null;
137
138
139     /**
140      * Thread local mapping data.
141      */

142     private ThreadLocal JavaDoc localMappingData = new ThreadLocal JavaDoc();
143
144
145     /**
146      * Thread local URI message bytes.
147      */

148     private ThreadLocal JavaDoc localUriMB = new ThreadLocal JavaDoc();
149
150
151     // --------------------------------------------------------- Public Methods
152

153
154     /**
155      * Return the resources object that is mapped to a specified path.
156      * The path must begin with a "/" and is interpreted as relative to the
157      * current context root.
158      */

159     public DirContext JavaDoc getResources() {
160
161         return context.getResources();
162
163     }
164
165
166     // ------------------------------------------------- ServletContext Methods
167

168
169     /**
170      * Return the value of the specified context attribute, if any;
171      * otherwise return <code>null</code>.
172      *
173      * @param name Name of the context attribute to return
174      */

175     public Object JavaDoc getAttribute(String JavaDoc name) {
176
177         return (attributes.get(name));
178
179     }
180
181
182     /**
183      * Return an enumeration of the names of the context attributes
184      * associated with this context.
185      */

186     public Enumeration JavaDoc getAttributeNames() {
187
188         return new Enumerator(attributes.keySet(), true);
189
190     }
191
192
193     /**
194      * Return a <code>ServletContext</code> object that corresponds to a
195      * specified URI on the server. This method allows servlets to gain
196      * access to the context for various parts of the server, and as needed
197      * obtain <code>RequestDispatcher</code> objects or resources from the
198      * context. The given path must be absolute (beginning with a "/"),
199      * and is interpreted based on our virtual host's document root.
200      *
201      * @param uri Absolute URI of a resource on the server
202      */

203     public ServletContext JavaDoc getContext(String JavaDoc uri) {
204
205         // Validate the format of the specified argument
206
if ((uri == null) || (!uri.startsWith("/")))
207             return (null);
208
209         Context JavaDoc child = null;
210         try {
211             Host host = (Host) context.getParent();
212             String JavaDoc mapuri = uri;
213             while (true) {
214                 child = (Context JavaDoc) host.findChild(mapuri);
215                 if (child != null)
216                     break;
217                 int slash = mapuri.lastIndexOf('/');
218                 if (slash < 0)
219                     break;
220                 mapuri = mapuri.substring(0, slash);
221             }
222         } catch (Throwable JavaDoc t) {
223             return (null);
224         }
225
226         if (child == null)
227             return (null);
228
229         if (context.getCrossContext()) {
230             // If crossContext is enabled, can always return the context
231
return child.getServletContext();
232         } else if (child == context) {
233             // Can still return the current context
234
return context.getServletContext();
235         } else {
236             // Nothing to return
237
return (null);
238         }
239     }
240
241     
242     /**
243      * Return the main path associated with this context.
244      */

245     public String JavaDoc getContextPath() {
246         return context.getPath();
247     }
248     
249
250     /**
251      * Return the value of the specified initialization parameter, or
252      * <code>null</code> if this parameter does not exist.
253      *
254      * @param name Name of the initialization parameter to retrieve
255      */

256     public String JavaDoc getInitParameter(final String JavaDoc name) {
257
258         mergeParameters();
259         return ((String JavaDoc) parameters.get(name));
260
261     }
262
263
264     /**
265      * Return the names of the context's initialization parameters, or an
266      * empty enumeration if the context has no initialization parameters.
267      */

268     public Enumeration JavaDoc getInitParameterNames() {
269
270         mergeParameters();
271         return (new Enumerator(parameters.keySet()));
272
273     }
274
275
276     /**
277      * Return the major version of the Java Servlet API that we implement.
278      */

279     public int getMajorVersion() {
280
281         return (Constants.MAJOR_VERSION);
282
283     }
284
285
286     /**
287      * Return the minor version of the Java Servlet API that we implement.
288      */

289     public int getMinorVersion() {
290
291         return (Constants.MINOR_VERSION);
292
293     }
294
295
296     /**
297      * Return the MIME type of the specified file, or <code>null</code> if
298      * the MIME type cannot be determined.
299      *
300      * @param file Filename for which to identify a MIME type
301      */

302     public String JavaDoc getMimeType(String JavaDoc file) {
303
304         if (file == null)
305             return (null);
306         int period = file.lastIndexOf(".");
307         if (period < 0)
308             return (null);
309         String JavaDoc extension = file.substring(period + 1);
310         if (extension.length() < 1)
311             return (null);
312         return (context.findMimeMapping(extension));
313
314     }
315
316
317     /**
318      * Return a <code>RequestDispatcher</code> object that acts as a
319      * wrapper for the named servlet.
320      *
321      * @param name Name of the servlet for which a dispatcher is requested
322      */

323     public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc name) {
324
325         // Validate the name argument
326
if (name == null)
327             return (null);
328
329         // Create and return a corresponding request dispatcher
330
Wrapper wrapper = (Wrapper) context.findChild(name);
331         if (wrapper == null)
332             return (null);
333         
334         return new ApplicationDispatcher(wrapper, null, null, null, null, name);
335
336     }
337
338
339     /**
340      * Return the real path for a given virtual path, if possible; otherwise
341      * return <code>null</code>.
342      *
343      * @param path The path to the desired resource
344      */

345     public String JavaDoc getRealPath(String JavaDoc path) {
346
347         if (!context.isFilesystemBased())
348             return null;
349
350         if (path == null) {
351             return null;
352         }
353
354         File JavaDoc file = new File JavaDoc(basePath, path);
355         return (file.getAbsolutePath());
356
357     }
358
359
360     /**
361      * Return a <code>RequestDispatcher</code> instance that acts as a
362      * wrapper for the resource at the given path. The path must begin
363      * with a "/" and is interpreted as relative to the current context root.
364      *
365      * @param path The path to the desired resource.
366      */

367     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
368
369         // Validate the path argument
370
if (path == null)
371             return (null);
372         if (!path.startsWith("/"))
373             throw new IllegalArgumentException JavaDoc
374                 (sm.getString
375                  ("applicationContext.requestDispatcher.iae", path));
376         path = normalize(path);
377         if (path == null)
378             return (null);
379
380         // Retrieve the thread local URI
381
MessageBytes uriMB = (MessageBytes) localUriMB.get();
382         if (uriMB == null) {
383             uriMB = MessageBytes.newInstance();
384             CharChunk uriCC = uriMB.getCharChunk();
385             uriCC.setLimit(-1);
386             localUriMB.set(uriMB);
387         } else {
388             uriMB.recycle();
389         }
390
391         // Get query string
392
String JavaDoc queryString = null;
393         int pos = path.indexOf('?');
394         if (pos >= 0) {
395             queryString = path.substring(pos + 1);
396         } else {
397             pos = path.length();
398         }
399  
400         // Retrieve the thread local mapping data
401
MappingData mappingData = (MappingData) localMappingData.get();
402         if (mappingData == null) {
403             mappingData = new MappingData();
404             localMappingData.set(mappingData);
405         }
406
407         // Map the URI
408
CharChunk uriCC = uriMB.getCharChunk();
409         try {
410             uriCC.append(context.getPath(), 0, context.getPath().length());
411             /*
412              * Ignore any trailing path params (separated by ';') for mapping
413              * purposes
414              */

415             int semicolon = path.indexOf(';');
416             if (pos >= 0 && semicolon > pos) {
417                 semicolon = -1;
418             }
419             uriCC.append(path, 0, semicolon > 0 ? semicolon : pos);
420             context.getMapper().map(uriMB, mappingData);
421             if (mappingData.wrapper == null) {
422                 return (null);
423             }
424             /*
425              * Append any trailing path params (separated by ';') that were
426              * ignored for mapping purposes, so that they're reflected in the
427              * RequestDispatcher's requestURI
428              */

429             if (semicolon > 0) {
430                 uriCC.append(path, semicolon, pos - semicolon);
431             }
432         } catch (Exception JavaDoc e) {
433             // Should never happen
434
log(sm.getString("applicationContext.mapping.error"), e);
435             return (null);
436         }
437
438         Wrapper wrapper = (Wrapper) mappingData.wrapper;
439         String JavaDoc wrapperPath = mappingData.wrapperPath.toString();
440         String JavaDoc pathInfo = mappingData.pathInfo.toString();
441
442         mappingData.recycle();
443         
444         // Construct a RequestDispatcher to process this request
445
return new ApplicationDispatcher
446             (wrapper, uriCC.toString(), wrapperPath, pathInfo,
447              queryString, null);
448
449     }
450
451
452
453     /**
454      * Return the URL to the resource that is mapped to a specified path.
455      * The path must begin with a "/" and is interpreted as relative to the
456      * current context root.
457      *
458      * @param path The path to the desired resource
459      *
460      * @exception MalformedURLException if the path is not given
461      * in the correct form
462      */

463     public URL JavaDoc getResource(String JavaDoc path)
464         throws MalformedURLException JavaDoc {
465
466         if (path == null || !path.startsWith("/")) {
467             throw new MalformedURLException JavaDoc(sm.getString("applicationContext.requestDispatcher.iae", path));
468         }
469         
470         path = normalize(path);
471         if (path == null)
472             return (null);
473
474         String JavaDoc libPath = "/WEB-INF/lib/";
475         if ((path.startsWith(libPath)) && (path.endsWith(".jar"))) {
476             File JavaDoc jarFile = null;
477             if (context.isFilesystemBased()) {
478                 jarFile = new File JavaDoc(basePath, path);
479             } else {
480                 jarFile = new File JavaDoc(context.getWorkPath(), path);
481             }
482             if (jarFile.exists()) {
483                 return jarFile.toURL();
484             } else {
485                 return null;
486             }
487         } else {
488
489             DirContext JavaDoc resources = context.getResources();
490             if (resources != null) {
491                 String JavaDoc fullPath = context.getName() + path;
492                 String JavaDoc hostName = context.getParent().getName();
493                 try {
494                     resources.lookup(path);
495                     return new URL JavaDoc
496                         ("jndi", "", 0, getJNDIUri(hostName, fullPath),
497                          new DirContextURLStreamHandler(resources));
498                 } catch (Exception JavaDoc e) {
499                     // Ignore
500
}
501             }
502         }
503
504         return (null);
505
506     }
507
508
509     /**
510      * Return the requested resource as an <code>InputStream</code>. The
511      * path must be specified according to the rules described under
512      * <code>getResource</code>. If no such resource can be identified,
513      * return <code>null</code>.
514      *
515      * @param path The path to the desired resource.
516      */

517     public InputStream JavaDoc getResourceAsStream(String JavaDoc path) {
518
519         path = normalize(path);
520         if (path == null)
521             return (null);
522
523         DirContext JavaDoc resources = context.getResources();
524         if (resources != null) {
525             try {
526                 Object JavaDoc resource = resources.lookup(path);
527                 if (resource instanceof Resource)
528                     return (((Resource) resource).streamContent());
529             } catch (Exception JavaDoc e) {
530             }
531         }
532         return (null);
533
534     }
535
536
537     /**
538      * Return a Set containing the resource paths of resources member of the
539      * specified collection. Each path will be a String starting with
540      * a "/" character. The returned set is immutable.
541      *
542      * @param path Collection path
543      */

544     public Set JavaDoc getResourcePaths(String JavaDoc path) {
545
546         // Validate the path argument
547
if (path == null) {
548             return null;
549         }
550         if (!path.startsWith("/")) {
551             throw new IllegalArgumentException JavaDoc
552                 (sm.getString("applicationContext.resourcePaths.iae", path));
553         }
554
555         path = normalize(path);
556         if (path == null)
557             return (null);
558
559         DirContext JavaDoc resources = context.getResources();
560         if (resources != null) {
561             return (getResourcePathsInternal(resources, path));
562         }
563         return (null);
564
565     }
566
567
568     /**
569      * Internal implementation of getResourcesPath() logic.
570      *
571      * @param resources Directory context to search
572      * @param path Collection path
573      */

574     private Set JavaDoc getResourcePathsInternal(DirContext JavaDoc resources, String JavaDoc path) {
575
576         ResourceSet set = new ResourceSet();
577         try {
578             listCollectionPaths(set, resources, path);
579         } catch (NamingException JavaDoc e) {
580             return (null);
581         }
582         set.setLocked(true);
583         return (set);
584
585     }
586
587
588     /**
589      * Return the name and version of the servlet container.
590      */

591     public String JavaDoc getServerInfo() {
592
593         return (ServerInfo.getServerInfo());
594
595     }
596
597
598     /**
599      * @deprecated As of Java Servlet API 2.1, with no direct replacement.
600      */

601     public Servlet JavaDoc getServlet(String JavaDoc name) {
602
603         return (null);
604
605     }
606
607
608     /**
609      * Return the display name of this web application.
610      */

611     public String JavaDoc getServletContextName() {
612
613         return (context.getDisplayName());
614
615     }
616
617
618     /**
619      * @deprecated As of Java Servlet API 2.1, with no direct replacement.
620      */

621     public Enumeration JavaDoc getServletNames() {
622         return (new Enumerator(empty));
623     }
624
625
626     /**
627      * @deprecated As of Java Servlet API 2.1, with no direct replacement.
628      */

629     public Enumeration JavaDoc getServlets() {
630         return (new Enumerator(empty));
631     }
632
633
634     /**
635      * Writes the specified message to a servlet log file.
636      *
637      * @param message Message to be written
638      */

639     public void log(String JavaDoc message) {
640
641         context.getLogger().info(message);
642
643     }
644
645
646     /**
647      * Writes the specified exception and message to a servlet log file.
648      *
649      * @param exception Exception to be reported
650      * @param message Message to be written
651      *
652      * @deprecated As of Java Servlet API 2.1, use
653      * <code>log(String, Throwable)</code> instead
654      */

655     public void log(Exception JavaDoc exception, String JavaDoc message) {
656         
657         context.getLogger().error(message, exception);
658
659     }
660
661
662     /**
663      * Writes the specified message and exception to a servlet log file.
664      *
665      * @param message Message to be written
666      * @param throwable Exception to be reported
667      */

668     public void log(String JavaDoc message, Throwable JavaDoc throwable) {
669         
670         context.getLogger().error(message, throwable);
671
672     }
673
674
675     /**
676      * Remove the context attribute with the specified name, if any.
677      *
678      * @param name Name of the context attribute to be removed
679      */

680     public void removeAttribute(String JavaDoc name) {
681
682         Object JavaDoc value = null;
683         boolean found = false;
684
685         // Remove the specified attribute
686
// Check for read only attribute
687
if (readOnlyAttributes.containsKey(name))
688             return;
689         found = attributes.containsKey(name);
690         if (found) {
691             value = attributes.get(name);
692             attributes.remove(name);
693         } else {
694             return;
695         }
696
697         // Notify interested application event listeners
698
Object JavaDoc listeners[] = context.getApplicationEventListeners();
699         if ((listeners == null) || (listeners.length == 0))
700             return;
701         ServletContextAttributeEvent JavaDoc event =
702           new ServletContextAttributeEvent JavaDoc(context.getServletContext(),
703                                             name, value);
704         for (int i = 0; i < listeners.length; i++) {
705             if (!(listeners[i] instanceof ServletContextAttributeListener JavaDoc))
706                 continue;
707             ServletContextAttributeListener JavaDoc listener =
708                 (ServletContextAttributeListener JavaDoc) listeners[i];
709             try {
710                 context.fireContainerEvent("beforeContextAttributeRemoved",
711                                            listener);
712                 listener.attributeRemoved(event);
713                 context.fireContainerEvent("afterContextAttributeRemoved",
714                                            listener);
715             } catch (Throwable JavaDoc t) {
716                 context.fireContainerEvent("afterContextAttributeRemoved",
717                                            listener);
718                 // FIXME - should we do anything besides log these?
719
log(sm.getString("applicationContext.attributeEvent"), t);
720             }
721         }
722
723     }
724
725
726     /**
727      * Bind the specified value with the specified context attribute name,
728      * replacing any existing value for that name.
729      *
730      * @param name Attribute name to be bound
731      * @param value New attribute value to be bound
732      */

733     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
734
735         // Name cannot be null
736
if (name == null)
737             throw new IllegalArgumentException JavaDoc
738                 (sm.getString("applicationContext.setAttribute.namenull"));
739
740         // Null value is the same as removeAttribute()
741
if (value == null) {
742             removeAttribute(name);
743             return;
744         }
745
746         Object JavaDoc oldValue = null;
747         boolean replaced = false;
748
749         // Add or replace the specified attribute
750
// Check for read only attribute
751
if (readOnlyAttributes.containsKey(name))
752             return;
753         oldValue = attributes.get(name);
754         if (oldValue != null)
755             replaced = true;
756         attributes.put(name, value);
757
758         // Notify interested application event listeners
759
Object JavaDoc listeners[] = context.getApplicationEventListeners();
760         if ((listeners == null) || (listeners.length == 0))
761             return;
762         ServletContextAttributeEvent JavaDoc event = null;
763         if (replaced)
764             event =
765                 new ServletContextAttributeEvent JavaDoc(context.getServletContext(),
766                                                  name, oldValue);
767         else
768             event =
769                 new ServletContextAttributeEvent JavaDoc(context.getServletContext(),
770                                                  name, value);
771
772         for (int i = 0; i < listeners.length; i++) {
773             if (!(listeners[i] instanceof ServletContextAttributeListener JavaDoc))
774                 continue;
775             ServletContextAttributeListener JavaDoc listener =
776                 (ServletContextAttributeListener JavaDoc) listeners[i];
777             try {
778                 if (replaced) {
779                     context.fireContainerEvent
780                         ("beforeContextAttributeReplaced", listener);
781                     listener.attributeReplaced(event);
782                     context.fireContainerEvent("afterContextAttributeReplaced",
783                                                listener);
784                 } else {
785                     context.fireContainerEvent("beforeContextAttributeAdded",
786                                                listener);
787                     listener.attributeAdded(event);
788                     context.fireContainerEvent("afterContextAttributeAdded",
789                                                listener);
790                 }
791             } catch (Throwable JavaDoc t) {
792                 if (replaced)
793                     context.fireContainerEvent("afterContextAttributeReplaced",
794                                                listener);
795                 else
796                     context.fireContainerEvent("afterContextAttributeAdded",
797                                                listener);
798                 // FIXME - should we do anything besides log these?
799
log(sm.getString("applicationContext.attributeEvent"), t);
800             }
801         }
802
803     }
804
805
806     // -------------------------------------------------------- Package Methods
807

808
809     /**
810      * Clear all application-created attributes.
811      */

812     void clearAttributes() {
813
814         // Create list of attributes to be removed
815
ArrayList JavaDoc list = new ArrayList JavaDoc();
816         Iterator JavaDoc iter = attributes.keySet().iterator();
817         while (iter.hasNext()) {
818             list.add(iter.next());
819         }
820
821         // Remove application originated attributes
822
// (read only attributes will be left in place)
823
Iterator JavaDoc keys = list.iterator();
824         while (keys.hasNext()) {
825             String JavaDoc key = (String JavaDoc) keys.next();
826             removeAttribute(key);
827         }
828         
829     }
830     
831     
832     /**
833      * Return the facade associated with this ApplicationContext.
834      */

835     protected ServletContext JavaDoc getFacade() {
836
837         return (this.facade);
838
839     }
840
841
842     /**
843      * Set an attribute as read only.
844      */

845     void setAttributeReadOnly(String JavaDoc name) {
846
847         if (attributes.containsKey(name))
848             readOnlyAttributes.put(name, name);
849
850     }
851
852
853     // -------------------------------------------------------- Private Methods
854

855
856     /**
857      * Return a context-relative path, beginning with a "/", that represents
858      * the canonical version of the specified path after ".." and "." elements
859      * are resolved out. If the specified path attempts to go outside the
860      * boundaries of the current context (i.e. too many ".." path elements
861      * are present), return <code>null</code> instead.
862      *
863      * @param path Path to be normalized
864      */

865     private String JavaDoc normalize(String JavaDoc path) {
866
867         if (path == null) {
868             return null;
869         }
870
871         String JavaDoc normalized = path;
872
873         // Normalize the slashes and add leading slash if necessary
874
if (normalized.indexOf('\\') >= 0)
875             normalized = normalized.replace('\\', '/');
876
877         // Resolve occurrences of "/../" in the normalized path
878
while (true) {
879             int index = normalized.indexOf("/../");
880             if (index < 0)
881                 break;
882             if (index == 0)
883                 return (null); // Trying to go outside our context
884
int index2 = normalized.lastIndexOf('/', index - 1);
885             normalized = normalized.substring(0, index2) +
886                 normalized.substring(index + 3);
887         }
888
889         // Return the normalized path that we have completed
890
return (normalized);
891
892     }
893
894
895     /**
896      * Merge the context initialization parameters specified in the application
897      * deployment descriptor with the application parameters described in the
898      * server configuration, respecting the <code>override</code> property of
899      * the application parameters appropriately.
900      */

901     private void mergeParameters() {
902
903         if (parameters != null)
904             return;
905         Map JavaDoc results = new ConcurrentHashMap JavaDoc();
906         String JavaDoc names[] = context.findParameters();
907         for (int i = 0; i < names.length; i++)
908             results.put(names[i], context.findParameter(names[i]));
909         ApplicationParameter params[] =
910             context.findApplicationParameters();
911         for (int i = 0; i < params.length; i++) {
912             if (params[i].getOverride()) {
913                 if (results.get(params[i].getName()) == null)
914                     results.put(params[i].getName(), params[i].getValue());
915             } else {
916                 results.put(params[i].getName(), params[i].getValue());
917             }
918         }
919         parameters = results;
920
921     }
922
923
924     /**
925      * List resource paths (recursively), and store all of them in the given
926      * Set.
927      */

928     private static void listCollectionPaths
929         (Set JavaDoc set, DirContext JavaDoc resources, String JavaDoc path)
930         throws NamingException JavaDoc {
931
932         Enumeration JavaDoc childPaths = resources.listBindings(path);
933         while (childPaths.hasMoreElements()) {
934             Binding JavaDoc binding = (Binding JavaDoc) childPaths.nextElement();
935             String JavaDoc name = binding.getName();
936             StringBuffer JavaDoc childPath = new StringBuffer JavaDoc(path);
937             if (!"/".equals(path) && !path.endsWith("/"))
938                 childPath.append("/");
939             childPath.append(name);
940             Object JavaDoc object = binding.getObject();
941             if (object instanceof DirContext JavaDoc) {
942                 childPath.append("/");
943             }
944             set.add(childPath.toString());
945         }
946
947     }
948
949
950     /**
951      * Get full path, based on the host name and the context path.
952      */

953     private static String JavaDoc getJNDIUri(String JavaDoc hostName, String JavaDoc path) {
954         if (!path.startsWith("/"))
955             return "/" + hostName + "/" + path;
956         else
957             return "/" + hostName + path;
958     }
959
960
961 }
962
Popular Tags