001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
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: */
016:
017: package org.romaframework.core.config;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.romaframework.aspect.persistence.PersistenceAspect;
022: import org.romaframework.core.Utility;
023: import org.romaframework.core.aspect.AspectManager;
024: import org.romaframework.core.flow.ObjectContext;
025: import org.romaframework.core.module.ModuleManager;
026: import org.springframework.context.ApplicationContext;
027:
028: /**
029: * Central singleton class to gather all service using Spring Application Context.
030: *
031: * @author Luca Garulli (luca.garulli@assetdata.it)
032: */
033: public class RomaApplicationContext {
034:
035: public static final byte STATUS_DOWN = 0;
036: public static final byte STATUS_STARTUP = 1;
037: public static final byte STATUS_UP = 2;
038: public static final byte STATUS_SHUTDOWN = 3;
039:
040: protected String applicationName;
041: protected String applicationVersion = "1.0";
042: protected String applicationPackage;
043: protected boolean applicationDevelopment;
044: protected ApplicationContext springContext;
045: protected static String applicationPath;
046: protected static byte status = STATUS_DOWN;
047:
048: protected static RomaApplicationContext instance = null;
049: protected static Log log = LogFactory
050: .getLog(RomaApplicationContext.class);
051:
052: /**
053: * Constructor called by Spring configuration.
054: */
055: public RomaApplicationContext() {
056: instance = this ;
057: }
058:
059: protected synchronized void startup(ApplicationContext iContext) {
060: status = STATUS_STARTUP;
061:
062: springContext = iContext;
063:
064: // TODO REMOVE THIS
065: PersistenceAspect db = null;
066: if (ObjectContext.getInstance().existComponent(
067: PersistenceAspect.class))
068: db = ObjectContext.getInstance().getContextComponent(
069: PersistenceAspect.class);
070:
071: try {
072: // START ALL ASPECTS
073: AspectManager.getInstance().startup();
074:
075: // START ALL MODULES
076: ModuleManager.getInstance().startup();
077:
078: // STAR USER APPLICATION CONFIGURATION OBJECT
079: getBean(ApplicationConfiguration.class).startup();
080:
081: if (db != null)
082: db.commit();
083: } finally {
084: if (db != null) {
085: db.close();
086: ObjectContext.getInstance().setContextComponent(
087: PersistenceAspect.class, null);
088: }
089: }
090:
091: status = STATUS_UP;
092: }
093:
094: public synchronized void shutdown() {
095: status = STATUS_SHUTDOWN;
096:
097: // STOP USER APPLICATION CONFIGURATION OBJECT
098: getBean(ApplicationConfiguration.class).shutdown();
099:
100: // STOP ALL MODULES
101: ModuleManager.getInstance().shutdown();
102:
103: // STOP ALL ASPECTS
104: AspectManager.getInstance().shutdown();
105:
106: springContext = null;
107:
108: status = STATUS_DOWN;
109: }
110:
111: public <T> T getBean(Class<T> iBeanClass) {
112: return (T) springContext.getBean(iBeanClass.getSimpleName(),
113: iBeanClass);
114: }
115:
116: public <T> T getBean(String iBeanName) {
117: return (T) springContext.getBean(iBeanName);
118: }
119:
120: /*
121: * public ServletConfig getServletConfig() { return servletConfig; }
122: */
123: public String getApplicationName() {
124: return applicationName;
125: }
126:
127: public void setApplicationName(String applicationName) {
128: this .applicationName = applicationName;
129: }
130:
131: public String getApplicationPackage() {
132: return applicationPackage;
133: }
134:
135: public void setApplicationPackage(String applicationPackage) {
136: this .applicationPackage = applicationPackage;
137: }
138:
139: public boolean isApplicationDevelopment() {
140: return applicationDevelopment;
141: }
142:
143: public void setApplicationDevelopment(boolean applicationDevelopment) {
144: this .applicationDevelopment = applicationDevelopment;
145: }
146:
147: public static String getApplicationPath() {
148: return applicationPath;
149: }
150:
151: public String getApplicationVersion() {
152: return applicationVersion;
153: }
154:
155: public void setApplicationVersion(String applicationVersion) {
156: this .applicationVersion = applicationVersion;
157: }
158:
159: public static void setApplicationPath(String iApplicationPath) {
160: iApplicationPath = Utility
161: .getUniversalResourcePath(iApplicationPath);
162:
163: if (iApplicationPath.endsWith(Utility.PATH_SEPARATOR_STRING))
164: iApplicationPath = iApplicationPath.substring(0,
165: iApplicationPath.length() - 1);
166:
167: applicationPath = iApplicationPath;
168:
169: log.info("[RomaApplicationContext.setApplicationPath] "
170: + applicationPath);
171: }
172:
173: public static RomaApplicationContext getInstance() {
174: return instance;
175: }
176:
177: public static void setup(ApplicationContext iSpringContext) {
178: if (instance != null) {
179: instance.startup(iSpringContext);
180: }
181: }
182:
183: public static byte getStatus() {
184: return status;
185: }
186:
187: public boolean existBean(String iComponentName) {
188: return springContext.containsBean(iComponentName);
189: }
190: }
|