KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > applications > ApplicationContext


1 // __/\ ______| |__/\. _______
2
// __ .____| | \ | +----+ \
3
// _______| /--| | | - \ _ | : - \_________
4
// \\______: :---| : : | : | \________>
5
// |__\---\_____________:______: :____|____:_____\
6
// /_____|
7
//
8
// . . . i n j a h i a w e t r u s t . . .
9
//
10
//
11
//
12
//
13
// NK 18.06.2002
14
//
15
//
16

17 package org.jahia.data.applications;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import org.jahia.services.applications.ServletIncludeRequestWrapper;
24 import org.jahia.utils.JahiaConsole;
25 import org.jahia.utils.InsertionSortedMap;
26 import java.util.Map JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29
30
31 /**
32  * Holds information for a given web application context.
33  * Some of them are: servlets, roles, servlet mapping, welcome files.
34  *
35  * @author Khue Nguyen <a HREF="mailto:khue@jahia.com">khue@jahia.com</a>
36  */

37 public class ApplicationContext implements Serializable JavaDoc
38 {
39     private static final String JavaDoc CLASS_NAME = ApplicationContext.class.getName();
40
41     /** the application display name **/
42     private String JavaDoc displayName = "";
43
44     /** the application context **/
45     private String JavaDoc context = "";
46
47     /** the application description **/
48     private String JavaDoc descr = "";
49
50     /** The HashMap of servlet bean , keyed by the servlet name **/
51     private InsertionSortedMap servlets = new InsertionSortedMap();
52
53     /**
54      * The hashMap of servlet mapping, keyed with the pattern used to map a servlet.
55      * The value is the servlet name.
56      **/

57     private HashMap JavaDoc servletMappings = new HashMap JavaDoc();
58
59     /** vector of security roles **/
60     private Vector JavaDoc roles = new Vector JavaDoc();
61
62     /** The list of Welcome files **/
63     private Vector JavaDoc welcomeFiles = new Vector JavaDoc();
64
65
66
67     //--------------------------------------------------------------------------
68
/**
69      * Constructor
70      *
71      */

72     public ApplicationContext(String JavaDoc context){
73         this.context = context;
74     }
75
76     //--------------------------------------------------------------------------
77
/**
78      * Constructor
79      *
80      * @param context , the application context
81      * @param displayName , the application display name
82      * @param descr, the application description
83      * @param servlets, a vector ServletBean
84      * @param servletMappings, a map of servletMappings keyed with the url pappern and value = the servlet name
85      * @param welcomeFiles, a vector of welcome files (String)
86      */

87     public ApplicationContext( String JavaDoc context,
88                                 String JavaDoc displayName,
89                                 String JavaDoc descr,
90                                 Vector JavaDoc servlets,
91                                 HashMap JavaDoc servletMappings,
92                                 Vector JavaDoc roles,
93                                 Vector JavaDoc welcomeFiles )
94     {
95         this.context = context;
96
97         if ( displayName != null ){
98             this.displayName = displayName;
99         }
100         if ( descr != null ){
101             this.descr = descr;
102         }
103         addServlets(servlets);
104         setServletMappings(servletMappings);
105         setRoles(roles);
106         setWelcomeFiles(welcomeFiles);
107     }
108
109     //--------------------------------------------------------------------------
110
/**
111      * Add a Vector of ServletBean.
112      *
113      * @param servlets
114      */

115     public void addServlets(Vector JavaDoc servlets) {
116         synchronized (this.servlets) {
117             if ( servlets!=null ){
118                 int size = servlets.size();
119                 ServletBean servlet = null;
120                 for ( int i=0 ; i<size ; i++ ){
121                     servlet = (ServletBean)servlets.get(i);
122                     if ( servlet!=null && servlet.getServletName() != null ){
123                         this.servlets.put(servlet.getServletName(), servlet);
124                     }
125                 }
126             }
127         }
128     }
129
130     //--------------------------------------------------------------------------
131
/**
132      * Add a new servlet bean.
133      *
134      * @param ServletBean
135      */

136     public void addServlet(ServletBean servlet) {
137         synchronized (servlets) {
138             if ( servlet!=null && servlet.getServletName() != null ){
139                 servlets.put(servlet.getServletName(), servlet);
140             }
141         }
142     }
143
144     //--------------------------------------------------------------------------
145
/**
146      * Get a servlet looking at it name.
147      *
148      * @param name , the servlet name
149      */

150     public ServletBean getServlet(String JavaDoc name) {
151         synchronized (servlets) {
152             if ( name!=null){
153                 return (ServletBean)servlets.get(name);
154             }
155         }
156         return null;
157     }
158
159     //--------------------------------------------------------------------------
160
/**
161      * Set the map of servlet mapping.
162      *
163      * @param servletMappings
164      */

165     public void setServletMappings(HashMap JavaDoc servletMappings) {
166         synchronized (this.servletMappings) {
167             if ( servletMappings != null ){
168                 this.servletMappings = servletMappings;
169             }
170         }
171     }
172
173     //--------------------------------------------------------------------------
174
/**
175      * Add a new servlet mapping and replace old mapping with same pattern.
176      *
177      * @param pattern
178      * @param name , the servlet name
179      */

180     public void addServletMapping(String JavaDoc pattern, String JavaDoc name) {
181         synchronized (servletMappings) {
182             servletMappings.put(pattern, name);
183         }
184     }
185
186     //--------------------------------------------------------------------------
187
/**
188      * Return the servlet name mapped by the given pattern
189      * otherwise return null.
190      *
191      * @param pattern the url-pattern
192      */

193     public String JavaDoc findServletMapping(String JavaDoc pattern) {
194         synchronized (servletMappings) {
195             return ((String JavaDoc) servletMappings.get(pattern));
196         }
197     }
198
199     //--------------------------------------------------------------------------
200
/**
201      * Return the vector of servlets.
202      *
203      * @return servlets
204      */

205     public Map JavaDoc getServlets() {
206         return servlets;
207     }
208
209     //--------------------------------------------------------------------------
210
/**
211      * Return a servlet mapping pattern that matches the given request URI.
212      *
213      * @param pattern the request URI
214      * @return pattern , the servlet mapping pattern or null if not found.
215      */

216     public String JavaDoc findServletMappingFromRequestURI(String JavaDoc requestURI) {
217
218         JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI",
219                                 "Started for requestURI [" + requestURI + "]");
220         if ( requestURI == null ){
221             return null;
222         }
223         String JavaDoc decRequestURI = ServletIncludeRequestWrapper.URLDecode(requestURI);
224
225         if ( !decRequestURI.startsWith(context) ){
226             // invalid request URI
227
return null;
228         }
229         int pos = decRequestURI.indexOf(context);
230         String JavaDoc str = decRequestURI.substring(context.length());
231         pos = str.indexOf("?");
232         if ( pos != -1 ){
233             str = str.substring(0,pos);
234         }
235         pos = str.indexOf(";");
236         if ( pos != -1 ){
237             str = str.substring(0,pos);
238         }
239
240         JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI",
241                                 "Servlet Path + PathInfo [" + str + "]");
242
243         // try matching with exact mapping
244
String JavaDoc pattern = findServletExactMapping(str);
245
246         if ( pattern == null ){
247             // try matching with path mapping
248
pattern = findServletPathMapping(str);
249         }
250
251         if ( pattern == null ){
252             // try matching with extension mapping
253
pattern = findServletExtensionMapping(str);
254         }
255
256         if ( pattern == null && findServletMapping("/")!=null ){
257             // the default servlet if any
258
pattern = "/";
259         }
260
261         JahiaConsole.println( CLASS_NAME+".findServletMappingFromRequestURI",
262                                 "Result [" + pattern + "]");
263
264         return pattern;
265     }
266
267
268     //--------------------------------------------------------------------------
269
/**
270      * Set roles.
271      *
272      * @param roles a vector of security roles
273      */

