001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.config;
016:
017: import java.util.*;
018: import java.io.File;
019:
020: /**
021: *
022: * date: Jun 13, 2004
023: * @author Rakesh Kalra
024: */
025: public class ApplicationConfigManager {
026:
027: private static List applicationConfigs = Collections
028: .synchronizedList(new LinkedList());
029:
030: private static final ConfigReader configReader = ConfigReader
031: .getInstance();
032:
033: static {
034: /* read the configuration */
035: applicationConfigs.addAll(configReader.read());
036: /* create a backup of configuration file */
037: new File(ConfigConstants.DEFAULT_CONFIG_FILE_NAME)
038: .renameTo(new File(
039: ConfigConstants.BOOTED_CONFIG_FILE_NAME));
040: /* write from memory */
041: ConfigWriter.getInstance().write(applicationConfigs);
042: }
043:
044: public static ApplicationConfig getApplicationConfigByName(
045: String appName) {
046:
047: for (Iterator it = applicationConfigs.iterator(); it.hasNext();) {
048: ApplicationConfig appConfig = (ApplicationConfig) it.next();
049: if (appConfig.getName().equals(appName)) {
050: return appConfig;
051: }
052: if (appConfig.isCluster()) {
053: for (Iterator it2 = appConfig.getApplications()
054: .iterator(); it2.hasNext();) {
055: appConfig = (ApplicationConfig) it2.next();
056: if (appConfig.getName().equals(appName)) {
057: return appConfig;
058: }
059: }
060: }
061: }
062: return null;
063: }
064:
065: public static ApplicationConfig getApplicationConfig(
066: String applicationId) {
067:
068: for (Iterator it = applicationConfigs.iterator(); it.hasNext();) {
069: ApplicationConfig appConfig = (ApplicationConfig) it.next();
070: if (appConfig.getApplicationId().equals(applicationId)) {
071: return appConfig;
072: }
073: if (appConfig.isCluster()) {
074: for (Iterator it2 = appConfig.getApplications()
075: .iterator(); it2.hasNext();) {
076: appConfig = (ApplicationConfig) it2.next();
077: if (appConfig.getApplicationId().equals(
078: applicationId)) {
079: return appConfig;
080: }
081: }
082: }
083: }
084: return null;
085: }
086:
087: /**
088: * Retrieve all configured applications.
089: *
090: * @return
091: */
092: public static List getApplications() {
093: return applicationConfigs;
094: }
095:
096: private static final Object writeLock = new Object();
097:
098: public static void addApplication(ApplicationConfig config)
099: throws DuplicateApplicationNameException {
100:
101: synchronized (writeLock) {
102: // validate the application name
103: validateAppName(config.getName(), null);
104: applicationConfigs.add(config);
105: saveConfig();
106: }
107: }
108:
109: public static void updateApplication(ApplicationConfig config)
110: throws DuplicateApplicationNameException {
111:
112: assert config != null : "application config is null";
113:
114: synchronized (writeLock) {
115: // validate the application name
116: validateAppName(config.getName(), config.getApplicationId());
117:
118: int index = applicationConfigs.indexOf(config);
119: if (index != -1) {
120: applicationConfigs.remove(index);
121: applicationConfigs.add(index, config);
122: } else {
123: /* its part of a cluster */
124: assert config.isCluster() == false;
125: ApplicationConfig clusterConfig = config
126: .getClusterConfig();
127: assert clusterConfig != null;
128: index = clusterConfig.getApplications().indexOf(config);
129: assert index != -1 : "application not found in cluster";
130: clusterConfig.getApplications().remove(index);
131: clusterConfig.getApplications().add(index, config);
132: }
133: saveConfig();
134: }
135: }
136:
137: public static ApplicationConfig deleteApplication(
138: String applicationId) {
139: assert applicationId != null : "applicationId is null";
140: ApplicationConfig config = getApplicationConfig(applicationId);
141: assert config != null : "there is no application with id="
142: + applicationId;
143: deleteApplication(config);
144: return (config);
145: }
146:
147: public static void deleteApplication(ApplicationConfig config) {
148: assert config != null : "application config is null";
149: if (!applicationConfigs.remove(config)) {
150: /* this app is in a cluster. remove from cluster */
151: for (Iterator it = applicationConfigs.iterator(); it
152: .hasNext();) {
153: ApplicationConfig appConfig = (ApplicationConfig) it
154: .next();
155: if (appConfig.isCluster()) {
156: ApplicationClusterConfig clusterConfig = (ApplicationClusterConfig) appConfig;
157: if (clusterConfig.removeApplication(config)) {
158: break;
159: }
160: }
161: }
162: }
163: saveConfig();
164: }
165:
166: private static void saveConfig() {
167: ConfigWriter writer = ConfigWriter.getInstance();
168: writer.write(applicationConfigs);
169: }
170:
171: public static List getAllApplications() {
172: Iterator appItr = applicationConfigs.iterator();
173: List applications = new LinkedList();
174: while (appItr.hasNext()) {
175: ApplicationConfig appConfig = (ApplicationConfig) appItr
176: .next();
177: applications.add(appConfig);
178: if (appConfig.isCluster()) {
179: applications.addAll(appConfig.getApplications());
180: }
181: }
182: return applications;
183: }
184:
185: public static List getAllAlerts() {
186: Iterator appItr = applicationConfigs.iterator();
187: List alerts = new LinkedList();
188: while (appItr.hasNext()) {
189: ApplicationConfig appConfig = (ApplicationConfig) appItr
190: .next();
191: alerts.addAll(appConfig.getAlerts());
192: if (appConfig.isCluster()) {
193: for (Iterator it = appConfig.getApplications()
194: .iterator(); it.hasNext();) {
195: ApplicationConfig childAppConfig = (ApplicationConfig) it
196: .next();
197: alerts.addAll(childAppConfig.getAlerts());
198: }
199: }
200: }
201: return alerts;
202: }
203:
204: private static void validateAppName(String appName,
205: String applicationId)
206: throws DuplicateApplicationNameException {
207: for (Iterator it = getApplications().iterator(); it.hasNext();) {
208: ApplicationConfig appConfig = (ApplicationConfig) it.next();
209: if (!appConfig.getApplicationId().equals(applicationId)
210: && (appConfig.getName().toUpperCase())
211: .equals(appName.toUpperCase())) {
212: throw new DuplicateApplicationNameException(appName);
213: }
214: }
215: }
216:
217: public static class DuplicateApplicationNameException extends
218: Exception {
219: // app name that is duplicate
220: private final String appName;
221:
222: public DuplicateApplicationNameException(String appName) {
223: super ("Application name: " + appName);
224: this .appName = appName;
225: }
226:
227: public String getAppName() {
228: return appName;
229: }
230: }
231: }
|