KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > cm > DependencyTracker


1 /* *****************************************************************************
2  * DependencyTracker.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.cm;
11
12 import org.apache.log4j.*;
13 import java.io.*;
14 import java.util.*;
15 import org.openlaszlo.server.LPS;
16 import org.openlaszlo.utils.ChainedException;
17
18 /** Tracks compilation dependencies, so that it can tell if a file
19  * needs to be recompiled. An instance stores version information
20  * about all the source files that an object file depends on (the
21  * files such that, if one changed, the object file would be out of
22  * date).
23  *
24  * @author Oliver steele
25  */

26 class DependencyTracker implements java.io.Serializable JavaDoc {
27     private static Logger mLogger = Logger.getLogger(DependencyTracker.class);
28
29     /** Records information about the version of a file.
30      */

31     private static class FileInfo implements java.io.Serializable JavaDoc {
32         /** pathname */
33         private String JavaDoc mPathname;
34         /** last mod time */
35         private long mLastMod;
36         /** can read? */
37         private boolean mCanRead;
38         /** File length */
39         private long mLength;
40         
41         /** Constructs an instance.
42          * @param pathname the name of a file
43          */

44         FileInfo(String JavaDoc pathname) {
45             mPathname = pathname;
46             File file = new File(pathname);
47             // Cope with directory indexes for now
48
// FIXME: [2003-05-09 bloch] is this the right place
49
// for this?
50
if (file.isDirectory()) {
51                 file = new File(pathname + File.separator + "library.lzx");
52             }
53             // Truncate to an seconds
54
mLastMod = ((long)(file.lastModified() / 1000L)) * 1000L;
55             mCanRead = file.canRead();
56             //mLogger.debug("lm: " + mLastMod);
57
mLength = file.length();
58         }
59         
60         /** Returns true iff this FileInfo has up to date information
61          * compared to the fileinfo argument.
62          * @param info another FileInfo
63          * @return see documentation
64          */

65         boolean isUpToDate(FileInfo info) {
66             return this.mLastMod == info.mLastMod
67                 && this.mCanRead == info.mCanRead
68                 && this.mLength == info.mLength;
69         }
70     };
71
72     /** A list of FileInfo records for files that are depended on. */
73     private final Vector mDependencies = new Vector();
74     private Properties mProperties;
75     private String JavaDoc mWebappPath;
76
77     DependencyTracker(Properties properties) {
78         this.mProperties = properties;
79         this.mWebappPath = LPS.HOME(); // get it from global
80
}
81     
82     /** Add the specified file to the list of file dependencies.
83      * @param file a file
84      */

85     void addFile(File file) {
86         mLogger.debug("addFile Path is " + file.getPath());
87         FileInfo fi = new FileInfo(file.getPath());
88         try {
89             fi.mPathname = file.getCanonicalPath();
90         } catch (java.io.IOException JavaDoc e) {
91             throw new ChainedException(e);
92         }
93         mDependencies.add(fi);
94     }
95
96
97     /**
98      * Copy file info from the given tracker to me, omitting
99      * omitting then given file.
100      */

101     void copyFiles(DependencyTracker t, File omitMe) {
102         try {
103             for (Iterator e = t.mDependencies.iterator(); e.hasNext(); ) {
104                 FileInfo f = (FileInfo)e.next();
105                 if (! f.mPathname.equals(omitMe.getCanonicalPath())) {
106                     mDependencies.add(f);
107                 }
108             }
109         } catch (java.io.IOException JavaDoc e) {
110             throw new ChainedException(e);
111         }
112     }
113
114
115     /** This will update the FileInfo object chain to use the (possibly new)
116      * webappPath once the DependencyTracker object has been reconstitutded
117      * from ondisk cache.
118      */

119     void updateWebappPath() {
120         String JavaDoc webappPath = LPS.HOME(); // get it from global
121
if (webappPath.equals(mWebappPath))
122             return;
123         mLogger.debug("updating webappPath from: " + mWebappPath);
124         mLogger.debug("updating webappPath to: " + webappPath);
125         for (Iterator e = mDependencies.iterator(); e.hasNext(); ) {
126             FileInfo saved = (FileInfo) e.next();
127             if (saved.mPathname.startsWith(mWebappPath)) {
128                 mLogger.debug("updating dependencies from: " + saved.mPathname);
129                 saved.mPathname = webappPath +
130                         saved.mPathname.substring(mWebappPath.length());
131                 mLogger.debug("updating dependencies to : " + saved.mPathname);
132             }
133         }
134         mWebappPath = webappPath;
135     }
136
137     /** Returns true iff all the files listed in this tracker's
138      * dependency list exist and are at the same version as when they
139      * were recorded.
140      * @return a boolean
141      */

142     boolean isUpToDate(Properties properties) {
143         Iterator e;
144         
145         // fixes bug 962
146
{
147             if (mProperties.size() != properties.size()) {
148                 mLogger.debug("my size " + mProperties.size());
149                 mLogger.debug("new size " + properties.size());
150                 return false;
151             }
152
153             for (e = mProperties.keySet().iterator(); e.hasNext(); ) {
154                 String JavaDoc key = (String JavaDoc) e.next();
155                 String JavaDoc val0 = mProperties.getProperty(key);
156                 String JavaDoc val1 = properties.getProperty(key);
157
158                 // val0 can't be null; properties don't allow that
159
if (val1 == null || ! val0.equals(val1)) {
160                     mLogger.debug("Missing or changed property: " + val0);
161                     return false;
162                 }
163             }
164         }
165
166         for (e = mDependencies.iterator(); e.hasNext(); ) {
167             FileInfo saved = (FileInfo) e.next();
168             FileInfo current = new FileInfo(saved.mPathname);
169             if (!saved.isUpToDate(current)) {
170                 mLogger.debug(saved.mPathname + " has changed");
171                 mLogger.debug("was " + saved.mLastMod);
172                 mLogger.debug(" is " + current.mLastMod);
173                 return false;
174             }
175         }
176         return true;
177     }
178 }
179
Popular Tags