KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > Monitor


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

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     // Package-level access for tests only
46
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 JavaDoc classname = (String JavaDoc) 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