001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.util.*;
036:
037: import org.apache.log4j.*;
038:
039: public class Monitor extends LoadListenerVisitorAdapter {
040: private RemoveVisitor removeVisitor;
041:
042: private Map fileToClass = new HashMap();
043: private boolean closedSession = true;
044:
045: // Package-level access for tests only
046: Collection previousFiles = new TreeSet();
047: Collection currentFiles = new TreeSet();
048:
049: public Monitor(Visitor addVisitor, RemoveVisitor removeVisitor) {
050: super (addVisitor);
051:
052: this .removeVisitor = removeVisitor;
053: }
054:
055: public boolean isClosedSession() {
056: return closedSession;
057: }
058:
059: public void setClosedSession(boolean closedSession) {
060: if (!this .closedSession && closedSession) {
061: closeSession();
062: }
063:
064: this .closedSession = closedSession;
065: }
066:
067: public void beginFile(LoadEvent event) {
068: Logger.getLogger(getClass()).debug(
069: "beginFile(..., " + event.getFilename() + ", ...)");
070:
071: currentFiles.add(event.getFilename());
072: }
073:
074: public void endClassfile(LoadEvent event) {
075: Logger.getLogger(getClass()).debug(
076: "endClassfile(..., " + event.getFilename() + ", "
077: + event.getClassfile() + ")");
078:
079: if (previousFiles.contains(event.getFilename())) {
080: Logger.getLogger(getClass()).debug(
081: "Removing " + event.getClassfile() + " ...");
082: removeVisitor.removeClass(event.getClassfile()
083: .getClassName());
084: }
085:
086: super .endClassfile(event);
087:
088: fileToClass.put(event.getFilename(), event.getClassfile()
089: .getClassName());
090: }
091:
092: public void endFile(LoadEvent event) {
093: Logger.getLogger(getClass()).debug(
094: "endFile(..., " + event.getFilename() + ", ...)");
095:
096: previousFiles.remove(event.getFilename());
097: }
098:
099: public void endSession(LoadEvent event) {
100: Logger.getLogger(getClass()).debug("endSession(...)");
101:
102: if (isClosedSession()) {
103: removeUnreadFiles();
104: closeSession();
105: }
106: }
107:
108: private void removeUnreadFiles() {
109: Iterator i = previousFiles.iterator();
110: while (i.hasNext()) {
111: String classname = (String) fileToClass.get(i.next());
112: Logger.getLogger(getClass()).debug(
113: "Removing " + classname + " ...");
114: removeVisitor.removeClass(classname);
115: }
116: }
117:
118: private void closeSession() {
119: previousFiles = currentFiles;
120: currentFiles = new TreeSet();
121: }
122: }
|