274     public void setRoles(Vector JavaDoc roles) {
275         synchronized (this.roles) {
276             if ( roles != null ){
277                 this.roles = roles;
278             }
279         }
280     }
281
282     //--------------------------------------------------------------------------
283
/**
284      * Add a new security role.
285      *
286      * @param role New security role
287      */

288     public void addRole(String JavaDoc role) {
289         synchronized (roles) {
290             roles.add(role);
291         }
292     }
293
294     //--------------------------------------------------------------------------
295
/**
296      * Return true if a given role is already in the list.
297      *
298      * @param role the security role to look for
299      */

300     public boolean findRole(String JavaDoc role) {
301         if ( role == null ){
302             return false;
303         }
304         synchronized (roles) {
305             return roles.contains(role);
306         }
307     }
308
309     //--------------------------------------------------------------------------
310
/**
311      * Return the security roles defined for this application.
312      */

313     public Vector JavaDoc getRoles() {
314         return roles;
315     }
316
317     //--------------------------------------------------------------------------
318
/**
319      * Set the welcome files.
320      *
321      * @param welcomeFiles
322      */

323     public void setWelcomeFiles(Vector JavaDoc welcomeFiles) {
324         synchronized (this.welcomeFiles) {
325             if ( welcomeFiles != null ){
326                 this.welcomeFiles = welcomeFiles;
327             }
328         }
329     }
330
331
332     //--------------------------------------------------------------------------
333
/**
334      * Add a new welcome file.
335      *
336      * @param filename New welcome file name
337      */

338     public void addWelcomeFile(String JavaDoc filename) {
339         synchronized (welcomeFiles) {
340             welcomeFiles.add(filename);
341         }
342     }
343
344     //--------------------------------------------------------------------------
345
/**
346      * Return the list of welcome file.
347      *
348      * @return welcomeFiles
349      */

350     public Vector JavaDoc getWelcomeFiles() {
351         return welcomeFiles;
352     }
353
354     //--------------------------------------------------------------------------
355
/**
356      * Return the context
357      *
358      */

359     public String JavaDoc getContext(){
360         return context;
361     }
362
363     //--------------------------------------------------------------------------
364
/**
365      * Set the context
366      *
367      * @param String val
368      */

