List of usage examples for org.springframework.core.io Resource getInputStream
InputStream getInputStream() throws IOException;
From source file:org.codehaus.groovy.grails.plugins.springsecurity.AuthorizeTools.java
@SuppressWarnings("unchecked") private static void loadExternalConfigs(final List<ConfigObject> configs, final ConfigObject config) { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); List<String> locations = (List<String>) ConfigurationHolder.getFlatConfig() .get("springsecurity.config.locations"); if (locations != null) { for (String location : locations) { if (StringUtils.hasLength(location)) { try { Resource resource = resolver.getResource(location); InputStream stream = null; try { stream = resource.getInputStream(); ConfigSlurper configSlurper = new ConfigSlurper(GrailsUtil.getEnvironment()); configSlurper.setBinding(config); if (resource.getFilename().endsWith(".groovy")) { configs.add(configSlurper.parse(IOUtils.toString(stream))); } else if (resource.getFilename().endsWith(".properties")) { Properties props = new Properties(); props.load(stream); configs.add(configSlurper.parse(props)); }/* w ww .j a v a 2 s.c o m*/ } finally { if (stream != null) { stream.close(); } } } catch (Exception e) { LOG.warn("Unable to load specified config location $location : ${e.message}"); LOG.debug("Unable to load specified config location $location : ${e.message}", e); } } } } }
From source file:net.solarnetwork.util.StringMerger.java
/** * Merge from a Resource, and return the merged output as a String. * //from w w w. j av a 2 s .co m * <p> * This method will read the Resource as character data line by line, * merging each line as it goes. * </p> * * @param resource * the resource * @param data * the data to merge with * @param nullValue * the value to substitute for null data elements * @return merged string * @throws IOException * if an error occurs */ public static String mergeResource(Resource resource, Object data, String nullValue) throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Merging " + resource.getFilename() + " with " + data); } InputStream in = null; try { in = resource.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder buf = new StringBuilder(); String oneLine = null; while ((oneLine = reader.readLine()) != null) { mergeString(oneLine, data, nullValue, buf); buf.append("\n"); } return buf.toString(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // ignore this } } } }
From source file:org.eclipse.gemini.blueprint.test.internal.util.PropertiesUtil.java
/** * Shortcut method - loads a property object from the given input stream and applies property expansion. The * returned properties object preserves order at the expense of speed. * /*ww w.j a v a 2s. c o m*/ * @param resource * @return */ public static Properties loadAndExpand(Resource resource) { Properties props = new OrderedProperties(); if (resource == null) return props; try { props.load(resource.getInputStream()); } catch (IOException ex) { return null; } return expandProperties(props); }
From source file:de.ingrid.iplug.ckan.utils.ScriptEngine.java
/** * Get the compiled version of the given script * @param script The script file// ww w . j a va2 s. co m * @return CompiledScript * @throws ScriptException * @throws IOException */ protected static CompiledScript getCompiledScript(Resource script) throws ScriptException, IOException { String filename = script.getFilename(); if (!compiledScripts.containsKey(filename)) { javax.script.ScriptEngine engine = getEngine(script); if (engine instanceof Compilable) { Compilable compilable = (Compilable) engine; CompiledScript compiledScript = compilable.compile(new InputStreamReader(script.getInputStream())); compiledScripts.put(filename, compiledScript); } } return compiledScripts.get(filename); }
From source file:com.sinosoft.one.mvc.scanning.ResourceRef.java
public static ResourceRef toResourceRef(Resource folder) throws IOException { ResourceRef rr = new ResourceRef(folder, null, null); String[] modifiers = null;/*from ww w . j a v a2 s .c o m*/ Resource mvcPropertiesResource = rr.getInnerResource("META-INF/mvc.properties"); if (mvcPropertiesResource.exists()) { if (logger.isDebugEnabled()) { logger.debug("found mvc.properties: " + mvcPropertiesResource.getURI()); } InputStream in = mvcPropertiesResource.getInputStream(); rr.properties.load(in); in.close(); String attrValue = rr.properties.getProperty("mvc"); if (attrValue == null) { attrValue = rr.properties.getProperty("Mvc"); } if (attrValue != null) { modifiers = StringUtils.split(attrValue, ", ;\n\r\t"); if (logger.isDebugEnabled()) { logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } } // if (modifiers == null) { if (!"jar".equals(rr.getProtocol())) { modifiers = new String[] { "**" }; if (logger.isDebugEnabled()) { logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } else { JarFile jarFile = new JarFile(rr.getResource().getFile()); Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); String attrValue = attributes.getValue("mvc"); if (attrValue == null) { attrValue = attributes.getValue("Mvc"); } if (attrValue != null) { modifiers = StringUtils.split(attrValue, ", ;\n\r\t"); if (logger.isDebugEnabled()) { logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } } } } rr.setModifiers(modifiers); return rr; }
From source file:com.gzj.tulip.load.ResourceRef.java
public static ResourceRef toResourceRef(Resource folder) throws IOException { ResourceRef rr = new ResourceRef(folder, null, null); String[] modifiers = null;//from w w w . j a v a2 s.c o m Resource rosePropertiesResource = rr.getInnerResource("META-INF/rose.properties"); if (rosePropertiesResource.exists()) { if (logger.isDebugEnabled()) { logger.debug("found rose.properties: " + rosePropertiesResource.getURI()); } InputStream in = rosePropertiesResource.getInputStream(); rr.properties.load(in); in.close(); String attrValue = rr.properties.getProperty("rose"); if (attrValue == null) { attrValue = rr.properties.getProperty("Rose"); } if (attrValue != null) { modifiers = StringUtils.split(attrValue, ", ;\n\r\t"); if (logger.isDebugEnabled()) { logger.debug("modifiers[by properties][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } } // if (modifiers == null) { if (!"jar".equals(rr.getProtocol())) { modifiers = new String[] { "**" }; if (logger.isDebugEnabled()) { logger.debug("modifiers[by default][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } else { JarFile jarFile = new JarFile(rr.getResource().getFile()); Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); String attrValue = attributes.getValue("rose"); if (attrValue == null) { attrValue = attributes.getValue("Rose"); } if (attrValue != null) { modifiers = StringUtils.split(attrValue, ", ;\n\r\t"); if (logger.isDebugEnabled()) { logger.debug("modifiers[by manifest.mf][" + rr.getResource().getURI() + "]=" + Arrays.toString(modifiers)); } } } } } rr.setModifiers(modifiers); return rr; }
From source file:org.psikeds.knowledgebase.xml.impl.XSDValidator.java
/** * Validate XML against specified XSD schmema. * //from w w w . j a va 2 s.c o m * @param xsd * Spring-Resource for the XSD-schema that will be used to validate the * XML * @param xml * Spring-Resource for the XML * @throws SAXException * if XML is not valid against XSD * @throws IOException */ public static void validate(final Resource xsd, final Resource xml) throws SAXException, IOException { InputStream xsdStream = null; InputStream xmlStream = null; try { xsdStream = xsd.getInputStream(); xmlStream = xml.getInputStream(); validate(xsdStream, xmlStream); } finally { // we opened the streams, we close them if (xsdStream != null) { try { xsdStream.close(); } catch (final Exception ex) { // ignore } finally { xsdStream = null; } } if (xmlStream != null) { try { xmlStream.close(); } catch (final Exception ex) { // ignore } finally { xmlStream = null; } } } }
From source file:io.lavagna.web.support.ResourceController.java
private static Map<String, Map<Object, Object>> fromResources(Resource[] resources) throws IOException { Pattern extractLanguage = Pattern.compile("^messages_(.*)\\.properties$"); Map<String, Map<Object, Object>> langs = new HashMap<>(); String version = Version.version(); for (Resource res : resources) { Matcher matcher = extractLanguage.matcher(res.getFilename()); matcher.find();/*from w w w .j av a 2 s . c om*/ String lang = matcher.group(1); Properties p = new Properties(); try (InputStream is = res.getInputStream()) { p.load(is); } langs.put(lang, new HashMap<>(p)); langs.get(lang).put("build.version", version); } return langs; }
From source file:org.eclipse.gemini.blueprint.test.internal.util.jar.JarUtils.java
/** * //w w w . j av a2 s . c o m * Writes a resource content to a jar. * * @param res * @param entryName * @param jarStream * @param bufferSize * @return the number of bytes written to the jar file * @throws Exception */ public static int writeToJar(Resource res, String entryName, JarOutputStream jarStream, int bufferSize) throws IOException { byte[] readWriteJarBuffer = new byte[bufferSize]; // remove leading / if present. if (entryName.charAt(0) == '/') entryName = entryName.substring(1); jarStream.putNextEntry(new ZipEntry(entryName)); InputStream entryStream = res.getInputStream(); int numberOfBytes; // read data into the buffer which is later on written to the jar. while ((numberOfBytes = entryStream.read(readWriteJarBuffer)) != -1) { jarStream.write(readWriteJarBuffer, 0, numberOfBytes); } return numberOfBytes; }
From source file:com.netxforge.oss2.core.xml.CastorUtils.java
/** * Unmarshal a Castor XML configuration file. Uses Java 5 generics for * return type.//w w w.j a v a2 s.com * * @param clazz the class representing the marshalled XML configuration file * @param resource the marshalled XML configuration file to unmarshal * @param preserveWhitespace whether or not to preserve whitespace * @return Unmarshalled object representing XML file * @throws org.exolab.castor.xml.MarshalException if the underlying Castor * Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.MarshalException * @throws org.exolab.castor.xml.ValidationException if the underlying Castor * Unmarshaller.unmarshal() call throws a org.exolab.castor.xml.ValidationException * @throws java.io.IOException if the resource could not be opened */ public static <T> T unmarshal(Class<T> clazz, Resource resource, boolean preserveWhitespace) throws MarshalException, ValidationException, IOException { InputStream in; try { in = resource.getInputStream(); } catch (IOException e) { IOException newE = new IOException( "Failed to open XML configuration file for resource '" + resource + "': " + e); newE.initCause(e); throw newE; } try { InputSource source = new InputSource(in); try { source.setSystemId(resource.getURL().toString()); } catch (Throwable t) { // ignore } return unmarshal(clazz, source, preserveWhitespace); } finally { IOUtils.closeQuietly(in); } }