Example usage for org.springframework.core.io Resource exists

List of usage examples for org.springframework.core.io Resource exists

Introduction

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

Prototype

boolean exists();

Source Link

Document

Determine whether this resource actually exists in physical form.

Usage

From source file:org.alfresco.repo.transfer.fsr.SchemaBootstrap.java

/**
 * Execute database creation script./* w ww  .ja v a 2 s .  c  o  m*/
 */
protected void createDBTables() {

    // check if tables in DB exist?
    if (isDBInitialized())
        return;
    Resource resourceScript = rpr.getResource(creationScript);
    if (!resourceScript.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Ressource " + creationScript + " does not exist!");
        }
        // throw exception
        throw new AlfrescoRuntimeException("Creation script " + creationScript + " not found!");
    }

    // execute script
    executeSript(resourceScript);

}

From source file:org.apache.cloudstack.spring.module.model.impl.DefaultModuleDefinition.java

protected void checkNameMatchesSelf() throws IOException {
    String expectedLocation = ModuleLocationUtils.getModuleLocation(baseDir, name);
    Resource self = resolver.getResource(expectedLocation);

    if (!self.exists()) {
        throw new IOException("Resource [" + location() + "] is expected to exist at [" + expectedLocation
                + "] please ensure the name property is correct");
    }/*from w w w .j a  v  a2s  .c o  m*/

    String moduleUrl = moduleProperties.getURL().toExternalForm();
    String selfUrl = self.getURL().toExternalForm();

    if (!moduleUrl.equals(selfUrl)) {
        throw new IOException("Resource [" + location() + "] and [" + self.getURL()
                + "] do not appear to be the same resource, "
                + "please ensure the name property is correct or that the " + "module is not defined twice");
    }
}

From source file:org.apache.cloudstack.spring.module.model.impl.DefaultModuleDefinitionSet.java

