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