001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
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: * Initial developer(s): Adriana Danes
022: * Contributor(s):
023: *
024: * --------------------------------------------------------------------------
025: * $Id: ReconfiguratorProp.java 6661 2005-04-28 08:43:27Z benoitf $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas.management;
030:
031: // general Java imports
032: import java.io.FileNotFoundException;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.util.Enumeration;
036: import java.util.Properties;
037: import java.util.StringTokenizer;
038:
039: import org.objectweb.util.monolog.api.BasicLevel;
040:
041: /**
042: * This class allows for persistent reconfiguration of a JOnAS service or of a JOnAS resource
043: * being configured by a .properties file.
044: * @author Adriana Danes
045: */
046: public class ReconfiguratorProp extends AbsReconfigurator {
047:
048: /**
049: * Initial configuration values, than saved reconfigured values
050: */
051: private Properties stableConfig;
052:
053: /**
054: * Reconfigured values (non yet saved)
055: */
056: private Properties currentConfig;
057:
058: /**
059: * Construct a reconfigurator for a JOnAS service or a JOnAS resource
060: * @param name the name of the .properties configuration file to be updated. It may be 'jonas.properties' if the Reconfigurator
061: * is associated to a JOnAS service, or a 'resource.properties' if the Reconfigurator is associated to a
062: * data source or a mail factory.
063: * @param conf the initial content of the .properties configuration file
064: */
065: public ReconfiguratorProp(String name, String configFileName,
066: Properties config) {
067: super (name, configFileName);
068: stableConfig = config;
069: currentConfig = new Properties();
070: }
071:
072: /**
073: * Updates the configuration file
074: * @param key the modified property name
075: * @param value the new value
076: * @param sequence the sequence number of management notification producing the update
077: */
078: public void updateConfig(String key, String value, long sequence) {
079: if (sequence > lastSequence) {
080: currentConfig.setProperty(key, value);
081: lastSequence = sequence;
082: if (logger.isLoggable(BasicLevel.DEBUG)) {
083: logger.log(BasicLevel.DEBUG, "- " + sequence
084: + " - Recufigured property to update " + key
085: + " with value : " + value);
086: }
087: } else {
088: logger.log(BasicLevel.WARN,
089: "Received out of order reconfiguration message !");
090: logger.log(BasicLevel.WARN,
091: "Reconfiguration value for property " + key + " : "
092: + value + " lost!");
093: }
094: }
095:
096: /**
097: * Updates the configuration file
098: * @param key the modified property name
099: * @param value the new value
100: * @param add if true, the value has to be appended to the old value, if false, it has to be removed from the old value
101: * @param sequence the sequence number of management notification producing the update
102: */
103: void updateConfig(String key, String value, boolean add,
104: long sequence) {
105: if (sequence > lastSequence) {
106: String oldValue = currentConfig.getProperty(key);
107: if (oldValue == null)
108: oldValue = stableConfig.getProperty(key);
109: if (logger.isLoggable(BasicLevel.DEBUG)) {
110: logger.log(BasicLevel.DEBUG, "- " + sequence
111: + " - Recufigured property to update " + key
112: + " having value : " + oldValue);
113: }
114: String newValue = updateValue(oldValue, value, add);
115: currentConfig.setProperty(key, newValue);
116: lastSequence = sequence;
117: if (logger.isLoggable(BasicLevel.DEBUG)) {
118: logger.log(BasicLevel.DEBUG, "- " + sequence
119: + " - Updated property " + key
120: + " with value : " + newValue);
121: }
122: } else {
123: logger.log(BasicLevel.WARN,
124: "Received out of order reconfiguration message !");
125: logger.log(BasicLevel.WARN,
126: "Reconfiguration value for property " + key + " : "
127: + value + " lost!");
128: }
129: }
130:
131: /**
132: * Updates the configuration file
133: * @param props set of modified properties with their associated values
134: * @param sequence the sequence number of management notification producing the update
135: */
136: void updateConfig(Properties props, long sequence) {
137: if (sequence > lastSequence) {
138: for (Enumeration pNames = props.propertyNames(); pNames
139: .hasMoreElements();) {
140: String aName = (String) pNames.nextElement();
141: String aValue = (String) props.getProperty(aName);
142: if (aValue != null) {
143: currentConfig.setProperty(aName, aValue);
144: if (logger.isLoggable(BasicLevel.DEBUG)) {
145: logger.log(BasicLevel.DEBUG, "- " + sequence
146: + " - Updated property " + aName
147: + " with value : " + aValue);
148: }
149: }
150: }
151: lastSequence = sequence;
152: } else {
153: logger.log(BasicLevel.WARN,
154: "Received out of order reconfiguration message !");
155: }
156: }
157:
158: String updateValue(String oldValue, String value, boolean add) {
159: value = value.trim();
160: oldValue = oldValue.trim();
161: String returnValue;
162: if (add) {
163: returnValue = new String(oldValue);
164: returnValue = returnValue + ',' + value;
165: } else {
166: if (logger.isLoggable(BasicLevel.DEBUG)) {
167: logger.log(BasicLevel.DEBUG, "Remove " + value
168: + " from " + oldValue);
169: }
170: returnValue = new String();
171: boolean firstToken = true;
172: StringTokenizer st = new StringTokenizer(oldValue, ",");
173: while (st.hasMoreTokens()) {
174: String token = st.nextToken().trim();
175: if (!token.equals(value)) {
176: // add the token
177: if (firstToken) {
178: returnValue = new String(token);
179: firstToken = false;
180: } else {
181: returnValue = returnValue + ',' + token;
182: }
183: }
184: }
185: }
186: return returnValue;
187: }
188:
189: /**
190: * Saves the updated configuration
191: * @param sequence the sequence number of management notification producing the save (in fact store) operation
192: */
193: public void saveConfig(long sequence) throws ReconfigException {
194: if (logger.isLoggable(BasicLevel.DEBUG)) {
195: logger.log(BasicLevel.DEBUG, "");
196: }
197: if (sequence > lastSequence) {
198: // Update stableConfig with properties in currentConfig
199: for (Enumeration props = currentConfig.keys(); props
200: .hasMoreElements();) {
201: String reconfiguredProp = (String) props.nextElement();
202: String reconfiguredPropValue = currentConfig
203: .getProperty(reconfiguredProp);
204: stableConfig.setProperty(reconfiguredProp,
205: reconfiguredPropValue);
206: }
207: try {
208: // Store stableConfig
209: FileOutputStream fo = new FileOutputStream(
210: configFileName);
211: stableConfig.store(fo, "Saved configuration file at ");
212: fo.close();
213: lastSequence = sequence;
214: if (logger.isLoggable(BasicLevel.DEBUG)) {
215: logger.log(BasicLevel.DEBUG, "Configuration file "
216: + configFileName + " updated");
217: }
218: } catch (FileNotFoundException e) {
219: throw new ReconfigException(
220: "Cant' save configuration file: "
221: + e.toString());
222: } catch (IOException ioe) {
223: throw new ReconfigException(
224: "Cant' save configuration file: "
225: + ioe.toString());
226: }
227: } else {
228: logger.log(BasicLevel.WARN,
229: "Received out of order save reconfiguration message for "
230: + name + " !");
231: logger.log(BasicLevel.WARN, "Can not save !!");
232: logger.log(BasicLevel.WARN,
233: "Please reconfigure and than save !!");
234: currentConfig = new Properties();
235: }
236: }
237: }
|