View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.core;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.lifecycle.LifecycleBean;
20  import org.springframework.beans.BeansException;
21  import org.springframework.beans.factory.BeanFactory;
22  import org.springframework.beans.factory.BeanFactoryAware;
23  import org.springframework.transaction.PlatformTransactionManager;
24  import org.springframework.transaction.support.TransactionTemplate;
25  
26  import javax.sql.DataSource;
27  
28  /**
29   * Eager-initializing singleton bean that performs some notification startup operations
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class NotificationLifeCycle extends LifecycleBean implements BeanFactoryAware {
33      private static final Logger LOG = Logger.getLogger(NotificationLifeCycle.class);
34  
35      //private String ojbPlatform;
36      private BeanFactory theFactory;
37      private PlatformTransactionManager txMgr;
38      private DataSource dataSource;
39  
40      /**
41       * This method sets the OJB platform.
42       * @param platform
43       */
44      /*
45      public void setOjbPlatform(String platform) {
46          this.ojbPlatform = platform;
47      }*/
48  
49      /**
50       * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
51       */
52      public void setBeanFactory(BeanFactory theFactory) throws BeansException {
53  	 this.theFactory = theFactory;
54      }
55  
56      public void setTransactionManager(PlatformTransactionManager txMgr) {
57          this.txMgr = txMgr;
58      }
59      
60      public void setDataSource(DataSource dataSource) {
61          this.dataSource = dataSource;
62      }
63  
64      /**
65       * Helper method for creating a TransactionTemplate initialized to create
66       * a new transaction
67       * @return a TransactionTemplate initialized to create a new transaction
68       */
69      protected TransactionTemplate createNewTransaction() {
70          TransactionTemplate tt = new TransactionTemplate(txMgr);
71          tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
72          return tt;
73      }
74  
75      /**
76       * @see org.kuali.rice.ken.core.BaseLifecycle#start()
77       */
78      public void start() throws Exception {
79          /*if (ojbPlatform == null) {
80              throw new BeanInitializationException("No platform was configured, please configure the datasource.ojb.platform property.");
81          }*/
82  
83          GlobalNotificationServiceLocator.init(theFactory);
84          /*
85          createNewTransaction().execute(new TransactionCallback() {
86              public Object doInTransaction(TransactionStatus txStatus) {
87                  JdbcTemplate t = new JdbcTemplate(dataSource);
88                  Boolean dataLoaded = (Boolean) t.execute(new StatementCallback() {
89                      public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
90                          ResultSet rs = stmt.executeQuery("select * from APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'");
91                          try {
92                              return rs.next();
93                          } finally {
94                              rs.close();
95                          }
96                      }
97                  });
98                  if (!dataLoaded.booleanValue()) {
99                      loadXmlFile("classpath:data/NotificationData.xml");
100                     
101                     t.execute(new StatementCallback() {
102                         public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
103                             ResultSet rs = stmt.executeQuery("update APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'");
104                             try {
105                                 return rs.next();
106                             } finally {
107                                 rs.close();
108                             }
109                         }
110                     });
111                 }
112             }
113         });
114         */
115         
116         //LOG.info("Setting OJB platform to: " + ojbPlatform);
117         //PersistenceBrokerFactory.defaultPersistenceBroker().serviceConnectionManager().getConnectionDescriptor().setDbms(ojbPlatform);
118         super.start();
119     }
120 
121     // yanked from KEWXmlDataLoaderLifecycle
122     /*
123     protected void loadXmlFile(String fileName) throws Exception {
124         Resource resource = new DefaultResourceLoader().getResource(fileName);
125         InputStream xmlFile = resource.getInputStream();
126         if (xmlFile == null) {
127                 throw new ConfigurationException("Didn't find file " + fileName);
128         }
129         List<XmlDocCollection> xmlFiles = new ArrayList<XmlDocCollection>();
130         XmlDocCollection docCollection = getFileXmlDocCollection(xmlFile, "UnitTestTemp");
131         xmlFiles.add(docCollection);
132         KEWServiceLocator.getXmlIngesterService().ingest(xmlFiles);
133         for (Iterator iterator = docCollection.getXmlDocs().iterator(); iterator.hasNext();) {
134                 XmlDoc doc = (XmlDoc) iterator.next();
135                 if (!doc.isProcessed()) {
136                         throw new RuntimeException("Failed to ingest xml doc: " + doc.getName());
137                 }
138         }
139     }
140 
141     protected FileXmlDocCollection getFileXmlDocCollection(InputStream xmlFile, String tempFileName) throws IOException {
142         if (xmlFile == null) {
143             throw new RuntimeException("Didn't find the xml file " + tempFileName);
144         }
145         File temp = File.createTempFile(tempFileName, ".xml");
146         FileOutputStream fos = new FileOutputStream(temp);
147         int data = -1;
148         while ((data = xmlFile.read()) != -1) {
149             fos.write(data);
150         }
151         fos.close();
152         return new FileXmlDocCollection(temp);
153     }*/
154 
155     /**
156      * @see org.kuali.rice.ken.core.BaseLifecycle#stop()
157      */
158     public void stop() throws Exception {
159         GlobalNotificationServiceLocator.destroy();
160         super.stop();
161     }
162 }