Coverage Report - org.kuali.rice.krad.datadictionary.ReloadingDataDictionary
 
Classes in this File Line Coverage Branch Coverage Complexity
ReloadingDataDictionary
0%
0/49
0%
0/6
2.5
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.krad.datadictionary;
 17  
 
 18  
 import java.io.File;
 19  
 import java.io.InputStream;
 20  
 import java.net.URL;
 21  
 import java.util.ArrayList;
 22  
 import java.util.List;
 23  
 
 24  
 import no.geosoft.cc.io.FileListener;
 25  
 import no.geosoft.cc.io.FileMonitor;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 import org.kuali.rice.core.api.config.property.ConfigurationService;
 31  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 32  
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 33  
 import org.springframework.core.io.FileSystemResource;
 34  
 import org.springframework.core.io.InputStreamResource;
 35  
 import org.springframework.core.io.Resource;
 36  
 
 37  
 
 38  
 /**
 39  
  * Extends the DataDictionary to add reloading of changed dictionary files
 40  
  * without a restart of the web container
 41  
  * 
 42  
  * <p>
 43  
  * To use modify the "dataDictionaryService" spring definition
 44  
  * (KRADSpringBeans.xml) and change the constructor arg bean class from
 45  
  * "org.kuali.rice.krad.datadictionary.DataDictionary" to
 46  
  * "ReloadingDataDictionary"
 47  
  * </p>
 48  
  * 
 49  
  * <p>
 50  
  * NOTE: For Development Purposes Only!
 51  
  * </p>
 52  
  * 
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  
 public class ReloadingDataDictionary extends DataDictionary implements FileListener, URLMonitor.URLContentChangedListener {
 56  0
         private static final Log LOG = LogFactory.getLog(DataDictionary.class);
 57  
 
 58  
         private static final String CLASS_DIR_CONFIG_PARM = "reload.data.dictionary.classes.dir";
 59  
         private static final String SOURCE_DIR_CONFIG_PARM = "reload.data.dictionary.source.dir";
 60  
         private static final String INTERVAL_CONFIG_PARM = "reload.data.dictionary.interval";
 61  
 
 62  
     private URLMonitor dictionaryUrlMonitor;
 63  
 
 64  
 
 65  
         public ReloadingDataDictionary() {
 66  0
                 super();
 67  0
         }
 68  
 
 69  
         /**
 70  
          * After dictionary has been loaded, determine the source files and add them
 71  
          * to the monitor
 72  
          * 
 73  
          * @see org.kuali.rice.krad.datadictionary.DataDictionary#parseDataDictionaryConfigurationFiles(boolean)
 74  
          */
 75  
         @Override
 76  
         public void parseDataDictionaryConfigurationFiles(boolean allowConcurrentValidation) {
 77  0
                 ConfigurationService configurationService = KRADServiceLocator.getKualiConfigurationService();
 78  
 
 79  
                 // class directory part of the path that should be replaced
 80  0
                 String classesDir = configurationService.getPropertyString(CLASS_DIR_CONFIG_PARM);
 81  
 
 82  
                 // source directory where dictionary files are found
 83  0
                 String sourceDir = configurationService.getPropertyString(SOURCE_DIR_CONFIG_PARM);
 84  
 
 85  
                 // interval to poll for changes in milliseconds
 86  0
                 int reloadInterval = Integer.parseInt(configurationService.getPropertyString(INTERVAL_CONFIG_PARM));
 87  
 
 88  0
                 FileMonitor dictionaryFileMonitor = new FileMonitor(reloadInterval);
 89  
 
 90  0
         dictionaryUrlMonitor = new URLMonitor(reloadInterval);
 91  0
         dictionaryUrlMonitor.addListener(this);
 92  
 
 93  
                 // need to copy the configFileLocations list here because it gets
 94  
                 // cleared out after processing by super
 95  0
                 List<String> configLocations = new ArrayList<String>(configFileLocations);
 96  
 
 97  0
                 super.parseDataDictionaryConfigurationFiles(allowConcurrentValidation);
 98  0
                 for (String configLocation : configLocations) {
 99  0
                         Resource classFileResource = getFileResource(configLocation);
 100  
                         try {
 101  0
                 if (classFileResource.getURI().toString().startsWith("jar:")) {
 102  0
                     LOG.debug("Monitoring dictionary file at URI: " + classFileResource.getURI().toString());
 103  0
                     dictionaryUrlMonitor.addURI(classFileResource.getURL());
 104  
                 } else {
 105  0
                     String filePathClassesDir = classFileResource.getFile().getAbsolutePath();
 106  0
                     String sourceFilePath = StringUtils.replace(filePathClassesDir, classesDir, sourceDir);
 107  0
                     File dictionaryFile = new File(filePathClassesDir);
 108  0
                     if (dictionaryFile.exists()) {
 109  0
                         LOG.debug("Monitoring dictionary file: " + dictionaryFile.getName());
 110  0
                         dictionaryFileMonitor.addFile(dictionaryFile);
 111  
                     }
 112  
                 }
 113  
                         }
 114  0
                         catch (Exception e) {
 115  0
                                 LOG.info("Exception in picking up dictionary file for monitoring:  " + e.getMessage(), e);
 116  0
                         }
 117  0
                 }
 118  
 
 119  
                 // add the dictionary as a listener for file changes
 120  0
                 dictionaryFileMonitor.addListener(this);
 121  0
         }
 122  
 
 123  
         /**
 124  
          * Call back when a dictionary file is changed. Calls the spring bean reader
 125  
          * to reload the file (which will override beans as necessary and destroy
 126  
          * singletons) and runs the indexer
 127  
          * 
 128  
          * @see no.geosoft.cc.io.FileListener#fileChanged(java.io.File)
 129  
          */
 130  
         @Override
 131  
         public void fileChanged(File file) {
 132  0
                 LOG.info("reloading dictionary configuration for " + file.getName());
 133  
                 try {
 134  0
                         Resource resource = new FileSystemResource(file);
 135  0
                         xmlReader.loadBeanDefinitions(resource);
 136  
 
 137  
                         // re-index
 138  0
                         ddIndex.run();
 139  
                 }
 140  0
                 catch (Exception e) {
 141  0
                         LOG.info("Exception in dictionary hot deploy: " + e.getMessage(), e);
 142  0
                 }
 143  0
         }
 144  
 
 145  
     public void urlContentChanged(final URL url) {
 146  0
         LOG.info("reloading dictionary configuration for " + url.toString());
 147  
         try {
 148  0
             InputStream urlStream = url.openStream();
 149  0
             InputStreamResource resource = new InputStreamResource(urlStream);
 150  
 
 151  0
             int originalValidationMode = xmlReader.getValidationMode();
 152  0
             xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
 153  0
             xmlReader.loadBeanDefinitions(resource);
 154  0
             xmlReader.setValidationMode(originalValidationMode);
 155  
 
 156  
             // re-index
 157  0
             ddIndex.run();
 158  
         }
 159  0
         catch (Exception e) {
 160  0
             LOG.info("Exception in dictionary hot deploy: " + e.getMessage(), e);
 161  0
         }
 162  0
     }
 163  
 }