1 23 24 31 package com.sun.enterprise.admin.event; 32 33 import java.io.PrintWriter ; 34 import java.io.Serializable ; 35 import java.io.StringWriter ; 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Map.Entry; 39 import java.util.Iterator ; 40 import java.util.Set ; 41 import java.util.Collection ; 42 import com.sun.enterprise.admin.common.AdminResponse; 43 44 import com.sun.enterprise.util.i18n.StringManager; 46 47 79 public class AdminEventResult implements Serializable { 80 81 84 private static final Class SERIALIZABLE = Serializable .class; 85 86 89 public static final String RESTART_NEEDED = "restart"; 90 91 94 public static final String RUNTIME_EXCEPTION = "runtime_exception"; 95 96 99 public static final String RUNTIME_ERROR = "runtime_error"; 100 101 104 public static final String TRANSMISSION_ERROR = "transmission_error"; 105 106 109 public static final String LISTENER_ERROR = "listener_error"; 110 111 114 public static final String CONFIG_SNAPSHOT_ERROR = "config_snapshot_error"; 115 116 119 public static final String MBEAN_NOT_FOUND = "mbean_not_found"; 120 121 125 public static final String MBEAN_ATTR_NOT_FOUND = "mbean_attr_not_found"; 126 127 130 public static final String SUCCESS = "success"; 131 132 135 public static final String MIXED_RESULT = "mixed_result"; 136 137 141 public static final String ERROR = "Event did not reach any recipient"; 142 143 private static HashMap resultList = new HashMap (); 144 145 private long eventId; 146 private String resultCode; 147 148 private HashMap resultCodes = new HashMap (); 149 private HashMap allMessages = new HashMap (); 150 private HashMap allExceptions = new HashMap (); 151 private HashMap allAttributes = new HashMap (); 152 153 private AdminEventListenerException firstAle =null; 155 156 private Throwable firstThr =null; 158 159 private static StringManager localStrings = 161 StringManager.getManager( AdminEventResult.class ); 162 163 166 public AdminEventResult(long eventId) { 169 this.eventId = eventId; 170 resultCode = SUCCESS; 171 } 172 173 176 public long getEventId() { 177 return eventId; 178 } 179 180 183 public String getResultCode() { 184 return resultCode; 185 } 186 187 190 public HashMap getResultCodes() { 191 return resultCodes; 192 } 193 194 197 public HashMap getMessages() { 198 return allMessages; 199 } 200 201 204 public Collection getMessages(String target) { 205 return (Collection )allMessages.get(target); 206 } 207 208 212 public HashMap getExceptions() { 213 return allExceptions; 214 } 215 216 220 public Collection getExceptions(String target) { 221 return (Collection )allExceptions.get(target); 222 } 223 224 230 public void setResultCode(String resultCode) { 231 if (!RESTART_NEEDED.equals(this.resultCode)) { 232 this.resultCode = resultCode; 233 } 234 } 235 236 241 public String getAllResultCodesAsString() { 242 StringBuffer sb = new StringBuffer (); 243 Set resultCodeSet = resultCodes.entrySet(); 245 246 Iterator 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 265 public String getAllMessagesAsString() { 266 StringBuffer sb = new StringBuffer (); 267 Set messageSet = allMessages.entrySet(); 269 270 Iterator 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 msgCol = (Collection ) nextMapEntry.getValue(); 278 Iterator 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 (msgCount).toString())); 288 String msg = (String ) msgs.next(); 289 sb.append(msg); 290 sb.append(" "); 291 } 292 } 293 } 294 Set exceptionSet = allExceptions.entrySet(); 296 297 iter = exceptionSet.iterator(); 298 while ( (iter != null) && (iter.hasNext())) { 299 Entry nextMapEntry = (Entry) iter.next(); 300 301 Collection excsCol = (Collection ) nextMapEntry.getValue(); 302 Iterator 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 tt = (Throwable ) excs.next(); 314 excCount++; 315 if (tt != null) { 316 sb.append(localStrings.getString( 317 "admin.event.exp_string", 318 new Integer (excCount).toString())); 319 String nextStr = tt.getMessage(); 320 sb.append(nextStr); 321 sb.append(System.getProperty("line.separator")); 322 StringWriter sw = new StringWriter (); 323 tt.printStackTrace(new PrintWriter (sw)); 324 sb.append(sw.toString()); 325 sb.append(System.getProperty("line.separator")); 326 } 327 } 328 } 329 } 330 return (sb.toString()); 331 } 332 333 336 public void addMessage(String target, String message) { 337 Collection msgs = (Collection )allMessages.get(target); 338 if ( msgs == null) { 339 msgs = new ArrayList (); 340 allMessages.put(target, msgs); 341 } 342 msgs.add(message); 343 } 344 345 349 public void addException(String target, Throwable tt) { 350 Collection excs = (Collection )allExceptions.get(target); 351 if (excs == null) { 352 excs = new ArrayList (); 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 373 public AdminEventListenerException getFirstAdminEventListenerException() { 374 return firstAle; 375 } 376 377 384 public Throwable getFirstThrowable() { 385 return firstThr; 386 } 387 388 399 void addAttribute(String target, String name, Object value) { 400 attributeCheck(name, value); 401 synchronized (allAttributes) { 402 HashMap attributes = (HashMap ) allAttributes.get(target); 403 if ( attributes == null ) { 404 attributes = new HashMap (); 405 allAttributes.put(target, attributes); 406 } 407 attributes.put(name, value); 408 } 409 } 410 411 void attributeCheck(String name, Object value) { 412 if (name == null) { 413 String msg = localStrings.getString( "admin.event.null_attribute_name" ); 414 throw new IllegalArgumentException ( msg ); 415 } 416 if (value != null) { 417 if (!SERIALIZABLE.isInstance(value)) { 418 String msg = localStrings.getString( "admin.event.value_not_serializable" ); 419 throw new IllegalArgumentException ( msg ); 420 } 421 } 422 } 423 424 429 public HashMap getAttributes(String target) { 430 return (HashMap ) allAttributes.get(target); 431 } 432 433 442 public Set getAttributeNames(String target) { 443 HashMap h = (HashMap ) allAttributes.get(target); 444 if ( h!=null) { 445 return h.keySet(); 446 } else { 447 return null; 448 } 449 } 450 451 459 public Object getAttribute(String target, String name) { 460 if (name == null) { 461 String msg = localStrings.getString( "admin.event.null_attribute_name" ); 462 throw new IllegalArgumentException ( msg ); 463 } 464 HashMap attributes = (HashMap ) allAttributes.get(target); 465 if (attributes != null) { 466 return attributes.get(name); 467 } else { 468 return null; 469 } 470 } 471 472 480 void removeAttribute(String target, String name) { 481 if (name == null) { 482 String msg = localStrings.getString( "admin.event.null_attribute_name" ); 483 throw new IllegalArgumentException ( msg ); 484 } 485 synchronized (allAttributes) { 486 HashMap attributes = (HashMap ) allAttributes.get(target); 487 if ( attributes != null) { 488 attributes.remove(name); 489 } 490 } 491 } 492 493 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 508 static void clearAdminEventResultFromCache(AdminEvent event) { 509 resultList.remove(event); 510 } 511 512 515 public void addEventResult(String 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 )eventResult.getAttributes(target)); 525 526 if ( resultCodes == null ) { 527 resultCodes = new HashMap (); 528 resultCodes.put(target, eventResult.getResultCode()); 529 } 530 } 531 } 532 533 | Popular Tags |