1 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 ; 23 import java.util.Collections ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.jcr.RepositoryException; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.log4j.Logger; 33 34 35 40 public final class VirtualMap { 41 42 45 private static Logger log = Logger.getLogger(VirtualMap.class); 46 47 50 private VirtualMap() { 51 } 53 54 57 private static Map cachedURImapping = new Hashtable (); 58 59 public static void update(String configPath) { 60 HierarchyManager configHierarchyManager = ContentRepository.getHierarchyManager(ContentRepository.CONFIG); 61 try { 62 log.info("Config : Loading VirtualMap - " + configPath); Content mappingNode = configHierarchyManager.getContent(configPath); 64 cacheURIMappings(mappingNode); 65 log.info("Config : VirtualMap loaded - " + configPath); } 67 catch (RepositoryException re) { 68 log.error("Config : Failed to load VirtualMap - " + configPath + " - " + re.getMessage(), re); } 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"); init(); 79 } 80 81 84 private static void cacheURIMappings(Content nodeList) { 85 Collection list = nodeList.getChildren(); 86 Collections.sort((List ) list, new StringComparator("fromURI")); Iterator it = list.iterator(); 88 while (it.hasNext()) { 89 Content container = (Content) it.next(); 90 NodeData fromURI = container.getNodeData("fromURI"); UrlPattern p = new SimpleUrlPattern(fromURI.getString()); 92 VirtualMap.cachedURImapping.put(p, container.getNodeData("toURI").getString()); } 94 } 95 96 101 public static String getURIMapping(String uri) { 102 Iterator e = VirtualMap.cachedURImapping.keySet().iterator(); 103 String 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 ) VirtualMap.cachedURImapping.get(p); 112 } 113 } 114 } 115 return mappedURI; 116 } 117 } 118 | Popular Tags |