Search in sources :

Example 16 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class JaxbResourceConfigurationTest method testFileSystemResourceExists.

@Test
public void testFileSystemResourceExists() throws ConfigurationResourceException, IOException {
    final File configFile = getConfigFile();
    final ConfigurationResource<CollectdConfiguration> collectd = new JaxbResourceConfiguration<CollectdConfiguration>(CollectdConfiguration.class, new FileSystemResource(configFile));
    assertNotNull(collectd);
    final CollectdConfiguration config = collectd.get();
    assertNotNull(config);
    assertEquals(5, config.getPackages().size());
    assertEquals("vmware3", config.getPackages().get(0).getName());
}
Also used : CollectdConfiguration(org.opennms.netmgt.config.collectd.CollectdConfiguration) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Example 17 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class ConfigurationTestUtils method getSpringResourceForResourceWithReplacements.

public static Resource getSpringResourceForResourceWithReplacements(final Object obj, final String resource, final String[]... replacements) throws IOException {
    try {
        String config = getConfigForResourceWithReplacements(obj, resource, replacements);
        File tmp = File.createTempFile("testConfigFile", ".xml");
        tmp.deleteOnExit();
        FileWriter fw = new FileWriter(tmp);
        fw.write(config);
        fw.close();
        return new FileSystemResource(tmp);
    } catch (final Throwable t) {
        return new InputStreamResource(getInputStreamForResourceWithReplacements(obj, resource, replacements));
    }
}
Also used : FileWriter(java.io.FileWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) InputStreamResource(org.springframework.core.io.InputStreamResource)

Example 18 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class AbstractMergingJaxbConfigDao method reconfigureDaos.

private void reconfigureDaos() {
    final Set<File> xmlFilesWithUnusedDaos = new HashSet<>();
    xmlFilesWithUnusedDaos.addAll(m_configDaosByPath.keySet());
    for (File xmlFile : m_xmlFiles) {
        // Try to fetch an existing DAO
        JaxbConfigDao dao = m_configDaosByPath.get(xmlFile);
        if (dao == null) {
            // We need to create one
            FileSystemResource fs = new FileSystemResource(xmlFile);
            dao = new JaxbConfigDao();
            dao.setConfigResource(fs);
            dao.afterPropertiesSet();
            m_configDaosByPath.put(xmlFile, dao);
        } else {
            xmlFilesWithUnusedDaos.remove(xmlFilesWithUnusedDaos);
        }
    }
    // Remove any DAOs we don't need anymore
    for (File fileWithUnusedDao : xmlFilesWithUnusedDaos) {
        m_configDaosByPath.remove(fileWithUnusedDao);
    }
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) HashSet(java.util.HashSet)

Example 19 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class JdbcDataCollectionConfigDaoJaxbTest method testAfterPropertiesSetWithBogusFileResource.

public void testAfterPropertiesSetWithBogusFileResource() throws Exception {
    Resource resource = new FileSystemResource("/bogus-file");
    JdbcDataCollectionConfigDaoJaxb dao = new JdbcDataCollectionConfigDaoJaxb();
    dao.setConfigResource(resource);
    ThrowableAnticipator ta = new ThrowableAnticipator();
    ta.anticipate(new MarshallingResourceFailureException(ThrowableAnticipator.IGNORE_MESSAGE));
    try {
        dao.afterPropertiesSet();
    } catch (Throwable t) {
        ta.throwableReceived(t);
    }
    ta.verifyAnticipated();
}
Also used : MarshallingResourceFailureException(org.opennms.core.xml.MarshallingResourceFailureException) InputStreamResource(org.springframework.core.io.InputStreamResource) FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) ThrowableAnticipator(org.opennms.test.ThrowableAnticipator)

Example 20 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class EventConfDaoReloadTest method canReloadEventsOnDisk.

/**
     * Verify that the reload operation does not throw any errors
     * when reloading event files on disk.
     */
@Test
public void canReloadEventsOnDisk() throws IOException {
    // Copy the resources to the file system
    File eventconfXml = tempFolder.newFile("eventconf.xml");
    File bgp4eventsXml = tempFolder.newFile("BGP4.events.xml");
    FileUtils.copyInputStreamToFile(getResourceForRelativePath("reloaded/eventconf.xml").getInputStream(), eventconfXml);
    FileUtils.copyInputStreamToFile(getResourceForRelativePath("reloaded/BGP4.events.xml").getInputStream(), bgp4eventsXml);
    // Load
    DefaultEventConfDao eventConfDao = new DefaultEventConfDao();
    eventConfDao.setConfigResource(new FileSystemResource(eventconfXml));
    eventConfDao.afterPropertiesSet();
    assertEquals(3, eventConfDao.getAllEvents().size());
    // Reload
    eventConfDao.reload();
    assertEquals(3, eventConfDao.getAllEvents().size());
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)128 File (java.io.File)66 Test (org.junit.Test)41 Resource (org.springframework.core.io.Resource)35 Before (org.junit.Before)21 ClassPathResource (org.springframework.core.io.ClassPathResource)13 IOException (java.io.IOException)11 FileWriter (java.io.FileWriter)10 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 HashMap (java.util.HashMap)7 Properties (java.util.Properties)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 DefaultDataCollectionConfigDao (org.opennms.netmgt.config.DefaultDataCollectionConfigDao)6 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Ehcache (net.sf.ehcache.Ehcache)4