KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > event > ExceptionEventProducer


1 /*
2  * Copyright(C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.event;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import alt.jiapi.InstrumentationDescriptor;
27 import alt.jiapi.instrumentor.ChainInstrumentor;
28 import alt.jiapi.instrumentor.InstrumentorChain;
29
30 import alt.jiapi.instrumentor.CatchInstrumentor;
31 import alt.jiapi.instrumentor.GrepInstrumentor;
32 import alt.jiapi.instrumentor.HeadInstrumentor;
33 import alt.jiapi.instrumentor.MethodCallInstrumentor;
34 import alt.jiapi.instrumentor.MethodDispatcherInstrumentor;
35
36 /**
37  * This class registers itself to Jiapi runtime and tracks Exception
38  * related events. When such an event happens, each registered listener
39  * will notified.
40  *
41  * @author Mika Riekkinen
42  * @author Joni Suominen
43  * @version $Revision: 1.9 $ $Date: 2004/03/15 14:47:52 $
44  */

45 public class ExceptionEventProducer extends EventProducer {
46     private List JavaDoc listeners = new ArrayList JavaDoc();
47
48     /**
49      * Constructor. Resolution is set to '*', which indicates all the
50      * ExceptionEvents are produced.
51      *
52      * @param id Instrumentation decsriptor, that this ExceptionEventProducer
53      * registers itself to.
54      */

55     public ExceptionEventProducer(InstrumentationDescriptor id) {
56         this(id, "*");
57     }
58
59     /**
60      * Constructor. Only ExceptionEvents, that match with resolution,
61      * are produced. Resolution is compared against the name of the
62      * Exception during instrumentation. Name of the Exception is exactly
63      * the same, as it is given in Java source code in
64      * <code>catch(....)</code> statement. This means, that if one is
65      * catching <code>java.lang.Exception</code>, and
66      * <code>java.lang.ClassNotFoundException</code> is trapped, the former
67      * is used in resolution comparison.
68      *
69      * @param id Instrumentation decsriptor, that this ExceptionEventProducer
70      * registers itself to.
71      * @param resolution Resolution, that is used further to select which
72      * Exceptions will trigger events to be produced.
73      */

74     public ExceptionEventProducer(InstrumentationDescriptor id, String JavaDoc resolution) {
75         super(resolution);
76
77         try {
78             InstrumentorChain chain = new InstrumentorChain();
79             ChainInstrumentor dispatcher = new MethodDispatcherInstrumentor();
80             CatchInstrumentor selectCatchBlocks = new CatchInstrumentor();
81             ChainInstrumentor selectHead = new HeadInstrumentor();
82             ChainInstrumentor callMethod =
83                 new MethodCallInstrumentor(new CatchHook(this));
84
85             selectCatchBlocks.setResolutions(getResolutions());
86
87             chain.add(dispatcher);
88             chain.add(selectCatchBlocks);
89             chain.add(selectHead);
90             chain.add(callMethod);
91
92             id.addInstrumentor(chain);
93         } catch (Exception JavaDoc e) {
94             // NOTE! Fix exception handling.
95
e.printStackTrace();
96         }
97     }
98
99
100     /**
101      * Adds a ExceptionListener.
102      * @param fl a ExceptionListener
103      */

104     public synchronized void addExceptionListener(ExceptionListener fl) {
105         listeners.add(fl);
106     }
107
108     /**
109      * Removes a ExceptionListener.
110      * @param fl a ExceptionListener
111      */

112     public synchronized void removeExceptionListener(ExceptionListener fl) {
113         listeners.remove(fl);
114     }
115
116     /**
117      * This method is called by the Jiapi runtime. It should not be called
118      * by others.
119      */

120     public void catchTrapped(Object JavaDoc sourceObject, String JavaDoc exceptionName) {
121         catchTrapped(sourceObject, exceptionName, null);
122     }
123     
124     /**
125      * This method is called by the Jiapi runtime. It should not be called
126      * by others.
127      */

128     public void catchTrapped(Object JavaDoc sourceObject, String JavaDoc exceptionName,
129                              Object JavaDoc target) {
130         if (!isProtected(sourceObject)) {
131             fireCatchEvent(sourceObject, exceptionName, target);
132         }
133     }
134     
135     /**
136      * Fires an event when exception has been caught.
137      * @param target Target object
138      * @param methodName Name of the method
139      */

140     protected synchronized void fireCatchEvent(Object JavaDoc sourceObject,
141                                                String JavaDoc exceptionName,
142                                                Object JavaDoc target) {
143         Iterator JavaDoc i = listeners.iterator();
144         ExceptionEvent event =
145             new ExceptionEvent(this, sourceObject, exceptionName,
146                                target,
147                                ExceptionEvent.CATCH_TRAPPED);
148         
149         while (i.hasNext()) {
150             ExceptionListener el = (ExceptionListener)i.next();
151             el.exceptionCaught(event);
152         }
153     }
154 }
155
156
Popular Tags