📄 shadingpathresolver.java
字号:
}
boolean removed = metaData.remove("uid:" + uid) != null; //$NON-NLS-1$
removed |= metaData.remove("source:" + uid) != null; //$NON-NLS-1$
removed |= metaData.remove("file:" + uid) != null; //$NON-NLS-1$
removed |= metaData.remove("modified:" + uid) != null; //$NON-NLS-1$
if (removed && log.isDebugEnabled()) {
log.debug("removed meta-data, UID=" + uid); //$NON-NLS-1$
}
}
private URL add(final String uid, final URL sourceUrl, final File file,
final Date modified) throws IOException {
URL result = IoUtil.file2url(file);
metaData.setProperty("uid:" + uid, uid); //$NON-NLS-1$
String source = ShadingUtil.getRelativeUrl(shadowFolder, sourceUrl);
metaData.setProperty("source:" + uid, source); //$NON-NLS-1$
metaData.setProperty("file:" + uid, file.getName()); //$NON-NLS-1$
metaData.setProperty("modified:" + uid, dtf.format(modified)); //$NON-NLS-1$
save();
if (log.isDebugEnabled()) {
log.debug("shading done, UID=" + uid + ", source=" //$NON-NLS-1$ //$NON-NLS-2$
+ source + ", file=" + result //$NON-NLS-1$
+ ", modified=" + dtf.format(modified)); //$NON-NLS-1$
}
return result;
}
URL shadowResource(final URL source, final String uid,
final boolean unpack) {
try {
URL result = deepCheck(source, uid);
if (result != null) {
if (log.isDebugEnabled()) {
log.debug("got actual shaded resource, UID=" + uid //$NON-NLS-1$
+ ", source=" + source //$NON-NLS-1$
+ ", file=" + result); //$NON-NLS-1$
}
return result;
}
} catch (Exception e) {
log.warn("deep check failed, UID=" + uid //$NON-NLS-1$
+ ", URL=" + source, e); //$NON-NLS-1$
remove(uid);
}
Date lastModified;
try {
lastModified = ShadingUtil.getLastModified(source);
} catch (IOException ioe) {
log.error("shading failed, can't get modification date for " //$NON-NLS-1$
+ source, ioe);
return source;
}
File file = IoUtil.url2file(source);
if ((file != null) && file.isDirectory()) {
// copy local folder to the shadow directory
try {
File rootFolder = new File(shadowFolder, uid);
IoUtil.copyFolder(file, rootFolder, true, true, fileFilter);
return add(uid, source, rootFolder, lastModified);
} catch (IOException ioe) {
log.error("failed shading local folder " + file, ioe); //$NON-NLS-1$
return source;
}
}
try {
if ("jar".equalsIgnoreCase(source.getProtocol())) { //$NON-NLS-1$
String urlStr = source.toExternalForm();
int p = urlStr.indexOf("!/"); //$NON-NLS-1$
if (p == -1) {
p = urlStr.length();
}
URL jarFileURL = new URL(urlStr.substring(4, p));
if (!unpack) {
String ext = ShadingUtil.getExtension(jarFileURL.getFile());
if (ext == null) {
ext = "jar"; //$NON-NLS-1$
}
File shadowFile = new File(shadowFolder, uid + '.' + ext);
File sourceFile = IoUtil.url2file(jarFileURL);
InputStream in;
if (sourceFile != null) {
in = new BufferedInputStream(
new FileInputStream(sourceFile));
} else {
in = jarFileURL.openStream();
}
try {
OutputStream out =
new FileOutputStream(shadowFile, false);
try {
IoUtil.copyStream(in, out, 1024);
} finally {
out.close();
}
} finally {
in.close();
}
return add(uid, source, shadowFile, lastModified);
}
URLConnection cnn = null;
try {
File sourceFile = IoUtil.url2file(jarFileURL);
ZipFile zipFile;
if (sourceFile != null) {
zipFile = new ZipFile(sourceFile);
} else {
cnn = source.openConnection();
cnn.setUseCaches(false);
zipFile = ((JarURLConnection) cnn).getJarFile();
}
File rootFolder = new File(shadowFolder, uid);
try {
ShadingUtil.unpack(zipFile, rootFolder);
} finally {
zipFile.close();
}
return add(uid, source, rootFolder, lastModified);
} finally {
if (cnn != null) {
cnn.getInputStream().close();
}
}
}
} catch (IOException ioe) {
log.error("failed shading URL connection " + source, ioe); //$NON-NLS-1$
return source;
}
String fileName = source.getFile();
if (fileName == null) {
log.warn("can't get file name from resource " + source //$NON-NLS-1$
+ ", shading failed"); //$NON-NLS-1$
return source;
}
String ext = ShadingUtil.getExtension(fileName);
if (ext == null) {
log.warn("can't get file name extension for resource " + source //$NON-NLS-1$
+ ", shading failed"); //$NON-NLS-1$
return source;
}
if (unpack && ("jar".equalsIgnoreCase(ext) //$NON-NLS-1$
|| "zip".equalsIgnoreCase(ext))) { //$NON-NLS-1$
try {
InputStream strm = source.openStream();
File rootFolder = new File(shadowFolder, uid);
try {
ShadingUtil.unpack(strm, rootFolder);
} finally {
strm.close();
}
return add(uid, source, rootFolder, lastModified);
} catch (IOException ioe) {
log.error("failed shading packed resource " + source, ioe); //$NON-NLS-1$
return source;
}
}
try {
File shadowFile = new File(shadowFolder, uid + '.' + ext);
InputStream in = source.openStream();
try {
OutputStream out = new FileOutputStream(shadowFile, false);
try {
IoUtil.copyStream(in, out, 1024);
} finally {
out.close();
}
} finally {
in.close();
}
return add(uid, source, shadowFile, lastModified);
} catch (IOException ioe) {
log.error("failed shading resource file " + source, ioe); //$NON-NLS-1$
return source;
}
}
private URL deepCheck(final URL source, final String uid) throws Exception {
String url = metaData.getProperty("source:" + uid, null); //$NON-NLS-1$
if (url == null) {
if (log.isDebugEnabled()) {
log.debug("URL not found in meta-data, UID=" + uid); //$NON-NLS-1$
}
remove(uid);
return null;
}
if (log.isDebugEnabled()) {
log.debug("URL found in meta-data, UID=" //$NON-NLS-1$
+ uid + ", source=" + source //$NON-NLS-1$
+ ", storedURL=" + url); //$NON-NLS-1$
}
URL storedSource = ShadingUtil.buildURL(shadowFolderUrl, url);
if (!storedSource.equals(source)) {
if (log.isDebugEnabled()) {
log.debug("inconsistent URL found in meta-data, UID=" //$NON-NLS-1$
+ uid + ", source=" + source //$NON-NLS-1$
+ ", storedSource=" + storedSource); //$NON-NLS-1$
}
remove(uid);
return null;
}
String modified = metaData.getProperty("modified:" + uid, null); //$NON-NLS-1$
if (modified == null) {
if (log.isDebugEnabled()) {
log.debug("modification info not found in meta-data, UID=" //$NON-NLS-1$
+ uid);
}
remove(uid);
return null;
}
if (!ShadingUtil.getLastModified(source).equals(dtf.parse(modified))) {
if (log.isDebugEnabled()) {
log.debug("source modification detected, UID=" + uid //$NON-NLS-1$
+ ", source=" + source); //$NON-NLS-1$
}
remove(uid);
return null;
}
String fileStr = metaData.getProperty("file:" + uid, null); //$NON-NLS-1$
if (fileStr == null) {
if (log.isDebugEnabled()) {
log.debug("file info not found in meta-data, UID=" + uid); //$NON-NLS-1$
}
remove(uid);
return null;
}
File file = new File(shadowFolder, fileStr);
if (!file.exists()) {
if (log.isDebugEnabled()) {
log.debug("shadow file not found, UID=" + uid //$NON-NLS-1$
+ ", source=" + source //$NON-NLS-1$
+ ", file=" + file); //$NON-NLS-1$
}
remove(uid);
return null;
}
File sourceFile = IoUtil.url2file(source);
if ((sourceFile != null) && sourceFile.isDirectory()) {
IoUtil.synchronizeFolders(sourceFile, file, fileFilter);
if (log.isDebugEnabled()) {
log.debug("folders synchronized, UID=" + uid //$NON-NLS-1$
+ ", srcFile=" + sourceFile //$NON-NLS-1$
+ ", destFile=" + file); //$NON-NLS-1$
}
} else {
if (log.isDebugEnabled()) {
log.debug("source " + source + " (file is " + sourceFile //$NON-NLS-1$ //$NON-NLS-2$
+ ") is not local folder, " //$NON-NLS-1$
+ "skipping synchronization, UID=" + uid); //$NON-NLS-1$
}
}
return IoUtil.file2url(file);
}
static class ShadowFileFilter implements FileFilter {
/**
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(final File file) {
return !META_FILE_NAME.equals(file.getName());
}
}
}
final class RegexpFileFilter implements FileFilter {
private final Pattern[] patterns;
RegexpFileFilter(final String str) {
StringTokenizer st = new StringTokenizer(str, "|", false); //$NON-NLS-1$
patterns = new Pattern[st.countTokens()];
for (int i = 0; i < patterns.length; i++) {
String pattern = st.nextToken();
if ((pattern == null) || (pattern.trim().length() == 0)) {
continue;
}
patterns[i] = Pattern.compile(pattern.trim());
}
}
/**
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(final File file) {
for (int i = 0; i < patterns.length; i++) {
if (patterns[i] == null) {
continue;
}
if (patterns[i].matcher(file.getName()).matches()) {
return true;
}
}
return false;
}
}
final class CombinedFileFilter implements FileFilter {
private final FileFilter includesFilter;
private final FileFilter excludesFilter;
CombinedFileFilter(final FileFilter includes, final FileFilter excludes) {
includesFilter = includes;
excludesFilter = excludes;
}
/**
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(final File file) {
if (includesFilter != null) {
if (includesFilter.accept(file)) {
return true;
}
}
if ((excludesFilter != null) && excludesFilter.accept(file)) {
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -