Search in sources :

Example 86 with ByteArrayResource

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

the class OpenNMSITCase method setUp.

@Before
public void setUp() throws Exception {
    MockUtil.println("------------ Begin Test " + this + " --------------------------");
    MockLogAppender.setupLogging();
    if (m_runSupers) {
        createMockNetwork();
        populateDatabase();
        DataSourceFactory.setInstance(m_db);
        SnmpPeerFactory.setInstance(new SnmpPeerFactory(new ByteArrayResource(getSnmpConfig().getBytes())));
        if (isStartEventd()) {
            m_eventdIpcMgr = new EventIpcManagerDefaultImpl(m_registry);
            EventUtilJdbcImpl eventUtil = new EventUtilJdbcImpl();
            AbstractEventUtil.setInstance(eventUtil);
            JdbcEventdServiceManager eventdServiceManager = new JdbcEventdServiceManager();
            eventdServiceManager.setDataSource(m_db);
            eventdServiceManager.afterPropertiesSet();
            /*
                 * Make sure we specify a full resource path since "this" is
                 * the unit test class, which is most likely in another package. 
                 */
            File configFile = ConfigurationTestUtils.getFileForResource(this, "/org/opennms/netmgt/mock/eventconf.xml");
            DefaultEventConfDao eventConfDao = new DefaultEventConfDao();
            eventConfDao.setConfigResource(new FileSystemResource(configFile));
            eventConfDao.afterPropertiesSet();
            EventExpander eventExpander = new EventExpander(m_registry);
            eventExpander.setEventConfDao(eventConfDao);
            eventExpander.setEventUtil(eventUtil);
            eventExpander.afterPropertiesSet();
            EventIpcBroadcastProcessor eventIpcBroadcastProcessor = new EventIpcBroadcastProcessor(m_registry);
            eventIpcBroadcastProcessor.setEventIpcBroadcaster(m_eventdIpcMgr);
            eventIpcBroadcastProcessor.afterPropertiesSet();
            List<EventProcessor> eventProcessors = new ArrayList<EventProcessor>(3);
            eventProcessors.add(eventExpander);
            eventProcessors.add(eventIpcBroadcastProcessor);
            DefaultEventHandlerImpl eventHandler = new DefaultEventHandlerImpl(m_registry);
            eventHandler.setEventProcessors(eventProcessors);
            eventHandler.afterPropertiesSet();
            m_eventdIpcMgr.setHandlerPoolSize(5);
            m_eventdIpcMgr.setEventHandler(eventHandler);
            m_eventdIpcMgr.afterPropertiesSet();
            m_eventProxy = m_eventdIpcMgr;
            EventIpcManagerFactory.setIpcManager(m_eventdIpcMgr);
            EventIpcManagerEventHandlerProxy proxy = new EventIpcManagerEventHandlerProxy();
            proxy.setEventIpcManager(m_eventdIpcMgr);
            proxy.afterPropertiesSet();
            List<EventHandler> eventHandlers = new ArrayList<EventHandler>(1);
            eventHandlers.add(proxy);
            TcpEventReceiver tcpEventReceiver = new TcpEventReceiver();
            tcpEventReceiver.setPort(5837);
            tcpEventReceiver.setEventHandlers(eventHandlers);
            UdpEventReceiver udpEventReceiver = new UdpEventReceiver();
            udpEventReceiver.setPort(5837);
            tcpEventReceiver.setEventHandlers(eventHandlers);
            List<EventReceiver> eventReceivers = new ArrayList<EventReceiver>(2);
            eventReceivers.add(tcpEventReceiver);
            eventReceivers.add(udpEventReceiver);
            m_eventd = new Eventd();
            m_eventd.setEventdServiceManager(eventdServiceManager);
            m_eventd.setEventReceivers(eventReceivers);
            m_eventd.setReceiver(new BroadcastEventProcessor(m_eventdIpcMgr, eventConfDao));
            m_eventd.init();
            m_eventd.start();
        }
    }
    m_transMgr = new DataSourceTransactionManager(DataSourceFactory.getInstance());
}
Also used : EventExpander(org.opennms.netmgt.eventd.EventExpander) EventIpcManagerEventHandlerProxy(org.opennms.netmgt.eventd.adaptors.EventIpcManagerEventHandlerProxy) JdbcEventdServiceManager(org.opennms.netmgt.dao.mock.JdbcEventdServiceManager) Eventd(org.opennms.netmgt.eventd.Eventd) SnmpPeerFactory(org.opennms.netmgt.config.SnmpPeerFactory) ArrayList(java.util.ArrayList) EventHandler(org.opennms.netmgt.eventd.adaptors.EventHandler) ByteArrayResource(org.springframework.core.io.ByteArrayResource) EventIpcManagerDefaultImpl(org.opennms.netmgt.eventd.EventIpcManagerDefaultImpl) FileSystemResource(org.springframework.core.io.FileSystemResource) TcpEventReceiver(org.opennms.netmgt.eventd.adaptors.tcp.TcpEventReceiver) DefaultEventConfDao(org.opennms.netmgt.config.DefaultEventConfDao) DefaultEventHandlerImpl(org.opennms.netmgt.eventd.DefaultEventHandlerImpl) EventReceiver(org.opennms.netmgt.eventd.adaptors.EventReceiver) TcpEventReceiver(org.opennms.netmgt.eventd.adaptors.tcp.TcpEventReceiver) UdpEventReceiver(org.opennms.netmgt.eventd.adaptors.udp.UdpEventReceiver) EventIpcBroadcastProcessor(org.opennms.netmgt.eventd.processor.EventIpcBroadcastProcessor) BroadcastEventProcessor(org.opennms.netmgt.eventd.BroadcastEventProcessor) EventProcessor(org.opennms.netmgt.events.api.EventProcessor) UdpEventReceiver(org.opennms.netmgt.eventd.adaptors.udp.UdpEventReceiver) BroadcastEventProcessor(org.opennms.netmgt.eventd.BroadcastEventProcessor) File(java.io.File) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) Before(org.junit.Before)

