Search in sources :

Example 91 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)

Example 92 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 93 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project leopard by tanhaichao.

the class DomainWhiteListConfigImpl method createTable.

protected void createTable() {
    StringBuilder sb = new StringBuilder();
    sb.append("CREATE TABLE  if not exists `leopard_domainwhitelist` (");
    sb.append("`domain` varchar(50) NOT NULL DEFAULT '',");
    sb.append("`username` varchar(50) NOT NULL DEFAULT '',");
    sb.append("`posttime` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',");
    sb.append("`comment` varchar(255) NOT NULL DEFAULT '',");
    sb.append("PRIMARY KEY (`domain`)");
    sb.append(");");
    String sql = sb.toString();
    Resource scripts = new ByteArrayResource(sql.getBytes());
    DatabasePopulator populator = new ResourceDatabasePopulator(scripts);
    try {
        DatabasePopulatorUtils.execute(populator, jdbcTemplate.getDataSource());
    } catch (ScriptStatementFailedException e) {
    }
}
Also used : ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) DatabasePopulator(org.springframework.jdbc.datasource.init.DatabasePopulator) ScriptStatementFailedException(org.springframework.jdbc.datasource.init.ScriptStatementFailedException)

Example 94 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project leopard by tanhaichao.

the class PropertyPlaceholderLeiImpl method decrypt.

/**
 * 解密.
 *
 * @param resource
 * @return
 */
protected Resource decrypt(Resource resource) {
    String content;
    try {
        InputStream input = resource.getInputStream();
        content = IOUtils.toString(input);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
    Pattern p = Pattern.compile("^[^#=\\s]+$", Pattern.MULTILINE);
    Matcher m = p.matcher(content);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String line = m.group();
        // System.err.println("body:" + body);
        String replacement;
        try {
            replacement = this.decode(line);
        } catch (Exception e) {
            System.err.println("app.properties解密出错:" + e.getMessage() + " line:" + line);
            replacement = line;
        }
        m.appendReplacement(sb, replacement);
    }
    m.appendTail(sb);
    // System.out.println("content:" + sb.toString());
    return new ByteArrayResource(sb.toString().getBytes());
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException)

Example 95 with ByteArrayResource

use of org.springframework.core.io.ByteArrayResource in project leopard by tanhaichao.

the class LogConfigLeiImpl method configure.

public static boolean configure(String content) {
    // System.err.println("log4j content:" + content);
    ByteArrayResource resource2 = new ByteArrayResource(content.getBytes(), "log4j.properties");
    try {
        InputStream input = resource2.getInputStream();
        configure(input);
        input.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    LogConfigLeiImpl.content = content;
    return true;
}
Also used : InputStream(java.io.InputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException)

Aggregations

ByteArrayResource (org.springframework.core.io.ByteArrayResource)95 Test (org.junit.Test)67 Resource (org.springframework.core.io.Resource)32 Properties (java.util.Properties)26 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 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 Before (org.junit.Before)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 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 MutablePropertySources (org.springframework.core.env.MutablePropertySources)4