KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
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 /**
36  * Mapps uri prefixes to repositories
37  * @author Philipp Bracher
38  * @version $Id: URI2RepositoryManager.java 6893 2006-10-25 15:36:41Z philipp $
39  */

40 public class URI2RepositoryManager {
41
42     public static final String JavaDoc SERVER_REPOSITORY_URIMAPPING = "/server/URI2RepositoryMapping";
43
44     /**
45      * The mappings
46      */

47     private List JavaDoc mappings = new ArrayList JavaDoc();
48
49     /**
50      * Mapping used if none configured
51      */

52     private URI2RepositoryMapping defaultMapping = new URI2RepositoryMapping("", ContentRepository.WEBSITE, "");
53
54     /**
55      * First initialization and starting observation of the config node.
56      */

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     /**
72      * Add the mappings found in the config repository
73      */

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 JavaDoc 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         // check first the longer prefixes
89
Collections.sort(this.mappings, new Comparator JavaDoc() {
90
91             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
92                 URI2RepositoryMapping m0 = (URI2RepositoryMapping) arg0;
93                 URI2RepositoryMapping m1 = (URI2RepositoryMapping) arg1;
94                 return m1.getUriPrefix().length() - m0.getUriPrefix().length();
95             }
96         });
97     }
98
99     /**
100      * The mapping to use for this uri
101      */

102     public URI2RepositoryMapping getMapping(String JavaDoc uri) {
103         for (Iterator JavaDoc 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     /**
113      * Get the handle for this uri
114      * @param uri
115      * @return
116      */

117     public String JavaDoc getHandle(String JavaDoc uri) {
118         return this.getMapping(uri).getHandle(uri);
119     }
120
121     /**
122      * Get the repository to use for this uri
123      * @param uri
124      * @return
125      */

126     public String JavaDoc getRepository(String JavaDoc uri) {
127         return this.getMapping(uri).getRepository();
128     }
129
130     /**
131      * Get the uri to use for this handle
132      * @param repository
133      * @param handle
134      * @return
135      */

136     public String JavaDoc getURI(String JavaDoc repository, String JavaDoc handle) {
137         for (Iterator JavaDoc 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