KickJava   Java API By Example, From Geeks To Geeks.

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


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-2005 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.HierarchyManager;
17 import info.magnolia.cms.core.NodeData;
18 import info.magnolia.cms.util.SimpleUrlPattern;
19 import info.magnolia.cms.util.StringComparator;
20 import info.magnolia.cms.util.UrlPattern;
21
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.jcr.RepositoryException;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.log4j.Logger;
33
34
35 /**
36  * Store for all virtual URI to template/page mapping.
37  * @author Sameer Charles
38  * @version 2.0
39  */

40 public final class VirtualMap {
41
42     /**
43      * Logger.
44      */

45     private static Logger log = Logger.getLogger(VirtualMap.class);
46
47     /**
48      * Don't instantiate.
49      */

50     private VirtualMap() {
51         // unused
52
}
53
54     /**
55      * all cached data.
56      */

57     private static Map JavaDoc cachedURImapping = new Hashtable JavaDoc();
58
59     public static void update(String JavaDoc configPath) {
60         HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG);
61         try {
62             log.info("Config : Loading VirtualMap - " + configPath); //$NON-NLS-1$
63
Content mappingNode = configHierarchyManager.getContent(configPath);
64             cacheURIMappings(mappingNode);
65             log.info("Config : VirtualMap loaded - " + configPath); //$NON-NLS-1$
66
}
67         catch (RepositoryException re) {
68             log.error("Config : Failed to load VirtualMap - " + configPath + " - " + re.getMessage(), re); //$NON-NLS-1$ //$NON-NLS-2$
69
}
70     }
71
72     protected static void init() {
73         VirtualMap.cachedURImapping.clear();
74     }
75
76     protected static void reload() {
77         log.info("Config : re-loading VirtualMap"); //$NON-NLS-1$
78
init();
79     }
80
81     /**
82      * @param nodeList to be added in cache
83      */

84     private static void cacheURIMappings(Content nodeList) {
85         Collection JavaDoc list = nodeList.getChildren();
86         Collections.sort((List JavaDoc) list, new StringComparator("fromURI")); //$NON-NLS-1$
87
Iterator JavaDoc it = list.iterator();
88         while (it.hasNext()) {
89             Content container = (Content) it.next();
90             NodeData fromURI = container.getNodeData("fromURI"); //$NON-NLS-1$
91
UrlPattern p = new SimpleUrlPattern(fromURI.getString());
92             VirtualMap.cachedURImapping.put(p, container.getNodeData("toURI").getString()); //$NON-NLS-1$
93
}
94     }
95
96     /**
97      * checks for the requested URI mapping in Server config : Servlet Specification 2.3 Section 10 "Mapping Requests to
98      * Servlets".
99      * @return URI string mapping
100      */

101     public static String JavaDoc getURIMapping(String JavaDoc uri) {
102         Iterator JavaDoc e = VirtualMap.cachedURImapping.keySet().iterator();
103         String JavaDoc mappedURI = StringUtils.EMPTY;
104         int lastMatchedPatternlength = 0;
105         while (e.hasNext()) {
106             UrlPattern p = (UrlPattern) e.next();
107             if (p.match(uri)) {
108                 int patternLength = p.getLength();
109                 if (lastMatchedPatternlength < patternLength) {
110                     lastMatchedPatternlength = patternLength;
111                     mappedURI = (String JavaDoc) VirtualMap.cachedURImapping.get(p);
112                 }
113             }
114         }
115         return mappedURI;
116     }
117 }
118
Popular Tags