Search in sources :

Example 66 with FileSystemResource

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

the class DataCollectionConfigParser method parseExternalResources.

/**
 * Read all XML files from datacollection directory and parse them to create a list of DatacollectionGroup objects.
 */
private void parseExternalResources() {
    // Ensure that this is called only once.
    if (externalGroupsMap != null && externalGroupsMap.size() > 0) {
        LOG.info("parseExternalResources: external data collection groups are already parsed");
        return;
    }
    // Check configuration files repository
    File folder = new File(configDirectory);
    if (!folder.exists() || !folder.isDirectory()) {
        LOG.info("parseExternalResources: directory {} does not exist or is not a folder.", folder);
        return;
    }
    // Get external configuration files
    File[] listOfFiles = folder.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File file, String name) {
            return name.endsWith(".xml");
        }
    });
    // Parse configuration files (populate external groups map)
    final CountDownLatch latch = new CountDownLatch(listOfFiles.length);
    int i = 0;
    for (final File file : listOfFiles) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    LOG.debug("parseExternalResources: parsing {}", file);
                    DatacollectionGroup group = JaxbUtils.unmarshal(DatacollectionGroup.class, new FileSystemResource(file));
                    // Synchronize around the map that holds the results
                    synchronized (externalGroupsMap) {
                        externalGroupsMap.put(group.getName(), group);
                    }
                } catch (Throwable e) {
                    throwException("Can't parse XML file " + file + "; nested exception: " + e.getMessage(), e);
                } finally {
                    latch.countDown();
                }
            }
        }, "DataCollectionConfigParser-Thread-" + i++);
        thread.start();
    }
    try {
        latch.await();
    } catch (InterruptedException e) {
        throwException("Exception while waiting for XML parsing threads to complete: " + e.getMessage(), e);
    }
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) CountDownLatch(java.util.concurrent.CountDownLatch) FilenameFilter(java.io.FilenameFilter) File(java.io.File) DatacollectionGroup(org.opennms.netmgt.config.datacollection.DatacollectionGroup)

Example 67 with FileSystemResource

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

the class DiscoveryConfigFactory method reload.

/**
 * Reload the config from the default config file.
 * @throws IOException
 *
 * @exception java.io.IOException
 *                Thrown if the specified config file cannot be read/loaded
 * @throws java.io.IOException if any.
 */
public synchronized void reload() throws IOException {
    try {
        File cfgFile = ConfigFileConstants.getFile(ConfigFileConstants.DISCOVERY_CONFIG_FILE_NAME);
        LOG.debug("reload: config file path {}", cfgFile.getPath());
        final FileSystemResource resource = new FileSystemResource(cfgFile);
        m_config = JaxbUtils.unmarshal(DiscoveryConfiguration.class, resource);
        try {
            getInitialSleepTime();
            getRestartSleepTime();
            getIntraPacketDelay();
            getConfiguredAddresses();
        } catch (final Throwable e) {
            throw new IOException("An error occurred while validating the configuration: " + e.getMessage(), e);
        }
    } catch (IOException e) {
        LOG.error("Could not unmarshal configuration file: " + ConfigFileConstants.getFileName(ConfigFileConstants.DISCOVERY_CONFIG_FILE_NAME), e);
        throw e;
    }
}
Also used : DiscoveryConfiguration(org.opennms.netmgt.config.discovery.DiscoveryConfiguration) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) File(java.io.File)

Example 68 with FileSystemResource

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

the class SyslogdConfigFactory method reload.

/**
 * Reload the config from the default config file
 *
 * @throws java.io.IOException Thrown if the specified config file cannot be
 *                             read/loaded
 */
public synchronized void reload() throws IOException {
    File configFile = ConfigFileConstants.getFile(ConfigFileConstants.SYSLOGD_CONFIG_FILE_NAME);
    m_config = JaxbUtils.unmarshal(SyslogdConfiguration.class, new FileSystemResource(configFile));
    parseIncludedFiles();
}
Also used : SyslogdConfiguration(org.opennms.netmgt.config.syslogd.SyslogdConfiguration) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File)

Example 69 with FileSystemResource

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

the class WmiPeerFactory method init.

/**
 * Load the config from the default config file and create the singleton
 * instance of this factory.
 *
 * @exception java.io.IOException
 *                Thrown if the specified config file cannot be read
 * @throws java.io.IOException
 *             if any.
 */
public static synchronized void init() throws IOException {
    if (m_loaded) {
        // to reload, reload() will need to be called
        return;
    }
    WmiPeerFactory factory = new WmiPeerFactory(new FileSystemResource(ConfigFileConstants.getFile(ConfigFileConstants.WMI_CONFIG_FILE_NAME)));
    factory.afterPropertiesSet();
    setInstance(factory);
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource)

Example 70 with FileSystemResource

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

the class EventConfMatcherTest method setUp.

@Before
public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    eventConfDao = new DefaultEventConfDao();
    eventConfDao.setConfigResource(new FileSystemResource(new File("src/test/resources/matcher-test.events.xml")));
    eventConfDao.afterPropertiesSet();
    Assert.assertEquals(5, eventConfDao.getAllEvents().size());
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Before(org.junit.Before)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)148 File (java.io.File)77 Test (org.junit.Test)44 Resource (org.springframework.core.io.Resource)42 Before (org.junit.Before)27 ClassPathResource (org.springframework.core.io.ClassPathResource)16 IOException (java.io.IOException)12 FileWriter (java.io.FileWriter)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Properties (java.util.Properties)8 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 URL (java.net.URL)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 UrlResource (org.springframework.core.io.UrlResource)7 Date (java.util.Date)5 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4