Example 87 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project geode by apache.

the class ConvertUtils method convert.

/**
   * Converts the 2-dimensional byte array of file data, which includes the name of the file as
   * bytes followed by the byte content of the file, for all files being transmitted by Gfsh to the
   * GemFire Manager.
   * <p/>
   * 
   * @param fileData a 2 dimensional byte array of files names and file content.
   * @return an array of Spring Resource objects encapsulating the details (name and content) of
   *         each file being transmitted by Gfsh to the GemFire Manager.
   * @see org.springframework.core.io.ByteArrayResource
   * @see org.springframework.core.io.Resource
   * @see org.apache.geode.management.internal.cli.CliUtil#bytesToData(byte[][])
   * @see org.apache.geode.management.internal.cli.CliUtil#bytesToNames(byte[][])
   */
public static Resource[] convert(final byte[][] fileData) {
    if (fileData != null) {
        final String[] fileNames = CliUtil.bytesToNames(fileData);
        final byte[][] fileContent = CliUtil.bytesToData(fileData);
        final List<Resource> resources = new ArrayList<Resource>(fileNames.length);
        for (int index = 0; index < fileNames.length; index++) {
            final String filename = fileNames[index];
            resources.add(new ByteArrayResource(fileContent[index], String.format("Contents of JAR file (%1$s).", filename)) {

                @Override
                public String getFilename() {
                    return filename;
                }
            });
        }
        return resources.toArray(new Resource[resources.size()]);
    }
    return new Resource[0];
}
Also used : ArrayList(java.util.ArrayList) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource)

Example 88 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project ignite by apache.

the class GridUriDeploymentSpringParser method parseTasksDocument.

/**
     * Converts given input stream expecting XML inside to
     * {@link GridUriDeploymentSpringDocument}.
     * <p>
     * This is a workaround for the {@link InputStreamResource} which does
     * not work properly.
     *
     * @param in Input stream with XML.
     * @param log Logger
     * @return Grid wrapper for the input stream.
     * @throws org.apache.ignite.spi.IgniteSpiException Thrown if incoming input stream could not be
     *      read or parsed by {@code Spring} {@link XmlBeanFactory}.
     * @see XmlBeanFactory
     */
