001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.plugin;
018:
019: import java.io.File;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: /**
027: * Tracks modifications to a set of files and directories.
028: *
029: * @author ewestfal
030: */
031: public class ModificationTracker implements Modifiable {
032:
033: protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
034: .getLogger(getClass());
035:
036: // Maintains a map of root directories that we are watching to sub directories
037: private final Map dirTree = new HashMap();
038:
039: public synchronized void addURL(URL url) {
040: addURL(dirTree, url);
041: }
042:
043: private void addURL(Map parentMap, URL url) {
044: Node node = new Node(url);
045: if (!parentMap.keySet().contains(node)) {
046: Map subDir = new HashMap();
047: parentMap.put(node, subDir);
048: if (node.getFile().isDirectory()) {
049: File[] files = node.getFile().listFiles();
050: for (int index = 0; index < files.length; index++) {
051: try {
052: addURL(subDir, files[index].toURL());
053: } catch (MalformedURLException e) {
054: // TODO fix up this error handling
055: throw new RuntimeException(e);
056: }
057: }
058: }
059: }
060: }
061:
062: public boolean isModified() {
063: return isModified(dirTree);
064: }
065:
066: private boolean isModified(Map dirTree) {
067: for (Iterator iterator = dirTree.keySet().iterator(); iterator
068: .hasNext();) {
069: Node node = (Node) iterator.next();
070: if (node.isModified()
071: || isModified((Map) dirTree.get(node))) {
072: return true;
073: }
074: }
075: return false;
076: }
077:
078: private class Node implements Modifiable {
079: private URL url;
080: private File file;
081: private long lastModified;
082:
083: public Node(URL url) {
084: this .url = url;
085: this .file = new File(url.getFile());
086: this .lastModified = file.lastModified();
087: }
088:
089: public URL getURL() {
090: return url;
091: }
092:
093: public File getFile() {
094: return file;
095: }
096:
097: public long getLastModified() {
098: return lastModified;
099: }
100:
101: public boolean isModified() {
102: return lastModified != file.lastModified();
103: }
104:
105: public boolean equals(Object object) {
106: if (object instanceof Node) {
107: return ((Node) object).getURL().equals(getURL());
108: }
109: return false;
110: }
111:
112: public int hashCode() {
113: return url.hashCode();
114: }
115: }
116:
117: }
|