Search in sources :

Example 86 with Resource

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

the class DroolsTicketerServiceLayerTest method setUp.

@Before
public void setUp() throws Exception {
    m_eventIpcManager = new MockEventIpcManager();
    EventIpcManagerFactory.setIpcManager(m_eventIpcManager);
    MockLogAppender.setupLogging();
    ResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource("classpath:/drools-ticketer-rules.drl");
    m_easyMockUtils = new EasyMockUtils();
    m_configDao = m_easyMockUtils.createMock(DroolsTicketerConfigDao.class);
    EasyMock.expect(m_configDao.getRulesFile()).andReturn(resource.getFile()).times(1);
    EasyMock.replay(m_configDao);
    m_alarmDao = m_easyMockUtils.createMock(AlarmDao.class);
    m_ticketerPlugin = m_easyMockUtils.createMock(Plugin.class);
    m_droolsTicketerServiceLayer = new DroolsTicketerServiceLayer(m_configDao);
    m_droolsTicketerServiceLayer.setAlarmDao(m_alarmDao);
    m_droolsTicketerServiceLayer.setTicketerPlugin(m_ticketerPlugin);
    EasyMock.reset(m_configDao);
    m_alarm = new OnmsAlarm();
    m_alarm.setId(1);
    m_alarm.setLogMsg("Test Logmsg");
    m_alarm.setDescription("Test Description");
    m_alarm.setUei("uei.opennms.org/nodes/nodeDown");
    m_ticket = new Ticket();
    m_ticket.setId("4");
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Ticket(org.opennms.api.integration.ticketing.Ticket) MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) Resource(org.springframework.core.io.Resource) EasyMockUtils(org.opennms.test.mock.EasyMockUtils) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Plugin(org.opennms.api.integration.ticketing.Plugin) Before(org.junit.Before)

Example 87 with Resource

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

the class OSReportPlugin method getEntries.

@Override
public Map<String, Resource> getEntries() {
    final Map<String, Resource> map = new TreeMap<String, Resource>();
    map.put("Name", getResourceFromProperty("os.name"));
    map.put("Architecture", getResourceFromProperty("os.arch"));
    map.put("Version", getResourceFromProperty("os.version"));
    map.put("Distribution", map.get("Name"));
    OperatingSystemMXBean osBean = getBean(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
    if (osBean == null) {
        LOG.info("falling back to local VM OperatingSystemMXBean");
        osBean = ManagementFactory.getOperatingSystemMXBean();
    }
    LOG.trace("bean = {}", osBean);
    addGetters(osBean, map);
    File lsb = new File("/bin/lsb_release");
    File solaris = new File("/var/sadm/softinfo/INST_RELEASE");
    if (lsb.exists()) {
        final String text = slurpCommand(new String[] { "/bin/lsb_release", "-a" });
        final Map<String, String> distMap = splitMultilineString(": +", text);
        for (final Map.Entry<String, String> entry : distMap.entrySet()) {
            map.put("Distribution " + entry.getKey(), getResource(entry.getValue()));
        }
    } else if (solaris.exists()) {
        map.put("Distribution OS", getResource("Solaris"));
        final String solarisText = slurp(solaris);
        final Map<String, String> distMap = splitMultilineString("=", solarisText);
        for (final Map.Entry<String, String> entry : distMap.entrySet()) {
            map.put("Distribution " + entry.getKey().toLowerCase().replaceFirst("^.", entry.getKey().substring(0, 1).toUpperCase()), getResource(entry.getValue()));
        }
        if (map.containsKey("Distribution OS")) {
            map.put("Distribution", map.remove("Distribution OS"));
        }
        File isainfo = new File("/usr/bin/isainfo");
        if (isainfo.exists()) {
            final String arch = slurpCommand(new String[] { "/usr/bin/isainfo", "-n" });
            if (arch != null) {
                map.put("Instruction Set", getResource(arch));
            }
        }
    } else {
        for (final Map.Entry<String, String> entry : m_oses.entrySet()) {
            final String description = slurp(new File(entry.getKey()));
            if (description != null) {
                map.put("Distribution", getResource(entry.getValue()));
                map.put("Description", getResource(description.trim()));
            }
        }
    }
    if (map.containsKey("Distribution Distributor ID")) {
        map.put("Distribution", map.remove("Distribution Distributor ID"));
    }
    if (map.containsKey("Distribution Description")) {
        map.put("Description", map.remove("Distribution Description"));
    }
    return map;
}
Also used : Resource(org.springframework.core.io.Resource) TreeMap(java.util.TreeMap) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 88 with Resource

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

the class LogReportPlugin method getEntries.

@Override
public Map<String, Resource> getEntries() {
    final TreeMap<String, Resource> map = new TreeMap<String, Resource>();
    File f = new File(System.getProperty("opennms.home") + File.separator + "logs");
    processFile(f, map);
    return map;
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) Resource(org.springframework.core.io.Resource) TreeMap(java.util.TreeMap) File(java.io.File)

Example 89 with Resource

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

the class OpenNMSReportPlugin method getEntries.

@Override
public Map<String, Resource> getEntries() {
    final TreeMap<String, Resource> map = new TreeMap<String, Resource>();
    final InputStream is = this.getClass().getResourceAsStream("/version.properties");
    if (is != null) {
        Properties p = new Properties();
        try {
            p.load(is);
            map.put("Version", getResource(p.getProperty("version.display")));
        } catch (final IOException e) {
            LOG.warn("Unable to load from version.properties", e);
        }
    }
    if (m_nodeDao != null) {
        map.put("Number of Nodes", getResource(Integer.toString(m_nodeDao.countAll())));
    }
    if (m_ipInterfaceDao != null) {
        map.put("Number of IP Interfaces", getResource(Integer.toString(m_ipInterfaceDao.countAll())));
    }
    if (m_snmpInterfaceDao != null) {
        map.put("Number of SNMP Interfaces", getResource(Integer.toString(m_snmpInterfaceDao.countAll())));
    }
    if (m_eventDao != null) {
        map.put("Number of Events", getResource(Integer.toString(m_eventDao.countAll())));
    }
    if (m_alarmDao != null) {
        map.put("Number of Alarms", getResource(Integer.toString(m_alarmDao.countAll())));
    }
    return map;
}
Also used : InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) TreeMap(java.util.TreeMap) Properties(java.util.Properties)

