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): Florent Benoit
022: * Contributor(s):
023: *
024: * --------------------------------------------------------------------------
025: * $Id: ReconfiguratorXml.java 6661 2005-04-28 08:43:27Z benoitf $
026: * --------------------------------------------------------------------------
027: */
028:
029: package org.objectweb.jonas.management;
030:
031: import java.io.BufferedWriter;
032: import java.io.File;
033: import java.io.FileNotFoundException;
034: import java.io.FileWriter;
035: import java.io.IOException;
036:
037: import org.objectweb.util.monolog.api.BasicLevel;
038:
039: /**
040: * This class allows for persistent reconfiguration of a JOnAS service or of a JOnAS resource
041: * being configured by a .xml file.
042: */
043: public class ReconfiguratorXml extends AbsReconfigurator {
044:
045: /**
046: * Content of the file
047: */
048: private String xml = null;
049:
050: /**
051: * Construct a reconfigurator for a JOnAS service or a JOnAS resource
052: * @param name Name of the JOnAS service or JOnAS resource to which this object is associated
053: * @param configFileName name of the config file
054: * @param xml XML content of the file
055: */
056: public ReconfiguratorXml(String name, String configFileName,
057: String xml) {
058: super (name, configFileName);
059: this .xml = xml;
060: }
061:
062: /**
063: * Updates the configuration file
064: * @param xml The new configuration
065: * @param sequence the sequence number of management notification producing the update
066: */
067: void updateConfig(String xml, long sequence) {
068: if (sequence > lastSequence) {
069: this .xml = xml;
070: lastSequence = sequence;
071: } else {
072: logger.log(BasicLevel.WARN,
073: "Received out of order reconfiguration message !");
074: }
075: }
076:
077: /**
078: * Saves the updated configuration
079: * @param sequence the sequence number of management notification producing the save (in fact store) operation
080: * @throws ReconfigException if the saveConfig could not be done
081: */
082: public void saveConfig(long sequence) throws ReconfigException {
083: if (sequence > lastSequence) {
084: try {
085:
086: BufferedWriter out = new BufferedWriter(new FileWriter(
087: new File(configFileName)));
088: out.write(xml);
089: out.flush();
090: out.close();
091: lastSequence = sequence;
092: if (logger.isLoggable(BasicLevel.DEBUG)) {
093: logger.log(BasicLevel.DEBUG, "Configuration file "
094: + configFileName + " updated");
095: }
096: } catch (FileNotFoundException e) {
097: throw new ReconfigException(
098: "Cant' save configuration file: "
099: + e.toString());
100: } catch (IOException ioe) {
101: throw new ReconfigException(
102: "Cant' save configuration file: "
103: + ioe.toString());
104: }
105: } else {
106: logger.log(BasicLevel.WARN,
107: "Received out of order save reconfiguration message for "
108: + name + " !");
109: logger.log(BasicLevel.WARN, "Can not save !!");
110: logger.log(BasicLevel.WARN,
111: "Please reconfigure and than save !!");
112: }
113:
114: }
115:
116: }
|