Coverage Report - org.kuali.rice.kew.plugin.ModificationTracker
 
Classes in this File Line Coverage Branch Coverage Complexity
ModificationTracker
0%
0/25
0%
0/12
2
ModificationTracker$Node
0%
0/13
0%
0/4
2
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  * 
 4  
  * 
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.plugin;
 18  
 
 19  
 import java.io.File;
 20  
 import java.net.MalformedURLException;
 21  
 import java.net.URL;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.Map;
 25  
 
 26  
 /**
 27  
  * Tracks modifications to a set of files and directories.
 28  
  * 
 29  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 30  
  */
 31  0
 public class ModificationTracker implements Modifiable {
 32  
 
 33  0
         protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
 34  
         
 35  
         // Maintains a map of root directories that we are watching to sub directories  
 36  0
     private final Map dirTree = new HashMap();
 37  
     
 38  
     public synchronized void addURL(URL url) {
 39  0
         addURL(dirTree, url);
 40  0
     }
 41  
             
 42  
     private void addURL(Map parentMap, URL url)  {
 43  0
         Node node = new Node(url);
 44  0
         if (!parentMap.keySet().contains(node)) {
 45  0
             Map subDir = new HashMap();
 46  0
             parentMap.put(node, subDir);
 47  0
             if (node.getFile().isDirectory()) {
 48  0
                 File[] files = node.getFile().listFiles();
 49  0
                 for (int index = 0; index < files.length; index++) {
 50  
                     try {
 51  0
                         addURL(subDir, files[index].toURL());
 52  0
                     } catch (MalformedURLException e) {
 53  
                         // TODO fix up this error handling
 54  0
                         throw new RuntimeException(e);
 55  0
                     }
 56  
                 }
 57  
             }
 58  
         }
 59  0
     }
 60  
     
 61  
     public boolean isModified() {
 62  0
             return isModified(dirTree);
 63  
     }
 64  
         
 65  
     private boolean isModified(Map dirTree) {
 66  0
             for (Iterator iterator = dirTree.keySet().iterator(); iterator.hasNext();) {
 67  0
                         Node node = (Node) iterator.next();
 68  0
                         if (node.isModified() || isModified((Map)dirTree.get(node))) {
 69  0
                                 return true;
 70  
                         }
 71  0
                 }
 72  0
             return false;
 73  
     }
 74  
     
 75  0
     private class Node implements Modifiable {
 76  
         private URL url;
 77  
         private File file;
 78  
         private long lastModified;
 79  0
         public Node(URL url) {
 80  0
             this.url = url;
 81  0
             this.file = new File(url.getFile());
 82  0
             this.lastModified = file.lastModified();
 83  0
         }
 84  
         public URL getURL() {
 85  0
             return url;
 86  
         }
 87  
         public File getFile() {
 88  0
             return file;
 89  
         }
 90  
         public long getLastModified() {
 91  0
             return lastModified;
 92  
         }
 93  
         public boolean isModified() {
 94  0
             return lastModified != file.lastModified();
 95  
         }
 96  
         public boolean equals(Object object) {
 97  0
             if (object instanceof Node) {
 98  0
                 return ((Node)object).getURL().equals(getURL());
 99  
             }
 100  0
             return false;
 101  
         }
 102  
         public int hashCode() {
 103  0
             return url.hashCode();
 104  
         }
 105  
     }
 106  
 
 107  
 }