Example 90 with Resource

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

the class XmlSystemReportFormatter method write.

@Override
public void write(final SystemReportPlugin plugin) {
    if (!hasDisplayable(plugin))
        return;
    if (m_handler == null) {
        try {
            StreamResult streamResult = new StreamResult(getOutputStream());
            SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            m_handler = tf.newTransformerHandler();
            Transformer serializer = m_handler.getTransformer();
            serializer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "entry");
            m_handler.setResult(streamResult);
        } catch (final Exception e) {
            LOG.error("Unable to create XML stream writer.", e);
            m_handler = null;
        }
        try {
            m_handler.startDocument();
            m_handler.startElement("", "", "systemReportPlugins", null);
        } catch (final Exception e) {
            LOG.warn("Unable to start document.", e);
            m_handler = null;
        }
    }
    if (m_handler == null) {
        LOG.warn("Unable to write, no handler defined!");
        return;
    }
    try {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "name", "CDATA", plugin.getName());
        atts.addAttribute("", "", "description", "CDATA", plugin.getDescription());
        m_handler.startElement("", "", "plugin", atts);
        for (final Map.Entry<String, Resource> entry : plugin.getEntries().entrySet()) {
            final boolean displayable = isDisplayable(entry.getValue());
            atts = new AttributesImpl();
            atts.addAttribute("", "", "key", "CDATA", entry.getKey());
            if (!displayable) {
                atts.addAttribute("", "", "skipped", "CDATA", "true");
            }
            m_handler.startElement("", "", "entry", atts);
            if (displayable) {
                final String value = getResourceText(entry.getValue());
                if (value != null) {
                    m_handler.startCDATA();
                    m_handler.characters(value.toCharArray(), 0, value.length());
                    m_handler.endCDATA();
                }
            }
            m_handler.endElement("", "", "entry");
        }
        m_handler.endElement("", "", "plugin");
    } catch (final Exception e) {
        LOG.warn("An error occurred while attempting to write XML data.", e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Resource(org.springframework.core.io.Resource) Map(java.util.Map)

Aggregations

Resource (org.springframework.core.io.Resource)610 Test (org.junit.Test)257 ClassPathResource (org.springframework.core.io.ClassPathResource)231 IOException (java.io.IOException)103 FileSystemResource (org.springframework.core.io.FileSystemResource)77 UrlResource (org.springframework.core.io.UrlResource)68 File (java.io.File)64 ArrayList (java.util.ArrayList)58 ByteArrayResource (org.springframework.core.io.ByteArrayResource)49 InputStream (java.io.InputStream)46 InputStreamResource (org.springframework.core.io.InputStreamResource)31 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)30 URL (java.net.URL)25 HashMap (java.util.HashMap)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)18 ServletContextResource (org.springframework.web.context.support.ServletContextResource)18 Map (java.util.Map)17 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)17 ResourceLoader (org.springframework.core.io.ResourceLoader)16 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)16