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

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

Introduction

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

Prototype

@Nullable
String getFilename();

Source Link

Document

Determine a filename for this resource, i.e.

Usage

From source file:org.globus.security.stores.ResourceSigningPolicyStore.java

private void loadPolicies() throws SigningPolicyStoreException {

    String locations = this.parameters.getTrustRootLocations();
    Resource[] resources;//from   www  .j a  v a 2  s.  c o  m

    try {
        resources = resolver.getResources(locations);
    } catch (IOException e) {
        throw new SigningPolicyStoreException(e);
    }
    Map<X500Principal, SigningPolicy> newPolicyMap = new HashMap<X500Principal, SigningPolicy>();
    Map<URI, ResourceSigningPolicy> newPolicyFileMap = new HashMap<URI, ResourceSigningPolicy>();

    for (Resource resource : resources) {

        if (!resource.isReadable()) {
            logger.fine("Cannot read: " + resource.getFilename());
            continue;
        }
        loadSigningPolicy(resource, newPolicyMap, newPolicyFileMap);
    }

    this.policyMap = newPolicyMap;
    this.signingPolicyFileMap = newPolicyFileMap;
}

From source file:org.apache.cxf.dosgi.systests.common.AbstractDosgiSystemTest.java

@Override
protected Resource[] getTestBundles() {
    // Return the bundles for the current distribution, filtering out the 
    // ones that are already installed as part of the testing framework.
    // At the end the test subclass is called to obtain the test bundles.

    List<String> frameworkBundleNames = new ArrayList<String>();
    for (Resource r : getTestFrameworkBundles()) {
        frameworkBundleNames.add(r.getFilename());
    }/*from   w  ww . ja  v a  2 s.  c om*/

    try {
        List<Resource> resources = new ArrayList<Resource>();
        for (File file : getDistributionBundles()) {
            if (!frameworkBundleNames.contains(file.getName())) {
                resources.add(new FileSystemResource(file));
            }
        }

        resources.addAll(Arrays.asList(super.getTestBundles()));
        return resources.toArray(new Resource[resources.size()]);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:io.wcm.devops.conga.resource.ClasspathResourceCollectionImpl.java

ClasspathResourceCollectionImpl(String path, ResourceLoader resourceLoader) {
    super(path, resourceLoader.getClassLoader());
    this.resourceLoader = resourceLoader;

    try {//w w  w  . j a v a  2 s.  c o  m
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
                this.classLoader);
        org.springframework.core.io.Resource[] classpathResources = resolver
                .getResources("classpath*:" + convertPath(path) + "/*");
        for (org.springframework.core.io.Resource resource : classpathResources) {
            if (isFolder(resource)) {
                folderPaths.add(path + "/" + resource.getFilename());
            } else {
                fileUrls.add(resource.getURL());
            }
        }
    } catch (FileNotFoundException ex) {
        // empty folder
    } catch (IOException ex) {
        throw new ResourceException("Unable to enumerate classpath resources at " + path, ex);
    }
}

From source file:org.obiba.onyx.spring.context.OnyxMessageSourceFactoryBean.java

protected Locale extractLocale(Resource resource, String suffix) {
    String filename = resource.getFilename();

    StringBuilder locale = new StringBuilder(filename);
    // Find the last part that fits the bundle's name
    int basenameIndex = filename.lastIndexOf(MESSAGES_BUNDLENAME);
    int length = MESSAGES_BUNDLENAME.length();

    // Remove everything before the basename, the basename and one more char to eat the '_'
    locale.delete(0, basenameIndex + length + 1);
    int suffixIndex = locale.lastIndexOf(suffix);
    locale.delete(suffixIndex, locale.length());

    return new Locale(locale.toString());
}

From source file:org.jasig.ssp.util.importer.job.tasklet.BatchInitializer.java

private void removeInvalidFiles()
        throws UnsupportedEncodingException, IOException, PartialUploadGuardException {
    TableColumnMetaDataRepository repo = metadataRepository.getRepository().getColumnMetadataRepository();
    Boolean missingKeysFound = false;
    String exceptionMessage = "";
    String EOL = System.getProperty("line.separator");
    List<String> tableNames = new ArrayList<String>();
    for (Resource resource : resources) {
        String fileName = resource.getFilename();
        String[] tableName = fileName.split("\\.");

        BufferedReader reader = bufferedReaderFactory.create(resource, encoding);
        String[] headers = StringUtils.tokenizeToStringArray(reader.readLine(), ",");
        TableReference tableReference = new TableReference(tableName[0]);
        tableNames.add(EOL + tableName[0]);
        TableMetadata tableMetadato = repo.getTableMetadata(tableReference);
        Map<String, String> tableMap = new HashMap<String, String>();
        for (String header : headers)
            tableMap.put(header, header);

        if (!tableMetadato.hasKeys(tableMap)) {
            missingKeysFound = true;//from   w  w  w  .  ja  va  2 s .c  o  m
            List<String> missingKeys = tableMetadato.missingKeys(tableMap);
            exceptionMessage = exceptionMessage + "Table: " + tableName[0]
                    + " can not be processed. Missing headers: "
                    + StringUtils.collectionToCommaDelimitedString(missingKeys) + EOL;
        }
    }

    if (missingKeysFound) {
        exceptionMessage = exceptionMessage + "All Tables Not Processed: "
                + StringUtils.collectionToCommaDelimitedString(tableNames) + EOL;
        throw new PartialUploadGuardException(exceptionMessage);
    }
}

From source file:uk.org.taverna.platform.OSGIFrameworkIT.java

public void testPrintConfig() throws IOException {
    Resource[] bundles = getTestBundles();
    Resource[] testBundles = getTestFrameworkBundles();
    StringBuilder sb = new StringBuilder();
    StringBuilder sb2 = new StringBuilder();
    System.out.println("mkdir platform");
    System.out.println("mkdir platform/configuration");
    sb2.append("cp ");
    sb.append("osgi.bundles=");
    boolean printComma = false;
    for (Resource resource : bundles) {
        if (printComma) {
            sb.append(", ");
            sb2.append(" ");
        }/*from w w  w  . j a v  a2 s.co m*/
        sb.append(resource.getFilename() + "@start");
        sb2.append(resource.getFile());
        printComma = true;
    }
    for (Resource resource : testBundles) {
        if (!resource.getFilename().contains("test")) {
            if (printComma) {
                sb.append(", ");
                sb2.append(" ");
            }
            sb.append(resource.getFilename() + "@start");
            sb2.append(resource.getFile());
            printComma = true;
        }
    }
    sb2.append(" platform");
    System.out.println("echo \"" + sb.toString() + "\" > platform/configuration/config.ini");
    System.out.println(sb2.toString());
    System.out.println("zip platform.zip platform/*");
}

From source file:corner.orm.spring.CornerPropertiesPersister.java

/**
 * resoure???//from w  w  w .j  a v a2s  . c  om
 * @param props ?
 * @param location ?
 * @throws IOException ?
 */
private void loadImportConfigFile(Properties props, Resource location) throws IOException {
    InputStream is = null;
    try {
        is = location.getInputStream();
        if (location.getFilename().endsWith(PropertiesLoaderSupport.XML_FILE_EXTENSION)) {
            this.loadFromXml(props, is);
        } else {

            super.load(props, is);

        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
        }

    } finally {
        if (is != null) {
            is.close();
        }
    }

}

From source file:org.red5.spring.ExtendedPropertyPlaceholderConfigurer.java

/**
 * String[] of wildcard locations of properties that are converted to
 * Resource[] using using {@link PathMatchingResourcePatternResolver}
 * //from ww  w  .  ja  v a  2s  . co  m
 * @param locations
 *            String[]
 * @throws IOException
 */
public void setWildcardLocations(String[] locations) throws IOException {

    List<Resource> resources = new ArrayList<Resource>();
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
            this.getClass().getClassLoader());

    for (String location : locations) {
        logger.debug("Loading location {}", location);
        try {
            // Get all Resources for a wildcard location
            Resource[] configs = resolver.getResources(location);
            if (configs != null && configs.length > 0) {
                List<Resource> resourceGroup = new ArrayList<Resource>();
                for (Resource resource : configs) {
                    logger.debug("Loading {} for location {}", resource.getFilename(), location);
                    resourceGroup.add(resource);
                }
                // Sort all Resources for a wildcard location by filename
                Collections.sort(resourceGroup, new ResourceFilenameComparator());

                // Add to master List
                resources.addAll(resourceGroup);

            } else {
                logger.info("Wildcard location does not exist: {}", location);
            }
        } catch (IOException ioException) {
            logger.error("Failed to resolve location: {} - {}", location, ioException);
        }
    }
    this.setLocations(resources.toArray(new Resource[resources.size()]));
}

