KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map.Entry;
39 import java.util.Iterator JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.Collection JavaDoc;
42 import com.sun.enterprise.admin.common.AdminResponse;
43
44 //i18n import
45
import com.sun.enterprise.util.i18n.StringManager;
46
47 /**
48  * Admin Event Result objects are used to communicate result of admin event
49  * notification.
50  *
51  * AdminEventResult object can be either used to represent composite
52  * result of multiple instances.
53  *
54  * getResultCode() returns one of the valid result codes either ERROR,
55  * SUCCESS or others like TRANSMISSION_ERROR, RESTART_NEEDED etc. This
56  * is composite result of all event's recipients' results. In order to get
57  * individual recipient's result getResultCodes() can be used. It returns
58  * hash map of instance name and result code.
59  *
60  * If some recipients got the notification and the others did not receive
61  * the notification or failed to process it for any reason.The composite result
62  * would be MIXED_RESULT.
63  *
64  * Similarly hash map of messages and exceptions can be obtained using
65  * getMessages and getExceptions respectively.
66  *
67  * All the methods (setters/getters) of AdminEventResult (except setResultCode
68  * and getResultCode) take instance name as argument.
69  *
70  * Helper methods are also available to format the results and
71  * messages/exceptions into a string format. Please look at
72  * getAllResultCodesAsString() and getAllMessagesAsString()
73  * for details.
74  *
75  * This class is not thread safe and usage from AdminEventMulticaster does
76  * not require it to be thread safe. The methods add/remove Attribute do
77  * synchronize internally but that might not be sufficient.
78  */

