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.acting;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021: import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
022: import org.apache.cocoon.Constants;
023: import org.apache.cocoon.components.source.SourceUtil;
024: import org.apache.cocoon.environment.SourceResolver;
025: import org.apache.excalibur.source.Source;
026:
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: /**
031: * Set up environment for configurable form handling data. This group
032: * of actions are unique in that they employ a terciary mapping.
033: *
034: * Each configuration file must use the same format in order to be
035: * effective. The name of the root configuration element is irrelevant.
036: *
037: * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
038: * @version CVS $Id: AbstractComplementaryConfigurableAction.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public abstract class AbstractComplementaryConfigurableAction extends
041: ConfigurableServiceableAction {
042: private static Map configurations = new HashMap();
043:
044: /**
045: * Set up the complementary configuration file. Please note that
046: * multiple Actions can share the same configurations. By using
047: * this approach, we can limit the number of config files.
048: * Also note that the configuration file does not have to be a file.
049: *
050: * Defaults to reload configuration file it has changed.
051: */
052: protected Configuration getConfiguration(String descriptor)
053: throws ConfigurationException {
054: boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
055: if (this .settings.containsKey("reloadable"))
056: reloadable = Boolean.valueOf(
057: (String) this .settings.get("reloadable"))
058: .booleanValue();
059: return this .getConfiguration(descriptor, null, reloadable);
060: }
061:
062: /**
063: * @deprecated please use the getConfiguration(String, SourceResolver, boolean)
064: * version of this method instead.
065: */
066: protected Configuration getConfiguration(String descriptor,
067: boolean reloadable) throws ConfigurationException {
068: return this .getConfiguration(descriptor, null, reloadable);
069: }
070:
071: /**
072: * Set up the complementary configuration file. Please note that
073: * multiple Actions can share the same configurations. By using
074: * this approach, we can limit the number of config files.
075: * Also note that the configuration file does not have to be a file.
076: */
077: protected Configuration getConfiguration(String descriptor,
078: SourceResolver resolver, boolean reloadable)
079: throws ConfigurationException {
080: ConfigurationHelper conf = null;
081:
082: if (descriptor == null) {
083: throw new ConfigurationException(
084: "The form descriptor is not set!");
085: }
086:
087: synchronized (AbstractComplementaryConfigurableAction.configurations) {
088: Source resource = null;
089: try {
090: resource = resolver.resolveURI(descriptor);
091: conf = (ConfigurationHelper) AbstractComplementaryConfigurableAction.configurations
092: .get(resource.getURI());
093: if (conf == null
094: || (reloadable && conf.lastModified != resource
095: .getLastModified())) {
096: getLogger().debug("(Re)Loading " + descriptor);
097:
098: if (conf == null) {
099: conf = new ConfigurationHelper();
100: }
101:
102: SAXConfigurationHandler builder = new SAXConfigurationHandler();
103: SourceUtil.parse(this .manager, resource, builder);
104:
105: conf.lastModified = resource.getLastModified();
106: conf.configuration = builder.getConfiguration();
107:
108: AbstractComplementaryConfigurableAction.configurations
109: .put(resource.getURI(), conf);
110: } else {
111: getLogger().debug(
112: "Using cached configuration for "
113: + descriptor);
114: }
115: } catch (Exception e) {
116: getLogger()
117: .error(
118: "Could not configure Database mapping environment",
119: e);
120: throw new ConfigurationException(
121: "Error trying to load configurations for resource: "
122: + (resource == null ? "null" : resource
123: .getURI()));
124: } finally {
125: resolver.release(resource);
126: }
127: }
128:
129: return conf.configuration;
130: }
131: }
|