View Javadoc

1   /*
2    * Copyright 2007 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 javax.sql.DataSource;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.rice.core.api.lifecycle.LifecycleBean;
22  import org.kuali.rice.core.api.lifecycle.LifecycleBean;
23  import org.springframework.beans.BeansException;
24  import org.springframework.beans.factory.BeanFactory;
25  import org.springframework.beans.factory.BeanFactoryAware;
26  import org.springframework.beans.factory.BeanInitializationException;
27  import org.springframework.transaction.PlatformTransactionManager;
28  import org.springframework.transaction.support.TransactionTemplate;
29  
30  /**
31   * Eager-initializing singleton bean that performs some notification startup operations
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class NotificationLifeCycle extends LifecycleBean implements BeanFactoryAware {
35      private static final Logger LOG = Logger.getLogger(NotificationLifeCycle.class);
36  
37      //private String ojbPlatform;
38      private BeanFactory theFactory;
39      private PlatformTransactionManager txMgr;
40      private DataSource dataSource;
41  
42      /**
43       * This method sets the OJB platform.
44       * @param platform
45       */
46      /*
47      public void setOjbPlatform(String platform) {
48          this.ojbPlatform = platform;
49      }*/
50  
51      /**
52       * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
53       */
54      public void setBeanFactory(BeanFactory theFactory) throws BeansException {
55  	 this.theFactory = theFactory;
56      }
57  
58      public void setTransactionManager(PlatformTransactionManager txMgr) {
59          this.txMgr = txMgr;
60      }
61      
62      public void setDataSource(DataSource dataSource) {
63          this.dataSource = dataSource;
64      }
65  
66      /**
67       * Helper method for creating a TransactionTemplate initialized to create
68       * a new transaction
69       * @return a TransactionTemplate initialized to create a new transaction
70       */
71      protected TransactionTemplate createNewTransaction() {
72          TransactionTemplate tt = new TransactionTemplate(txMgr);
73          tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
74          return tt;
75      }
76  
77      /**
78       * @see org.kuali.rice.ken.core.BaseLifecycle#start()
79       */
80      public void start() throws Exception {
81          /*if (ojbPlatform == null) {
82              throw new BeanInitializationException("No platform was configured, please configure the datasource.ojb.platform property.");
83          }*/
84  
85          GlobalNotificationServiceLocator.init(theFactory);
86          /*
87          createNewTransaction().execute(new TransactionCallback() {
88              public Object doInTransaction(TransactionStatus txStatus) {
89                  JdbcTemplate t = new JdbcTemplate(dataSource);
90                  Boolean dataLoaded = (Boolean) t.execute(new StatementCallback() {
91                      public Object doInStatement(Statement stmt) throws SQLException, DataAccessException {
92                          ResultSet rs = stmt.executeQuery("select * from APP_META_T where NAME = 'ken.bootstrap.loaded' and VALUE = 'true'");
93                          try {
94                              return rs.next();
95                          } finally {
96                              rs.close();
97                          }
98                      }
99                  });
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 }