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: */
018:
019: /* $Id: RCEnvironment.java 531479 2007-04-23 14:21:35Z andreas $ */
020:
021: package org.apache.lenya.cms.rc;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import org.apache.avalon.framework.configuration.Configurable;
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
032: import org.apache.avalon.framework.logger.AbstractLogEnabled;
033: import org.apache.avalon.framework.logger.Logger;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * Helper class that holds the revision controller configuration
038: */
039: public class RCEnvironment extends AbstractLogEnabled implements
040: Configurable {
041:
042: /**
043: * <code>CONFIGURATION_FILE</code> The configuration file
044: */
045: public static final String CONFIGURATION_FILE = "lenya"
046: + File.separator + "config" + File.separator + "rc"
047: + File.separator + "revision-controller.xconf";
048: /**
049: * <code>RCML_DIRECTORY</code> The RCML directory
050: */
051: public static final String RCML_DIRECTORY = "rcml-directory";
052: /**
053: * <code>BACKUP_DIRECTORY</code> The backup directory
054: */
055: public static final String BACKUP_DIRECTORY = "backup-directory";
056: private String rcmlDirectory;
057: private String backupDirectory;
058:
059: private static Map instances = new HashMap();
060:
061: /**
062: * Returns the singleton RC environment for this context path.
063: * @param contextPath The context path (the Lenya webapp directory).
064: * @param logger The logger.
065: * @return An RC environment.
066: */
067: public static RCEnvironment getInstance(String contextPath,
068: Logger logger) {
069: RCEnvironment instance = (RCEnvironment) instances
070: .get(contextPath);
071: if (instance == null) {
072: instance = new RCEnvironment(contextPath, logger);
073: instances.put(contextPath, instance);
074: }
075: return instance;
076: }
077:
078: /**
079: * Creates a new RCEnvironment object from the context path
080: * @param contextPath The context path
081: * @param logger The logger.
082: */
083: public RCEnvironment(String contextPath, Logger logger) {
084: enableLogging(logger);
085: getLogger().debug("context path:" + contextPath);
086:
087: String configurationFilePath = contextPath + "/"
088: + CONFIGURATION_FILE;
089: getLogger().debug(
090: "configuration file path:" + configurationFilePath);
091:
092: File configurationFile = new File(configurationFilePath);
093:
094: try {
095: DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
096: Configuration configuration = builder
097: .buildFromFile(configurationFile);
098: configure(configuration);
099: } catch (final ConfigurationException e) {
100: getLogger().error(
101: "Cannot load revision controller configuration! ",
102: e);
103: } catch (final SAXException e) {
104: getLogger().error(
105: "Cannot load revision controller configuration! ",
106: e);
107: } catch (final IOException e) {
108: getLogger().error(
109: "Cannot load revision controller configuration! ",
110: e);
111: }
112: }
113:
114: /**
115: @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
116: */
117: public void configure(
118: org.apache.avalon.framework.configuration.Configuration configuration)
119: throws ConfigurationException {
120: // revision controller
121: setRCMLDirectory(configuration.getChild("rcmlDirectory")
122: .getAttribute("href"));
123: setBackupDirectory(configuration.getChild("backupDirectory")
124: .getAttribute("href"));
125:
126: getLogger().debug(
127: "CONFIGURATION:\nRCML Directory: href="
128: + getRCMLDirectory());
129: getLogger().debug(
130: "CONFIGURATION:\nBackup Directory: href="
131: + getBackupDirectory());
132: }
133:
134: /**
135: * Get the RCML directory
136: * @return The RCML directory
137: */
138: public String getRCMLDirectory() {
139: return this .rcmlDirectory;
140: }
141:
142: /**
143: * Set the rcml directory
144: * @param rcmlDir the path to the rcml directory
145: */
146: protected void setRCMLDirectory(String rcmlDir) {
147: this .rcmlDirectory = rcmlDir;
148: }
149:
150: /**
151: * Get the backup directory
152: * @return The backup directory
153: */
154: public String getBackupDirectory() {
155: return this .backupDirectory;
156: }
157:
158: /**
159: * Set the backup directory
160: * @param backupDir path to the backup directory
161: */
162: protected void setBackupDirectory(String backupDir) {
163: this.backupDirectory = backupDir;
164: }
165: }
|