KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.EventObject JavaDoc;
22
23 /**
24  * Base class for Jiapi events. This method is safe, in terms of
25  * event loop protection. That is, calling methods of JiapiEvent
26  * is guaranteed not to produce any further events from occuring.
27  *
28  * @author Mika Riekkinen
29  * @author Joni Suominen
30  * @version $Revision: 1.7 $ $Date: 2002/03/26 08:16:13 $
31  */

32 public class JiapiEvent extends EventObject JavaDoc {
33     protected int id;
34     protected Object JavaDoc sourceObject;
35     protected Object JavaDoc target;
36     protected String JavaDoc targetName;
37
38     /**
39      * Constructor for JiapiEvent. Target is set to null.
40      *
41      * @param ep EventProducer
42      * @param sourceObject Source Object.
43      * @param targetName name of the target Object
44      * @param id Id of the event
45      */

46     public JiapiEvent(EventProducer ep, Object JavaDoc sourceObject, String JavaDoc targetName,
47                       int id) {
48         this(ep, sourceObject, targetName, null, id);
49     }
50
51     /**
52      * Constructor for JiapiEvent.
53      *
54      * @param ep EventProducer. This is passed to parent class with
55      * <code>super(ep);</code>
56      * @param sourceObject Source Object.
57      * @param targetName name of the target Object
58      * @param target target Object
59      * @param id Id of the event
60      */

61     public JiapiEvent(EventProducer ep, Object JavaDoc sourceObject,
62                       String JavaDoc targetName, Object JavaDoc target, int id) {
63         super(ep);
64
65         this.sourceObject = sourceObject;
66         this.targetName = targetName;
67         this.target = target;
68         this.id = id;
69     }
70
71     /**
72      * Get the source Object.
73      * @return source Object
74      */

75     public Object JavaDoc getSourceObject() {
76         return sourceObject;
77     }
78
79     
80     /**
81      * Gets the name associated with this event. For example,
82      * MethodEvent provides name of the method and FieldEvent
83      * provides name of the field.
84      * @return name of the target
85      */

86     public String JavaDoc getTargetName() {
87         return targetName;
88     }
89
90
91     /**
92      * Gets target Object.
93      * @return target Object or null, if target is not specified.
94      */

95     public Object JavaDoc getTarget() {
96         return target;
97     }
98
99
100     /**
101      * This method protects application from entering into
102      * recursive event loop. This situation may occur, if a method
103      * has been instrumented, and instrumentation produces an
104      * event. If that method is called, directly or indirectly,
105      * with the aid of JiapiEvent, application will enter to an
106      * endless event loop, eventually crashing Virtual Machine.<p>
107      *
108      * Calling this method prevents <code>EventProducer</code>
109      * from producing further events for the same sourceObject.
110      * This protection mechanism allows applications to call
111      * methods of sourceObject and targetObject without worrying
112      * about event loops.<p>
113      *
114      * To enable events again, one will have to release
115      * <code>EventProducer</code> with method <code>release</code>.<p>
116      *
117      * @see EventProducer#protect(JiapiEvent)
118      * @see #release()
119      */

120     public void protect() {
121         ((EventProducer)getSource()).protect(this);
122     }
123
124     /**
125      * This method releases <code>EventProducer</code> so, that
126      * it is able to produce more events for the sourceObject.
127      *
128      * @see EventProducer#release(JiapiEvent)
129      * @see #protect()
130      */

131     public void release() {
132         ((EventProducer)getSource()).release(this);
133     }
134
135     private boolean testProtection = false; // If true, Tomcat crashes
136
public String JavaDoc toString() {
137         if(testProtection) {
138             String JavaDoc s = null;
139
140             if (sourceObject != null) {
141                 protect();
142                 s = sourceObject.toString();
143                 release();
144             }
145
146             return s;
147         }
148
149         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
150         sb.append(", source=");
151
152         if (sourceObject instanceof String JavaDoc) {
153             sb.append(sourceObject);
154         }
155         else if (sourceObject instanceof Class JavaDoc) {
156             sb.append(sourceObject.toString());
157         }
158         else {
159             sb.append(sourceObject.getClass().getName());
160         }
161
162         sb.append(", target name=");
163         sb.append(targetName);
164         sb.append(", target=" );
165
166         if (target != null) {
167             sb.append(target.getClass().getName());
168         }
169         else {
170             sb.append("null");
171         }
172
173         sb.append(", id=");
174         sb.append(id);
175
176         return sb.toString();
177 // return super.toString() + ", source=" + sourceObject +
178
// ", target name=" + targetName + ", target=" + target +
179
// ", id=" + id;
180
}
181
182
183     /**
184      * Finalization releases possible locks made on
185      * <code>EventProducer</code>.
186      */

187     public void finalize() {
188         try {
189             ((EventProducer)getSource()).release(this);
190         }
191         catch (Throwable JavaDoc t) {
192             // Ignore
193
}
194     }
195 }
196
197
Popular Tags