369     public void setContext(String JavaDoc val){
370         context = val;
371     }
372
373     //--------------------------------------------------------------------------
374
/**
375      * Return the display name
376      *
377      */

378     public String JavaDoc getDisplayName(){
379         return displayName;
380     }
381
382     //--------------------------------------------------------------------------
383
/**
384      * Set the display name
385      *
386      * @param String val
387      */

388     public void setDisplayName(String JavaDoc val){
389         displayName = val;
390     }
391
392     //--------------------------------------------------------------------------
393
/**
394      * Return the descr
395      *
396      */

397     public String JavaDoc getDescr(){
398         return descr;
399     }
400
401     //--------------------------------------------------------------------------
402
/**
403      * Set the descr
404      *
405      * @param String val
406      */

407     public void setDescr(String JavaDoc val){
408         descr = val;
409     }
410
411     //--------------------------------------------------------------------------
412
/**
413      * Find an exact servlet mapping pattern matching the given path if any
414      *
415      * @param String path ( servlet pat + path info + querystring )
416      */

417     private String JavaDoc findServletExactMapping(String JavaDoc path){
418         if ( path == null ){
419             return null;
420         }
421
422         Iterator JavaDoc iterator = servletMappings.keySet().iterator();
423         String JavaDoc pattern = null;
424         String JavaDoc result = "";
425         while ( iterator.hasNext() )
426         {
427             pattern = (String JavaDoc)iterator.next();
428             if ( !pattern.equals("/")
429                  || !pattern.startsWith("*.")
430                  || !(pattern.startsWith("/") && pattern.endsWith("/*")) )
431             {
432                 // we've got an exact mapping pattern
433

434                 if ( path.startsWith(pattern) ){
435                     boolean match = false;
436                     if ( pattern.length() == path.length() ){
437                         match = true;
438                     } else if ( pattern.endsWith("/") ){
439                         match = true;
440                     } else if ( path.charAt(pattern.length())=='/' ) {
441                         match = true;
442                     }
443                     if ( match && (pattern.length() > result.length()) ){
444                         result = pattern;
445                     }
446                 }
447             }
448         }
449
450         JahiaConsole.println( CLASS_NAME+".findServletExactMapping",
451                                 "result [" + result + "]");
452
453         if ( result.length() == 0 ){
454             return null;
455         }
456         return result;
457     }
458
459     //--------------------------------------------------------------------------
460
/**
461      * Find a servlet path mapping pattern matching the given path if any
462      *
463      * @param String path ( servlet pat + path info + querystring )
464      */

465     private String JavaDoc findServletPathMapping(String JavaDoc path){
466         if ( path == null ){
467             return null;
468         }
469
470         Iterator JavaDoc iterator = servletMappings.keySet().iterator();
471         String JavaDoc pattern = null;
472         String JavaDoc result = "";
473         while ( iterator.hasNext() )
474         {
475             pattern = (String JavaDoc)iterator.next();
476             if ( pattern.startsWith("/") && pattern.endsWith("/*") )
477             {
478
479                 // we've got a path mapping pattern
480
if ( path.startsWith(pattern.substring(0,pattern.length()-2)) ){
481                     String JavaDoc str = pattern.substring(0,pattern.length()-2);
482                     if ( (str.startsWith("/") || str.startsWith("?") || str.startsWith(";")
483                          || str.startsWith("#") || str.startsWith("&") ) && pattern.length() > result.length() ){
484                        result = pattern;
485                    }
486                 }
487             }
488         }
489
490         JahiaConsole.println( CLASS_NAME+".findServletPathMapping",
491                                 "result [" + result + "]");
492
493         if ( result.length() == 0 ){
494             return null;
495         }
496         return result;
497     }
498
499     //--------------------------------------------------------------------------
500
/**
501      * Find a servlet extension mapping pattern matching the given path if any
502      *
503      * @param String path ( servlet pat + path info + querystring )
504      */

505     private String JavaDoc findServletExtensionMapping(String JavaDoc path){
506         if ( path == null ){
507             return null;
508         }
509
510         int pos = path.indexOf("?");
511         String JavaDoc str = path;
512
513         if ( pos != -1 ){
514             str = path.substring(0,pos);
515         }
516
517         pos = str.lastIndexOf("/");
518
519         Iterator JavaDoc iterator = servletMappings.keySet().iterator();
520         String JavaDoc pattern = null;
521         String JavaDoc result = "";
522         int strPos = -1;
523         while ( iterator.hasNext() )
524         {
525             pattern = (String JavaDoc)iterator.next();
526             if ( pattern.startsWith("*.") )
527             {
528                 strPos = str.indexOf(pattern.substring(1));
529                 // we've got a path mapping pattern
530
if ( strPos != -1 && pos < strPos
531                      && pattern.length() > result.length() )
532                 {
533                     result = pattern;
534                 }
535             }
536         }
537
538         JahiaConsole.println( CLASS_NAME+".findServletExtensionMapping",
539                                 "result [" + result + "]");
540
541         if ( result.length() == 0 ){
542             return null;
543         }
544
545         return result;
546     }
547
548
549 }
550
Popular Tags