001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: /**
036: * Class for keeping track of modifications.
037: */
038: public class PathExistsDependency implements Dependency {
039: private static final Logger log = Logger
040: .getLogger(PathExistsDependency.class.getName());
041:
042: Path _source;
043: boolean _exists;
044:
045: /**
046: * Create a new dependency.
047: *
048: * @param source the source file
049: */
050: public PathExistsDependency(Path source) {
051: if (source instanceof JarPath)
052: source = ((JarPath) source).getContainer();
053:
054: _source = source;
055: _exists = source.exists();
056: }
057:
058: /**
059: * Create a new dependency with an already known modified time and length.
060: *
061: * @param source the source file
062: */
063: public PathExistsDependency(Path source, boolean exists) {
064: _source = source;
065: _exists = exists;
066: }
067:
068: /**
069: * Returns the underlying source path.
070: */
071: public Path getPath() {
072: return _source;
073: }
074:
075: /**
076: * If the source modified date changes at all, treat it as a modification.
077: * This protects against the case where multiple computers have
078: * misaligned dates and a '<' comparison may fail.
079: */
080: public boolean isModified() {
081: boolean exists = _source.exists();
082:
083: if (exists == _exists)
084: return false;
085: else if (exists) {
086: if (log.isLoggable(Level.FINE))
087: log
088: .fine(_source.getNativePath()
089: + " has been created.");
090:
091: return true;
092: } else {
093: if (log.isLoggable(Level.FINE))
094: log
095: .fine(_source.getNativePath()
096: + " has been deleted.");
097:
098: return true;
099: }
100: }
101:
102: /**
103: * Log the reason for the modification.
104: */
105: public boolean logModified(Logger log) {
106: boolean exists = _source.exists();
107:
108: if (exists == _exists)
109: return false;
110: else if (exists) {
111: log.info(_source.getNativePath() + " has been created.");
112:
113: return true;
114: } else {
115: log.info(_source.getNativePath() + " has been deleted.");
116:
117: return true;
118: }
119: }
120:
121: /**
122: * Returns true if the test Dependency has the same source path as
123: * this dependency.
124: */
125: public boolean equals(Object obj) {
126: if (!(obj instanceof PathExistsDependency))
127: return false;
128:
129: PathExistsDependency depend = (PathExistsDependency) obj;
130:
131: return _source.equals(depend._source);
132: }
133:
134: /**
135: * Returns a printable version of the dependency.
136: */
137: public String toString() {
138: return ("PathExistsDependency[" + _source + "]");
139: }
140: }
|