Example usage for org.springframework.core.io.support EncodedResource getResource

List of usage examples for org.springframework.core.io.support EncodedResource getResource

Introduction

In this page you can find the example usage for org.springframework.core.io.support EncodedResource getResource.

Prototype

public final Resource getResource() 

Source Link

Document

Return the Resource held by this EncodedResource .

Usage

From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java

/**
 * Actually load properties from the given EncodedResource into the given Properties instance.
 * @param props the Properties instance to load into
 * @param resource the resource to load from
 * @param persister the PropertiesPersister to use
 * @throws IOException in case of I/O errors
 *///from  ww  w .  java 2 s .c o  m
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
        throws IOException {

    InputStream stream = null;
    Reader reader = null;
    try {
        String filename = resource.getResource().getFilename();
        if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
            stream = resource.getInputStream();
            persister.loadFromXml(props, stream);
        } else if (resource.requiresReader()) {
            reader = resource.getReader();
            persister.load(props, reader);
        } else {
            stream = resource.getInputStream();
            persister.load(props, stream);
        }
    } finally {
        if (stream != null) {
            stream.close();
        }
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.brienwheeler.lib.spring.beans.SmartXmlBeanDefinitionReader.java

@Override
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    String canonicalPath = null;//w w  w .j a v  a2 s  .c  o m
    try {
        canonicalPath = encodedResource.getResource().getURL().getPath();
    } catch (IOException e) {
        // on FileNotFoundException, fall through to normal behavior -- this will probably result in a
        // FNFE from somewhere else that will make more sense.
        if (!(e instanceof FileNotFoundException))
            throw new BeanDefinitionStoreException("Error resolving canonical path for context resource", e);
    }

    if (canonicalPath != null && loadedPaths.contains(canonicalPath)) {
        if (logger.isDebugEnabled())
            logger.debug("skipping already loaded path " + canonicalPath);
        return 0;
    } else {
        int count = super.loadBeanDefinitions(encodedResource);
        if (canonicalPath != null)
            loadedPaths.add(canonicalPath);
        return count;
    }

}

From source file:org.solmix.runtime.support.spring.ContainerXmlBeanDefinitionReader.java

private int fastInfosetLoadBeanDefinitions(EncodedResource encodedResource)
        throws IOException, StaleFastinfosetException, ParserConfigurationException, XMLStreamException {

    URL resUrl = encodedResource.getResource().getURL();
    // There are XML files scampering around that don't end in .xml.
    // We don't apply the optimization to them.
    if (!resUrl.getPath().endsWith(".xml")) {
        throw new StaleFastinfosetException();
    }//  w w w  .  j av a2 s .com
    String fixmlPath = resUrl.getPath().replaceFirst("\\.xml$", ".fixml");
    String protocol = resUrl.getProtocol();
    // beware of the relative URL rules for jar:, which are surprising.
    if ("jar".equals(protocol)) {
        fixmlPath = fixmlPath.replaceFirst("^.*!", "");
    }

    URL fixmlUrl = new URL(resUrl, fixmlPath);

    // if we are in unpacked files, we take some extra time
    // to ensure that we aren't using a stale Fastinfoset file.
    if ("file".equals(protocol)) {
        URLConnection resCon = null;
        URLConnection fixCon = null;
        resCon = resUrl.openConnection();
        fixCon = fixmlUrl.openConnection();
        if (resCon.getLastModified() > fixCon.getLastModified()) {
            throw new StaleFastinfosetException();
        }
    }

    Resource newResource = new UrlResource(fixmlUrl);
    Document doc = TunedDocumentLoader.loadFastinfosetDocument(fixmlUrl);
    if (doc == null) {
        //something caused FastinfoSet to not be able to read the doc
        throw new StaleFastinfosetException();
    }
    return registerBeanDefinitions(doc, newResource);
}

From source file:org.data.support.beans.factory.xml.XmlQueryDefinitionReader.java

