From 580fc34119092800ea5eb11552b340b4e5990120 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Fri, 19 Jul 2024 14:49:12 +0200 Subject: [PATCH] FileResource with file logging --- .../io/res/type/file/FileResource.java | 1060 +++++++++++------ 1 file changed, 694 insertions(+), 366 deletions(-) diff --git a/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java b/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java index 425d70893d..cc3cddb7f6 100644 --- a/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java +++ b/core/src/main/java/lucee/commons/io/res/type/file/FileResource.java @@ -1,21 +1,3 @@ -/** - * Copyright (c) 2014, the Railo Company Ltd. - * Copyright (c) 2015, Lucee Association Switzerland - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - * - */ package lucee.commons.io.res.type.file; import java.io.BufferedInputStream; @@ -35,6 +17,9 @@ import java.nio.file.attribute.DosFileAttributes; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; import lucee.commons.cli.Command; import lucee.commons.io.IOUtil; @@ -48,9 +33,16 @@ import lucee.commons.io.res.util.ResourceOutputStream; import lucee.commons.io.res.util.ResourceUtil; import lucee.commons.lang.ExceptionUtil; +import lucee.commons.lang.Pair; +import lucee.commons.lang.types.RefDouble; +import lucee.commons.lang.types.RefDoubleImpl; +import lucee.commons.lang.types.RefInteger; +import lucee.commons.lang.types.RefIntegerImpl; +import lucee.runtime.type.Query; +import lucee.runtime.type.QueryImpl; /** - * Implementation og Resource for the local filesystem (java.io.File) + * Implementation of Resource for the local filesystem (java.io.File) */ public final class FileResource extends File implements Resource { @@ -59,6 +51,8 @@ public final class FileResource extends File implements Resource { private final FileResourceProvider provider; + private static Map> logs = new ConcurrentHashMap<>(); + /** * Constructor for the factory * @@ -67,10 +61,11 @@ public final class FileResource extends File implements Resource { FileResource(FileResourceProvider provider, String pathname) { super(pathname); this.provider = provider; + log(getAbsolutePath() + ":FileResource(String pathname)", SystemUtil.millis()); } /** - * Inner Constr constructor to create parent/child + * Inner constructor to create parent/child * * @param parent * @param child @@ -78,418 +73,659 @@ public final class FileResource extends File implements Resource { private FileResource(FileResourceProvider provider, File parent, String child) { super(parent, child); this.provider = provider; + log(getAbsolutePath() + ":FileResource(File parent, String child)", SystemUtil.millis()); } - @Override - public void copyFrom(Resource res, boolean append) throws IOException { - - if (res instanceof File && (!append || !this.isFile())) { - try { - Files.copy(((File) res).toPath(), this.toPath(), COPY_OPTIONS); - return; - } - catch (Exception exception) { + private static void log(String msg, double start) { + double end = SystemUtil.millis(); + String key = msg + "|" + ExceptionUtil.getStacktrace(new Throwable(), false); + Pair pair = logs.get(key); + if (pair == null) { + synchronized (SystemUtil.createToken("file", key)) { + pair = logs.get(key); + if (pair == null) { + logs.put(key, new Pair(new RefDoubleImpl(end - start), new RefIntegerImpl(1))); + return; + } } } + pair.getName().plus(end - start); + pair.getValue().plus(1); + } - IOUtil.copy(res, this.getOutputStream(append), true); + public static Query getLogs() { + Query q = new QueryImpl(new String[] { "path", "method", "stacktrace", "time", "count" }, 0, "logs"); - // executable? - boolean e = res instanceof File && ((File) res).canExecute(); - boolean w = res.canWrite(); - boolean r = res.canRead(); + for (Entry> e: logs.entrySet()) { + int row = q.addRow(); - if (e) this.setExecutable(true); - if (w != this.canWrite()) this.setWritable(w); - if (r != this.canRead()) this.setReadable(r); + String val = e.getKey(); + int index = val.indexOf(':'); + q.setAtEL("path", row, val.substring(0, index)); + + val = val.substring(index + 1); + index = val.indexOf('|'); + q.setAtEL("method", row, val.substring(0, index)); + q.setAtEL("stacktrace", row, val.substring(index + 1)); + + q.setAtEL("time", row, e.getValue().getName().toDouble()); + q.setAtEL("count", row, e.getValue().getValue().toDouble()); + } + return q; } @Override - public void copyTo(Resource res, boolean append) throws IOException { - - if (res instanceof File && (!append || !res.isFile())) { - try { - Files.copy(this.toPath(), ((File) res).toPath(), COPY_OPTIONS); - return; - } - catch (Exception exception) { + public void copyFrom(Resource res, boolean append) throws IOException { + double start = SystemUtil.millis(); + try { + if (res instanceof File && (!append || !this.isFile())) { + try { + Files.copy(((File) res).toPath(), this.toPath(), COPY_OPTIONS); + return; + } + catch (Exception exception) { + } } + + IOUtil.copy(res, this.getOutputStream(append), true); + + // executable? + boolean e = res instanceof File && ((File) res).canExecute(); + boolean w = res.canWrite(); + boolean r = res.canRead(); + + if (e) this.setExecutable(true); + if (w != this.canWrite()) this.setWritable(w); + if (r != this.canRead()) this.setReadable(r); } + finally { + log(getAbsolutePath() + ":copyFrom(Resource res, boolean append)", start); + } + } - IOUtil.copy(this, res.getOutputStream(append), true); - boolean e = canExecute(); - boolean w = canWrite(); - boolean r = canRead(); + @Override + public void copyTo(Resource res, boolean append) throws IOException { + double start = SystemUtil.millis(); + try { + if (res instanceof File && (!append || !res.isFile())) { + try { + Files.copy(this.toPath(), ((File) res).toPath(), COPY_OPTIONS); + return; + } + catch (Exception exception) { + } + } - if (e && res instanceof File) ((File) res).setExecutable(true); - if (w != res.canWrite()) res.setWritable(w); - if (r != res.canRead()) res.setReadable(r); + IOUtil.copy(this, res.getOutputStream(append), true); + boolean e = canExecute(); + boolean w = canWrite(); + boolean r = canRead(); + if (e && res instanceof File) ((File) res).setExecutable(true); + if (w != res.canWrite()) res.setWritable(w); + if (r != res.canRead()) res.setReadable(r); + } + finally { + log(getAbsolutePath() + ":copyTo(Resource res, boolean append)", start); + } } @Override public Resource getAbsoluteResource() { - return new FileResource(provider, getAbsolutePath()); + double start = SystemUtil.millis(); + try { + return new FileResource(provider, getAbsolutePath()); + } + finally { + log(getAbsolutePath() + ":getAbsoluteResource()", start); + } } @Override public Resource getCanonicalResource() throws IOException { - return new FileResource(provider, getCanonicalPath()); + double start = SystemUtil.millis(); + try { + return new FileResource(provider, getCanonicalPath()); + } + finally { + log(getAbsolutePath() + ":getCanonicalResource()", start); + } } @Override public Resource getParentResource() { - String p = getParent(); - if (p == null) return null; - return new FileResource(provider, p); + double start = SystemUtil.millis(); + try { + String p = getParent(); + if (p == null) return null; + return new FileResource(provider, p); + } + finally { + log(getAbsolutePath() + ":getParentResource()", start); + } } @Override public Resource[] listResources() { - String[] files = list(); - if (files == null) return null; + double start = SystemUtil.millis(); + try { + String[] files = list(); + if (files == null) return null; - Resource[] resources = new Resource[files.length]; - for (int i = 0; i < files.length; i++) { - resources[i] = getRealResource(files[i]); + Resource[] resources = new Resource[files.length]; + for (int i = 0; i < files.length; i++) { + resources[i] = getRealResource(files[i]); + } + return resources; + } + finally { + log(getAbsolutePath() + ":listResources()", start); } - return resources; } @Override public String[] list(ResourceFilter filter) { - String[] files = list(); - if (files == null) return null; - - List list = new ArrayList(); - FileResource res; - for (int i = 0; i < files.length; i++) { - res = new FileResource(provider, this, files[i]); - if (filter.accept(res)) list.add(files[i]); + double start = SystemUtil.millis(); + try { + String[] files = list(); + if (files == null) return null; + + List list = new ArrayList(); + FileResource res; + for (int i = 0; i < files.length; i++) { + res = new FileResource(provider, this, files[i]); + if (filter.accept(res)) list.add(files[i]); + } + return list.toArray(new String[list.size()]); + } + finally { + log(getAbsolutePath() + ":list(ResourceFilter filter)", start); } - return list.toArray(new String[list.size()]); } @Override public Resource[] listResources(ResourceFilter filter) { - String[] files = list(); - if (files == null) return null; - - List list = new ArrayList(); - Resource res; - for (int i = 0; i < files.length; i++) { - res = getRealResource(files[i]); - if (filter.accept(res)) list.add(res); + double start = SystemUtil.millis(); + try { + String[] files = list(); + if (files == null) return null; + + List list = new ArrayList(); + Resource res; + for (int i = 0; i < files.length; i++) { + res = getRealResource(files[i]); + if (filter.accept(res)) list.add(res); + } + return list.toArray(new FileResource[list.size()]); + } + finally { + log(getAbsolutePath() + ":listResources(ResourceFilter filter)", start); } - return list.toArray(new FileResource[list.size()]); } @Override public String[] list(ResourceNameFilter filter) { - String[] files = list(); - if (files == null) return null; - List list = new ArrayList(); - for (int i = 0; i < files.length; i++) { - if (filter.accept(this, files[i])) list.add(files[i]); + double start = SystemUtil.millis(); + try { + String[] files = list(); + if (files == null) return null; + List list = new ArrayList(); + for (int i = 0; i < files.length; i++) { + if (filter.accept(this, files[i])) list.add(files[i]); + } + return list.toArray(new String[list.size()]); + } + finally { + log(getAbsolutePath() + ":list(ResourceNameFilter filter)", start); } - return list.toArray(new String[list.size()]); } @Override public Resource[] listResources(ResourceNameFilter filter) { - String[] files = list(); - if (files == null) return null; + double start = SystemUtil.millis(); + try { + String[] files = list(); + if (files == null) return null; - List list = new ArrayList(); - for (int i = 0; i < files.length; i++) { - if (filter.accept(this, files[i])) list.add(getRealResource(files[i])); + List list = new ArrayList(); + for (int i = 0; i < files.length; i++) { + if (filter.accept(this, files[i])) list.add(getRealResource(files[i])); + } + return list.toArray(new Resource[list.size()]); + } + finally { + log(getAbsolutePath() + ":listResources(ResourceNameFilter filter)", start); } - return list.toArray(new Resource[list.size()]); } @Override public void moveTo(Resource dest) throws IOException { - if (this.equals(dest)) return; - boolean done = false; - if (dest instanceof File) { - provider.lock(this); - try { - if (dest.exists() && !dest.delete()) - throw new IOException("Can't move file [" + this.getAbsolutePath() + "] cannot remove existing file [" + dest.getAbsolutePath() + "]"); + double start = SystemUtil.millis(); + try { + if (this.equals(dest)) return; + boolean done = false; + if (dest instanceof File) { + provider.lock(this); + try { + if (dest.exists() && !dest.delete()) + throw new IOException("Can't move file [" + this.getAbsolutePath() + "] cannot remove existing file [" + dest.getAbsolutePath() + "]"); - done = super.renameTo((File) dest); - /* - * if(!super.renameTo((File)dest)) { throw new - * IOException("can't move file "+this.getAbsolutePath()+" to destination resource "+dest. - * getAbsolutePath()); } - */ + done = super.renameTo((File) dest); + } + finally { + provider.unlock(this); + } } - finally { - provider.unlock(this); + if (!done) { + ResourceUtil.checkMoveToOK(this, dest); + IOUtil.copy(getInputStream(), dest, true); + if (!this.delete()) { + throw new IOException("Can't delete resource [" + this.getAbsolutePath() + "]"); + } } } - if (!done) { - ResourceUtil.checkMoveToOK(this, dest); - IOUtil.copy(getInputStream(), dest, true); - if (!this.delete()) { - throw new IOException("Can't delete resource [" + this.getAbsolutePath() + "]"); - } + finally { + log(getAbsolutePath() + ":moveTo(Resource dest)", start); } } @Override public InputStream getInputStream() throws IOException { - // provider.lock(this); - provider.read(this); + double start = SystemUtil.millis(); try { - return new BufferedInputStream(Files.newInputStream(toPath(), StandardOpenOption.READ)); - // return new BufferedInputStream(new FileInputStream(this)); - } - catch (NoSuchFileException nsfe) { - throw ExceptionUtil.toFileNotFoundException(nsfe); + provider.read(this); + try { + return new BufferedInputStream(Files.newInputStream(toPath(), StandardOpenOption.READ)); + } + catch (NoSuchFileException nsfe) { + throw ExceptionUtil.toFileNotFoundException(nsfe); + } + catch (IOException ioe) { + throw ioe; + } } - catch (IOException ioe) { - // provider.unlock(this); - throw ioe; + finally { + log(getAbsolutePath() + ":getInputStream()", start); } } @Override public OutputStream getOutputStream() throws IOException { - return getOutputStream(false); + double start = SystemUtil.millis(); + try { + return getOutputStream(false); + } + finally { + log(getAbsolutePath() + ":getOutputStream()", start); + } } @Override public OutputStream getOutputStream(boolean append) throws IOException { - provider.lock(this); + double start = SystemUtil.millis(); try { - if (!super.exists() && !super.createNewFile()) { - // maybe createNewFile failed, because it got generated by another process - try { - Files.createFile(toPath()); - } - // ignore this - catch (FileAlreadyExistsException faee) { + provider.lock(this); + try { + if (!super.exists() && !super.createNewFile()) { + try { + Files.createFile(toPath()); + } + catch (FileAlreadyExistsException faee) { + } } + return new BufferedOutputStream(new ResourceOutputStream(this, new FileOutputStream(this, append))); + } + catch (IOException ioe) { + provider.unlock(this); + throw ioe; } - return new BufferedOutputStream(new ResourceOutputStream(this, new FileOutputStream(this, append))); } - catch (IOException ioe) { - provider.unlock(this); - throw ioe; + finally { + log(getAbsolutePath() + ":getOutputStream(boolean append)", start); } } @Override public void createFile(boolean createParentWhenNotExists) throws IOException { - provider.lock(this); + double start = SystemUtil.millis(); try { - if (createParentWhenNotExists) { - File p = super.getParentFile(); - if (!p.exists()) Files.createDirectories(p.toPath()); + provider.lock(this); + try { + if (createParentWhenNotExists) { + File p = super.getParentFile(); + if (!p.exists()) Files.createDirectories(p.toPath()); + } + Files.createFile(toPath()); + } + finally { + provider.unlock(this); } - Files.createFile(toPath()); } finally { - provider.unlock(this); + log(getAbsolutePath() + ":createFile(boolean createParentWhenNotExists)", start); } } @Override public void remove(boolean alsoRemoveChildren) throws IOException { - if (alsoRemoveChildren && isDirectory()) { - Resource[] children = listResources(); - if (children != null) { - for (int i = 0; i < children.length; i++) { - children[i].remove(alsoRemoveChildren); + double start = SystemUtil.millis(); + try { + if (alsoRemoveChildren && isDirectory()) { + Resource[] children = listResources(); + if (children != null) { + for (int i = 0; i < children.length; i++) { + children[i].remove(alsoRemoveChildren); + } } } - } - provider.lock(this); - try { - if (!super.delete()) { - if (!super.exists()) throw new IOException("Can't delete file [" + this + "], file does not exist"); - if (!super.canWrite()) throw new IOException("Can't delete file [" + this + "], no access"); - throw new IOException("Can't delete file [" + this + "]"); + provider.lock(this); + try { + if (!super.delete()) { + if (!super.exists()) throw new IOException("Can't delete file [" + this + "], file does not exist"); + if (!super.canWrite()) throw new IOException("Can't delete file [" + this + "], no access"); + throw new IOException("Can't delete file [" + this + "]"); + } + } + finally { + provider.unlock(this); } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":remove(boolean alsoRemoveChildren)", start); } } @Override public String getReal(String realpath) { - if (realpath.length() <= 2) { - if (realpath.length() == 0) return getPath(); - if (realpath.equals(".")) return getPath(); - if (realpath.equals("..")) return getParent(); + double start = SystemUtil.millis(); + try { + if (realpath.length() <= 2) { + if (realpath.length() == 0) return getPath(); + if (realpath.equals(".")) return getPath(); + if (realpath.equals("..")) return getParent(); + } + return new FileResource(provider, this, realpath).getPath(); + } + finally { + log(getAbsolutePath() + ":getReal(String realpath)", start); } - return new FileResource(provider, this, realpath).getPath(); } @Override public Resource getRealResource(String realpath) { - if (realpath.length() <= 2) { - if (realpath.length() == 0) return this; - if (realpath.equals(".")) return this; - if (realpath.equals("..")) return getParentResource(); + double start = SystemUtil.millis(); + try { + if (realpath.length() <= 2) { + if (realpath.length() == 0) return this; + if (realpath.equals(".")) return this; + if (realpath.equals("..")) return getParentResource(); + } + return new FileResource(provider, this, realpath); + } + finally { + log(getAbsolutePath() + ":getRealResource(String realpath)", start); } - return new FileResource(provider, this, realpath); } public ContentType getContentType() { - return ResourceUtil.getContentType(this); + double start = SystemUtil.millis(); + try { + return ResourceUtil.getContentType(this); + } + finally { + log(getAbsolutePath() + ":getContentType()", start); + } } @Override public void createDirectory(boolean createParentWhenNotExists) throws IOException { - provider.lock(this); + double start = SystemUtil.millis(); try { - if (createParentWhenNotExists) Files.createDirectories(toPath()); - else Files.createDirectory(toPath()); + provider.lock(this); + try { + if (createParentWhenNotExists) Files.createDirectories(toPath()); + else Files.createDirectory(toPath()); + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":createDirectory(boolean createParentWhenNotExists)", start); } } @Override public ResourceProvider getResourceProvider() { - return provider; + double start = SystemUtil.millis(); + try { + return provider; + } + finally { + log(getAbsolutePath() + ":getResourceProvider()", start); + } } @Override public boolean isReadable() { - return canRead(); + double start = SystemUtil.millis(); + try { + return canRead(); + } + finally { + log(getAbsolutePath() + ":isReadable()", start); + } } @Override public boolean isWriteable() { - return canWrite(); + double start = SystemUtil.millis(); + try { + return canWrite(); + } + finally { + log(getAbsolutePath() + ":isWriteable()", start); + } } @Override public boolean renameTo(Resource dest) { + double start = SystemUtil.millis(); try { - moveTo(dest); - return true; + try { + moveTo(dest); + return true; + } + catch (IOException e) { + } + return false; } - catch (IOException e) { + finally { + log(getAbsolutePath() + ":renameTo(Resource dest)", start); } - return false; } @Override public boolean isArchive() { - return getAttribute(ATTRIBUTE_ARCHIVE); + double start = SystemUtil.millis(); + try { + return getAttribute(ATTRIBUTE_ARCHIVE); + } + finally { + log(getAbsolutePath() + ":isArchive()", start); + } } @Override public boolean isSystem() { - return getAttribute(ATTRIBUTE_SYSTEM); + double start = SystemUtil.millis(); + try { + return getAttribute(ATTRIBUTE_SYSTEM); + } + finally { + log(getAbsolutePath() + ":isSystem()", start); + } } @Override public int getMode() { - if (!exists()) return 0; - if (SystemUtil.isUnix()) { - try { - // TODO geht nur fuer file - String line = Command.execute("ls -ld " + getPath(), false).getOutput(); + double start = SystemUtil.millis(); + try { + if (!exists()) return 0; + if (SystemUtil.isUnix()) { + try { + String line = Command.execute("ls -ld " + getPath(), false).getOutput(); - line = line.trim(); - line = line.substring(0, line.indexOf(' ')); - // print.ln(getPath()); - return ModeUtil.toOctalMode(line); + line = line.trim(); + line = line.substring(0, line.indexOf(' ')); + return ModeUtil.toOctalMode(line); - } - catch (Exception e) { - } + } + catch (Exception e) { + } + } + int mode = SystemUtil.isWindows() && exists() ? 0111 : 0; + if (super.canRead()) mode += 0444; + if (super.canWrite()) mode += 0222; + return mode; + } + finally { + log(getAbsolutePath() + ":getMode()", start); } - int mode = SystemUtil.isWindows() && exists() ? 0111 : 0; - if (super.canRead()) mode += 0444; - if (super.canWrite()) mode += 0222; - return mode; } public static int getMode(Path path) { - if (!Files.exists(path)) return 0; - if (SystemUtil.isUnix()) { - try { - // TODO geht nur fuer file - String line = Command.execute("ls -ld " + path.toAbsolutePath().toString(), false).getOutput(); + double start = SystemUtil.millis(); + try { + if (!Files.exists(path)) return 0; + if (SystemUtil.isUnix()) { + try { + String line = Command.execute("ls -ld " + path.toAbsolutePath().toString(), false).getOutput(); - line = line.trim(); - line = line.substring(0, line.indexOf(' ')); - // print.ln(getPath()); - return ModeUtil.toOctalMode(line); + line = line.trim(); + line = line.substring(0, line.indexOf(' ')); + return ModeUtil.toOctalMode(line); - } - catch (Exception e) { - } + } + catch (Exception e) { + } + } + int mode = SystemUtil.isWindows() ? 0111 : 0; + if (Files.isReadable(path)) mode += 0444; + if (Files.isWritable(path)) mode += 0222; + return mode; + } + finally { + log(path.toAbsolutePath().toString() + ":getMode(Path path)", start); } - int mode = SystemUtil.isWindows() ? 0111 : 0; - if (Files.isReadable(path)) mode += 0444; - if (Files.isWritable(path)) mode += 0222; - return mode; } @Override public void setMode(int mode) throws IOException { - // TODO unter windows mit setReadable usw. - if (!SystemUtil.isUnix()) return; - provider.lock(this); + double start = SystemUtil.millis(); try { - // print.ln(ModeUtil.toStringMode(mode)); - if (Runtime.getRuntime().exec(new String[] { "chmod", ModeUtil.toStringMode(mode), getPath() }).waitFor() != 0) - throw new IOException("chmod [" + ModeUtil.toStringMode(mode) + "] [" + toString() + "] failed"); - } - catch (InterruptedException e) { - throw new IOException("Interrupted waiting for chmod [" + toString() + "]"); + if (!SystemUtil.isUnix()) return; + provider.lock(this); + try { + if (Runtime.getRuntime().exec(new String[] { "chmod", ModeUtil.toStringMode(mode), getPath() }).waitFor() != 0) + throw new IOException("chmod [" + ModeUtil.toStringMode(mode) + "] [" + toString() + "] failed"); + } + catch (InterruptedException e) { + throw new IOException("Interrupted waiting for chmod [" + toString() + "]"); + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":setMode(int mode)", start); } } @Override public void setArchive(boolean value) throws IOException { - setAttribute(ATTRIBUTE_ARCHIVE, value); + double start = SystemUtil.millis(); + try { + setAttribute(ATTRIBUTE_ARCHIVE, value); + } + finally { + log(getAbsolutePath() + ":setArchive(boolean value)", start); + } } @Override public void setHidden(boolean value) throws IOException { - setAttribute(ATTRIBUTE_HIDDEN, value); + double start = SystemUtil.millis(); + try { + setAttribute(ATTRIBUTE_HIDDEN, value); + } + finally { + log(getAbsolutePath() + ":setHidden(boolean value)", start); + } } @Override public void setSystem(boolean value) throws IOException { - setAttribute(ATTRIBUTE_SYSTEM, value); + double start = SystemUtil.millis(); + try { + setAttribute(ATTRIBUTE_SYSTEM, value); + } + finally { + log(getAbsolutePath() + ":setSystem(boolean value)", start); + } } @Override - public boolean setReadable(boolean value) { - if (!SystemUtil.isUnix()) return false; + double start = SystemUtil.millis(); try { - setMode(ModeUtil.setReadable(getMode(), value)); - return true; + if (!SystemUtil.isUnix()) return false; + try { + setMode(ModeUtil.setReadable(getMode(), value)); + return true; + } + catch (IOException e) { + return false; + } } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":setReadable(boolean value)", start); } } @Override public boolean setWritable(boolean value) { - // setReadonly - if (!value) { + double start = SystemUtil.millis(); + try { + if (!value) { + try { + provider.lock(this); + if (!super.setReadOnly()) throw new IOException("Can't set resource read-only"); + } + catch (IOException ioe) { + return false; + } + finally { + provider.unlock(this); + } + return true; + } + + if (SystemUtil.isUnix()) { + try { + setMode(ModeUtil.setWritable(getMode(), value)); + } + catch (IOException e) { + return false; + } + return true; + } + try { provider.lock(this); - if (!super.setReadOnly()) throw new IOException("Can't set resource read-only"); + Runtime.getRuntime().exec("attrib -R " + getAbsolutePath()); } catch (IOException ioe) { return false; @@ -499,195 +735,258 @@ public boolean setWritable(boolean value) { } return true; } - - if (SystemUtil.isUnix()) { - // need no lock because get/setmode has one - try { - setMode(ModeUtil.setWritable(getMode(), value)); - } - catch (IOException e) { - return false; - } - return true; - } - - try { - provider.lock(this); - Runtime.getRuntime().exec("attrib -R " + getAbsolutePath()); - } - catch (IOException ioe) { - return false; - } finally { - provider.unlock(this); + log(getAbsolutePath() + ":setWritable(boolean value)", start); } - return true; } @Override public boolean createNewFile() { + double start = SystemUtil.millis(); try { - provider.lock(this); - return super.createNewFile(); - } - catch (IOException e) { - return false; + try { + provider.lock(this); + return super.createNewFile(); + } + catch (IOException e) { + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":createNewFile()", start); } } @Override public boolean canRead() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.canRead(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":canRead()", start); } - return super.canRead(); } @Override public boolean canWrite() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.canWrite(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":canWrite()", start); } - return super.canWrite(); } @Override public boolean delete() { + double start = SystemUtil.millis(); try { - provider.lock(this); - return super.delete(); - } - catch (IOException e) { - return false; + try { + provider.lock(this); + return super.delete(); + } + catch (IOException e) { + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":delete()", start); } } @Override public boolean exists() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + } + return super.exists(); } - catch (IOException e) { + finally { + log(getAbsolutePath() + ":exists()", start); } - - return super.exists(); } @Override public boolean isAbsolute() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.isAbsolute(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":isAbsolute()", start); } - return super.isAbsolute(); } @Override public boolean isDirectory() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.isDirectory(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":isDirectory()", start); } - return super.isDirectory(); } @Override public boolean isFile() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.isFile(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":isFile()", start); } - return super.isFile(); } @Override public boolean isHidden() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return false; + } + return super.isHidden(); } - catch (IOException e) { - return false; + finally { + log(getAbsolutePath() + ":isHidden()", start); } - return super.isHidden(); } @Override public long lastModified() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return 0; + } + return super.lastModified(); } - catch (IOException e) { - return 0; + finally { + log(getAbsolutePath() + ":lastModified()", start); } - return super.lastModified(); } @Override public long length() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return 0; + } + return super.length(); } - catch (IOException e) { - return 0; + finally { + log(getAbsolutePath() + ":length()", start); } - return super.length(); } @Override public String[] list() { + double start = SystemUtil.millis(); try { - provider.read(this); + try { + provider.read(this); + } + catch (IOException e) { + return null; + } + return super.list(); } - catch (IOException e) { - return null; + finally { + log(getAbsolutePath() + ":list()", start); } - return super.list(); } @Override public boolean mkdir() { + double start = SystemUtil.millis(); try { - provider.lock(this); - return super.mkdir(); - } - catch (IOException e) { - return false; + try { + provider.lock(this); + return super.mkdir(); + } + catch (IOException e) { + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":mkdir()", start); } } @Override public boolean mkdirs() { + double start = SystemUtil.millis(); try { - provider.lock(this); - return _mkdirs(); + try { + provider.lock(this); + return _mkdirs(); - } - catch (IOException e) { - return false; + } + catch (IOException e) { + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":mkdirs()", start); } } @@ -701,90 +1000,119 @@ private boolean _mkdirs() { @Override public boolean setLastModified(long time) { + double start = SystemUtil.millis(); try { - provider.lock(this); - return super.setLastModified(time); - } - catch (Throwable t) {// IllegalArgumentException or IOException - ExceptionUtil.rethrowIfNecessary(t); - return false; + try { + provider.lock(this); + return super.setLastModified(time); + } + catch (Throwable t) { + ExceptionUtil.rethrowIfNecessary(t); + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":setLastModified(long time)", start); } - } @Override public boolean setReadOnly() { + double start = SystemUtil.millis(); try { - provider.lock(this); - return super.setReadOnly(); - } - catch (IOException e) { - return false; + try { + provider.lock(this); + return super.setReadOnly(); + } + catch (IOException e) { + return false; + } + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":setReadOnly()", start); } } @Override public boolean getAttribute(short attribute) { - if (!SystemUtil.isWindows()) return false; - + double start = SystemUtil.millis(); try { - provider.lock(this); - DosFileAttributes attr = Files.readAttributes(this.toPath(), DosFileAttributes.class); - if (attribute == ATTRIBUTE_ARCHIVE) { - return attr.isArchive(); - } - else if (attribute == ATTRIBUTE_HIDDEN) { - return attr.isHidden(); - } - else if (attribute == ATTRIBUTE_SYSTEM) { - return attr.isSystem(); + if (!SystemUtil.isWindows()) return false; + + try { + provider.lock(this); + DosFileAttributes attr = Files.readAttributes(this.toPath(), DosFileAttributes.class); + if (attribute == ATTRIBUTE_ARCHIVE) { + return attr.isArchive(); + } + else if (attribute == ATTRIBUTE_HIDDEN) { + return attr.isHidden(); + } + else if (attribute == ATTRIBUTE_SYSTEM) { + return attr.isSystem(); + } + else { + return false; + } } - else { + catch (Exception e) { return false; } - } - catch (Exception e) { - return false; + finally { + provider.unlock(this); + } } finally { - provider.unlock(this); + log(getAbsolutePath() + ":getAttribute(short attribute)", start); } } @Override public void setAttribute(short attribute, boolean value) throws IOException { - if (!SystemUtil.isWindows()) return; - - provider.lock(this); + double start = SystemUtil.millis(); try { - if (attribute == ATTRIBUTE_ARCHIVE) { - Files.setAttribute(this.toPath(), "dos:archive", value); + if (!SystemUtil.isWindows()) return; + + provider.lock(this); + try { + if (attribute == ATTRIBUTE_ARCHIVE) { + Files.setAttribute(this.toPath(), "dos:archive", value); + } + else if (attribute == ATTRIBUTE_HIDDEN) { + Files.setAttribute(this.toPath(), "dos:hidden", value); + } + else if (attribute == ATTRIBUTE_SYSTEM) { + Files.setAttribute(this.toPath(), "dos:system", value); + } } - else if (attribute == ATTRIBUTE_HIDDEN) { - Files.setAttribute(this.toPath(), "dos:hidden", value); + catch (IOException e) { + return; } - else if (attribute == ATTRIBUTE_SYSTEM) { - Files.setAttribute(this.toPath(), "dos:system", value); + finally { + provider.unlock(this); } } - catch (IOException e) { - return; - } finally { - provider.unlock(this); + log(getAbsolutePath() + ":setAttribute(short attribute, boolean value)", start); } } @Override public boolean equals(Object other) { - if (provider.isCaseSensitive()) return super.equals(other); - if (!(other instanceof File)) return false; - return getAbsolutePath().equalsIgnoreCase(((File) other).getAbsolutePath()); + double start = SystemUtil.millis(); + try { + if (provider.isCaseSensitive()) return super.equals(other); + if (!(other instanceof File)) return false; + return getAbsolutePath().equalsIgnoreCase(((File) other).getAbsolutePath()); + } + finally { + log(getAbsolutePath() + ":equals(Object other)", start); + } } }