Merge "Omaha #4912 Add observer to read a modified configuation file." into omaha_16.2.1

Former-commit-id: a2cae7227b54a9f9b522a59317e51fd3fa68d56d
This commit is contained in:
Richard Peter 2015-10-09 10:55:19 -05:00 committed by Gerrit Code Review
commit 203bb82f0f

View file

@ -20,21 +20,30 @@ package com.raytheon.uf.common.monitor.config;
* further licensing information. * further licensing information.
**/ **/
import java.io.File; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import org.opengis.referencing.crs.ProjectedCRS; 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.IPathManager;
import com.raytheon.uf.common.localization.LocalizationContext; import com.raytheon.uf.common.localization.LocalizationContext;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel;
import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationType;
import com.raytheon.uf.common.localization.LocalizationFile; import com.raytheon.uf.common.localization.LocalizationFile;
import com.raytheon.uf.common.localization.PathManagerFactory; 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.ScanUtils;
import com.raytheon.uf.common.monitor.scan.ThreatLocation; import com.raytheon.uf.common.monitor.scan.ThreatLocation;
import com.raytheon.uf.common.monitor.scan.xml.CWATLocationsXML; 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.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; import com.vividsolutions.jts.geom.Coordinate;
/** /**
@ -48,6 +57,8 @@ import com.vividsolutions.jts.geom.Coordinate;
* ------------ ---------- ----------- -------------------------- * ------------ ---------- ----------- --------------------------
* --/--/---- Initial creation * --/--/---- Initial creation
* Oct 02, 2013 2361 njensen Use JAXBManager for XML * Oct 02, 2013 2361 njensen Use JAXBManager for XML
* Oct 08, 2015 4912 rferrel Update configXml when configuration file changes
* and removed deprecated code.
* *
* </pre> * </pre>
* *
@ -55,10 +66,22 @@ import com.vividsolutions.jts.geom.Coordinate;
* @version 1.0 * @version 1.0
*/ */
public class CWATLocationConfigManager { public class CWATLocationConfigManager {
private static final IUFStatusHandler statusHandler = UFStatus
.getHandler(CWATLocationConfigManager.class);
private static final SingleTypeJAXBManager<CWATLocationsXML> jaxb = SingleTypeJAXBManager private static final SingleTypeJAXBManager<CWATLocationsXML> jaxb = SingleTypeJAXBManager
.createWithoutException(CWATLocationsXML.class); .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. * FFMP Source Configuration XML object.
*/ */
@ -82,9 +105,31 @@ public class CWATLocationConfigManager {
/** Singleton instance of this class */ /** Singleton instance of this class */
private static CWATLocationConfigManager instance = null; 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() { private CWATLocationConfigManager() {
configXml = new CWATLocationsXML(); 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; 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 * Read the XML configuration data for the current XML file name with
* coordinate * coordinate
@ -116,28 +169,33 @@ public class CWATLocationConfigManager {
* Read the XML configuration data for the current XML file name. * Read the XML configuration data for the current XML file name.
*/ */
public void readConfigXml() { public void readConfigXml() {
LocalizationFile lFile = getConfigFile();
try { try {
IPathManager pm = PathManagerFactory.getPathManager(); if (lFile.exists()) {
LocalizationContext lc = pm.getContext( statusHandler.info("Reading CWAT configuration file: "
LocalizationType.COMMON_STATIC, LocalizationLevel.SITE); + lFile.getName());
File file = pm.getFile(lc, "cwat" + File.separatorChar try (InputStream stream = lFile.openInputStream()) {
+ getSiteName() + "Locations.xml"); // This closes the stream
System.out.println("Reading -- " + file.getAbsolutePath()); CWATLocationsXML configXmltmp = (CWATLocationsXML) jaxb
.unmarshalFromInputStream(stream);
CWATLocationsXML configXmltmp = jaxb.unmarshalFromXmlFile(file configXml = configXmltmp;
.getAbsolutePath()); }
} else {
configXml = configXmltmp; statusHandler.handle(Priority.WARN,
"No CWAT locations file found. Generating the file: "
+ lFile.getName());
// create a new one
ArrayList<ThreatLocation> locations = ScanUtils.getCWASites(
getSiteCoor(), getCRS());
configXml = new CWATLocationsXML();
configXml.setThreats(locations);
// writes one to site
saveConfigXml();
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("No CWAT locations file found"); statusHandler.handle(Priority.WARN,
// create a new one "Unable to load location file: " + lFile.getName(), e);
ArrayList<ThreatLocation> locations = ScanUtils.getCWASites(
getSiteCoor(), getCRS());
configXml = new CWATLocationsXML();
configXml.setThreats(locations);
// writes one to site
saveConfigXml();
} }
} }
@ -146,28 +204,15 @@ public class CWATLocationConfigManager {
*/ */
public void saveConfigXml() { public void saveConfigXml() {
// Save the xml object to disk // Save the xml object to disk
IPathManager pm = PathManagerFactory.getPathManager(); LocalizationFile lFile = getConfigFile();
LocalizationContext lc = pm.getContext(LocalizationType.COMMON_STATIC, statusHandler
LocalizationLevel.SITE); .info("Saving CWAT configuration file: " + lFile.getName());
LocalizationFile newXmlFile = pm.getLocalizationFile(lc, "cwat" try (SaveableOutputStream stream = lFile.openOutputStream()) {
+ File.separatorChar + getSiteName() + "Locations.xml"); jaxb.marshalToStream(configXml, stream);
stream.save();
if (newXmlFile.getFile().getParentFile().exists() == false) { } catch (LocalizationException | SerializationException | IOException e1) {
// System.out.println("Creating new directory"); statusHandler.handle(Priority.PROBLEM,
"Unable to save localized file: " + lFile.getName(), e1);
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();
} }
} }
@ -203,5 +248,4 @@ public class CWATLocationConfigManager {
public ArrayList<ThreatLocation> getLocations() { public ArrayList<ThreatLocation> getLocations() {
return configXml.getThreats(); return configXml.getThreats();
} }
} }