Source Code Cross Referenced for ApplicationConfigManager.java in  » JMX » jmanage » org » jmanage » core » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » jmanage » org.jmanage.core.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.