Search in sources :

Example 1 with ResourceLocation

use of org.nutz.resource.impl.ResourceLocation in project nutz by nutzam.

the class Scans method scan.

/**
     * 在磁盘目录或者 CLASSPATH(包括 jar) 中搜索资源
     * <p/>
     * <b>核心方法</b>
     * 
     * @param src
     *            起始路径
     * @param regex
     *            资源名需要匹配的正则表达式
     * @return 资源列表
     */
public List<NutResource> scan(String src, String regex) {
    if (src.isEmpty())
        throw new RuntimeException("emtry src is NOT allow");
    if ("/".equals(src))
        throw new RuntimeException("root path is NOT allow");
    List<NutResource> list = new ArrayList<NutResource>();
    Pattern pattern = regex == null ? null : Pattern.compile(regex);
    // 先看看是不是文件系统上一个具体的文件
    if (src.startsWith("~/"))
        src = Disks.normalize(src);
    File srcFile = new File(src);
    if (srcFile.exists()) {
        if (srcFile.isDirectory()) {
            Disks.visitFile(srcFile, new ResourceFileVisitor(list, src), new ResourceFileFilter(pattern));
        } else {
            list.add(new FileResource(src, srcFile));
        }
    }
    for (ResourceLocation location : locations.values()) {
        location.scan(src, pattern, list);
    }
    // 如果啥都没找到,那么,用增强扫描
    if (list.isEmpty()) {
        try {
            Enumeration<URL> enu = ClassTools.getClassLoader().getResources(src);
            if (enu != null && enu.hasMoreElements()) {
                while (enu.hasMoreElements()) {
                    try {
                        URL url = enu.nextElement();
                        ResourceLocation loc = makeResourceLocation(url);
                        if (url.toString().contains("jar!"))
                            loc.scan(src, pattern, list);
                        else
                            loc.scan("", pattern, list);
                    } catch (Throwable e) {
                        if (log.isTraceEnabled())
                            log.trace("", e);
                    }
                }
            }
        } catch (Throwable e) {
            if (log.isDebugEnabled())
                log.debug("Fail to run deep scan!", e);
        }
        // 依然是空?
        if (list.isEmpty()) {
            try {
                InputStream ins = getClass().getClassLoader().getResourceAsStream(src);
                if (ins != null) {
                    list.add(new SimpleResource(src, src, ins));
                }
            } catch (Exception e) {
            }
        }
    }
    Collections.sort(list);
    if (log.isDebugEnabled())
        log.debugf("Found %s resource by src( %s ) , regex( %s )", list.size(), src, regex);
    return list;
}
Also used : Pattern(java.util.regex.Pattern) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileResource(org.nutz.resource.impl.FileResource) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceLocation(org.nutz.resource.impl.ResourceLocation) ErrorResourceLocation(org.nutz.resource.impl.ErrorResourceLocation) SimpleResource(org.nutz.resource.impl.SimpleResource) File(java.io.File)

Example 2 with ResourceLocation

use of org.nutz.resource.impl.ResourceLocation in project nutz by nutzam.

the class Scans method addConcurrency.

public boolean addConcurrency(String[] _jars) {
    ExecutorService es = Executors.newFixedThreadPool(8);
    List<Future<ResourceLocation>> tmps = new ArrayList<Future<ResourceLocation>>(_jars.length);
    for (int i = 0; i < _jars.length; i++) {
        final String path = _jars[i];
        if (path == null || !path.toLowerCase().endsWith(".jar"))
            continue;
        tmps.add(es.submit(new Callable<ResourceLocation>() {

            public ResourceLocation call() {
                return ResourceLocation.jar(path);
            }
        }));
    }
    es.shutdown();
    try {
        es.awaitTermination(5, TimeUnit.MINUTES);
        for (Future<ResourceLocation> f : tmps) {
            try {
                if (f.get() != null)
                    addResourceLocation(f.get());
            } catch (ExecutionException e) {
            }
        }
        return true;
    } catch (InterruptedException e) {
        return false;
    }
}
Also used : ResourceLocation(org.nutz.resource.impl.ResourceLocation) ErrorResourceLocation(org.nutz.resource.impl.ErrorResourceLocation) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Example 3 with ResourceLocation

use of org.nutz.resource.impl.ResourceLocation in project nutz by nutzam.

the class Scans method printLocations.

protected void printLocations(Stopwatch sw) {
    if (log.isDebugEnabled()) {
        log.debugf("Locations count=%d time use %sms", locations.size(), sw.du());
    }
    if (log.isTraceEnabled()) {
        StringBuilder sb = new StringBuilder();
        for (ResourceLocation rc : locations.values()) {
            sb.append('\t').append(rc.toString()).append("\r\n");
        }
        log.trace("Locations for Scans:\n" + sb);
    }
}
Also used : ResourceLocation(org.nutz.resource.impl.ResourceLocation) ErrorResourceLocation(org.nutz.resource.impl.ErrorResourceLocation)

Aggregations

ErrorResourceLocation (org.nutz.resource.impl.ErrorResourceLocation)3 ResourceLocation (org.nutz.resource.impl.ResourceLocation)3 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Pattern (java.util.regex.Pattern)1 ZipInputStream (java.util.zip.ZipInputStream)1 FileResource (org.nutz.resource.impl.FileResource)1 SimpleResource (org.nutz.resource.impl.SimpleResource)1