KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > event > AdminEvent


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.event;
32
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import javax.management.Notification JavaDoc;
37 import com.sun.enterprise.config.ConfigChange;
38 import com.sun.enterprise.config.ConfigContext;
39
40 //i18n import
41
import com.sun.enterprise.util.i18n.StringManager;
42
43 /**
44  * Superclass of all admin events. This is the marker object for all events
45  * in administration.
46  */

47 public class AdminEvent extends Notification JavaDoc implements Cloneable JavaDoc {
48
49     private EventKey eKey = null;
50     
51     private static long eventCounter = 0;
52
53     /** unresolved/abstract target destination */
54     private String JavaDoc targetDest;
55
56     /**
57      * Resolved destination based on target destination. This is
58      * where the notification event is delivered. Target destination
59      * always refers to concrete server end point.
60      */

61     private String JavaDoc effectiveDest;
62
63     /**
64      * Number of times this event is forwarded from one server instance
65      * to another server.
66      */

67     private int hops = 0;
68
69     /** maximum number of times an event is allowed to be forwarded */
70     private static final int MAX_HOPS = 3;
71
72     /**
73      * Event type is required for all sub-classes of Notification. It is a
74      * string representation for the event using dotted notation (for example -
75      * network.alarm.router)
76      */

77     static final String JavaDoc eventType = AdminEvent.class.getName();
78
79     /**
80      * Config context. This object provides access to a snapshot of config
81      * updated with all changes in this event at the time event processing
82      * starts on the instance to which the event applies.
83      */

84     transient private ConfigContext configContext;
85
86     /**
87      * Old config context. The object provides access to config context before
88      * applying changes from this event.
89      */

90     transient private ConfigContext oldConfigContext;
91
92     // i18n StringManager
93
private static StringManager localStrings =
94         StringManager.getManager( AdminEvent.class );
95
96     /**
97      * List of config changes.
98      */

99     ArrayList JavaDoc configChangeList;
100
101     /**
102      * List of dependent config changes.
103      */

104     List JavaDoc dependentChangeList;
105
106     /**
107      * Create a new admin event for specified ias instance.
108      * @param instanceName name of ias instance
109      */

110     public AdminEvent(String JavaDoc instanceName) {
111         this(eventType, instanceName);
112     }
113
114     /**
115      * Create a new admin event for specified ias instance.
116      * @param eventType type of the event
117      * @param instanceName name of ias instance
118      */

119     public AdminEvent(String JavaDoc eventType, String JavaDoc instanceName) {
120         super(eventType, instanceName, ++eventCounter,
121                 System.currentTimeMillis());
122     }
123
124     protected AdminEvent(String JavaDoc type, Object JavaDoc source,
125                          long seqNumber, long time) {
126         super(type, source, seqNumber, time);
127     }
128
129     /**
130      * Return name of the instance to which this event applies. (This
131      * interpretation is slightly different from EventObject.getSource()
132      * where it is expected to return the object on which event happened -
133      * so instead of the instance object it returns the name of the instance.)
134      * @return name of the instance to which event applies
135      */

136     public Object JavaDoc getSource() {
137         return super.getSource();
138     }
139
140     /**
141      * Return name of the instance to which this event applies. The difference
142      * from getSource() is that this returns a String object, so there is no
143      * need to cast the return value.
144      * @see getSource()
145      * @return name of the instance to which the event applies
146      */

147     public String JavaDoc getInstanceName() {
148         return (String JavaDoc)getSource();
149     }
150
151     /**
152      * Set config context to specified value. Event listeners can use this
153      * to get a consistent view of config. This config context should contain
154      * all changes from this event.
155      * @param ctx the config context
156      */

157     void setContext(ConfigContext ctx) {
158         configContext = ctx;
159     }
160
161     /**
162      * Set old config context to specified value. This context will not have
163      * changes from this event.
164      * @param ctx the old config context
165      */

166     void setOldContext(ConfigContext ctx) {
167         oldConfigContext = ctx;
168     }
169
170     /**
171      * Get config context. This returns a snapshot of config context with all
172      * changes in this event at the time event processing started on the
173      * receiving server instance. Event listeners should use this object to get
174      * a consistent view of context instead of system wide context because that
175      * can be updated during event processing. This method will return null if
176      * called before the event processing has started. So, it is intended for
177      * use by event listeners only.
178      * @return the snapshot of config context.
179      */

180     public ConfigContext getConfigContext() {
181         return configContext;
182     }
183
184     /**
185      * Get old config context. This returns a reference of config context prior
186      * to applying any changes from this event. This method will return null if
187      * called before event processing has started. This is just a reference to
188      * global config context at the time event processing started. Even though
189      * this is not a snapshot of config context (i.e. cloned copy), it is still
190      * sufficient for the usage in getting old config values, because once a
191      * context has been made global it is immutable.
192      */

193     public ConfigContext getOldConfigContext() {
194         return oldConfigContext;
195     }
196
197     /**
198      * Return a String representation.
199      */

200     public String JavaDoc toString() {
201         int numChg = (configChangeList == null) ? 0 : configChangeList.size();
202         return this.getClass().getName() + " -- " + this.getInstanceName()
203                 + " [" + numChg + " Change(s), Id:" + this.getSequenceNumber()
204                 + ", ts:" + this.getTimeStamp() + "]";
205     }
206
207     /**
208      * Get detailed event information. Unless overidden in sub classes, this
209      * method returns exactly same value as concatenation of toString() and
210      * getConfigChangeInfo() methods. The intent of this method is to return a
211      * detailed string representation of the event that can be used for
212      * debugging.
213      */

214     public String JavaDoc getEventInfo() {
215         return toString() + getConfigChangeInfo();
216     }
217
218     /**
219      * Get config change info. This method returns a string that lists config
220      * changes associated to this event. Returns an empty string if no changes
221      * are associated to this event.
222      */

223     public String JavaDoc getConfigChangeInfo() {
224         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
225         if (configChangeList != null) {
226             Iterator JavaDoc iter = configChangeList.iterator();
227             while (iter.hasNext()) {
228                 ConfigChange change = (ConfigChange)iter.next();
229                 buf.append(change.toString());
230             }
231         }
232         return buf.toString();
233     }
234
235     /**
236      * Add specified change to the event.
237      * @param change the change to add to this event
238      */

239     synchronized void addConfigChange(ConfigChange change) {
240         assertNotNull(change);
241         if (configChangeList == null) {
242             configChangeList = new ArrayList JavaDoc();
243         }
244         configChangeList.add(change);
245     }
246
247     /**
248      * Add specified changes to the event.
249      * @param changeList the list of changes to add to this event
250      */

251     public synchronized void addConfigChange(ArrayList JavaDoc changeList) {
252         if (changeList == null) {
253             String JavaDoc msg = localStrings.getString( "admin.event.null_configchangelist" );
254             throw new IllegalArgumentException JavaDoc( msg );
255         }
256         if (configChangeList == null) {
257             configChangeList = new ArrayList JavaDoc();
258         }
259         configChangeList.addAll(changeList);
260     }
261
262     public synchronized void addDependentConfigChange(List JavaDoc list) {
263         if (list == null) {
264             String JavaDoc msg = localStrings.getString( "admin.event.null_configchangelist" );
265             throw new IllegalArgumentException JavaDoc( msg );
266         }
267
268         if (dependentChangeList == null) {
269             dependentChangeList = new ArrayList JavaDoc();
270         }
271         dependentChangeList.addAll(list);
272     }
273
274     public List JavaDoc getDependentChangeList() {
275         return dependentChangeList;
276     }
277
278     public ArrayList JavaDoc getConfigChangeList() {
279         return configChangeList;
280     }
281
282     /**
283      * Remove specified config change from the event.
284      * @param change the change to add to this event
285      */

286     synchronized void removeConfigChange(ConfigChange change) {
287         assertNotNull(change);
288         if (configChangeList != null) {
289             int ndx = configChangeList.indexOf(change);
290             if (ndx != -1) {
291                 configChangeList.remove(ndx);
292             }
293         }
294     }
295
296     /**
297      * Is this event a no-op. The default implementation always returns false.
298      * However, the sub-classes can override and provide a more intelligent
299      * implementation.
300      * @return true if the event is a no op, false otherwise.
301      */

302     boolean isNoOp() {
303         return false;
304     }
305
306     /**
307      * Assert that specified ConfigChange is not null.
308      * @throws IllegalArgumentException if specified ConfigChange is null.
309      */

310     // Replace this by JDK 1.4 assert
311
private void assertNotNull(ConfigChange change) {
312         if (change == null) {
313             String JavaDoc msg = localStrings.getString( "admin.event.null_configchange" );
314             throw new IllegalArgumentException JavaDoc( msg );
315         }
316     }
317
318     /**
319      * Each event will have 2 destinations, target and effective destination
320      * target destination would be for example cluster1. effective destination
321      * would be endpoints of target destination.
322      *
323      * @return name of the target destination
324      */

325     public String JavaDoc getTargetDestination() {
326         return targetDest;
327     }
328
329     /**
330      * Sets the target destination
331      *
332      * @param tarDest target destination info
333      */

334     public void setTargetDestination(String JavaDoc tarDest) {
335         targetDest = tarDest;
336     }
337
338     /**
339      * Gets the effective destination for this event
340      *
341      * @return name of the effective destination
342      */

343     public String JavaDoc getEffectiveDestination() {
344         return effectiveDest;
345     }
346
347     /**
348      * Sets the effective destination
349      *
350      * @param eDest effective destination info
351      */

352     public void setEffectiveDestination(String JavaDoc eDest) {
353         effectiveDest = eDest;
354     }
355
356     /**
357      * Set event key information
358      */

359     public void setEventId(EventKey ek) {
360         eKey = ek;
361      }
362
363     /**
364      * Get event key information
365      */

366     public EventKey getEventId() {
367         return eKey;
368     }
369
370     /**
371      * Returns a top level clone of this object.
372      *
373      * @return a clone of this object
374      */

375     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
376         return super.clone();
377     }
378
379     /**
380      * Returns the current hop count (number of times this event has been
381      * forwarded) of this event.
382      *
383      * @return current hop count
384      */

385     public int getHopCount() {
386         return hops;
387     }
388
389     /**
390      * Increments the current hop count.
391      *
392      * @return incremented hop count
393      */

394     public int incrementHopCount() {
395         return ++hops;
396     }
397
398     /**
399      * Returns true if current hop count is less than or equal to maximum
400      * hop allowed for this event.
401      *
402      * @return true if current hop count is less than or equal to maximum
403      * hop allowed
404      */

405     public boolean isValidHopCount() {
406         return (hops <= MAX_HOPS) ? true : false;
407     }
408
409     // sub classes of AdminEvent, override the following two method
410
// to provide appropriate action codes.
411

412     /**
413      * Get action type for this event.
414      */

415     public int getActionType() {
416         return 0;
417     }
418
419     /**
420      * Set action to specified value. If action is not one of allowed,
421      */

422     private void setAction(int action) {
423         return;
424     }
425
426 }
427
Popular Tags