KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > management > ReconfiguratorXml


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Florent Benoit
22  * Contributor(s):
23  *
24  * --------------------------------------------------------------------------
25  * $Id: ReconfiguratorXml.java,v 1.3 2005/04/28 08:43:25 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package org.objectweb.jonas.management;
30
31 import java.io.BufferedWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.FileWriter JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * This class allows for persistent reconfiguration of a JOnAS service or of a JOnAS resource
41  * being configured by a .xml file.
42  */

43 public class ReconfiguratorXml extends AbsReconfigurator {
44
45
46     /**
47      * Content of the file
48      */

49     private String JavaDoc xml = null;
50
51     /**
52      * Construct a reconfigurator for a JOnAS service or a JOnAS resource
53      * @param name Name of the JOnAS service or JOnAS resource to which this object is associated
54      * @param configFileName name of the config file
55      * @param xml XML content of the file
56      */

57     public ReconfiguratorXml(String JavaDoc name, String JavaDoc configFileName, String JavaDoc xml) {
58         super(name, configFileName);
59         this.xml = xml;
60     }
61
62     /**
63      * Updates the configuration file
64      * @param xml The new configuration
65      * @param sequence the sequence number of management notification producing the update
66      */

67     void updateConfig(String JavaDoc xml, long sequence) {
68         if (sequence > lastSequence) {
69             this.xml = xml;
70             lastSequence = sequence;
71         } else {
72             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
73         }
74     }
75
76
77     /**
78      * Saves the updated configuration
79      * @param sequence the sequence number of management notification producing the save (in fact store) operation
80      * @throws ReconfigException if the saveConfig could not be done
81      */

82     public void saveConfig(long sequence) throws ReconfigException {
83         if (sequence > lastSequence) {
84             try {
85
86                 BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(new File JavaDoc(configFileName)));
87                 out.write(xml);
88                 out.flush();
89                 out.close();
90                 lastSequence = sequence;
91                 if (logger.isLoggable(BasicLevel.DEBUG)) {
92                     logger.log(BasicLevel.DEBUG, "Configuration file " + configFileName + " updated");
93                 }
94             } catch (FileNotFoundException JavaDoc e) {
95                 throw new ReconfigException("Cant' save configuration file: " + e.toString());
96             } catch(IOException JavaDoc ioe) {
97                 throw new ReconfigException("Cant' save configuration file: " + ioe.toString());
98             }
99         } else {
100             logger.log(BasicLevel.WARN, "Received out of order save reconfiguration message for " + name + " !");
101             logger.log(BasicLevel.WARN, "Can not save !!");
102             logger.log(BasicLevel.WARN, "Please reconfigure and than save !!");
103         }
104
105     }
106
107
108 }
109
Popular Tags