From source file:com.wavemaker.commons.classloader.ThrowawayFileClassLoader.java

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

    if (this.classPath == null) {
        throw new ClassNotFoundException("invalid search root: " + this.classPath);
    } else if (name == null) {
        throw new ClassNotFoundException(MessageResource.NULL_CLASS.getMessage());
    }//ww w  . j  a  v  a2s.  com

    String classNamePath = name.replace('.', '/') + ".class";

    byte[] fileBytes = null;
    try {
        InputStream is = null;
        JarFile jarFile = null;

        for (Resource entry : this.classPath) {
            if (entry.getFilename().toLowerCase().endsWith(".jar")) {
                jarFile = new JarFile(entry.getFile());
                ZipEntry ze = jarFile.getEntry(classNamePath);

                if (ze != null) {
                    is = jarFile.getInputStream(ze);
                    break;
                } else {
                    jarFile.close();
                }
            } else {
                Resource classFile = entry.createRelative(classNamePath);
                if (classFile.exists()) {
                    is = classFile.getInputStream();
                    break;
                }
            }
        }

        if (is != null) {
            try {
                fileBytes = IOUtils.toByteArray(is);
                is.close();
            } finally {
                if (jarFile != null) {
                    jarFile.close();
                }
            }
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(e.getMessage(), e);
    }

    if (name.contains(".")) {
        String packageName = name.substring(0, name.lastIndexOf('.'));
        if (getPackage(packageName) == null) {
            definePackage(packageName, "", "", "", "", "", "", null);
        }
    }

    Class<?> ret;
    if (fileBytes == null) {
        ret = ClassLoaderUtils.loadClass(name, this.parentClassLoader);
    } else {
        ret = defineClass(name, fileBytes, 0, fileBytes.length);
    }

    if (ret == null) {
        throw new ClassNotFoundException(
                "Couldn't find class " + name + " in expected classpath: " + this.classPath);
    }

    return ret;
}