KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39
40 public abstract class ClassfileLoaderEventSource extends ClassfileLoader {
41     public static final ClassfileLoaderDispatcher DEFAULT_DISPATCHER = new PermissiveDispatcher();
42     
43     private ClassfileLoaderDispatcher dispatcher;
44     
45     private ClassfileLoader dirLoader = new DirectoryClassfileLoader(this);
46     private ClassfileLoader jarLoader = new JarClassfileLoader(this);
47     private ClassfileLoader zipLoader = new ZipClassfileLoader(this);
48
49     private HashSet loadListeners = new HashSet();
50
51     private LinkedList groupNames = new LinkedList();
52     private LinkedList groupSizes = new LinkedList();
53
54     private int previousDispatch;
55     
56     public ClassfileLoaderEventSource() {
57         this(DEFAULT_DISPATCHER);
58     }
59
60     public ClassfileLoaderEventSource(ClassfileLoaderDispatcher dispatcher) {
61         this.dispatcher = dispatcher;
62     }
63     
64     protected void load(String JavaDoc filename) {
65         int dispatch = dispatcher.dispatch(filename);
66
67         previousDispatch = dispatch;
68         
69         switch (dispatch) {
70             case ClassfileLoaderDispatcher.ACTION_IGNORE:
71                 Logger.getLogger(getClass()).debug("IGNORE \"" + filename + "\"");
72                 break;
73
74             case ClassfileLoaderDispatcher.ACTION_CLASS:
75             case ClassfileLoaderDispatcher.ACTION_DIRECTORY:
76                 Logger.getLogger(getClass()).debug("DIRECTORY or CLASS \"" + filename + "\"");
77                 dirLoader.load(filename);
78                 break;
79
80             case ClassfileLoaderDispatcher.ACTION_ZIP:
81                 Logger.getLogger(getClass()).debug("ZIP \"" + filename + "\"");
82                 zipLoader.load(filename);
83                 break;
84
85             case ClassfileLoaderDispatcher.ACTION_JAR:
86                 Logger.getLogger(getClass()).debug("JAR \"" + filename + "\"");
87                 jarLoader.load(filename);
88                 break;
89
90             default:
91                 Logger.getLogger(getClass()).debug("default (IGNORE) \"" + filename + "\"");
92                 break;
93         }
94     }
95
96     protected void load(String JavaDoc filename, InputStream in) {
97         int dispatch = dispatcher.dispatch(filename);
98
99         if (dispatch == ClassfileLoaderDispatcher.ACTION_IGNORE && getTopGroupSize() == 1 && filename.equals(getTopGroupName())) {
100             dispatch = previousDispatch;
101         }
102         
103         switch (dispatch) {
104             case ClassfileLoaderDispatcher.ACTION_IGNORE:
105                 Logger.getLogger(getClass()).debug("IGNORE \"" + filename + "\"");
106                 break;
107
108             case ClassfileLoaderDispatcher.ACTION_DIRECTORY:
109                 Logger.getLogger(getClass()).debug("DIRECTORY \"" + filename + "\"");
110                 dirLoader.load(filename, in);
111                 break;
112
113             case ClassfileLoaderDispatcher.ACTION_ZIP:
114                 Logger.getLogger(getClass()).debug("ZIP \"" + filename + "\"");
115                 zipLoader.load(filename, in);
116                 break;
117
118             case ClassfileLoaderDispatcher.ACTION_JAR:
119                 Logger.getLogger(getClass()).debug("JAR \"" + filename + "\"");
120                 jarLoader.load(filename, in);
121                 break;
122
123             case ClassfileLoaderDispatcher.ACTION_CLASS:
124                 Logger.getLogger(getClass()).debug("CLASS \"" + filename + "\"");
125                 try {
126                     fireBeginClassfile(filename);
127                     Classfile classfile = load(new DataInputStream(in));
128                     fireEndClassfile(filename, classfile);
129                 } catch (IOException ex) {
130                     Logger.getLogger(getClass()).warn("Cannot load class from file \"" + filename + "\"", ex);
131                 }
132                 break;
133                 
134             default:
135                 Logger.getLogger(getClass()).debug("default (IGNORE) \"" + filename + "\"");
136                 break;
137         }
138     }
139
140     public void addLoadListener(LoadListener listener) {
141         synchronized(loadListeners) {
142             loadListeners.add(listener);
143         }
144     }
145
146     public void removeLoadListener(LoadListener listener) {
147         synchronized(loadListeners) {
148             loadListeners.remove(listener);
149         }
150     }
151
152     protected void fireBeginSession() {
153         Logger.getLogger(getClass()).debug("Begin session");
154         
155         LoadEvent event = new LoadEvent(this, null, null, null);
156
157         HashSet listeners;
158         synchronized(loadListeners) {
159             listeners = (HashSet) loadListeners.clone();
160         }
161
162         Iterator i = listeners.iterator();
163         while(i.hasNext()) {
164             ((LoadListener) i.next()).beginSession(event);
165         }
166     }
167
168     protected void fireBeginGroup(String JavaDoc groupName, int size) {
169         Logger.getLogger(getClass()).debug("Begin group \"" + groupName + "\" of size " + size);
170
171         LoadEvent event = new LoadEvent(this, groupName, size);
172
173         HashSet listeners;
174         synchronized(loadListeners) {
175             listeners = (HashSet) loadListeners.clone();
176         }
177
178         Iterator i = listeners.iterator();
179         while(i.hasNext()) {
180             ((LoadListener) i.next()).beginGroup(event);
181         }
182
183         pushGroupName(groupName);
184         pushGroupSize(size);
185     }
186     
187     protected void fireBeginFile(String JavaDoc filename) {
188         Logger.getLogger(getClass()).debug("Begin file \"" + filename + "\"");
189         
190         LoadEvent event = new LoadEvent(this, getTopGroupName(), filename, null);
191
192         HashSet listeners;
193         synchronized(loadListeners) {
194             listeners = (HashSet) loadListeners.clone();
195         }
196
197         Iterator i = listeners.iterator();
198         while(i.hasNext()) {
199             ((LoadListener) i.next()).beginFile(event);
200         }
201     }
202     
203     protected void fireBeginClassfile(String JavaDoc filename) {
204         Logger.getLogger(getClass()).debug("Begin classfile \"" + filename + "\"");
205         
206         LoadEvent event = new LoadEvent(this, getTopGroupName(), filename, null);
207
208         HashSet listeners;
209         synchronized(loadListeners) {
210             listeners = (HashSet) loadListeners.clone();
211         }
212
213         Iterator i = listeners.iterator();
214         while(i.hasNext()) {
215             ((LoadListener) i.next()).beginClassfile(event);
216         }
217     }
218
219     protected void fireEndClassfile(String JavaDoc filename, Classfile classfile) {
220         Logger.getLogger(getClass()).debug("End classfile \"" + filename + "\": " + ((classfile != null) ? classfile.getClassName() : "nothing"));
221         
222         LoadEvent event = new LoadEvent(this, getTopGroupName(), filename, classfile);
223
224         HashSet listeners;
225         synchronized(loadListeners) {
226             listeners = (HashSet) loadListeners.clone();
227         }
228
229         Iterator i = listeners.iterator();
230         while(i.hasNext()) {
231             ((LoadListener) i.next()).endClassfile(event);
232         }
233     }
234
235     protected void fireEndFile(String JavaDoc filename) {
236         Logger.getLogger(getClass()).debug("End file \"" + filename + "\"");
237         
238         LoadEvent event = new LoadEvent(this, getTopGroupName(), filename, null);
239
240         HashSet listeners;
241         synchronized(loadListeners) {
242             listeners = (HashSet) loadListeners.clone();
243         }
244
245         Iterator i = listeners.iterator();
246         while(i.hasNext()) {
247             ((LoadListener) i.next()).endFile(event);
248         }
249     }
250
251     protected void fireEndGroup(String JavaDoc groupName) {
252         Logger.getLogger(getClass()).debug("End group \"" + groupName + "\"");
253         
254         LoadEvent event = new LoadEvent(this, groupName, null, null);
255
256         HashSet listeners;
257         synchronized(loadListeners) {
258             listeners = (HashSet) loadListeners.clone();
259         }
260
261         Iterator i = listeners.iterator();
262         while(i.hasNext()) {
263             ((LoadListener) i.next()).endGroup(event);
264         }
265
266         popGroupName();
267         popGroupSize();
268     }
269
270     protected void fireEndSession() {
271         Logger.getLogger(getClass()).debug("End session");
272         
273         LoadEvent event = new LoadEvent(this, null, null, null);
274
275         HashSet listeners;
276         synchronized(loadListeners) {
277             listeners = (HashSet) loadListeners.clone();
278         }
279
280         Iterator i = listeners.iterator();
281         while(i.hasNext()) {
282             ((LoadListener) i.next()).endSession(event);
283         }
284     }
285
286     private String JavaDoc getTopGroupName() {
287         String JavaDoc result = null;
288
289         if (!groupNames.isEmpty()) {
290             result = (String JavaDoc) groupNames.getLast();
291         }
292
293         return result;
294     }
295
296     private void pushGroupName(String JavaDoc groupName) {
297         groupNames.addLast(groupName);
298     }
299
300     private String JavaDoc popGroupName() {
301         return (String JavaDoc) groupNames.removeLast();
302     }
303
304     private int getTopGroupSize() {
305         Integer JavaDoc result = null;
306
307         if (!groupSizes.isEmpty()) {
308             result = (Integer JavaDoc) groupSizes.getLast();
309         }
310
311         return result.intValue();
312     }
313
314     private void pushGroupSize(int size) {
315         groupSizes.addLast(new Integer JavaDoc(size));
316     }
317
318     private int popGroupSize() {
319         return ((Integer JavaDoc) groupSizes.removeLast()).intValue();
320     }
321 }
322
Popular Tags