KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > locationmap > LocationMapModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.forrest.locationmap;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.forrest.locationmap.lm.LocationMap;
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.avalon.framework.configuration.Configurable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.configuration.NamespacedSAXConfigurationHandler;
30 import org.apache.avalon.framework.logger.AbstractLogEnabled;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34 import org.apache.avalon.framework.thread.ThreadSafe;
35 import org.apache.cocoon.components.modules.input.InputModule;
36 import org.apache.excalibur.source.Source;
37 import org.apache.excalibur.source.SourceResolver;
38 import org.apache.excalibur.source.SourceValidity;
39 import org.apache.excalibur.xml.sax.SAXParser;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * Resolves a request against a LocationMap.
45  */

46 public class LocationMapModule extends AbstractLogEnabled
47     implements InputModule, Serviceable, Configurable, Disposable, ThreadSafe {
48     
49     private static final Iterator JavaDoc ATTNAMES = Collections.EMPTY_LIST.iterator();
50     
51     private ServiceManager m_manager;
52     private SourceResolver m_resolver;
53     private String JavaDoc m_src;
54     private SourceValidity m_srcVal;
55     private LocationMap m_lm;
56     
57     // ---------------------------------------------------- lifecycle
58

59     public LocationMapModule() {
60     }
61     
62     public void service(ServiceManager manager) throws ServiceException {
63         m_manager = manager;
64         m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
65     }
66     
67     public void configure(Configuration configuration) throws ConfigurationException {
68         m_src = configuration.getChild("file").getAttribute("src");
69     }
70     
71     public void dispose() {
72         m_lm.dispose();
73     }
74     
75     private LocationMap getLocationMap() throws Exception JavaDoc {
76         Source source = null;
77         try {
78             source = m_resolver.resolveURI(m_src);
79             if (m_lm == null) {
80                 synchronized (this) {
81                     if (m_lm == null) {
82                         if (getLogger().isDebugEnabled()) {
83                             getLogger().debug("loading location map at " + m_src);
84                         }
85                         m_srcVal = source.getValidity();
86                         m_lm = new LocationMap(m_manager);
87                         m_lm.enableLogging(getLogger());
88                         m_lm.build(loadConfiguration(source));
89                     }
90                 }
91             }
92             else {
93                 SourceValidity valid = source.getValidity();
94                 if (m_srcVal != null && m_srcVal.isValid(valid) != 1) {
95                     synchronized (this) {
96                         if (m_srcVal != null && m_srcVal.isValid(valid) != 1) {
97                             if (getLogger().isDebugEnabled()) {
98                                 getLogger().debug("reloading location map at " + m_src);
99                             }
100                             m_srcVal = valid;
101                             m_lm.dispose();
102                             m_lm = new LocationMap(m_manager);
103                             m_lm.enableLogging(getLogger());
104                             m_lm.build(loadConfiguration(source));
105                         }
106                     }
107                 }
108             }
109         }
110         finally {
111             if (source != null) {
112                 m_resolver.release(source);
113             }
114         }
115         return m_lm;
116     }
117     
118     private Configuration loadConfiguration(Source source) throws ConfigurationException {
119         Configuration configuration = null;
120         SAXParser parser = null;
121         try {
122             parser = (SAXParser) m_manager.lookup(SAXParser.ROLE);
123             NamespacedSAXConfigurationHandler handler =
124                 new NamespacedSAXConfigurationHandler();
125             parser.parse(new InputSource JavaDoc(source.getInputStream()),handler);
126             configuration = handler.getConfiguration();
127         }
128         catch (IOException JavaDoc e) {
129             throw new ConfigurationException("Unable to build LocationMap.",e);
130         }
131         catch (SAXException JavaDoc e) {
132             throw new ConfigurationException("Unable to build LocationMap.",e);
133         }
134         catch (ServiceException e) {
135             throw new ConfigurationException("Unable to build LocationMap.",e);
136         }
137         finally {
138             if (parser != null) {
139                 m_manager.release(parser);
140             }
141         }
142         return configuration;
143     }
144     
145     // ---------------------------------------------------- Module implementation
146

147     /**
148      * Execute the current request against the locationmap returning the
149      * resulting string.
150      */

151     public Object JavaDoc getAttribute(
152         final String JavaDoc name,
153         final Configuration modeConf,
154         final Map JavaDoc objectModel)
155         throws ConfigurationException {
156         
157         try {
158             return getLocationMap().locate(name,objectModel);
159         }
160         catch (ConfigurationException e) {
161             throw e;
162         }
163         catch (Exception JavaDoc e) {
164             getLogger().error("Failure processing LocationMap.",e);
165         }
166         return null;
167     }
168     
169     /**
170      * The possibilities are endless. No way to enumerate them all.
171      * Therefore returns null.
172      */

173     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
174         throws ConfigurationException {
175         
176         return null;
177     }
178     
179     /**
180      * Always returns only one value. Use getAttribute() instead.
181      */

182     public Object JavaDoc[] getAttributeValues(
183         String JavaDoc name,
184         Configuration modeConf,
185         Map JavaDoc objectModel)
186         throws ConfigurationException {
187         
188         return new Object JavaDoc[] {getAttribute(name,modeConf,objectModel)};
189     }
190
191 }
192
Popular Tags