001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.modules.input;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.avalon.framework.service.Serviceable;
029: import org.apache.avalon.framework.thread.ThreadSafe;
030: import org.apache.excalibur.source.Source;
031: import org.apache.excalibur.source.SourceResolver;
032:
033: /**
034: * Input module for accessing properties in a properties file.
035: *
036: * <p>
037: * The properties file can only be configured statically and
038: * is resolved via the SourceResolver system.
039: * </p>
040: *
041: * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
042: * @version $Id: PropertiesFileModule.java 433543 2006-08-22 06:22:54Z crossley $
043: */
044: public class PropertiesFileModule extends AbstractJXPathModule
045: implements Serviceable, ThreadSafe {
046:
047: private ServiceManager m_manager;
048: private SourceResolver m_resolver;
049: private Properties m_properties;
050:
051: /* (non-Javadoc)
052: * @see Serviceable#service(ServiceManager)
053: */
054: public void service(ServiceManager manager) throws ServiceException {
055: m_manager = manager;
056: m_resolver = (SourceResolver) m_manager
057: .lookup(SourceResolver.ROLE);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.apache.avalon.framework.activity.Disposable#dispose()
062: */
063: public void dispose() {
064: super .dispose();
065: if (this .m_manager != null) {
066: this .m_manager.release(this .m_resolver);
067: this .m_manager = null;
068: this .m_resolver = null;
069: }
070: }
071:
072: /**
073: * Configure the location of the properties file:
074: * <p>
075: * <code><file src="resource://my.properties" /></code>
076: * </p>
077: */
078: public void configure(Configuration configuration)
079: throws ConfigurationException {
080: super .configure(configuration);
081: String file = configuration.getChild("file")
082: .getAttribute("src");
083: load(file);
084: }
085:
086: private void load(String file) throws ConfigurationException {
087: Source source = null;
088: InputStream stream = null;
089: try {
090: source = m_resolver.resolveURI(file);
091: stream = source.getInputStream();
092: m_properties = new Properties();
093: m_properties.load(stream);
094: } catch (IOException e) {
095: throw new ConfigurationException(
096: "Cannot load properties file " + file);
097: } finally {
098: if (source != null) {
099: m_resolver.release(source);
100: }
101: if (stream != null) {
102: try {
103: stream.close();
104: } catch (IOException ignored) {
105: }
106: }
107: }
108: }
109:
110: protected Object getContextObject(Configuration modeConf,
111: Map objectModel) throws ConfigurationException {
112:
113: return m_properties;
114: }
115: }
|