static GridUriDeploymentSpringDocument parseTasksDocument(InputStream in, IgniteLogger log) throws IgniteSpiException {
    assert in != null;
    // Note: use ByteArrayResource instead of InputStreamResource because InputStreamResource doesn't work.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        U.copy(in, out);
        XmlBeanFactory factory = new XmlBeanFactory(new ByteArrayResource(out.toByteArray()));
        return new GridUriDeploymentSpringDocument(factory);
    } catch (BeansException | IOException e) {
        throw new IgniteSpiException("Failed to parse spring XML file.", e);
    } finally {
        U.close(out, log);
    }
}
Also used : XmlBeanFactory(org.springframework.beans.factory.xml.XmlBeanFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BeansException(org.springframework.beans.BeansException)

Example 89 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project ignite by apache.

the class IgniteSpringHelperImpl method userVersion.

/** {@inheritDoc} */
@Override
public String userVersion(ClassLoader ldr, IgniteLogger log) {
    assert ldr != null;
    assert log != null;
    // For system class loader return cached version.
    if (ldr == U.gridClassLoader() && SYS_LDR_VER.get() != null)
        return SYS_LDR_VER.get();
    String usrVer = U.DFLT_USER_VERSION;
    InputStream in = ldr.getResourceAsStream(IGNITE_XML_PATH);
    if (in != null) {
        // Note: use ByteArrayResource instead of InputStreamResource because
        // InputStreamResource doesn't work.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            U.copy(in, out);
            DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
            reader.loadBeanDefinitions(new ByteArrayResource(out.toByteArray()));
            usrVer = (String) factory.getBean("userVersion");
            usrVer = usrVer == null ? U.DFLT_USER_VERSION : usrVer.trim();
        } catch (NoSuchBeanDefinitionException ignored) {
            if (log.isInfoEnabled())
                log.info("User version is not explicitly defined (will use default version) [file=" + IGNITE_XML_PATH + ", clsLdr=" + ldr + ']');
            usrVer = U.DFLT_USER_VERSION;
        } catch (BeansException e) {
            U.error(log, "Failed to parse Spring XML file (will use default user version) [file=" + IGNITE_XML_PATH + ", clsLdr=" + ldr + ']', e);
            usrVer = U.DFLT_USER_VERSION;
        } catch (IOException e) {
            U.error(log, "Failed to read Spring XML file (will use default user version) [file=" + IGNITE_XML_PATH + ", clsLdr=" + ldr + ']', e);
            usrVer = U.DFLT_USER_VERSION;
        } finally {
            U.close(out, log);
        }
    }
    // For system class loader return cached version.
    if (ldr == U.gridClassLoader())
        SYS_LDR_VER.compareAndSet(null, usrVer);
    return usrVer;
}
Also used : InputStream(java.io.InputStream) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeansException(org.springframework.beans.BeansException)

Aggregations

ByteArrayResource (org.springframework.core.io.ByteArrayResource)89 Test (org.junit.Test)66 Resource (org.springframework.core.io.Resource)30 Properties (java.util.Properties)26 JmsNorthbounderConfig (org.opennms.netmgt.alarmd.northbounder.jms.JmsNorthbounderConfig)6 JmsNorthbounderConfigDao (org.opennms.netmgt.alarmd.northbounder.jms.JmsNorthbounderConfigDao)6 InetAddress (java.net.InetAddress)5 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 NorthboundAlarm (org.opennms.netmgt.alarmd.api.NorthboundAlarm)5 JmsDestination (org.opennms.netmgt.alarmd.northbounder.jms.JmsDestination)5 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)5 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)5 OnmsNode (org.opennms.netmgt.model.OnmsNode)5 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 MutablePropertySources (org.springframework.core.env.MutablePropertySources)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 InputStream (java.io.InputStream)3