/**
 * Load query definitions from the specified XML file.
 * @param encodedResource the resource descriptor for the XML file,
 * allowing to specify an encoding to use for parsing the file
 * @return the number of query definitions found
 * @throws QueryDefinitionStoreException in case of loading or parsing errors
 *///from   w w w .  j a v  a2 s  .  c  om
public int loadQueryDefinitions(EncodedResource encodedResource) throws QueryDefinitionStoreException {
    Assert.notNull(encodedResource, "EncodedResource must not be null");
    if (logger.isInfoEnabled()) {
        logger.info("Loading XML bean definitions from " + encodedResource.getResource());
    }

    Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
    if (currentResources == null) {
        currentResources = new HashSet<EncodedResource>(4);
        this.resourcesCurrentlyBeingLoaded.set(currentResources);
    }
    if (!currentResources.add(encodedResource)) {
        throw new QueryDefinitionStoreException(
                "Detected cyclic loading of " + encodedResource + " - check your import definitions!");
    }
    try {
        InputStream inputStream = encodedResource.getResource().getInputStream();
        try {
            InputSource inputSource = new InputSource(inputStream);
            if (encodedResource.getEncoding() != null) {
                inputSource.setEncoding(encodedResource.getEncoding());
            }
            return doLoadQueryDefinitions(inputSource, encodedResource.getResource());
        } finally {
            inputStream.close();
        }
    } catch (IOException ex) {
        throw new QueryDefinitionStoreException(
                "IOException parsing XML document from " + encodedResource.getResource(), ex);
    } finally {
        currentResources.remove(encodedResource);
        if (currentResources.isEmpty()) {
            this.resourcesCurrentlyBeingLoaded.remove();
        }
    }
}

From source file:org.bytesoft.bytejta.supports.springcloud.property.TransactionPropertySource.java

public TransactionPropertySource(String name, EncodedResource source) {
    super(name, source);

    EncodedResource encoded = (EncodedResource) this.getSource();
    AbstractResource resource = (AbstractResource) encoded.getResource();
    String path = resource.getFilename();

    if (StringUtils.isBlank(path)) {
        return;//from  www.ja  va2  s  .c  o  m
    }

    String[] values = path.split(":");
    if (values.length != 2) {
        return;
    }

    String protocol = values[0];
    String resName = values[1];
    if ("bytejta".equalsIgnoreCase(protocol) == false) {
        return;
    } else if ("loadbalancer.config".equalsIgnoreCase(resName) == false) {
        return;
    }

    this.enabled = true;

}

From source file:org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader.java

/**
 * Load bean definitions from the specified Groovy script or XML file.
 * <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
 * of resources will be parsed as Groovy scripts.
 * @param encodedResource the resource descriptor for the Groovy script or XML file,
 * allowing specification of an encoding to use for parsing the file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 *//*from  w  w w  . j av  a2  s.c om*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
    // Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
    String filename = encodedResource.getResource().getFilename();
    if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
        return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
    }

    Closure beans = new Closure(this) {
        public Object call(Object[] args) {
            invokeBeanDefiningClosure((Closure) args[0]);
            return null;
        }
    };
    Binding binding = new Binding() {
        @Override
        public void setVariable(String name, Object value) {
            if (currentBeanDefinition != null) {
                applyPropertyToBeanDefinition(name, value);
            } else {
                super.setVariable(name, value);
            }
        }
    };
    binding.setVariable("beans", beans);

    int countBefore = getRegistry().getBeanDefinitionCount();
    try {
        GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
        shell.evaluate(encodedResource.getReader(), "beans");
    } catch (Throwable ex) {
        throw new BeanDefinitionParsingException(
                new Problem("Error evaluating Groovy script: " + ex.getMessage(),
                        new Location(encodedResource.getResource()), null, ex));
    }
    return getRegistry().getBeanDefinitionCount() - countBefore;
}