001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.ken.core; 017 018import javax.sql.DataSource; 019 020import org.apache.log4j.Logger; 021import org.kuali.rice.core.api.lifecycle.LifecycleBean; 022import org.kuali.rice.core.api.lifecycle.LifecycleBean; 023import org.springframework.beans.BeansException; 024import org.springframework.beans.factory.BeanFactory; 025import org.springframework.beans.factory.BeanFactoryAware; 026import org.springframework.beans.factory.BeanInitializationException; 027import org.springframework.transaction.PlatformTransactionManager; 028import org.springframework.transaction.support.TransactionTemplate; 029 030/** 031 * Eager-initializing singleton bean that performs some notification startup operations 032 * @author Kuali Rice Team (rice.collab@kuali.org) 033 */ 034public class NotificationLifeCycle extends LifecycleBean implements BeanFactoryAware { 035 private static final Logger LOG = Logger.getLogger(NotificationLifeCycle.class); 036 037 //private String ojbPlatform; 038 private BeanFactory theFactory; 039 private PlatformTransactionManager txMgr; 040 private DataSource dataSource; 041 042 /** 043 * This method sets the OJB platform. 044 * @param platform 045 */ 046 /* 047 public void setOjbPlatform(String platform) { 048 this.ojbPlatform = platform; 049 }*/ 050 051 /** 052 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory) 053 */ 054 public void setBeanFactory(BeanFactory theFactory) throws BeansException { 055 this.theFactory = theFactory; 056 } 057 058 public void setTransactionManager(PlatformTransactionManager txMgr) { 059 this.txMgr = txMgr; 060 } 061 062 public void setDataSource(DataSource dataSource) { 063 this.dataSource = dataSource; 064 } 065 066 /** 067 * Helper method for creating a TransactionTemplate initialized to create 068 * a new transaction 069 * @return a TransactionTemplate initialized to create a new transaction 070 */ 071 protected TransactionTemplate createNewTransaction() { 072 TransactionTemplate tt = new TransactionTemplate(txMgr); 073 tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); 074 return tt; 075 } 076 077 /** 078 * @see org.kuali.rice.ken.core.BaseLifecycle#start() 079 */ 080 public void start() throws Exception { 081 /*if (ojbPlatform == null) { 082 throw new BeanInitializationException("No platform was configured, please configure the datasource.ojb.platform property."); 083 }*/ 084 085 GlobalNotificationServiceLocator.init(theFactory); 086 /* 087 createNewTransaction().execute(new TransactionCallback() { 088 public Object doInTransaction(TransactionStatus txStatus) { 089 JdbcTemplate t = new JdbcTemplate(dataSource); 090 Boolean dataLoaded = (Boolean) t.execute(new StatementCallback() { 091 public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { 092 ResultSet rs = stmt.executeQuery("select * from APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'"); 093 try { 094 return rs.next(); 095 } finally { 096 rs.close(); 097 } 098 } 099 }); 100 if (!dataLoaded.booleanValue()) { 101 loadXmlFile("classpath:data/NotificationData.xml"); 102 103 t.execute(new StatementCallback() { 104 public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { 105 ResultSet rs = stmt.executeQuery("update APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'"); 106 try { 107 return rs.next(); 108 } finally { 109 rs.close(); 110 } 111 } 112 }); 113 } 114 } 115 }); 116 */ 117 118 //LOG.info("Setting OJB platform to: " + ojbPlatform); 119 //PersistenceBrokerFactory.defaultPersistenceBroker().serviceConnectionManager().getConnectionDescriptor().setDbms(ojbPlatform); 120 super.start(); 121 } 122 123 // yanked from KEWXmlDataLoaderLifecycle 124 /* 125 protected void loadXmlFile(String fileName) throws Exception { 126 Resource resource = new DefaultResourceLoader().getResource(fileName); 127 InputStream xmlFile = resource.getInputStream(); 128 if (xmlFile == null) { 129 throw new ConfigurationException("Didn't find file " + fileName); 130 } 131 List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>(); 132 XmlDocCollection docCollection = getFileXmlDocCollection(xmlFile, "UnitTestTemp"); 133 xmlFiles.add(docCollection); 134 KEWServiceLocator.getXmlIngesterService().ingest(xmlFiles); 135 for (Iterator iterator = docCollection.getXmlDocs().iterator(); iterator.hasNext();) { 136 XmlDoc doc = (XmlDoc) iterator.next(); 137 if (!doc.isProcessed()) { 138 throw new RuntimeException("Failed to ingest xml doc: " + doc.getName()); 139 } 140 } 141 } 142 143 protected FileXmlDocCollection getFileXmlDocCollection(InputStream xmlFile, String tempFileName) throws IOException { 144 if (xmlFile == null) { 145 throw new RuntimeException("Didn't find the xml file " + tempFileName); 146 } 147 File temp = File.createTempFile(tempFileName, ".xml"); 148 FileOutputStream fos = new FileOutputStream(temp); 149 int data = -1; 150 while ((data = xmlFile.read()) != -1) { 151 fos.write(data); 152 } 153 fos.close(); 154 return new FileXmlDocCollection(temp); 155 }*/ 156 157 /** 158 * @see org.kuali.rice.ken.core.BaseLifecycle#stop() 159 */ 160 public void stop() throws Exception { 161 GlobalNotificationServiceLocator.destroy(); 162 super.stop(); 163 } 164}