001: package hero.ra.config;
002:
003: /**
004: *
005: * Bonita
006: * Copyright (C) 1999 Bull S.A.
007: * Bull 68 route de versailles 78434 Louveciennes Cedex France
008: * Further information: bonita@objectweb.org
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023: * USA
024: *
025: *
026: --------------------------------------------------------------------------
027: * $Id$
028: *
029: --------------------------------------------------------------------------
030: */
031:
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035: import java.util.Properties;
036:
037: import org.apache.log4j.Level;
038: import org.apache.log4j.Logger;
039:
040: import hero.interfaces.Constants;
041: import hero.ra.BonitaService;
042: import hero.user.ReadEnv;
043: import hero.util.ServerType;
044:
045: public class Config implements BonitaService, ConfigMBean {
046: private Properties bonitaConfig = new Properties();
047: private String bonitaHome = null;
048: private boolean jms = true;
049: private String historic = Constants.TRANSFER;
050: private boolean jabberEnabled = false;
051: private String jabberPassword = null;
052: private int jabberPort = 0;
053: private String jabberHost = null;
054: private String jabberUser = null;
055: private static final Logger log = Logger.getLogger("log");
056: private static final Logger trace = Logger.getLogger("hero");
057: private Properties log4jConfig = new Properties();
058: private String log4jConfigPath = null;
059:
060: public void startService() throws Exception {
061: ReadEnv re = new ReadEnv();
062: try {
063: this .bonitaHome = re.getVariable("BONITA_HOME");
064: bonitaConfig.load(new FileInputStream(bonitaHome
065: + File.separator + ".ant.properties"));
066: } catch (Exception bh) {
067: throw new Exception(
068: "Error while starting the Configuration service, please check your BONITA_HOME/.ant.properties file");
069: }
070:
071: String jms = (String) bonitaConfig.getProperty("bonita.Jms");
072: if (jms.equalsIgnoreCase("true")
073: || jms.equalsIgnoreCase("false")) {
074: this .jms = new Boolean(jms).booleanValue();
075: } else {
076: this .jms = true;
077: throw new Exception(
078: "Error while retrieving the \"bonita.Jms\" config value, please check your BONITA_HOME/.ant.properties file");
079: }
080:
081: String historic = (String) bonitaConfig
082: .getProperty("bonita.Historic");
083: if (historic.equalsIgnoreCase(Constants.TRANSFER)
084: || historic.equalsIgnoreCase(Constants.PURGE)) {
085: this .historic = historic;
086: } else {
087: this .historic = Constants.TRANSFER;
088: throw new Exception(
089: "Error while retrieving the \"bonita.Historic\" config value, please check your BONITA_HOME/.ant.properties file");
090: }
091:
092: jabberEnabled = new Boolean((String) bonitaConfig
093: .getProperty("jabber.enabled")).booleanValue();
094: jabberPassword = (String) bonitaConfig
095: .getProperty("jabber.password");
096: jabberPort = Integer.parseInt((String) bonitaConfig
097: .getProperty("jabber.port"));
098: jabberHost = (String) bonitaConfig.getProperty("jabber.host");
099: jabberUser = (String) bonitaConfig.getProperty("jabber.user");
100:
101: /*
102: * In some application servers such as JBoss, the log4j configuration
103: * is stored in an XML file instead of a properties file. This is why
104: * we retrieve the levels directly from the API and do not parse
105: * log4j.properties like in previous versions of Bonita. We still
106: * persist log level updates in case the server is JOnAS.
107: */
108: if (ServerType.isJonas()) {
109: this .log4jConfigPath = re.getVariable("JONAS_BASE")
110: + File.separator + "conf" + File.separator
111: + "log4j.properties";
112: this .log4jConfig.load(new FileInputStream(log4jConfigPath));
113: }
114: }
115:
116: public void stopService() {
117: }
118:
119: public boolean getJabberEnabled() {
120: return jabberEnabled;
121: }
122:
123: public String getJabberHost() {
124: return jabberHost;
125: }
126:
127: public String getJabberPassword() {
128: return jabberPassword;
129: }
130:
131: public int getJabberPort() {
132: return jabberPort;
133: }
134:
135: public String getJabberUser() {
136: return jabberUser;
137: }
138:
139: public boolean getJms() {
140: return (this .jms);
141: }
142:
143: public String getLogLevel() {
144: return log.getLevel().toString();
145: }
146:
147: public String getTraceLevel() {
148: return trace.getLevel().toString();
149: }
150:
151: public String getHistoric() {
152: return (this .historic);
153: }
154:
155: public String getProperty(String key) {
156: return (String) bonitaConfig.getProperty(key);
157: }
158:
159: public void updateJabberEnabled(boolean enabled) throws Exception {
160: try {
161: this .jabberEnabled = enabled;
162: bonitaConfig.setProperty("jabber.enabled", new Boolean(
163: enabled).toString());
164: bonitaConfig.save(new FileOutputStream(this .bonitaHome
165: + File.separator + ".ant.properties"), null);
166: } catch (Exception ex) {
167: throw new Exception(
168: "Error while storing config settings... ", ex);
169: }
170: }
171:
172: public void updateJabberHost(String jabberHost) throws Exception {
173: try {
174: this .jabberHost = jabberHost;
175: bonitaConfig.setProperty("jabber.host", jabberHost);
176: bonitaConfig.save(new FileOutputStream(this .bonitaHome
177: + File.separator + ".ant.properties"), null);
178: } catch (Exception ex) {
179: throw new Exception(
180: "Error while storing config settings... ", ex);
181: }
182: }
183:
184: public void updateJabberPassword(String jabberPassword)
185: throws Exception {
186: try {
187: this .jabberPassword = jabberPassword;
188: bonitaConfig.setProperty("jabber.password", jabberPassword);
189: bonitaConfig.save(new FileOutputStream(this .bonitaHome
190: + File.separator + ".ant.properties"), null);
191: } catch (Exception ex) {
192: throw new Exception(
193: "Error while storing config settings... ", ex);
194: }
195: }
196:
197: public void updateJabberPort(int jabberPort) throws Exception {
198: try {
199: this .jabberPort = jabberPort;
200: bonitaConfig.setProperty("jabber.port", String
201: .valueOf(jabberPort));
202: bonitaConfig.save(new FileOutputStream(this .bonitaHome
203: + File.separator + ".ant.properties"), null);
204: } catch (Exception ex) {
205: throw new Exception(
206: "Error while storing config settings... ", ex);
207: }
208: }
209:
210: public void updateJabberUser(String jabberUser) throws Exception {
211: try {
212: this .jabberUser = jabberUser;
213: bonitaConfig.setProperty("jabber.user", jabberUser);
214: bonitaConfig.save(new FileOutputStream(this .bonitaHome
215: + File.separator + ".ant.properties"), null);
216: } catch (Exception ex) {
217: throw new Exception(
218: "Error while storing config settings... ", ex);
219: }
220: }
221:
222: public void updateJms(boolean value) throws Exception {
223: try {
224: this .jms = value;
225: bonitaConfig.setProperty("bonita.Jms", new Boolean(value)
226: .toString());
227: bonitaConfig.save(new FileOutputStream(this .bonitaHome
228: + File.separator + ".ant.properties"), null);
229: } catch (Exception ex) {
230: throw new Exception(
231: "Error while storing config settings... ", ex);
232: }
233: }
234:
235: public void updateLogLevel(String value) throws Exception {
236: try {
237: if (value.equalsIgnoreCase(Constants.DEBUG)
238: || value.equalsIgnoreCase(Constants.INFO)
239: || value.equalsIgnoreCase(Constants.ERROR)) {
240: log.setLevel(Level.toLevel(value));
241:
242: if (ServerType.isJonas()) {
243: log4jConfig.setProperty("log4j.logger.log", value
244: + ", bonitaLog");
245: log4jConfig.save(new FileOutputStream(
246: this .log4jConfigPath), null);
247: } else {
248: trace
249: .info("Do not persist the log level update as the application server may not support the log4j.properties file.");
250: }
251: }
252: } catch (Exception ex) {
253: throw new Exception(
254: "Error while storing config settings... ", ex);
255: }
256: }
257:
258: public void updateTraceLevel(String value) throws Exception {
259: try {
260: if (value.equalsIgnoreCase(Constants.DEBUG)
261: || value.equalsIgnoreCase(Constants.INFO)
262: || value.equalsIgnoreCase(Constants.ERROR)) {
263: trace.setLevel(Level.toLevel(value));
264:
265: if (ServerType.isJonas()) {
266: log4jConfig.setProperty("log4j.logger.hero", value
267: + ", bonita");
268: log4jConfig.save(new FileOutputStream(
269: this .log4jConfigPath), null);
270: } else {
271: trace
272: .info("Do not persist the trace level update as the application server may not support the log4j.properties file.");
273: }
274: }
275: } catch (Exception ex) {
276: throw new Exception(
277: "Error while storing config settings... ", ex);
278: }
279: }
280:
281: public void updateHistoric(String value) throws Exception {
282: try {
283: if (value.equalsIgnoreCase(Constants.TRANSFER)
284: || value.equalsIgnoreCase(Constants.PURGE)) {
285: this .historic = value;
286: bonitaConfig.setProperty("bonita.Historic", value);
287: bonitaConfig.save(new FileOutputStream(this .bonitaHome
288: + File.separator + ".ant.properties"), null);
289: }
290: } catch (Exception ex) {
291: throw new Exception(
292: "Error while storing config settings... ", ex);
293: }
294: }
295:
296: public void updateProperty(String key, String value)
297: throws Exception {
298: try {
299: bonitaConfig.setProperty(key, value);
300: bonitaConfig.save(new FileOutputStream(this .bonitaHome
301: + File.separator + ".ant.properties"), null);
302: } catch (Exception ex) {
303: throw new Exception(
304: "Error while storing config settings... ", ex);
305: }
306: }
307: }
|