KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > AbstractComplementaryConfigurableAction


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

16 package org.apache.cocoon.acting;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
21 import org.apache.cocoon.Constants;
22 import org.apache.cocoon.components.source.SourceUtil;
23 import org.apache.cocoon.environment.SourceResolver;
24 import org.apache.excalibur.source.Source;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Set up environment for configurable form handling data. This group
31  * of actions are unique in that they employ a terciary mapping.
32  *
33  * Each configuration file must use the same format in order to be
34  * effective. The name of the root configuration element is irrelevant.
35  *
36  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
37  * @version CVS $Id: AbstractComplementaryConfigurableAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
38  */

39 public abstract class AbstractComplementaryConfigurableAction extends ConfigurableServiceableAction {
40     private static Map JavaDoc configurations = new HashMap JavaDoc();
41
42     /**
43      * Set up the complementary configuration file. Please note that
44      * multiple Actions can share the same configurations. By using
45      * this approach, we can limit the number of config files.
46      * Also note that the configuration file does not have to be a file.
47      *
48      * Defaults to reload configuration file it has changed.
49      */

50     protected Configuration getConfiguration(String JavaDoc descriptor) throws ConfigurationException {
51         boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
52         if (this.settings.containsKey("reloadable"))
53             reloadable = Boolean.valueOf((String JavaDoc) this.settings.get("reloadable")).booleanValue();
54         return this.getConfiguration(descriptor, null, reloadable);
55     }
56
57     /**
58      * @deprecated please use the getConfiguration(String, SourceResolver, boolean)
59      * version of this method instead.
60      */

61     protected Configuration getConfiguration(String JavaDoc descriptor, boolean reloadable) throws ConfigurationException {
62         return this.getConfiguration( descriptor, null, reloadable );
63     }
64
65     /**
66      * Set up the complementary configuration file. Please note that
67      * multiple Actions can share the same configurations. By using
68      * this approach, we can limit the number of config files.
69      * Also note that the configuration file does not have to be a file.
70      */

71     protected Configuration getConfiguration(String JavaDoc descriptor, SourceResolver resolver, boolean reloadable) throws ConfigurationException {
72         ConfigurationHelper conf = null;
73
74         if (descriptor == null) {
75             throw new ConfigurationException("The form descriptor is not set!");
76         }
77
78         synchronized (AbstractComplementaryConfigurableAction.configurations) {
79             Source resource = null;
80             try {
81                 resource = resolver.resolveURI(descriptor);
82                 conf = (ConfigurationHelper) AbstractComplementaryConfigurableAction.configurations.get(resource.getURI());
83                 if (conf == null || (reloadable && conf.lastModified != resource.getLastModified())) {
84                     getLogger().debug("(Re)Loading " + descriptor);
85
86                     if (conf == null) {
87                         conf = new ConfigurationHelper();
88                     }
89
90                     SAXConfigurationHandler builder = new SAXConfigurationHandler();
91                     SourceUtil.parse(this.manager, resource, builder);
92
93                     conf.lastModified = resource.getLastModified();
94                     conf.configuration = builder.getConfiguration();
95
96                     AbstractComplementaryConfigurableAction.configurations.put(resource.getURI(), conf);
97                 } else {
98                     getLogger().debug("Using cached configuration for " + descriptor);
99                 }
100             } catch (Exception JavaDoc e) {
101                 getLogger().error("Could not configure Database mapping environment", e);
102                 throw new ConfigurationException("Error trying to load configurations for resource: "
103                     + (resource == null ? "null" : resource.getURI()));
104             } finally {
105                 resolver.release(resource);
106             }
107         }
108
109         return conf.configuration;
110     }
111 }
112
Popular Tags