Skip to content

Commit

Permalink
fixed #1173
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Nov 19, 2016
1 parent fdda2ef commit aa763fe
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 92 deletions.
12 changes: 12 additions & 0 deletions src/org/nutz/resource/Scans.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.nutz.resource.impl.ErrorResourceLocation;
import org.nutz.resource.impl.FileResource;
import org.nutz.resource.impl.ResourceLocation;
import org.nutz.resource.impl.SimpleResource;

/**
* 资源扫描的帮助函数集
Expand Down Expand Up @@ -242,6 +243,17 @@ public List<NutResource> scan(String src, String regex) {
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) {
}
}
}
ArrayList<NutResource> tmp = new ArrayList<NutResource>();
for (NutResource r : list) {
Expand Down
40 changes: 40 additions & 0 deletions src/org/nutz/resource/impl/FileSystemResourceLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.nutz.resource.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.nutz.NutRuntimeException;
import org.nutz.lang.util.Disks;
import org.nutz.resource.NutResource;
import org.nutz.resource.Scans;

public class FileSystemResourceLocation extends ResourceLocation {

public String id() {
return root.getAbsolutePath();
}

public void scan(final String base, final Pattern pattern, final List<NutResource> list) {
final File baseFile = new File(root.getAbsolutePath()+"/"+base);
if (baseFile.isFile()) {
list.add(new FileResource(baseFile));
return;
}

Disks.visitFile(baseFile, new Scans.ResourceFileVisitor(list, base), new Scans.ResourceFileFilter(pattern));
}

public String toString() {
return "Dir[path=" + root + "]";
}

private File root;

public FileSystemResourceLocation(File root) throws IOException {
if (root == null)
throw new NutRuntimeException("FileSystemResourceLocation root can't be NULL");
this.root = root.getAbsoluteFile().getCanonicalFile();
}
}
67 changes: 67 additions & 0 deletions src/org/nutz/resource/impl/JarResourceLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.nutz.resource.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.nutz.lang.Lang;
import org.nutz.lang.Streams;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.resource.NutResource;
import org.nutz.resource.Scans;

public class JarResourceLocation extends ResourceLocation {

public String id() {
return jarPath;
}

public void scan(String base, Pattern regex, List<NutResource> list) {
for (String ensName : names) {
String name = ensName;
if (name.contains("/"))
name = name.substring(name.lastIndexOf('/') + 1);
if (ensName.startsWith(base) && (null == regex || regex.matcher(name).find())) {
try {
list.add(Scans.makeJarNutResource(jarPath, ensName, base));
} catch (IOException e) {
if (log.isInfoEnabled())
log.info("Jar delete while scan?!! " + jarPath,e);
}
}
}
}

public String toString() {
return "Jar[path=" + jarPath + "]";
}

private static final Log log = Logs.get();

private List<String> names = new ArrayList<String>();

private String jarPath;

public JarResourceLocation(String jarPath) throws IOException {
this.jarPath = ResourceLocation.getJarPath(jarPath);
ZipInputStream zis = null;
try {
zis = Scans.makeZipInputStream(jarPath);
ZipEntry ens = null;
while (null != (ens = zis.getNextEntry())) {
if (ens.isDirectory())
continue;
names.add(ens.getName());
}
}
catch (Throwable e) {
throw Lang.wrapThrow(e);
} finally {
Streams.safeClose(zis);
}
}
}
93 changes: 1 addition & 92 deletions src/org/nutz/resource/impl/ResourceLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.nutz.NutRuntimeException;
import org.nutz.lang.Lang;
import org.nutz.lang.Streams;
import org.nutz.lang.util.Disks;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.resource.NutResource;
import org.nutz.resource.Scans;

public abstract class ResourceLocation {

Expand Down Expand Up @@ -73,85 +63,4 @@ public boolean equals(Object obj) {
public int hashCode() {
return id().hashCode();
}
}

class FileSystemResourceLocation extends ResourceLocation {

public String id() {
return root.getAbsolutePath();
}

public void scan(final String base, final Pattern pattern, final List<NutResource> list) {
final File baseFile = new File(root.getAbsolutePath()+"/"+base);
if (baseFile.isFile()) {
list.add(new FileResource(baseFile));
return;
}

Disks.visitFile(baseFile, new Scans.ResourceFileVisitor(list, base), new Scans.ResourceFileFilter(pattern));
}

public String toString() {
return "Dir[path=" + root + "]";
}

private File root;

public FileSystemResourceLocation(File root) throws IOException {
if (root == null)
throw new NutRuntimeException("FileSystemResourceLocation root can't be NULL");
this.root = root.getAbsoluteFile().getCanonicalFile();
}
}

class JarResourceLocation extends ResourceLocation {

public String id() {
return jarPath;
}

public void scan(String base, Pattern regex, List<NutResource> list) {
for (String ensName : names) {
String name = ensName;
if (name.contains("/"))
name = name.substring(name.lastIndexOf('/') + 1);
if (ensName.startsWith(base) && (null == regex || regex.matcher(name).find())) {
try {
list.add(Scans.makeJarNutResource(jarPath, ensName, base));
} catch (IOException e) {
if (log.isInfoEnabled())
log.info("Jar delete while scan?!! " + jarPath,e);
}
}
}
}

public String toString() {
return "Jar[path=" + jarPath + "]";
}

private static final Log log = Logs.get();

private List<String> names = new ArrayList<String>();

private String jarPath;

public JarResourceLocation(String jarPath) throws IOException {
this.jarPath = ResourceLocation.getJarPath(jarPath);
ZipInputStream zis = null;
try {
zis = Scans.makeZipInputStream(jarPath);
ZipEntry ens = null;
while (null != (ens = zis.getNextEntry())) {
if (ens.isDirectory())
continue;
names.add(ens.getName());
}
}
catch (Throwable e) {
throw Lang.wrapThrow(e);
} finally {
Streams.safeClose(zis);
}
}
}
}
29 changes: 29 additions & 0 deletions src/org/nutz/resource/impl/SimpleResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.nutz.resource.impl;

import java.io.IOException;
import java.io.InputStream;

import org.nutz.resource.NutResource;

public class SimpleResource extends NutResource {

protected InputStream ins;

public SimpleResource() {}

public SimpleResource(String name, String source, InputStream ins) {
super();
this.ins = ins;
this.name = name;
this.source = source;
}


public InputStream getInputStream() throws IOException {
return ins;
}

public void setInputStream(InputStream ins) {
this.ins = ins;
}
}

0 comments on commit aa763fe

Please sign in to comment.