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.
**/
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.
*
* </pre>
*
@ -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<CWATLocationsXML> 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<ThreatLocation> 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<ThreatLocation> 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<ThreatLocation> getLocations() {
return configXml.getThreats();
}
}