001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClusterDaemonConfigurationManager.java 8443 2006-06-09 10:01:52Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_clusterd.lib;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.InputStreamReader;
031: import java.io.Reader;
032:
033: import org.objectweb.jonas_clusterd.api.ClusterDaemonConfiguration;
034: import org.objectweb.jonas_clusterd.api.ClusterDaemonConfigurationException;
035: import org.objectweb.jonas_clusterd.api.ClusterDaemonSchemas;
036: import org.objectweb.jonas_clusterd.rules.ClusterDaemonRuleSet;
037: import org.objectweb.jonas_clusterd.xml.ClusterDaemon;
038:
039: import org.objectweb.jonas.common.Log;
040: import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
041: import org.objectweb.jonas_lib.deployment.digester.JDigester;
042: import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * Description of the cluster daemon configuration
048: * @author Benoit Pelletier
049: */
050: public class ClusterDaemonConfigurationManager extends
051: AbsDeploymentDescManager {
052:
053: /**
054: * Path of the clusterd.xml configuration file
055: */
056: public static final String CLUSTERD_FILE_NAME = "clusterd.xml";
057:
058: /**
059: * Digester used to parse clusterd.xml
060: */
061: private static JDigester clusterDaemonDigester = null;
062:
063: /**
064: * Rules to parse the application.xml
065: */
066: private static ClusterDaemonRuleSet clusterDaemonRuleSet = new ClusterDaemonRuleSet();
067:
068: /**
069: * logger
070: */
071: private static Logger logger = Log
072: .getLogger(Log.JONAS_CLUSTER_DAEMON);
073:
074: /**
075: * Flag for parser validation
076: */
077: private static boolean parsingWithValidation = true;
078:
079: /**
080: * Private Empty constructor for utility class
081: */
082: private ClusterDaemonConfigurationManager() {
083: }
084:
085: /**
086: * Get an instance of a ClusterDaemonConfiguration by parsing the clusterd.xml configuration file.
087: * @param clusterDaemonFileName used when specific cluster daemon configuration file name has to be used
088: * @param classLoaderForCls the classloader for the classes.
089: * @return a ClusterDaemonConfiguration instance by parsing the clusterd.xml file
090: * @throws ClusterDaemonConfigurationException if the clusterd.xml file is corrupted.
091: */
092: public static ClusterDaemonConfiguration getClusterDaemonConfiguration(
093: String clusterDaemonFileName, ClassLoader classLoaderForCls)
094: throws ClusterDaemonConfigurationException {
095:
096: //Input Stream
097: InputStream is = null;
098: String fileName = null;
099: if (clusterDaemonFileName == null) {
100: // clusterd.xml in JONAS_BASE/conf
101: fileName = System.getProperty("jonas.base")
102: + File.separator + "conf" + File.separator
103: + CLUSTERD_FILE_NAME;
104: } else {
105: fileName = clusterDaemonFileName;
106: }
107: // load clusterd.xml
108: File clusterDaemonFile = new File(fileName);
109: if (!clusterDaemonFile.exists()) {
110: is = classLoaderForCls
111: .getResourceAsStream(CLUSTERD_FILE_NAME);
112: if (is == null) {
113: throw new ClusterDaemonConfigurationException(
114: "Cannot read the " + fileName + " and "
115: + CLUSTERD_FILE_NAME
116: + " is not accessible in the classpath");
117: }
118: } else {
119: try {
120: is = new FileInputStream(clusterDaemonFile);
121: } catch (Exception e) {
122: throw new ClusterDaemonConfigurationException(
123: "Cannot read the " + CLUSTERD_FILE_NAME, e);
124: }
125: }
126: ClusterDaemon clusterDaemon = loadClusterDaemon(
127: new InputStreamReader(is), CLUSTERD_FILE_NAME);
128: try {
129: is.close();
130: } catch (IOException e) {
131: // Can't close the file
132: logger.log(BasicLevel.WARN, "Cannot close InputStream for "
133: + CLUSTERD_FILE_NAME);
134: }
135:
136: // instantiate the domain map
137: ClusterDaemonConfiguration clusterDaemonConfiguration = new ClusterDaemonConfiguration(
138: clusterDaemon);
139: return clusterDaemonConfiguration;
140: }
141:
142: /**
143: * Load the clusterd.xml file.
144: * @param reader the Reader of the XML file.
145: * @param fileName the name of the file (clusterd.xml).
146: * @throws ClusterDaemonConfigurationException if the file is corrupted.
147: * @return a ClusterDaemon object.
148: */
149: public static ClusterDaemon loadClusterDaemon(Reader reader,
150: String fileName) throws ClusterDaemonConfigurationException {
151:
152: ClusterDaemon clusterDaemon = new ClusterDaemon();
153: // Create if domainDigester is null
154: if (clusterDaemonDigester == null) {
155: try {
156: // Create and initialize the digester
157:
158: clusterDaemonDigester = new JDigester(
159: clusterDaemonRuleSet,
160: getParsingWithValidation(), true, null,
161: new ClusterDaemonSchemas());
162: } catch (DeploymentDescException e) {
163: throw new ClusterDaemonConfigurationException(e);
164: }
165: }
166:
167: try {
168: clusterDaemonDigester
169: .parse(reader, fileName, clusterDaemon);
170: } catch (DeploymentDescException e) {
171: throw new ClusterDaemonConfigurationException(e);
172: } finally {
173: clusterDaemonDigester.push(null);
174: }
175:
176: return clusterDaemon;
177: }
178:
179: /**
180: * Controls whether the parser is reporting all validity errors.
181: * @return if true, all external entities will be read.
182: */
183: public static boolean getParsingWithValidation() {
184: return parsingWithValidation;
185: }
186:
187: /**
188: * Controls whether the parser is reporting all validity errors.
189: * @param validation if true, all external entities will be read.
190: */
191: public static void setParsingWithValidation(boolean validation) {
192: ClusterDaemonConfigurationManager.parsingWithValidation = validation;
193: }
194: }
|