Search in sources :

Example 1 with ContextResource

use of org.springframework.core.io.ContextResource in project liquibase by liquibase.

the class SpringResourceAccessor method getResourcePath.

/**
 * Returns the lookup path to the given resource.
 */
protected String getResourcePath(Resource resource) {
    if (resource instanceof ContextResource) {
        return ((ContextResource) resource).getPathWithinContext();
    }
    if (resource instanceof ClassPathResource) {
        return ((ClassPathResource) resource).getPath();
    }
    // have to fall back to figuring out the path as best we can
    try {
        String url = resource.getURL().toExternalForm();
        if (url.contains("!")) {
            return url.replaceFirst(".*!", "");
        } else {
            while (!resourceLoader.getResource("classpath:" + url).exists()) {
                String newUrl = url.replaceFirst("^/?.*?/", "");
                if (newUrl.equals(url)) {
                    throw new UnexpectedLiquibaseException("Could determine path for " + resource.getURL().toExternalForm());
                }
                url = newUrl;
            }
            return url;
        }
    } catch (IOException e) {
        // so throw a breaking error now rather than wait for bigger problems down the line
        throw new UnexpectedLiquibaseException("Cannot determine resource path for " + resource.getDescription());
    }
}
Also used : IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ClassPathResource(org.springframework.core.io.ClassPathResource) ContextResource(org.springframework.core.io.ContextResource)

Example 2 with ContextResource

use of org.springframework.core.io.ContextResource in project camunda-bpm-platform by camunda.

the class SpringTransactionsProcessEngineConfiguration method autoDeployResources.

protected void autoDeployResources(ProcessEngine processEngine) {
    if (deploymentResources != null && deploymentResources.length > 0) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment().enableDuplicateFiltering(false).name(deploymentName).tenantId(deploymentTenantId);
        for (Resource resource : deploymentResources) {
            String resourceName = null;
            if (resource instanceof ContextResource) {
                resourceName = ((ContextResource) resource).getPathWithinContext();
            } else if (resource instanceof ByteArrayResource) {
                resourceName = resource.getDescription();
            } else {
                try {
                    resourceName = resource.getFile().getAbsolutePath();
                } catch (IOException e) {
                    resourceName = resource.getFilename();
                }
            }
            try {
                if (resourceName.endsWith(".bar") || resourceName.endsWith(".zip") || resourceName.endsWith(".jar")) {
                    deploymentBuilder.addZipInputStream(new ZipInputStream(resource.getInputStream()));
                } else {
                    deploymentBuilder.addInputStream(resourceName, resource.getInputStream());
                }
            } catch (IOException e) {
                throw new ProcessEngineException("couldn't auto deploy resource '" + resource + "': " + e.getMessage(), e);
            }
        }
        deploymentBuilder.deploy();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayResource(org.springframework.core.io.ByteArrayResource) ContextResource(org.springframework.core.io.ContextResource) Resource(org.springframework.core.io.Resource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) DeploymentBuilder(org.camunda.bpm.engine.repository.DeploymentBuilder) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) RepositoryService(org.camunda.bpm.engine.RepositoryService) ContextResource(org.springframework.core.io.ContextResource)

Aggregations

IOException (java.io.IOException)2 ContextResource (org.springframework.core.io.ContextResource)2 ZipInputStream (java.util.zip.ZipInputStream)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)1 RepositoryService (org.camunda.bpm.engine.RepositoryService)1 DeploymentBuilder (org.camunda.bpm.engine.repository.DeploymentBuilder)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 Resource (org.springframework.core.io.Resource)1