1 32 33 package com.jeantessier.classreader; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 39 public class Monitor extends LoadListenerVisitorAdapter { 40 private RemoveVisitor removeVisitor; 41 42 private Map fileToClass = new HashMap(); 43 private boolean closedSession = true; 44 45 Collection previousFiles = new TreeSet(); 47 Collection currentFiles = new TreeSet(); 48 49 public Monitor(Visitor addVisitor, RemoveVisitor removeVisitor) { 50 super(addVisitor); 51 52 this.removeVisitor = removeVisitor; 53 } 54 55 public boolean isClosedSession() { 56 return closedSession; 57 } 58 59 public void setClosedSession(boolean closedSession) { 60 if (!this.closedSession && closedSession) { 61 closeSession(); 62 } 63 64 this.closedSession = closedSession; 65 } 66 67 public void beginFile(LoadEvent event) { 68 Logger.getLogger(getClass()).debug("beginFile(..., " + event.getFilename() + ", ...)"); 69 70 currentFiles.add(event.getFilename()); 71 } 72 73 public void endClassfile(LoadEvent event) { 74 Logger.getLogger(getClass()).debug("endClassfile(..., " + event.getFilename() + ", " + event.getClassfile() + ")"); 75 76 if (previousFiles.contains(event.getFilename())) { 77 Logger.getLogger(getClass()).debug("Removing " + event.getClassfile() + " ..."); 78 removeVisitor.removeClass(event.getClassfile().getClassName()); 79 } 80 81 super.endClassfile(event); 82 83 fileToClass.put(event.getFilename(), event.getClassfile().getClassName()); 84 } 85 86 public void endFile(LoadEvent event) { 87 Logger.getLogger(getClass()).debug("endFile(..., " + event.getFilename() + ", ...)"); 88 89 previousFiles.remove(event.getFilename()); 90 } 91 92 public void endSession(LoadEvent event) { 93 Logger.getLogger(getClass()).debug("endSession(...)"); 94 95 if (isClosedSession()) { 96 removeUnreadFiles(); 97 closeSession(); 98 } 99 } 100 101 102 private void removeUnreadFiles() { 103 Iterator i = previousFiles.iterator(); 104 while (i.hasNext()) { 105 String classname = (String ) fileToClass.get(i.next()); 106 Logger.getLogger(getClass()).debug("Removing " + classname + " ..."); 107 removeVisitor.removeClass(classname); 108 } 109 } 110 111 private void closeSession() { 112 previousFiles = currentFiles; 113 currentFiles = new TreeSet(); 114 } 115 } 116 | Popular Tags |