001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.kuali.rice.config;
018:
019: import java.util.LinkedList;
020: import java.util.List;
021: import java.util.Properties;
022:
023: import javax.sql.DataSource;
024: import javax.transaction.TransactionManager;
025: import javax.transaction.UserTransaction;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.log4j.Logger;
029: import org.kuali.rice.core.Core;
030: import org.kuali.rice.lifecycle.BaseCompositeLifecycle;
031: import org.kuali.rice.lifecycle.Lifecycle;
032: import org.kuali.rice.resourceloader.GlobalResourceLoader;
033: import org.kuali.rice.resourceloader.ResourceLoader;
034: import org.kuali.rice.resourceloader.RootResourceLoaderLifecycle;
035: import org.kuali.rice.security.credentials.CredentialsSourceFactory;
036: import org.springframework.beans.factory.DisposableBean;
037: import org.springframework.beans.factory.InitializingBean;
038:
039: /**
040: * Used to configure common Rice configuration properties.
041: *
042: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
043: *
044: */
045: public class RiceConfigurer extends BaseCompositeLifecycle implements
046: Configurer, InitializingBean, DisposableBean {
047:
048: private static final Logger LOG = Logger
049: .getLogger(RiceConfigurer.class);
050:
051: private String environment = "dev";
052: private String messageEntity;
053: private DataSource dataSource;
054: private String platform;
055: private UserTransaction userTransaction;
056: private TransactionManager transactionManager;
057: private String dataSourceJndiLocation;
058: private String userTransactionJndiLocation;
059: private String transactionManagerJndiLocation;
060: private CredentialsSourceFactory credentialsSourceFactory;
061:
062: private Config rootConfig;
063: private ResourceLoader rootResourceLoader;
064: private Properties properties;
065: private List<String> configLocations;
066:
067: private List<ModuleConfigurer> modules = new LinkedList<ModuleConfigurer>();
068:
069: public void afterPropertiesSet() throws Exception {
070: start();
071: }
072:
073: public void destroy() throws Exception {
074: stop();
075: }
076:
077: public void start() throws Exception {
078: initializeConfiguration();
079: super .start();
080: if (getRootResourceLoader() != null) {
081: if (!getRootResourceLoader().isStarted()) {
082: getRootResourceLoader().start();
083: }
084: GlobalResourceLoader
085: .addResourceLoaderFirst(getRootResourceLoader());
086: }
087: }
088:
089: protected List<Lifecycle> loadLifecycles() {
090: List<Lifecycle> lifecycles = new LinkedList<Lifecycle>();
091: lifecycles.add(new RootResourceLoaderLifecycle(
092: getRootResourceLoader()));
093: for (ModuleConfigurer module : this .modules) {
094: lifecycles.add(module);
095: }
096: return lifecycles;
097: }
098:
099: @SuppressWarnings("unchecked")
100: protected void initializeConfiguration() throws Exception {
101: LOG.info("Starting Rice configuration for message entity "
102: + this .messageEntity);
103: Config currentConfig = parseConfig();
104: configureEnvironment(currentConfig);
105: configureMessageEntity(currentConfig);
106: configureJta(currentConfig);
107: configureDataSource(currentConfig);
108: configurePlatform(currentConfig);
109: configureCredentialsSourceFactory(currentConfig);
110: parseModuleConfigs(currentConfig);
111: }
112:
113: protected Config parseConfig() throws Exception {
114: if (this .rootConfig == null) {
115: this .rootConfig = new SimpleConfig();
116: }
117: // append current root config to existing core config if config has already been initialized
118: Config currentRootConfig = Core.getRootConfig();
119: if (currentRootConfig != null) {
120: currentRootConfig.getProperties().putAll(
121: this .rootConfig.getProperties());
122: this .rootConfig = currentRootConfig;
123: } else {
124: Core.init(this .rootConfig);
125: }
126: if (this .configLocations != null) {
127: Config config = new SimpleConfig(this .configLocations,
128: this .properties);
129: config.parseConfig();
130: // merge the configs
131: // TODO with a refactoring of the config system, should we move toward a CompositeConfig? THat way we can preserve the info about where this config was
132: // loaded from instead of just copying the properties?
133: this .rootConfig.getProperties().putAll(
134: config.getProperties());
135: this .rootConfig.getObjects().putAll(config.getObjects());
136: } else if (this .properties != null) {
137: this .rootConfig.getProperties().putAll(this .properties);
138: }
139: return this .rootConfig;
140: }
141:
142: protected void configureCredentialsSourceFactory(
143: final Config rootConfig) {
144: if (credentialsSourceFactory != null) {
145: rootConfig.getObjects().put(
146: Config.CREDENTIALS_SOURCE_FACTORY,
147: this .credentialsSourceFactory);
148: }
149:
150: }
151:
152: protected void parseModuleConfigs(Config rootConfig)
153: throws Exception {
154: for (ModuleConfigurer module : this .modules) {
155: // TODO should there be a hierarchy here?
156: Config moduleConfig = module.loadConfig(rootConfig);
157: if (moduleConfig != null) {
158: rootConfig.getProperties().putAll(
159: moduleConfig.getProperties());
160: rootConfig.getObjects().putAll(
161: moduleConfig.getObjects());
162: }
163: }
164: }
165:
166: protected void configureEnvironment(Config config) {
167: if (!StringUtils.isBlank(this .environment)) {
168: config.getProperties().put(Config.ENVIRONMENT,
169: this .environment);
170: }
171: }
172:
173: protected void configureMessageEntity(Config config) {
174: if (!StringUtils.isBlank(this .messageEntity)) {
175: config.getProperties().put(Config.MESSAGE_ENTITY,
176: this .messageEntity);
177: }
178: }
179:
180: protected void configurePlatform(Config config) {
181: if (!StringUtils.isBlank(this .platform)) {
182: String platformClassName = "edu.iu.uis.eden.database.platform."
183: + this .platform + "Platform";
184: config.getProperties().setProperty(
185: Config.DATASOURCE_PLATFORM, platformClassName);
186: config.getProperties().setProperty(Config.OJB_PLATFORM,
187: this .platform);
188: }
189: }
190:
191: protected void configureDataSource(Config config) {
192: if (this .dataSource != null) {
193: config.getObjects().put(Config.DATASOURCE_OBJ,
194: this .dataSource);
195: } else if (!StringUtils.isBlank(this .dataSourceJndiLocation)) {
196: config.getProperties().put(Config.DATASOURCE_JNDI,
197: this .dataSourceJndiLocation);
198: }
199: }
200:
201: /**
202: * If the user injected JTA classes into this configurer, verify that both the
203: * UserTransaction and TransactionManager are set and then attach them to
204: * the configuration.
205: */
206: protected void configureJta(Config config) {
207: if (this .userTransaction != null) {
208: config.getObjects().put(Config.USER_TRANSACTION_OBJ,
209: this .userTransaction);
210: }
211: if (this .transactionManager != null) {
212: config.getObjects().put(Config.TRANSACTION_MANAGER_OBJ,
213: this .transactionManager);
214: }
215: if (!StringUtils.isEmpty(this .userTransactionJndiLocation)) {
216: config.getProperties().put(Config.USER_TRANSACTION_JNDI,
217: this .userTransactionJndiLocation);
218: }
219: if (!StringUtils.isEmpty(this .transactionManagerJndiLocation)) {
220: config.getProperties().put(Config.TRANSACTION_MANAGER_JNDI,
221: this .transactionManagerJndiLocation);
222: }
223: boolean userTransactionConfigured = this .userTransaction != null
224: || !StringUtils
225: .isEmpty(this .userTransactionJndiLocation);
226: boolean transactionManagerConfigured = this .transactionManager != null
227: || !StringUtils
228: .isEmpty(this .transactionManagerJndiLocation);
229: if (userTransactionConfigured && !transactionManagerConfigured) {
230: throw new ConfigurationException(
231: "When configuring JTA, both a UserTransaction and a TransactionManager are required. Only the UserTransaction was configured.");
232: }
233: if (transactionManagerConfigured && !userTransactionConfigured) {
234: throw new ConfigurationException(
235: "When configuring JTA, both a UserTransaction and a TransactionManager are required. Only the TransactionManager was configured.");
236: }
237: }
238:
239: public String getEnvironment() {
240: return this .environment;
241: }
242:
243: public void setEnvironment(String environment) {
244: this .environment = environment;
245: }
246:
247: public String getMessageEntity() {
248: return this .messageEntity;
249: }
250:
251: public void setMessageEntity(String messageEntity) {
252: this .messageEntity = messageEntity;
253: }
254:
255: public DataSource getDataSource() {
256: return this .dataSource;
257: }
258:
259: public void setDataSource(DataSource dataSource) {
260: this .dataSource = dataSource;
261: }
262:
263: public String getPlatform() {
264: return this .platform;
265: }
266:
267: public void setPlatform(String platform) {
268: this .platform = platform;
269: }
270:
271: public TransactionManager getTransactionManager() {
272: return this .transactionManager;
273: }
274:
275: public void setTransactionManager(
276: TransactionManager transactionManager) {
277: this .transactionManager = transactionManager;
278: }
279:
280: public UserTransaction getUserTransaction() {
281: return this .userTransaction;
282: }
283:
284: public void setUserTransaction(UserTransaction userTransaction) {
285: this .userTransaction = userTransaction;
286: }
287:
288: public ResourceLoader getRootResourceLoader() {
289: return this .rootResourceLoader;
290: }
291:
292: public void setRootResourceLoader(ResourceLoader rootResourceLoader) {
293: this .rootResourceLoader = rootResourceLoader;
294: }
295:
296: public Properties getProperties() {
297: return this .properties;
298: }
299:
300: public void setProperties(Properties properties) {
301: this .properties = properties;
302: }
303:
304: public List<String> getConfigLocations() {
305: return this .configLocations;
306: }
307:
308: public void setConfigLocations(List<String> configLocations) {
309: this .configLocations = configLocations;
310: }
311:
312: public void setDataSourceJndiLocation(String dataSourceJndiLocation) {
313: this .dataSourceJndiLocation = dataSourceJndiLocation;
314: }
315:
316: public String getTransactionManagerJndiLocation() {
317: return this .transactionManagerJndiLocation;
318: }
319:
320: public void setTransactionManagerJndiLocation(
321: String transactionManagerJndiLocation) {
322: this .transactionManagerJndiLocation = transactionManagerJndiLocation;
323: }
324:
325: public String getUserTransactionJndiLocation() {
326: return this .userTransactionJndiLocation;
327: }
328:
329: public void setUserTransactionJndiLocation(
330: String userTransactionJndiLocation) {
331: this .userTransactionJndiLocation = userTransactionJndiLocation;
332: }
333:
334: public List<ModuleConfigurer> getModules() {
335: return this .modules;
336: }
337:
338: public void setModules(List<ModuleConfigurer> modules) {
339: this .modules = modules;
340: }
341:
342: public Config getRootConfig() {
343: return this .rootConfig;
344: }
345:
346: public void setRootConfig(Config rootConfig) {
347: this .rootConfig = rootConfig;
348: }
349:
350: public CredentialsSourceFactory getCredentialsSourceFactory() {
351: return credentialsSourceFactory;
352: }
353:
354: public void setCredentialsSourceFactory(
355: final CredentialsSourceFactory credentialsSourceFactory) {
356: this.credentialsSourceFactory = credentialsSourceFactory;
357: }
358:
359: }
|