From d254634f37ebf69bf94c1e37bf0f2996e6bd1052 Mon Sep 17 00:00:00 2001 From: Roger Ferrel Date: Thu, 8 Oct 2015 08:52:37 -0500 Subject: [PATCH] Omaha #4912 Add observer to read a modified configuation file. Change-Id: I4878dc6c3bd70932ca8ca6811f5fd3d01293efa2 Former-commit-id: fd555ae5694aab0017b040c29fd4649285ba0785 --- .../config/CWATLocationConfigManager.java | 132 ++++++++++++------ 1 file changed, 88 insertions(+), 44 deletions(-) diff --git a/edexOsgi/com.raytheon.uf.common.monitor/src/com/raytheon/uf/common/monitor/config/CWATLocationConfigManager.java b/edexOsgi/com.raytheon.uf.common.monitor/src/com/raytheon/uf/common/monitor/config/CWATLocationConfigManager.java index c5788ae308..da6342aaa9 100644 --- a/edexOsgi/com.raytheon.uf.common.monitor/src/com/raytheon/uf/common/monitor/config/CWATLocationConfigManager.java +++ b/edexOsgi/com.raytheon.uf.common.monitor/src/com/raytheon/uf/common/monitor/config/CWATLocationConfigManager.java @@ -20,21 +20,30 @@ package com.raytheon.uf.common.monitor.config; * further licensing information. **/ -import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import org.opengis.referencing.crs.ProjectedCRS; +import com.raytheon.uf.common.localization.FileUpdatedMessage; +import com.raytheon.uf.common.localization.ILocalizationFileObserver; import com.raytheon.uf.common.localization.IPathManager; import com.raytheon.uf.common.localization.LocalizationContext; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; import com.raytheon.uf.common.localization.LocalizationFile; import com.raytheon.uf.common.localization.PathManagerFactory; +import com.raytheon.uf.common.localization.SaveableOutputStream; +import com.raytheon.uf.common.localization.exception.LocalizationException; import com.raytheon.uf.common.monitor.scan.ScanUtils; import com.raytheon.uf.common.monitor.scan.ThreatLocation; import com.raytheon.uf.common.monitor.scan.xml.CWATLocationsXML; +import com.raytheon.uf.common.serialization.SerializationException; import com.raytheon.uf.common.serialization.SingleTypeJAXBManager; +import com.raytheon.uf.common.status.IUFStatusHandler; +import com.raytheon.uf.common.status.UFStatus; +import com.raytheon.uf.common.status.UFStatus.Priority; import com.vividsolutions.jts.geom.Coordinate; /** @@ -48,6 +57,8 @@ import com.vividsolutions.jts.geom.Coordinate; * ------------ ---------- ----------- -------------------------- * --/--/---- Initial creation * Oct 02, 2013 2361 njensen Use JAXBManager for XML + * Oct 08, 2015 4912 rferrel Update configXml when configuration file changes + * and removed deprecated code. * * * @@ -55,10 +66,22 @@ import com.vividsolutions.jts.geom.Coordinate; * @version 1.0 */ public class CWATLocationConfigManager { + private static final IUFStatusHandler statusHandler = UFStatus + .getHandler(CWATLocationConfigManager.class); private static final SingleTypeJAXBManager jaxb = SingleTypeJAXBManager .createWithoutException(CWATLocationsXML.class); + /** + * Location of configuration files. + */ + private static final String CWAT_DIR = "cwat"; + + /** + * Append to the site name to get the configuration file name. + */ + private static final String CONFIG_FILE_SUFFIX = "Locations.xml"; + /** * FFMP Source Configuration XML object. */ @@ -82,9 +105,31 @@ public class CWATLocationConfigManager { /** Singleton instance of this class */ private static CWATLocationConfigManager instance = null; - /* Private Constructor */ + /** + * Observer to force updating the configXml. + */ + private ILocalizationFileObserver configXmlObserver = new ILocalizationFileObserver() { + + @Override + public void fileUpdated(FileUpdatedMessage message) { + readConfigXml(); + } + }; + + /** + * Private Constructor. + */ private CWATLocationConfigManager() { configXml = new CWATLocationsXML(); + IPathManager pm = PathManagerFactory.getPathManager(); + LocalizationContext lc = pm.getContext(LocalizationType.COMMON_STATIC, + LocalizationLevel.SITE); + /* + * No need to remove the observer since the instance of this class + * remains until the JRE is shutdown. + */ + pm.getLocalizationFile(lc, CWAT_DIR).addFileUpdatedObserver( + configXmlObserver); } /** @@ -100,6 +145,14 @@ public class CWATLocationConfigManager { return instance; } + private LocalizationFile getConfigFile() { + IPathManager pm = PathManagerFactory.getPathManager(); + LocalizationContext lc = pm.getContext(LocalizationType.COMMON_STATIC, + LocalizationLevel.SITE); + return pm.getLocalizationFile(lc, CWAT_DIR + IPathManager.SEPARATOR + + getSiteName() + CONFIG_FILE_SUFFIX); + } + /** * Read the XML configuration data for the current XML file name with * coordinate @@ -116,28 +169,33 @@ public class CWATLocationConfigManager { * Read the XML configuration data for the current XML file name. */ public void readConfigXml() { + LocalizationFile lFile = getConfigFile(); try { - IPathManager pm = PathManagerFactory.getPathManager(); - LocalizationContext lc = pm.getContext( - LocalizationType.COMMON_STATIC, LocalizationLevel.SITE); - File file = pm.getFile(lc, "cwat" + File.separatorChar - + getSiteName() + "Locations.xml"); - System.out.println("Reading -- " + file.getAbsolutePath()); - - CWATLocationsXML configXmltmp = jaxb.unmarshalFromXmlFile(file - .getAbsolutePath()); - - configXml = configXmltmp; + if (lFile.exists()) { + statusHandler.info("Reading CWAT configuration file: " + + lFile.getName()); + try (InputStream stream = lFile.openInputStream()) { + // This closes the stream + CWATLocationsXML configXmltmp = (CWATLocationsXML) jaxb + .unmarshalFromInputStream(stream); + configXml = configXmltmp; + } + } else { + statusHandler.handle(Priority.WARN, + "No CWAT locations file found. Generating the file: " + + lFile.getName()); + // create a new one + ArrayList locations = ScanUtils.getCWASites( + getSiteCoor(), getCRS()); + configXml = new CWATLocationsXML(); + configXml.setThreats(locations); + // writes one to site + saveConfigXml(); + } } catch (Exception e) { - System.err.println("No CWAT locations file found"); - // create a new one - ArrayList locations = ScanUtils.getCWASites( - getSiteCoor(), getCRS()); - configXml = new CWATLocationsXML(); - configXml.setThreats(locations); - // writes one to site - saveConfigXml(); + statusHandler.handle(Priority.WARN, + "Unable to load location file: " + lFile.getName(), e); } } @@ -146,28 +204,15 @@ public class CWATLocationConfigManager { */ public void saveConfigXml() { // Save the xml object to disk - IPathManager pm = PathManagerFactory.getPathManager(); - LocalizationContext lc = pm.getContext(LocalizationType.COMMON_STATIC, - LocalizationLevel.SITE); - LocalizationFile newXmlFile = pm.getLocalizationFile(lc, "cwat" - + File.separatorChar + getSiteName() + "Locations.xml"); - - if (newXmlFile.getFile().getParentFile().exists() == false) { - // System.out.println("Creating new directory"); - - if (newXmlFile.getFile().getParentFile().mkdirs() == false) { - // System.out.println("Could not create new directory..."); - } - } - - try { - System.out.println("Saving -- " - + newXmlFile.getFile().getAbsolutePath()); - jaxb.marshalToXmlFile(configXml, newXmlFile.getFile() - .getAbsolutePath()); - newXmlFile.save(); - } catch (Exception e) { - e.printStackTrace(); + LocalizationFile lFile = getConfigFile(); + statusHandler + .info("Saving CWAT configuration file: " + lFile.getName()); + try (SaveableOutputStream stream = lFile.openOutputStream()) { + jaxb.marshalToStream(configXml, stream); + stream.save(); + } catch (LocalizationException | SerializationException | IOException e1) { + statusHandler.handle(Priority.PROBLEM, + "Unable to save localized file: " + lFile.getName(), e1); } } @@ -203,5 +248,4 @@ public class CWATLocationConfigManager { public ArrayList getLocations() { return configXml.getThreats(); } - }