KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > beans > config > VirtualURIManager


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.beans.config;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.NodeData;
18 import info.magnolia.cms.util.FactoryUtil;
19 import info.magnolia.cms.util.NodeDataUtil;
20 import info.magnolia.cms.util.SimpleUrlPattern;
21 import info.magnolia.cms.util.StringComparator;
22 import info.magnolia.cms.util.UrlPattern;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36 /**
37  * Store for all virtual URI to template/page mapping.
38  * @author Sameer Charles
39  * @version 2.0
40  */

41 public final class VirtualURIManager extends ObservedManager {
42     public static final String JavaDoc FROM_URI_NODEDATANAME = "fromURI";
43     public static final String JavaDoc TO_URI_NODEDATANAME = "toURI";
44
45     /**
46      * Logger.
47      */

48     private static Logger log = LoggerFactory.getLogger(VirtualURIManager.class);
49
50     /**
51      * Instantiated by the system.
52      */

53     public VirtualURIManager() {
54     }
55
56     /**
57      * all cached data. <UrlPattern, String[target, pattern]>
58      */

59     private Map JavaDoc cachedURImapping = new Hashtable JavaDoc();
60
61     /**
62      * checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
63      * Servlets".
64      * @return URI string mapping
65      */

66     public String JavaDoc getURIMapping(String JavaDoc uri) {
67         Iterator JavaDoc e = cachedURImapping.keySet().iterator();
68         String JavaDoc mappedURI = StringUtils.EMPTY;
69         int lastMatchedPatternlength = 0;
70         while (e.hasNext()) {
71             UrlPattern p = (UrlPattern) e.next();
72             if (p.match(uri)) {
73                 int patternLength = p.getLength();
74                 if (lastMatchedPatternlength < patternLength) {
75                     lastMatchedPatternlength = patternLength;
76                     mappedURI = ((String JavaDoc[]) cachedURImapping.get(p))[0];
77                 }
78             }
79         }
80         return mappedURI;
81     }
82
83     protected void onRegister(Content node) {
84         try {
85             log.info("Config : Loading VirtualMap - " + node.getHandle()); //$NON-NLS-1$
86
Collection JavaDoc list = node.getChildren(ItemType.CONTENTNODE);
87             Collections.sort((List JavaDoc) list, new StringComparator(FROM_URI_NODEDATANAME)); //$NON-NLS-1$
88
Iterator JavaDoc it = list.iterator();
89             while (it.hasNext()) {
90                 Content container = (Content) it.next();
91                 NodeData fromURI = NodeDataUtil.getOrCreate(container, FROM_URI_NODEDATANAME); //$NON-NLS-1$
92
UrlPattern p = new SimpleUrlPattern(fromURI.getString());
93                 cachedURImapping.put(p, new String JavaDoc[]{NodeDataUtil.getString(container, TO_URI_NODEDATANAME), fromURI.getString()}); //$NON-NLS-1$
94
}
95             log.info("Config : VirtualMap loaded - " + node.getHandle()); //$NON-NLS-1$
96
}
97         catch (Exception JavaDoc e) {
98             log.error("Config : Failed to load VirtualMap - " + node.getHandle() + " - " + e.getMessage(), e); //$NON-NLS-1$ //$NON-NLS-2$
99
}
100     }
101
102     protected void onClear() {
103         this.cachedURImapping.clear();
104     }
105
106     // TODO : should this really be public ?
107
public Map JavaDoc getURIMappings() {
108         return cachedURImapping;
109     }
110
111     /**
112      * @return Returns the instance.
113      */

114     public static VirtualURIManager getInstance() {
115         return (VirtualURIManager) FactoryUtil.getSingleton(VirtualURIManager.class);
116     }
117 }
118
Popular Tags