public class AppConfigUtils extends Object
Constructor and Description |
---|
AppConfigUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
castUnmarshalled(Object unmarshalled,
Class<T> clazz) |
static File |
createAppDir(String appName) |
static <T> T |
loadAppConfig(String appName,
String filename,
URL fallbackUrl,
Class<T> configClass,
Collection<Class<?>> dataClasses)
Load a config object from the specified XML file in the application's config directory
(see
createAppDir(String) . |
static Marshaller |
newMarshaller(Collection<Class<?>> dataClasses) |
static Unmarshaller |
newUnmarshaller(Collection<Class<?>> dataClasses) |
static void |
saveAppConfig(String appName,
String filename,
Object config,
Collection<Class<?>> dataClasses)
Save the specified config object to an XML file in the application's config directory
(see
createAppDir(String) . |
public static void saveAppConfig(String appName, String filename, Object config, Collection<Class<?>> dataClasses)
createAppDir(String)
.
Every class that could possibly be serialized, including the class of the config object,
the classes of its fields, and any relevant subclasses, should be included in dataClasses
.
In many cases it is helpful to have a separate package of classes just for serialization.
Notably, this makes it easier to avoid accidentally changing the serialization format
(e.g. by renaming a class or package). See the com.metsci.glimpse.docking.xml
package (including its package-info.java
) for an example.
NOTE: This function catches exceptions and logs them, instead of letting them propagate up the call stack. Ordinarily this would be a terrible idea, but for this function, terse usage is more important than careful error handling.
public static <T> T loadAppConfig(String appName, String filename, URL fallbackUrl, Class<T> configClass, Collection<Class<?>> dataClasses)
createAppDir(String)
. If that fails, load the config object
from fallbackUrl
(if it is non-null).
Every class that could possibly be deserialized, including the class of the config object,
the classes of its fields, and any relevant subclasses, should be included in dataClasses
.
In many cases it is helpful to have a separate package of classes just for serialization.
Notably, this makes it easier to avoid accidentally changing the serialization format
(e.g. by renaming a class or package). See the com.metsci.glimpse.docking.xml
package (including its package-info.java
) for an example.
NOTE: This function catches exceptions and logs them, instead of letting them propagate up the call stack. Ordinarily this would be a terrible idea, but for this function, terse usage is more important than careful error handling.
public static Marshaller newMarshaller(Collection<Class<?>> dataClasses) throws IOException, JAXBException
IOException
JAXBException
public static Unmarshaller newUnmarshaller(Collection<Class<?>> dataClasses) throws JAXBException, IOException
JAXBException
IOException
Copyright © 2016 Metron, Inc.. All rights reserved.