1 13 package info.magnolia.cms.beans.config; 14 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import javax.jcr.observation.EventIterator; 22 import javax.jcr.observation.EventListener; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import info.magnolia.cms.core.Content; 27 import info.magnolia.cms.core.ItemType; 28 import info.magnolia.cms.util.ContentUtil; 29 import info.magnolia.cms.util.FactoryUtil; 30 import info.magnolia.cms.util.NodeDataUtil; 31 import info.magnolia.cms.util.ObservationUtil; 32 import info.magnolia.context.MgnlContext; 33 34 35 40 public class URI2RepositoryManager { 41 42 public static final String SERVER_REPOSITORY_URIMAPPING = "/server/URI2RepositoryMapping"; 43 44 47 private List mappings = new ArrayList (); 48 49 52 private URI2RepositoryMapping defaultMapping = new URI2RepositoryMapping("", ContentRepository.WEBSITE, ""); 53 54 57 public URI2RepositoryManager() { 58 init(); 59 ObservationUtil.registerChangeListener( 60 ContentRepository.CONFIG, 61 SERVER_REPOSITORY_URIMAPPING, 62 new EventListener() { 63 64 public void onEvent(EventIterator arg0) { 65 MgnlContext.setInstance(MgnlContext.getSystemContext()); 66 init(); 67 } 68 }); 69 } 70 71 74 public void init() { 75 this.mappings.clear(); 76 Content node = ContentUtil.getContent(ContentRepository.CONFIG, SERVER_REPOSITORY_URIMAPPING); 77 if (node != null) { 78 for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) { 79 Content mappingNode = (Content) iter.next(); 80 URI2RepositoryMapping mapping = new URI2RepositoryMapping(); 81 mapping.setRepository(NodeDataUtil.getString(mappingNode, "repository", "")); 82 mapping.setUriPrefix(NodeDataUtil.getString(mappingNode, "URIPrefix", "")); 83 mapping.setHandlePrefix(NodeDataUtil.getString(mappingNode, "handlePrefix", "")); 84 this.addMapping(mapping); 85 } 86 } 87 88 Collections.sort(this.mappings, new Comparator () { 90 91 public int compare(Object arg0, Object arg1) { 92 URI2RepositoryMapping m0 = (URI2RepositoryMapping) arg0; 93 URI2RepositoryMapping m1 = (URI2RepositoryMapping) arg1; 94 return m1.getUriPrefix().length() - m0.getUriPrefix().length(); 95 } 96 }); 97 } 98 99 102 public URI2RepositoryMapping getMapping(String uri) { 103 for (Iterator iter = mappings.iterator(); iter.hasNext();) { 104 URI2RepositoryMapping mapping = (URI2RepositoryMapping) iter.next(); 105 if (mapping.matches(uri)) { 106 return mapping; 107 } 108 } 109 return this.defaultMapping; 110 } 111 112 117 public String getHandle(String uri) { 118 return this.getMapping(uri).getHandle(uri); 119 } 120 121 126 public String getRepository(String uri) { 127 return this.getMapping(uri).getRepository(); 128 } 129 130 136 public String getURI(String repository, String handle) { 137 for (Iterator iter = mappings.iterator(); iter.hasNext();) { 138 URI2RepositoryMapping mapping = (URI2RepositoryMapping) iter.next(); 139 if (StringUtils.equals(mapping.getRepository(), repository) && handle.startsWith(mapping.getHandlePrefix())) { 140 return mapping.getURI(handle); 141 } 142 } 143 return handle; 144 } 145 146 public void addMapping(URI2RepositoryMapping mapping) { 147 mappings.add(mapping); 148 } 149 150 public static URI2RepositoryManager getInstance() { 151 return (URI2RepositoryManager) FactoryUtil.getSingleton(URI2RepositoryManager.class); 152 } 153 154 } 155 | Popular Tags |