001: /*
002: * Copyright 2005-2007 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 edu.iu.uis.eden.core.dependencylifecycles;
018:
019: import org.apache.commons.lang.StringUtils;
020: import org.kuali.rice.config.Config;
021: import org.kuali.rice.core.Core;
022: import org.kuali.rice.lifecycle.BaseLifecycle;
023: import org.kuali.rice.ojb.OjbPlatformConfigurer;
024: import org.springframework.context.ApplicationEvent;
025: import org.springframework.context.ApplicationListener;
026: import org.springframework.context.event.ContextClosedEvent;
027: import org.springframework.context.event.ContextRefreshedEvent;
028:
029: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
030:
031: public class DatabaseLifeCycle extends BaseLifecycle implements
032: ApplicationListener {
033:
034: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(DatabaseLifeCycle.class);
036:
037: private String ojbPropertyFile;
038:
039: public DatabaseLifeCycle() {
040: String ojbFileLocation = Core.getCurrentContextConfig()
041: .getAlternateOJBFile();
042: if (StringUtils.isBlank(ojbFileLocation)) {
043: ojbFileLocation = "OJB.properties";
044: }
045: this .ojbPropertyFile = ojbFileLocation;
046: }
047:
048: public DatabaseLifeCycle(String ojbPropertyFile) {
049: this .ojbPropertyFile = ojbPropertyFile;
050: }
051:
052: public void start() throws Exception {
053: LOG.info("Configuring database...");
054:
055: if (StringUtils.isBlank(ojbPropertyFile)) {
056: System.setProperty("OJB.properties", "OJB.properties");
057: } else {
058: System.setProperty("OJB.properties", ojbPropertyFile);
059: }
060:
061: String platform = Core.getCurrentContextConfig().getProperty(
062: Config.OJB_PLATFORM);
063: if (platform == null) {
064: throw new WorkflowRuntimeException(
065: "No platform was configured, please configure the datasource.ojb.platform property.");
066: }
067:
068: LOG.info("Setting OJB platform to: " + platform);
069: OjbPlatformConfigurer
070: .configureDefaultOJBConnectionDescriptor(platform);
071:
072: // important to fetch this directory from the SpringLoader because we
073: // don't want it wrapped in a proxy so that instanceof Current works
074: // properly
075: // TransactionManager transactionManager = (TransactionManager)SpringLoader.getInstance().getService(new QName(KEWServiceLocator.JTA_TRANSACTION_MANAGER));
076: // if (transactionManager instanceof Current) {
077: // Current jotm = (Current) transactionManager;
078: // if (!Utilities.isEmpty(Core.getRootConfig().getTransactionTimeout())) {
079: // jotm.setDefaultTimeout(Integer.parseInt(Core.getRootConfig().getTransactionTimeout()));
080: // jotm.setTransactionTimeout(jotm.getDefaultTimeout());
081: // } else {
082: // jotm.setDefaultTimeout(EdenConstants.DEFAULT_TRANSACTION_TIMEOUT_SECONDS);
083: // jotm.setTransactionTimeout(jotm.getDefaultTimeout());
084: // }
085: // }
086: //
087: // Platform p = KEWServiceLocator.getDbPlatform();
088: // LOG.info("Workflow database Platform from Spring: " + p);
089:
090: LOG.info("Trying to clear OJB cache from start()");
091: super .start();
092: }
093:
094: /**
095: * Initialize this LifeCycle when spring starts.
096: */
097: public void onApplicationEvent(ApplicationEvent event) {
098: try {
099: if (event instanceof ContextRefreshedEvent) {
100: if (isStarted()) {
101: stop();
102: }
103: start();
104: } else if (event instanceof ContextClosedEvent) {
105: stop();
106: }
107: } catch (Exception e) {
108: if (e instanceof RuntimeException) {
109: throw (RuntimeException) e;
110: }
111: throw new WorkflowRuntimeException(
112: "Failed to handle application context event: "
113: + event, e);
114: }
115: }
116:
117: }
|