KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > Depend


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 /**
36  * Class for keeping track of modifications.
37  */

38 public class Depend implements PersistentDependency {
39   private static final Logger JavaDoc log
40     = Logger.getLogger(Depend.class.getName());
41   
42   Path _source;
43   long _lastModified;
44   long _length;
45
46   boolean _requireSource = true;
47   boolean _isDigestModified;
48
49   /**
50    * Create a new dependency with an already known modified time and length.
51    *
52    * @param source the source file
53    */

54   public Depend(Path source, long lastModified, long length)
55   {
56     _source = source;
57     _lastModified = lastModified;
58     _length = length;
59   }
60
61   /**
62    * Create a new dependency.
63    *
64    * @param source the source file
65    */

66   public Depend(Path source)
67   {
68     /* XXX:
69     if (source instanceof JarPath)
70       source = ((JarPath) source).getContainer();
71     */

72
73     _source = source;
74     _lastModified = source.getLastModified();
75     _length = source.getLength();
76   }
77
78   /**
79    * Create a new dependency with a given digest.
80    *
81    * @param source the source file
82    * @param digest the CRC64 digest
83    */

84   public Depend(Path source, long digest)
85   {
86     this(source, digest, true);
87   }
88
89   /**
90    * Create a new dependency with a given digest.
91    *
92    * @param source the source file
93    * @param digest the CRC64 digest
94    */

95   public Depend(Path source, long digest, boolean requireSource)
96   {
97     _source = source;
98
99     long newDigest = source.getCrc64();
100
101     _requireSource = requireSource;
102
103     if (newDigest == digest) {
104     }
105     else if (! requireSource && newDigest == -1) {
106     }
107     else if (newDigest == -1) {
108       if (log.isLoggable(Level.FINE))
109         log.fine(_source.getNativePath() + " source is deleted.");
110       
111       _isDigestModified = true;
112     }
113     else {
114       /*
115       if (log.isLoggable(Level.FINE))
116         log.fine(_source.getNativePath() + " digest is modified.");
117       */

118
119       _isDigestModified = true;
120     }
121
122     _lastModified = _source.getLastModified();
123     _length = _source.getLength();
124   }
125
126   /**
127    * Returns the underlying source path.
128    */

129   public Path getPath()
130   {
131     return _source;
132   }
133
134   /**
135    * Returns the current last-modified time of the file.
136    */

137   public long getLastModified()
138   {
139     return _source.getLastModified();
140   }
141
142   /**
143    * Returns the current length time of the file.
144    */

145   public long getLength()
146   {
147     return _source.getLength();
148   }
149
150   /**
151    * If true, deleting the source counts as a change.
152    */

153   public boolean getRequireSource()
154   {
155     return _requireSource;
156   }
157
158   /**
159    * If true, deleting the source counts as a change.
160    */

161   public void setRequireSource(boolean requireSource)
162   {
163     _requireSource = requireSource;
164   }
165
166   /**
167    * If the source modified date changes at all, treat it as a modification.
168    * This protects against the case where multiple computers have
169    * misaligned dates and a '<' comparison may fail.
170    */

171   public boolean isModified()
172   {
173     if (_isDigestModified) {
174       if (log.isLoggable(Level.FINE))
175         log.fine(_source.getNativePath() + " digest is modified.");
176
177       return true;
178     }
179
180     long sourceLastModified = _source.getLastModified();
181     long sourceLength = _source.getLength();
182
183     // if the source was deleted and we need the source
184
if (! _requireSource && sourceLastModified == 0)
185       return false;
186     // if the length changed
187
else if (sourceLength != _length) {
188       if (log.isLoggable(Level.FINE))
189         log.fine(_source.getNativePath() + " length is modified (" +
190          _length + " -> " + sourceLength + ")");
191
192       return true;
193     }
194     // if the source is newer than the old value
195
else if (sourceLastModified != _lastModified) {
196       if (log.isLoggable(Level.FINE))
197         log.fine(_source.getNativePath() + " time is modified.");
198       
199       return true;
200     }
201     else
202       return false;
203   }
204
205   /**
206    * Returns the digest.
207    */

208   public long getDigest()
209   {
210     return _source.getCrc64();
211   }
212   
213   /**
214    * Returns true if the test Dependency has the same source path as
215    * this dependency.
216    */

217   public boolean equals(Object JavaDoc obj)
218   {
219     if (! (obj instanceof Depend))
220       return false;
221
222     Depend depend = (Depend) obj;
223
224     return _source.equals(depend._source);
225   }
226
227   /**
228    * Returns the string to recreate the Dependency.
229    */

230   public String JavaDoc getJavaCreateString()
231   {
232     return ("new com.caucho.vfs.Depend(com.caucho.vfs.Vfs.lookup(\"" +
233             _source.getPath() + "\"), " + _source.getCrc64() + "L)");
234   }
235
236   /**
237    * Returns a printable version of the dependency.
238    */

239   public String JavaDoc toString()
240   {
241     return ("Depend[" + _source + " " + _lastModified + " " +
242             (_source.getLastModified() - _lastModified) + "]");
243   }
244 }
245
Popular Tags