Search in sources :

Example 21 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project disconf by knightliao.

the class ReloadablePropertiesFactoryBean method setLocations.

/**
     */
public void setLocations(List<String> fileNames) {
    List<Resource> resources = new ArrayList<Resource>();
    for (String filename : fileNames) {
        // trim
        filename = filename.trim();
        String realFileName = getFileName(filename);
        //
        // register to disconf
        //
        DisconfMgr.getInstance().reloadableScan(realFileName);
        //
        // only properties will reload
        //
        String ext = FilenameUtils.getExtension(filename);
        if (ext.equals("properties")) {
            PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
            try {
                Resource[] resourceList = pathMatchingResourcePatternResolver.getResources(filename);
                for (Resource resource : resourceList) {
                    resources.add(resource);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    this.locations = resources.toArray(new Resource[resources.size()]);
    lastModified = new long[locations.length];
    super.setLocations(locations);
}
Also used : ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 22 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project disconf by knightliao.

the class TestAntPathMatcher method ResourceLoaderTest.

@Test
public void ResourceLoaderTest() throws Exception {
    /* 资源地址表达式
         * classpath:相对于类的根路径,可访问jar或zip中的资源哦
         * classpath*:和上面类似,只不过上面是加载找到的第一个资源,这个是全部加载
         * file:文件系统目录中加载,可以是绝对,也可以是相对
         * http:// 不用多说了吧
         * ftp:// 不用多说了吧
         *
         * ant风格:可以使用通配符
         *  ?:匹配一个字符
         *  *:匹配多个字符
         *  **:匹配多层路径
         * */
    ResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
    Resource[] rs = rpr.getResources("classpath:testXml.xml");
    for (Resource one : rs) {
        showResourceInfo(one, true);
    }
    System.out.println("=============================");
    //file:访问文件系统(绝对 和 相对路径方式)
    //绝对路径 类似于FileSystemResource
    rs = rpr.getResources("file:src/test/resources/res/testXml.xml");
    //rs=rpr.getResources("file:src/aop.xml");
    for (Resource one : rs) {
        showResourceInfo(one, true);
    }
    System.out.println("=============================");
    //http:方式
    rs = rpr.getResources("http://www.baidu.com/img/bdlogo.gif");
    //为了测试的简便,这里直接取第一个资源
    byte[] gifByte = IOUtils.toByteArray(rs[0].getInputStream());
    FileUtils.writeByteArrayToFile(new File("tmp/bdlogo1.gif"), gifByte);
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) File(java.io.File) Test(org.junit.Test)

Example 23 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project head by mifos.

the class MifosViewerServletContextListener method copyFromClassPathToDirectory.

private void copyFromClassPathToDirectory(String directoryToScan, File rootDirectory) throws IOException {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources(LOCATION_PATTERN);
    LOGGER.info("Found " + resources.length + " Resources on " + LOCATION_PATTERN);
    for (Resource resource : resources) {
        if (resource.exists() & resource.isReadable() && resource.contentLength() > 0) {
            URL url = resource.getURL();
            String urlString = url.toExternalForm();
            String targetName = urlString.substring(urlString.indexOf(directoryToScan));
            File destination = new File(rootDirectory, targetName);
            FileUtils.copyURLToFile(url, destination);
            LOGGER.info("Copied " + url + " to " + destination.getAbsolutePath());
        } else {
            LOGGER.debug("Did not copy, seems to be directory: " + resource.getDescription());
        }
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) File(java.io.File) URL(java.net.URL)

Example 24 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project spring-security by spring-projects.

the class ApacheDSContainer method importLdifs.

private void importLdifs() throws Exception {
    // Import any ldif files
    Resource[] ldifs;
    if (ctxt == null) {
        // Not running within an app context
        ldifs = new PathMatchingResourcePatternResolver().getResources(ldifResources);
    } else {
        ldifs = ctxt.getResources(ldifResources);
    }
    if (ldifs == null || ldifs.length == 0) {
        return;
    }
    if (ldifs.length == 1) {
        String ldifFile;
        try {
            ldifFile = ldifs[0].getFile().getAbsolutePath();
        } catch (IOException e) {
            ldifFile = ldifs[0].getURI().toString();
        }
        logger.info("Loading LDIF file: " + ldifFile);
        LdifFileLoader loader = new LdifFileLoader(service.getAdminSession(), new File(ldifFile), null, getClass().getClassLoader());
        loader.execute();
    } else {
        throw new IllegalArgumentException("More than one LDIF resource found with the supplied pattern:" + ldifResources + " Got " + Arrays.toString(ldifs));
    }
}
Also used : LdifFileLoader(org.apache.directory.server.protocol.shared.store.LdifFileLoader) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) File(java.io.File)

Example 25 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project spring-security by spring-projects.

the class PythonInterpreterPreInvocationAdvice method before.

public boolean before(Authentication authentication, MethodInvocation mi, PreInvocationAttribute preAttr) {
    PythonInterpreterPreInvocationAttribute pythonAttr = (PythonInterpreterPreInvocationAttribute) preAttr;
    String script = pythonAttr.getScript();
    PythonInterpreter python = new PythonInterpreter();
    python.set("authentication", authentication);
    python.set("args", createArgumentMap(mi));
    python.set("method", mi.getMethod().getName());
    Resource scriptResource = new PathMatchingResourcePatternResolver().getResource(script);
    try {
        python.execfile(scriptResource.getInputStream());
    } catch (IOException e) {
        throw new IllegalArgumentException("Couldn't run python script, " + script, e);
    }
    PyObject allowed = python.get("allow");
    if (allowed == null) {
        throw new IllegalStateException("Python script did not set the permit flag");
    }
    return (Boolean) Py.tojava(allowed, Boolean.class);
}
Also used : PythonInterpreter(org.python.util.PythonInterpreter) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) PyObject(org.python.core.PyObject)

Aggregations

PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)34 Resource (org.springframework.core.io.Resource)22 Test (org.junit.Test)15 IOException (java.io.IOException)11 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)11 PersistenceUnitInfo (javax.persistence.spi.PersistenceUnitInfo)10 File (java.io.File)9 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)9 ClassPathResource (org.springframework.core.io.ClassPathResource)7 ArrayList (java.util.ArrayList)6 FileOutputStream (java.io.FileOutputStream)4 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)4 UrlResource (org.springframework.core.io.UrlResource)3 URL (java.net.URL)2 Properties (java.util.Properties)2 DataSource (javax.sql.DataSource)2 Ignore (org.junit.Ignore)2 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)2 LangDetectException (com.cybozu.labs.langdetect.LangDetectException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1