KickJava   Java API By Example, From Geeks To Geeks.

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


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): Adriana Danes
22  * Contributor(s):
23  *
24  * --------------------------------------------------------------------------
25  * $Id: ReconfiguratorProp.java,v 1.5 2005/04/28 08:43:25 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28
29 package org.objectweb.jonas.management;
30
31 // general Java imports
32
import java.io.FileNotFoundException JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Properties JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38
39 import org.objectweb.util.monolog.api.BasicLevel;
40 /**
41  * This class allows for persistent reconfiguration of a JOnAS service or of a JOnAS resource
42  * being configured by a .properties file.
43  * @author Adriana Danes
44  */

45 public class ReconfiguratorProp extends AbsReconfigurator {
46
47     /**
48      * Initial configuration values, than saved reconfigured values
49      */

50     private Properties JavaDoc stableConfig;
51
52     /**
53      * Reconfigured values (non yet saved)
54      */

55     private Properties JavaDoc currentConfig;
56
57     /**
58      * Construct a reconfigurator for a JOnAS service or a JOnAS resource
59      * @param name the name of the .properties configuration file to be updated. It may be 'jonas.properties' if the Reconfigurator
60      * is associated to a JOnAS service, or a 'resource.properties' if the Reconfigurator is associated to a
61      * data source or a mail factory.
62      * @param conf the initial content of the .properties configuration file
63      */

64     public ReconfiguratorProp(String JavaDoc name, String JavaDoc configFileName, Properties JavaDoc config) {
65         super(name, configFileName);
66         stableConfig = config;
67         currentConfig = new Properties JavaDoc();
68     }
69
70     /**
71      * Updates the configuration file
72      * @param key the modified property name
73      * @param value the new value
74      * @param sequence the sequence number of management notification producing the update
75      */

76     public void updateConfig(String JavaDoc key, String JavaDoc value, long sequence) {
77         if (sequence > lastSequence) {
78             currentConfig.setProperty(key, value);
79             lastSequence = sequence;
80             if (logger.isLoggable(BasicLevel.DEBUG)) {
81                 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " with value : " + value);
82             }
83         } else {
84             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
85             logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!");
86         }
87     }
88
89     /**
90      * Updates the configuration file
91      * @param key the modified property name
92      * @param value the new value
93      * @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
94      * @param sequence the sequence number of management notification producing the update
95      */

96     void updateConfig(String JavaDoc key, String JavaDoc value, boolean add, long sequence) {
97         if (sequence > lastSequence) {
98             String JavaDoc oldValue = currentConfig.getProperty(key);
99             if (oldValue == null)
100                 oldValue = stableConfig.getProperty(key);
101             if (logger.isLoggable(BasicLevel.DEBUG)) {
102                 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " having value : " + oldValue);
103             }
104             String JavaDoc newValue = updateValue(oldValue, value, add);
105             currentConfig.setProperty(key, newValue);
106             lastSequence = sequence;
107             if (logger.isLoggable(BasicLevel.DEBUG)) {
108                 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + key + " with value : " + newValue);
109             }
110         } else {
111             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
112             logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!");
113         }
114     }
115     /**
116      * Updates the configuration file
117      * @param props set of modified properties with their associated values
118      * @param sequence the sequence number of management notification producing the update
119      */

120     void updateConfig(Properties JavaDoc props, long sequence) {
121         if (sequence > lastSequence) {
122             for(Enumeration JavaDoc pNames = props.propertyNames(); pNames.hasMoreElements() ; ) {
123                 String JavaDoc aName = (String JavaDoc)pNames.nextElement();
124                 String JavaDoc aValue = (String JavaDoc)props.getProperty(aName);
125                 if (aValue != null) {
126                     currentConfig.setProperty(aName, aValue);
127                     if (logger.isLoggable(BasicLevel.DEBUG)) {
128                         logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + aName + " with value : " + aValue);
129                     }
130                 }
131             }
132             lastSequence = sequence;
133         } else {
134             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
135         }
136     }
137
138     String JavaDoc updateValue(String JavaDoc oldValue, String JavaDoc value, boolean add) {
139         value = value.trim();
140         oldValue = oldValue.trim();
141         String JavaDoc returnValue;
142         if (add) {
143             returnValue = new String JavaDoc(oldValue);
144             returnValue = returnValue + ',' + value;
145         } else {
146             if (logger.isLoggable(BasicLevel.DEBUG)) {
147                 logger.log(BasicLevel.DEBUG, "Remove " + value + " from " + oldValue);
148             }
149             returnValue = new String JavaDoc();
150             boolean firstToken = true;
151             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(oldValue, ",");
152             while (st.hasMoreTokens()) {
153                 String JavaDoc token = st.nextToken().trim();
154                 if (!token.equals(value)) {
155                     // add the token
156
if (firstToken) {
157                         returnValue = new String JavaDoc(token);
158                         firstToken = false;
159                     } else {
160                         returnValue = returnValue + ',' + token;
161                     }
162                 }
163             }
164         }
165         return returnValue;
166     }
167
168     /**
169      * Saves the updated configuration
170      * @param sequence the sequence number of management notification producing the save (in fact store) operation
171      */

172     public void saveConfig(long sequence) throws ReconfigException {
173         if (logger.isLoggable(BasicLevel.DEBUG)) {
174             logger.log(BasicLevel.DEBUG, "");
175         }
176         if (sequence > lastSequence) {
177             // Update stableConfig with properties in currentConfig
178
for (Enumeration JavaDoc props = currentConfig.keys() ; props.hasMoreElements() ;) {
179                 String JavaDoc reconfiguredProp = (String JavaDoc)props.nextElement();
180                 String JavaDoc reconfiguredPropValue = currentConfig.getProperty(reconfiguredProp);
181                 stableConfig.setProperty(reconfiguredProp, reconfiguredPropValue);
182             }
183             try {
184                 // Store stableConfig
185
FileOutputStream JavaDoc fo = new FileOutputStream JavaDoc(configFileName);
186                 stableConfig.store(fo, "Saved configuration file at ");
187                 fo.close();
188                 lastSequence = sequence;
189                 if (logger.isLoggable(BasicLevel.DEBUG)) {
190                     logger.log(BasicLevel.DEBUG, "Configuration file " + configFileName + " updated");
191                 }
192             } catch (FileNotFoundException JavaDoc e) {
193                 throw new ReconfigException("Cant' save configuration file: " + e.toString());
194             } catch(IOException JavaDoc ioe) {
195                 throw new ReconfigException("Cant' save configuration file: " + ioe.toString());
196             }
197         } else {
198             logger.log(BasicLevel.WARN, "Received out of order save reconfiguration message for " + name + " !");
199             logger.log(BasicLevel.WARN, "Can not save !!");
200             logger.log(BasicLevel.WARN, "Please reconfigure and than save !!");
201             currentConfig = new Properties JavaDoc();
202         }
203     }
204 }
205
Popular Tags