1 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 ; 25 import java.util.Collections ; 26 import java.util.Hashtable ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.slf4j.Logger; 33 import org.slf4j.LoggerFactory; 34 35 36 41 public final class VirtualURIManager extends ObservedManager { 42 public static final String FROM_URI_NODEDATANAME = "fromURI"; 43 public static final String TO_URI_NODEDATANAME = "toURI"; 44 45 48 private static Logger log = LoggerFactory.getLogger(VirtualURIManager.class); 49 50 53 public VirtualURIManager() { 54 } 55 56 59 private Map cachedURImapping = new Hashtable (); 60 61 66 public String getURIMapping(String uri) { 67 Iterator e = cachedURImapping.keySet().iterator(); 68 String 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 []) 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()); Collection list = node.getChildren(ItemType.CONTENTNODE); 87 Collections.sort((List ) list, new StringComparator(FROM_URI_NODEDATANAME)); Iterator it = list.iterator(); 89 while (it.hasNext()) { 90 Content container = (Content) it.next(); 91 NodeData fromURI = NodeDataUtil.getOrCreate(container, FROM_URI_NODEDATANAME); UrlPattern p = new SimpleUrlPattern(fromURI.getString()); 93 cachedURImapping.put(p, new String []{NodeDataUtil.getString(container, TO_URI_NODEDATANAME), fromURI.getString()}); } 95 log.info("Config : VirtualMap loaded - " + node.getHandle()); } 97 catch (Exception e) { 98 log.error("Config : Failed to load VirtualMap - " + node.getHandle() + " - " + e.getMessage(), e); } 100 } 101 102 protected void onClear() { 103 this.cachedURImapping.clear(); 104 } 105 106 public Map getURIMappings() { 108 return cachedURImapping; 109 } 110 111 114 public static VirtualURIManager getInstance() { 115 return (VirtualURIManager) FactoryUtil.getSingleton(VirtualURIManager.class); 116 } 117 } 118 | Popular Tags |