Search in sources :

Example 1 with ConfigurationResourceException

use of org.opennms.core.config.api.ConfigurationResourceException in project opennms by OpenNMS.

the class JaxbResourceConfiguration method save.

public void save(final T config) throws ConfigurationResourceException {
    final Resource r = getResource();
    if (!(r instanceof WritableResource)) {
        throw new ConfigurationResourceException("Resource " + r + " is not writable!");
    }
    final WritableResource resource = (WritableResource) r;
    OutputStream os = null;
    OutputStreamWriter osw = null;
    try {
        os = resource.getOutputStream();
        osw = new OutputStreamWriter(os);
        JaxbUtils.marshal(config, osw);
    } catch (final IOException e) {
        throw new ConfigurationResourceException("Failed to write to " + r, e);
    } catch (final Exception e) {
        throw new ConfigurationResourceException("Failed to marshal configuration " + getClassType() + " to resource " + r, e);
    } finally {
        IOUtils.closeQuietly(osw);
        IOUtils.closeQuietly(os);
    }
}
Also used : WritableResource(org.springframework.core.io.WritableResource) OutputStream(java.io.OutputStream) ConfigurationResource(org.opennms.core.config.api.ConfigurationResource) WritableResource(org.springframework.core.io.WritableResource) Resource(org.springframework.core.io.Resource) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) IOException(java.io.IOException)

Example 2 with ConfigurationResourceException

use of org.opennms.core.config.api.ConfigurationResourceException in project opennms by OpenNMS.

the class JaxbResourceConfiguration method get.

public T get() throws ConfigurationResourceException {
    final Class<T> classType = getClassType();
    final Resource resource = getResource();
    try {
        return JaxbUtils.unmarshal(classType, resource);
    } catch (final Exception e) {
        throw new ConfigurationResourceException("Failed to unmarshal " + resource + " to class " + classType, e);
    }
}
Also used : ConfigurationResource(org.opennms.core.config.api.ConfigurationResource) WritableResource(org.springframework.core.io.WritableResource) Resource(org.springframework.core.io.Resource) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ConfigurationResource (org.opennms.core.config.api.ConfigurationResource)2 ConfigurationResourceException (org.opennms.core.config.api.ConfigurationResourceException)2 Resource (org.springframework.core.io.Resource)2 WritableResource (org.springframework.core.io.WritableResource)2 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1