79 public class AdminEventResult implements Serializable JavaDoc {
80
81     /**
82      * Constant denoting Serializable class
83      */

84     private static final Class JavaDoc SERIALIZABLE = Serializable JavaDoc.class;
85
86     /**
87      * Constant denoting - restart of server required.
88      */

89     public static final String JavaDoc RESTART_NEEDED = "restart";
90
91     /**
92      * Constant denoting - runtime exception while processing event
93      */

94     public static final String JavaDoc RUNTIME_EXCEPTION = "runtime_exception";
95
96     /**
97      * Constant denoting - runtime error while processing event
98      */

99     public static final String JavaDoc RUNTIME_ERROR = "runtime_error";
100
101     /**
102      * Constant denoting - event transmission error
103      */

104     public static final String JavaDoc TRANSMISSION_ERROR = "transmission_error";
105
106     /**
107      * Constant denoting - error while processing event in listener
108      */

109     public static final String JavaDoc LISTENER_ERROR = "listener_error";
110
111     /**
112      * Constant denoting - error while creating snapshot of config context
113      */

114     public static final String JavaDoc CONFIG_SNAPSHOT_ERROR = "config_snapshot_error";
115
116     /**
117      * Constant denoting - MBean not found.
118      */

119     public static final String JavaDoc MBEAN_NOT_FOUND = "mbean_not_found";
120
121     /**
122      * Constant denoting - MBean attribute not found. This is used to denote
123      * invalid attribute name for Monitoring GET command.
124      */

125     public static final String JavaDoc MBEAN_ATTR_NOT_FOUND = "mbean_attr_not_found";
126
127     /**
128      * Constant denoting - successful event processing
129      */

130     public static final String JavaDoc SUCCESS = "success";
131
132     /**
133      * Constant denoting - some events failed and some events passsed
134      */

135     public static final String JavaDoc MIXED_RESULT = "mixed_result";
136
137     /**
138      * Constant denoting - that event notification was not successful,
139      * resultCodes needs to checked for individual status
140      */

141     public static final String JavaDoc ERROR = "Event did not reach any recipient";
142
143     private static HashMap JavaDoc resultList = new HashMap JavaDoc();
144
145     private long eventId;
146     private String JavaDoc resultCode;
147
148     private HashMap JavaDoc resultCodes = new HashMap JavaDoc();
149     private HashMap JavaDoc allMessages = new HashMap JavaDoc();
150     private HashMap JavaDoc allExceptions = new HashMap JavaDoc();
151     private HashMap JavaDoc allAttributes = new HashMap JavaDoc();
152
153     // Set this, when an AdminEventListenerException is added to this result
154
private AdminEventListenerException firstAle =null;
155
156     // Set this, when an Throwable is added to this result
157
private Throwable JavaDoc firstThr =null;
158
159     // i18n StringManager
160
private static StringManager localStrings =
161         StringManager.getManager( AdminEventResult.class );
162
163     /**
164      * Create a new admin event result for specified event id.
165      */

166     // FIX - Review whether public constructor is required, there is a
167
// package factory method.
168
public AdminEventResult(long eventId) {
169         this.eventId = eventId;
170         resultCode = SUCCESS;
171     }
172
173     /**
174      * Get event id
175      */

176     public long getEventId() {
177         return eventId;
178     }
179
180     /**
181      * Get result code
182      */

183     public String JavaDoc getResultCode() {
184         return resultCode;
185     }
186
187     /**
188      * Get result codes, used in clustered environment
189      */

190      public HashMap JavaDoc getResultCodes() {
191         return resultCodes;
192      }
193
194     /**
195      * Get all event messages, used in clustered environment
196      */

197      public HashMap JavaDoc getMessages() {
198         return allMessages;
199      }
200
201     /**
202      * Get all event messages for an event sent to a target
203      */

204      public Collection JavaDoc getMessages(String JavaDoc target) {
205         return (Collection JavaDoc)allMessages.get(target);
206      }
207
208     /**
209      * Get all event exceptions occured during the event delivery.
210      * This method should be used in clustered environment
211      */

212      public HashMap JavaDoc getExceptions() {
213         return allExceptions;
214      }
215
216     /**
217      * Get all event exceptions occured during the event delivery.
218      * This method should be used in clustered environment
219      */

220      public Collection JavaDoc getExceptions(String JavaDoc target) {
221         return (Collection JavaDoc)allExceptions.get(target);
222      }
223
224     /**
225     /**
226      * Set result code to specified value
227      * WARNING: This should be used for setting indiviual instance
228      * result code.
229      */

230     public void setResultCode(String JavaDoc resultCode) {
231         if (!RESTART_NEEDED.equals(this.resultCode)) {
232             this.resultCode = resultCode;
233         }
234     }
235
236     /**
237      * Return all the result codes as a String. Note that the Returned
238      * String can be really big. Returns a non null empty string if there
239      * are no messages. Never returns a null.
240      */

241     public String JavaDoc getAllResultCodesAsString() {
242         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
243         // Process messages array list
244
Set JavaDoc resultCodeSet = resultCodes.entrySet();
245
246         Iterator JavaDoc iter = resultCodeSet.iterator();
247
248         while (( iter != null) && (iter.hasNext())) {
249             Entry nextMapEntry = (Entry) iter.next();
250             sb.append(localStrings.getString("admin.event.target_string"));
251             sb.append(nextMapEntry.getKey());
252             sb.append(" ");
253             sb.append(localStrings.getString("admin.event.result_code_string"));
254             sb.append(nextMapEntry.getValue());
255             sb.append(" ");
256         }
257         return (sb.toString());
258     }
259
260     /**
261      * Return all the messages as a String. Note that the Returned
262      * String can be really big. Returns a non null empty string if there
263      * are no messages. Never returns a null.
264      */

265     public String JavaDoc getAllMessagesAsString() {
266         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
267         // Process messages array list
268
Set JavaDoc messageSet = allMessages.entrySet();
269
270         Iterator JavaDoc iter = messageSet.iterator();
271
272         while (( iter != null) && (iter.hasNext())) {
273             Entry nextMapEntry = (Entry) iter.next();
274             sb.append(localStrings.getString("admin.event.target_string"));
275             sb.append(nextMapEntry.getKey());
276             sb.append(" ");
277             Collection JavaDoc msgCol = (Collection JavaDoc) nextMapEntry.getValue();
278             Iterator JavaDoc msgs = null;
279             if ( msgCol != null) {
280                 msgs = msgCol.iterator();
281             }
282             int msgCount = 0;
283             if (msgs != null) {
284                 while (msgs.hasNext()) {
285                     msgCount++;
286                     sb.append(localStrings.getString(
287                        "admin.event.msg_string", new Integer JavaDoc(msgCount).toString()));
288                     String JavaDoc msg = (String JavaDoc) msgs.next();
289                     sb.append(msg);
290                     sb.append(" ");
291                 }
292             }
293         }
294         // Process exceptions array list
295
Set JavaDoc exceptionSet = allExceptions.entrySet();
296
297         iter = exceptionSet.iterator();
298         while ( (iter != null) && (iter.hasNext())) {
299             Entry nextMapEntry = (Entry) iter.next();
300
301             Collection JavaDoc excsCol = (Collection JavaDoc) nextMapEntry.getValue();
302             Iterator JavaDoc excs = null;
303             if ( excsCol != null) {
304                 excs = excsCol.iterator();
305             }
306
307             if (excs !=null) {
308                 sb.append(localStrings.getString("admin.event.target_string"));
309                 sb.append(nextMapEntry.getKey());
310                 sb.append(" ");
311                 int excCount = 0;
312                 while ( excs.hasNext()) {
313                     Throwable JavaDoc tt = (Throwable JavaDoc) excs.next();
314                     excCount++;
315                     if (tt != null) {
316                         sb.append(localStrings.getString(
317                            "admin.event.exp_string",
318                            new Integer JavaDoc(excCount).toString()));
319                         String JavaDoc nextStr = tt.getMessage();
320                         sb.append(nextStr);
321                         sb.append(System.getProperty("line.separator"));
322                         StringWriter JavaDoc sw = new StringWriter JavaDoc();
323                         tt.printStackTrace(new PrintWriter JavaDoc(sw));
324                         sb.append(sw.toString());
325                         sb.append(System.getProperty("line.separator"));
326                     }
327                 }
328             }
329         }
330         return (sb.toString());
331     }
332
333     /**
334      * Add another message
335      */

336     public void addMessage(String JavaDoc target, String JavaDoc message) {
337         Collection JavaDoc msgs = (Collection JavaDoc)allMessages.get(target);
338         if ( msgs == null) {
339             msgs = new ArrayList JavaDoc();
340             allMessages.put(target, msgs);
341         }
342         msgs.add(message);
343     }
344
345     /**
346      * Add an exception to the result.
347      * @param tt the exception to add.
348      */

349     public void addException(String JavaDoc target, Throwable JavaDoc tt) {
350         Collection JavaDoc excs = (Collection JavaDoc)allExceptions.get(target);
351         if (excs == null) {
352             excs = new ArrayList JavaDoc();
353             allExceptions.put(target, excs);
354         }
355         excs.add(tt);
356         if ((firstAle == null) && (tt instanceof AdminEventListenerException)) {
357             firstAle = (AdminEventListenerException)tt;
358         }
359         if (firstThr == null) {
360             firstThr = tt;
361         }
362     }
363
364
365     /**
366      * This method returns the first exception added to AdminEventResult
367      * on behalf of any target.
368      *
369      * @return AdminEventListenerException It is the first added
370      * AdminEventListenerException to this
371      * AdminEventResult.
372      */

373     public AdminEventListenerException getFirstAdminEventListenerException() {
374         return firstAle;
375     }
376
377     /**
378      * This method returns the first throwbale added to AdminEventResult
379      * on behalf of any target.
380      *
381      * @return Throwable It is the first added Throwable
382      * to this AdminEventResult.
383      */

384     public Throwable JavaDoc getFirstThrowable() {
385         return firstThr;
386     }
387
388     /**
389      * Add specified attribute. This method can be used to add any
390      * Serializable object to event result. The method throws
391      * IllegalArgumentException, if name is null or the value is not
392      * Serializable.
393      * @param target name of the target for this event
394      * @param name name of the attribute
395      * @param value value of the specified attribute
396      * @throws IllegalArgumentException if name is null or value is not
397      * Serializable.
398      */

399     void addAttribute(String JavaDoc target, String JavaDoc name, Object JavaDoc value) {
400         attributeCheck(name, value);
401         synchronized (allAttributes) {
402             HashMap JavaDoc attributes = (HashMap JavaDoc) allAttributes.get(target);
403             if ( attributes == null ) {
404                 attributes = new HashMap JavaDoc();
405                 allAttributes.put(target, attributes);
406             }
407             attributes.put(name, value);
408         }
409     }
410
411     void attributeCheck(String JavaDoc name, Object JavaDoc value) {
412         if (name == null) {
413             String JavaDoc msg = localStrings.getString( "admin.event.null_attribute_name" );
414             throw new IllegalArgumentException JavaDoc( msg );
415         }
416         if (value != null) {
417             if (!SERIALIZABLE.isInstance(value)) {
418                 String JavaDoc msg = localStrings.getString( "admin.event.value_not_serializable" );
419                 throw new IllegalArgumentException JavaDoc( msg );
420             }
421         }
422     }
423
424     /**
425      * Returns all the attributes for a target as a map
426      *
427      * @return attributes hash map
428      */

429     public HashMap JavaDoc getAttributes(String JavaDoc target) {
430         return (HashMap JavaDoc) allAttributes.get(target);
431     }
432
433     /**
434      * Get names of all attributes for a target.The returned Set is empty if
435      * there are no attributes associated to this event result,
436      * otherwise it contains String objects representing names of all attributes.
437      *
438      * @param target name of the target
439      *
440      * @return a set of all attribute names.
441      */

442     public Set JavaDoc getAttributeNames(String JavaDoc target) {
443         HashMap JavaDoc h = (HashMap JavaDoc) allAttributes.get(target);
444         if ( h!=null) {
445             return h.keySet();
446         } else {
447             return null;
448         }
449     }
450
451     /**
452      * Get value of attribute with specified name.
453      * @param name name of the attribute
454      * @throws IllegalArgumentException if name is null.
455      * @return Value of the specified attribute name, if it exists, null,
456      * otherwise. A null return value may also mean that a null value
457      * was associated with specified name.
458      */

459     public Object JavaDoc getAttribute(String JavaDoc target, String JavaDoc name) {
460         if (name == null) {
461             String JavaDoc msg = localStrings.getString( "admin.event.null_attribute_name" );
462             throw new IllegalArgumentException JavaDoc( msg );
463         }
464         HashMap JavaDoc attributes = (HashMap JavaDoc) allAttributes.get(target);
465         if (attributes != null) {
466             return attributes.get(name);
467         } else {
468             return null;
469         }
470     }
471
472     /**
473      * Remove specified attribute. This method can be used to remove any
474      * previously added attribute to event result. If name is null,
475      * IllegalArgumentException is thrown.
476      * @param target name of the target
477      * @param name name of the attribute
478      * @throws IllegalArgumentException if name is null.
479      */

480     void removeAttribute(String JavaDoc target, String JavaDoc name) {
481         if (name == null) {
482             String JavaDoc msg = localStrings.getString( "admin.event.null_attribute_name" );
483             throw new IllegalArgumentException JavaDoc( msg );
484         }
485         synchronized (allAttributes) {
486             HashMap JavaDoc attributes = (HashMap JavaDoc) allAttributes.get(target);
487             if ( attributes != null) {
488                 attributes.remove(name);
489             }
490         }
491     }
492
493     /**
494      * Get admin event result from the cache
495      */

496     public static AdminEventResult getAdminEventResult(AdminEvent event) {
497         AdminEventResult result = (AdminEventResult)resultList.get(event);
498         if (result == null) {
499             result = new AdminEventResult(event.getSequenceNumber());
500             resultList.put(event, result);
501         }
502         return result;
503     }
504
505     /**
506      * Remove specified event from cache
507      */

508     static void clearAdminEventResultFromCache(AdminEvent event) {
509         resultList.remove(event);
510     }
511
512     /**
513      * Merge another event result into the current EventResult
514     */

515     public void addEventResult(String JavaDoc target, AdminEventResult eventResult) {
516         if ( eventResult == null )
517             return;
518
519         this.allMessages.put( target,
520             eventResult.getMessages().get(target));
521         this.allExceptions.put( target,
522             eventResult.getExceptions().get(target));
523         this.allAttributes.put(target,
524             (HashMap JavaDoc)eventResult.getAttributes(target));
525
526         if ( resultCodes == null ) {
527             resultCodes = new HashMap JavaDoc();
528             resultCodes.put(target, eventResult.getResultCode());
529         }
530     }
531 }
532
533
Popular Tags