protected void load(Resource resource, Properties props) {
    if (resource == null) {
        return;/*  w  ww . ja  v a 2s .  c o m*/
    }

    InputStream is = null;
    try {
        if (resource.exists()) {
            is = resource.getInputStream();
            props.load(is);
        }
    } catch (IOException e) {
        throw new IllegalStateException("Failed to load resource [" + resource + "]", e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.apache.cocoon.core.container.spring.avalon.ConfigurationReader.java

/**
 * Handle includes of avalon configurations.
 *
 * @param contextURI// w  ww  . j  a  va 2 s . c  om
 * @param loadedURIs
 * @param includeStatement
 * @throws ConfigurationException
 */
protected void handleInclude(final String contextURI, final Set loadedURIs,
        final Configuration includeStatement) throws ConfigurationException {
    final String includeURI = includeStatement.getAttribute("src", null);
    String directoryURI = null;
    if (includeURI == null) {
        // check for directories
        directoryURI = includeStatement.getAttribute("dir", null);
    }
    if (includeURI == null && directoryURI == null) {
        throw new ConfigurationException("Include statement must either have a 'src' or 'dir' attribute, at "
                + includeStatement.getLocation());
    }

    if (includeURI != null) {
        try {
            Resource src = this.resolver.getResource(getUrl(includeURI, contextURI));
            loadURI(src, loadedURIs, includeStatement);
        } catch (Exception e) {
            throw new ConfigurationException(
                    "Cannot load '" + includeURI + "' at " + includeStatement.getLocation(), e);
        }

    } else {
        boolean load = true;
        // test if directory exists (only if not classpath protocol is used)
        if (!ResourceUtils.isClasspathUri(directoryURI)) {
            Resource dirResource = this.resolver.getResource(this.getUrl(directoryURI, contextURI));
            if (!dirResource.exists()) {
                if (!includeStatement.getAttributeAsBoolean("optional", false)) {
                    throw new ConfigurationException("Directory '" + directoryURI + "' does not exist ("
                            + includeStatement.getLocation() + ").");
                }
                load = false;
            }
        }
        if (load) {
            final String pattern = includeStatement.getAttribute("pattern", null);
            try {
                Resource[] resources = this.resolver
                        .getResources(this.getUrl(directoryURI + '/' + pattern, contextURI));
                if (resources != null) {
                    Arrays.sort(resources, ResourceUtils.getResourceComparator());
                    for (int i = 0; i < resources.length; i++) {
                        loadURI(resources[i], loadedURIs, includeStatement);
                    }
                }
            } catch (Exception e) {
                throw new ConfigurationException("Cannot load from directory '" + directoryURI + "' at "
                        + includeStatement.getLocation(), e);
            }
        }
    }
}

From source file:org.apache.cocoon.core.container.spring.avalon.ConfigurationReader.java

/**
 * Handle include for spring bean configurations.
 *
 * @param contextURI/*from   ww w . ja  va 2s.  c  o m*/
 * @param includeStatement
 * @throws ConfigurationException
 */
protected void handleBeanInclude(final String contextURI, final Configuration includeStatement)
        throws ConfigurationException {
    final String includeURI = includeStatement.getAttribute("src", null);
    String directoryURI = null;
    if (includeURI == null) {
        // check for directories
        directoryURI = includeStatement.getAttribute("dir", null);
    }
    if (includeURI == null && directoryURI == null) {
        throw new ConfigurationException("Include statement must either have a 'src' or 'dir' attribute, at "
                + includeStatement.getLocation());
    }

    if (includeURI != null) {
        try {
            Resource src = this.resolver.getResource(getUrl(includeURI, contextURI));
            this.configInfo.addImport(getUrl(src));
        } catch (Exception e) {
            throw new ConfigurationException(
                    "Cannot load '" + includeURI + "' at " + includeStatement.getLocation(), e);
        }

    } else {
        // test if directory exists
        Resource dirResource = this.resolver.getResource(this.getUrl(directoryURI, contextURI));
        if (dirResource.exists()) {
            final String pattern = includeStatement.getAttribute("pattern", null);
            try {
                Resource[] resources = this.resolver
                        .getResources(this.getUrl(directoryURI + '/' + pattern, contextURI));
                if (resources != null) {
                    Arrays.sort(resources, ResourceUtils.getResourceComparator());
                    for (int i = 0; i < resources.length; i++) {
                        this.configInfo.addImport(getUrl(resources[i]));
                    }
                }
            } catch (IOException ioe) {
                throw new ConfigurationException("Unable to read configurations from " + directoryURI);
            }
        } else {
            if (!includeStatement.getAttributeAsBoolean("optional", false)) {
                throw new ConfigurationException("Directory '" + directoryURI + "' does not exist ("
                        + includeStatement.getLocation() + ").");
            }
        }
    }
}

From source file:org.apache.syncope.core.misc.spring.ResourceWithFallbackLoader.java

@Override
public Resource getResource(final String location) {
    Resource resource = resolver.getResource(primary + location);
    if (!resource.exists()) {
        resource = resolver.getResource(fallback + location);
    }/*from   w  w  w  . j  a v a  2 s.  com*/

    return resource;
}

From source file:org.apereo.portal.portlets.portletadmin.xmlsupport.XmlChannelPublishingDefinitionDao.java

private PortletPublishingDefinition loadChannelPublishingDefinition(int channelTypeId) {
    // if the CPD is not already in the cache, determine the CPD URI
    final String cpdUri;
    if (channelTypeId >= 0) {
        final IPortletType type = this.portletTypeRegistry.getPortletType(channelTypeId);
        if (type == null) {
            throw new IllegalArgumentException("No ChannelType registered with id: " + channelTypeId);
        }//from   w  w  w  .  j  a va 2s  . c o  m
        cpdUri = type.getCpdUri();
    } else {
        throw new IllegalArgumentException("No ChannelType registered with id: " + channelTypeId);
    }

    // read and parse the CPD
    final PortletPublishingDefinition def;
    final Resource cpdResource = this.resourceLoader.getResource("classpath:" + cpdUri);
    if (!cpdResource.exists()) {
        throw new MissingResourceException(
                "Failed to find CPD '" + cpdUri + "' for channel type " + channelTypeId,
                this.getClass().getName(), cpdUri);
    }

    final InputStream cpdStream;
    try {
        cpdStream = cpdResource.getInputStream();
    } catch (IOException e) {
        throw new MissingResourceException(
                "Failed to load CPD '" + cpdUri + "' for channel type " + channelTypeId,
                this.getClass().getName(), cpdUri);
    }

    try {
        def = (PortletPublishingDefinition) this.unmarshaller.unmarshal(cpdStream);
        final List<Step> sharedParameters = this.getSharedParameters();
        def.getSteps().addAll(sharedParameters);
        // add the CPD to the cache and return it
        this.cpdCache.put(channelTypeId, def);

        return def;
    } catch (JAXBException e) {
    } finally {
        IOUtils.closeQuietly(cpdStream);
    }

    return null;

}

From source file:org.apereo.portal.portlets.portletadmin.xmlsupport.XmlChannelPublishingDefinitionDao.java

private List<Step> getSharedParameters() {
    if (this.sharedParameters != null) {
        return this.sharedParameters;
    }//from w ww.j  a va 2 s. c om

    // read and parse the shared CPD
    final Resource paramResource = this.resourceLoader.getResource("classpath:" + SHARED_PARAMETERS_PATH);
    if (!paramResource.exists()) {
        throw new MissingResourceException(
                "Failed to find shared parameters CPD '" + SHARED_PARAMETERS_PATH + "'",
                this.getClass().getName(), SHARED_PARAMETERS_PATH);
    }

    final InputStream paramStream;
    try {
        paramStream = paramResource.getInputStream();
    } catch (IOException e) {
        throw new MissingResourceException("Failed to load CPD '" + SHARED_PARAMETERS_PATH + "'",
                this.getClass().getName(), SHARED_PARAMETERS_PATH);
    }

    // parse the shared CPD and add its steps to the end of the type-specific
    try {
        PortletPublishingDefinition config = (PortletPublishingDefinition) unmarshaller.unmarshal(paramStream);
        this.sharedParameters = config.getSteps();
    } catch (JAXBException e) {
        logger.warn("Failed to parse: " + paramResource, e);
    } finally {
        IOUtils.closeQuietly(paramStream);
    }
    return this.sharedParameters;
}

From source file:org.apereo.portal.tools.dbloader.HibernateDbLoader.java

protected ITableDataProvider loadTables(DbLoaderConfig configuration, Dialect dialect)
        throws ParserConfigurationException, SAXException, IOException {
    //Locate tables.xml
    final String tablesFileName = configuration.getTablesFile();
    final Resource tablesFile = this.resourceLoader.getResource(tablesFileName);
    if (!tablesFile.exists()) {
        throw new IllegalArgumentException("Could not find tables file: " + tablesFile);
    }//from   w ww  . ja v a 2  s .  c  o  m

    //Setup parser with custom handler to generate Table model and parse
    final SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    final TableXmlHandler dh = new TableXmlHandler(dialect);
    saxParser.parse(new InputSource(tablesFile.getInputStream()), dh);

    return dh;
}

From source file:org.apereo.portal.tools.dbloader.HibernateDbLoader.java

protected void populateTables(DbLoaderConfig configuration, Map<String, Map<String, Integer>> tableColumnTypes)
        throws ParserConfigurationException, SAXException, IOException {
    //Locate tables.xml
    final String dataFileName = configuration.getDataFile();
    final Resource dataFile = this.resourceLoader.getResource(dataFileName);
    if (!dataFile.exists()) {
        throw new IllegalArgumentException("Could not find data file: " + dataFile);
    }// ww w.  j  a va2  s .  c o m

    //Setup parser with custom handler to generate Table model and parse
    final SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    final DataXmlHandler dh = new DataXmlHandler(jdbcOperations, transactionOperations, tableColumnTypes);
    saxParser.parse(new InputSource(dataFile.getInputStream()), dh);
}