1 17 package org.apache.forrest.locationmap; 18 19 import java.io.IOException ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.Map ; 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 ; 41 import org.xml.sax.SAXException ; 42 43 46 public class LocationMapModule extends AbstractLogEnabled 47 implements InputModule, Serviceable, Configurable, Disposable, ThreadSafe { 48 49 private static final Iterator ATTNAMES = Collections.EMPTY_LIST.iterator(); 50 51 private ServiceManager m_manager; 52 private SourceResolver m_resolver; 53 private String m_src; 54 private SourceValidity m_srcVal; 55 private LocationMap m_lm; 56 57 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 { 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 (source.getInputStream()),handler); 126 configuration = handler.getConfiguration(); 127 } 128 catch (IOException e) { 129 throw new ConfigurationException("Unable to build LocationMap.",e); 130 } 131 catch (SAXException 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 147 151 public Object getAttribute( 152 final String name, 153 final Configuration modeConf, 154 final Map 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 e) { 164 getLogger().error("Failure processing LocationMap.",e); 165 } 166 return null; 167 } 168 169 173 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 174 throws ConfigurationException { 175 176 return null; 177 } 178 179 182 public Object [] getAttributeValues( 183 String name, 184 Configuration modeConf, 185 Map objectModel) 186 throws ConfigurationException { 187 188 return new Object [] {getAttribute(name,modeConf,objectModel)}; 189 } 190 191 } 192 | Popular Tags |