Example usage for org.springframework.core.io ByteArrayResource ByteArrayResource

List of usage examples for org.springframework.core.io ByteArrayResource ByteArrayResource

Introduction

In this page you can find the example usage for org.springframework.core.io ByteArrayResource ByteArrayResource.

Prototype

public ByteArrayResource(byte[] byteArray) 

Source Link

Document

Create a new ByteArrayResource .

Usage

From source file:org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.java

/** {@inheritDoc} */
@Override//ww  w . j  ava  2 s . c  o  m
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;
}

From source file:org.broadleafcommerce.common.extensibility.context.merge.MergeXmlConfigResource.java

public Resource getMergedConfigResource(ResourceInputStream[] sources) throws BeansException {
    Resource configResource = null;
    ResourceInputStream merged = null;/*from w w  w.ja v a 2 s . c om*/
    try {
        merged = merge(sources);

        //read the final stream into a byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean eof = false;
        while (!eof) {
            int temp = merged.read();
            if (temp == -1) {
                eof = true;
            } else {
                baos.write(temp);
            }
        }
        configResource = new ByteArrayResource(baos.toByteArray());

        if (LOG.isDebugEnabled()) {
            LOG.debug("Merged config: \n" + serialize(configResource));
        }
    } catch (MergeException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (MergeManagerSetupException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (IOException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } finally {
        if (merged != null) {
            try {
                merged.close();
            } catch (Throwable e) {
                LOG.error("Unable to merge source and patch locations", e);
            }
        }
    }

    return configResource;
}

From source file:org.broadleafcommerce.common.extensibility.context.MergeApplicationContextXmlConfigResource.java

/**
 * Generate a merged configuration resource, loading the definitions from the given streams. Note,
 * all sourceLocation streams will be merged using standard Spring configuration override rules. However, the patch
 * streams are fully merged into the result of the sourceLocations simple merge. Patch merges are first executed according
 * to beans with the same id. Subsequent merges within a bean are executed against tagnames - ignoring any
 * further id attributes.//from w  w w. j a  va2  s .  c  om
 *
 * @param sources array of input streams for the source application context files
 * @param patches array of input streams for the patch application context files
 * @throws BeansException
 */
public Resource[] getConfigResources(ResourceInputStream[] sources, ResourceInputStream[] patches)
        throws BeansException {
    Resource[] configResources = null;
    ResourceInputStream merged = null;
    try {
        merged = merge(sources);

        if (patches != null) {
            ResourceInputStream[] patches2 = new ResourceInputStream[patches.length + 1];
            patches2[0] = merged;
            System.arraycopy(patches, 0, patches2, 1, patches.length);

            merged = merge(patches2);
        }

        //read the final stream into a byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean eof = false;
        while (!eof) {
            int temp = merged.read();
            if (temp == -1) {
                eof = true;
            } else {
                baos.write(temp);
            }
        }
        configResources = new Resource[] { new ByteArrayResource(baos.toByteArray()) };

        if (LOG.isDebugEnabled()) {
            LOG.debug("Merged ApplicationContext Including Patches: \n" + serialize(configResources[0]));
        }
    } catch (MergeException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (MergeManagerSetupException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (IOException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } finally {
        if (merged != null) {
            try {
                merged.close();
            } catch (Throwable e) {
                LOG.error("Unable to merge source and patch locations", e);
            }
        }
    }

    return configResources;
}

From source file:org.broadleafcommerce.common.extensibility.jpa.MergeJPAPersistenceResource.java

public Resource getMergedConfigResource(ResourceInputStream[] sources) throws BeansException {
    Resource configResource = null;
    ResourceInputStream merged = null;//  www.  j  a v  a  2s . c  o  m
    try {
        List<String> mappingFiles = new ArrayList<String>(20);
        ResourceInputStream[] inMemoryStreams = new ResourceInputStream[sources.length];
        for (int j = 0; j < sources.length; j++) {
            byte[] sourceArray = buildArrayFromStream(sources[j]);
            compileMappingFiles(mappingFiles, sourceArray);
            inMemoryStreams[j] = new ResourceInputStream(new ByteArrayInputStream(sourceArray),
                    sources[j].getName());
        }

        merged = merge(inMemoryStreams);

        //read the final stream into a byte array
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        boolean eof = false;
        while (!eof) {
            int temp = merged.read();
            if (temp == -1) {
                eof = true;
            } else {
                baos.write(temp);
            }
        }
        configResource = new ByteArrayResource(baos.toByteArray());

        if (LOG.isDebugEnabled()) {
            LOG.debug("Merged config: \n" + serialize(configResource));
        }
    } catch (MergeException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (MergeManagerSetupException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (IOException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (SAXException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } catch (ParserConfigurationException e) {
        throw new FatalBeanException("Unable to merge source and patch locations", e);
    } finally {
        if (merged != null) {
            try {
                merged.close();
            } catch (Throwable e) {
                LOG.error("Unable to merge source and patch locations", e);
            }
        }
    }

    return configResource;
}

From source file:org.broadleafcommerce.common.resource.service.ResourceMinificationServiceImpl.java

@Override
public byte[] minify(String filename, byte[] bytes) {
    if (!getEnabled()) {
        LOG.trace("Minification is disabled, returning original resource");
        return bytes;
    }//from w  ww  .  j  a va2s.  co  m

    Resource modifiedResource = minify(new ByteArrayResource(bytes), filename);

    if (modifiedResource instanceof GeneratedResource) {
        return ((GeneratedResource) modifiedResource).getBytes();
    } else {
        return bytes;
    }
}

From source file:org.cloudfoundry.identity.uaa.provider.saml.IdentityProviderConfiguratorTests.java

private static Map<String, Map<String, Object>> parseYaml(String sampleYaml) {
    YamlMapFactoryBean factory = new YamlMapFactoryBean();
    factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE_AND_IGNORE);
    List<Resource> resources = new ArrayList<>();
    ByteArrayResource resource = new ByteArrayResource(sampleYaml.getBytes());
    resources.add(resource);//from  w  w  w.ja  v  a2s .com
    factory.setResources(resources.toArray(new Resource[resources.size()]));
    Map<String, Object> tmpdata = factory.getObject();
    Map<String, Map<String, Object>> dataMap = new HashMap<>();
    for (Map.Entry<String, Object> entry : ((Map<String, Object>) tmpdata.get("providers")).entrySet()) {
        dataMap.put(entry.getKey(), (Map<String, Object>) entry.getValue());
    }
    return Collections.unmodifiableMap(dataMap);
}

From source file:org.gridgain.grid.kernal.processors.spring.GridSpringProcessorImpl.java

/** {@inheritDoc} */
@Override//from  w w w.  java 2s .  c  o  m
public String userVersion(ClassLoader ldr, GridLogger 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(GRIDGAIN_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="
                        + GRIDGAIN_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="
                    + GRIDGAIN_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="
                    + GRIDGAIN_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;
}

From source file:org.jwebsocket.plugins.scripting.app.BaseScriptApp.java

/**
 * Load an Spring IOC XML configuration file into the script application
 * bean factory.//from   w w w  . ja  v a 2 s  .  c o  m
 *
 * @param aFile
 * @throws Exception
 */
public void loadToAppBeanFactory(String aFile) throws Exception {
    // beans definitions file
    aFile = aFile.replace("${APP_HOME}", mAppPath);

    // creating the XML definitions reader
    XmlBeanDefinitionReader lXmlReader = new XmlBeanDefinitionReader(mBeanFactory);
    lXmlReader.setBeanClassLoader(mClassLoader);

    // path for dtd and xsd files location
    String lBeansDef = FileUtils.readFileToString(new File(aFile));
    lBeansDef = lBeansDef.replace("${JWEBSOCKET_HOME}", mJWSHome);
    lBeansDef = lBeansDef.replace("${APP_HOME}", mAppPath);

    // loading XML definitions file into app bean factory
    lXmlReader.loadBeanDefinitions(new ByteArrayResource(lBeansDef.getBytes()));
}

From source file:org.kaaproject.kaa.server.common.admin.AdminClient.java

/**
 * Read file from disk and return it binary format represented as ByteArrayResource.
 *
 * @param resource the name of file resource
 * @throws IOException the io exception//from   w ww . j  ava 2 s  .c  o m
 */
public static ByteArrayResource getFileResource(final String resource) throws IOException {
    byte[] data = FileUtils.readResourceBytes(resource);
    ByteArrayResource bar = new ByteArrayResource(data) {
        @Override
        public String getFilename() {
            return resource;
        }
    };
    return bar;
}

From source file:org.kaaproject.kaa.server.common.admin.AdminClient.java

/**
 * Represented string resource as ByteArrayResource. The resource body encoded in UTF-8.
 *
 * @throws IOException the io exception/*from ww w.  j  a  v  a  2 s  .  c o  m*/
 */
public static ByteArrayResource getStringResource(final String resourceName, final String resourceBody)
        throws IOException {
    byte[] data = resourceBody.getBytes("UTF-8");
    ByteArrayResource bar = new ByteArrayResource(data) {
        @Override
        public String getFilename() {
            return resourceName;
        }
    };
    return bar;
}