1 16 package org.apache.cocoon.components.modules.input; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.util.Map ; 21 import java.util.Properties ; 22 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.excalibur.source.Source; 31 import org.apache.excalibur.source.SourceResolver; 32 33 43 public class PropertiesFileModule extends AbstractJXPathModule 44 implements InputModule, Serviceable, Configurable, ThreadSafe { 45 46 private ServiceManager m_manager; 47 private SourceResolver m_resolver; 48 private Properties m_properties; 49 50 51 54 public void service(ServiceManager manager) throws ServiceException { 55 m_manager = manager; 56 m_resolver = (SourceResolver) m_manager.lookup(SourceResolver.ROLE); 57 } 58 59 62 public void dispose() { 63 super.dispose(); 64 if ( this.m_manager != null ) { 65 this.m_manager.release( this.m_resolver ); 66 this.m_manager = null; 67 this.m_resolver = null; 68 } 69 } 70 71 77 public void configure(Configuration configuration) throws ConfigurationException { 78 super.configure(configuration); 79 String file = configuration.getChild("file").getAttribute("src"); 80 load(file); 81 } 82 83 private void load(String file) throws ConfigurationException { 84 Source source = null; 85 InputStream stream = null; 86 try { 87 source = m_resolver.resolveURI(file); 88 stream = source.getInputStream(); 89 m_properties = new Properties (); 90 m_properties.load(stream); 91 } catch (IOException e) { 92 throw new ConfigurationException("Cannot load properties file " + file); 93 } finally { 94 if (source != null) { 95 m_resolver.release(source); 96 } 97 if (stream != null) { 98 try { 99 stream.close(); 100 } catch (IOException ignored) { 101 } 102 } 103 } 104 } 105 106 protected Object getContextObject(Configuration modeConf, Map objectModel) 107 throws ConfigurationException { 108 109 return m_properties; 110 } 111 } 112 | Popular Tags |