1 23 24 package com.sun.enterprise.admin.event; 25 26 import java.util.List ; 27 import java.util.Iterator ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.NoSuchMethodException ; 31 32 33 39 public class BaseAdminEventHandler { 40 41 44 public BaseAdminEventHandler(AdminEvent event) { 45 _event = event; 46 } 47 48 51 public int processEvent() { 52 List list = AdminEventMulticaster.getInstance().getListeners( 53 _event.getType()); 54 if (list != null && !list.isEmpty()) { 55 Iterator iter = list.iterator(); 56 while (iter.hasNext()) { 57 AdminEventListener listener = 58 (AdminEventListener)iter.next(); 59 invokeAdminEventListener(listener, _event); 60 } 61 } 62 return ((list != null) ? list.size() : 0); 63 } 64 65 private void invokeNotification(AdminEventListener ae, String methodName, 66 AdminEvent e) throws Exception 67 { 68 invokeNotification(ae, methodName, e, ((Object )e).getClass()); 69 } 70 71 private void invokeNotification(AdminEventListener ae, String methodName, 72 AdminEvent e, Class eventParamClass) throws Exception 73 { 74 Method meth = null; 75 meth = ae.getClass().getMethod( methodName, new Class []{eventParamClass}); 76 try { 77 meth.invoke(ae, new Object []{e}); 78 } catch (Exception ex) { 79 Throwable t = ex.getCause(); 80 if (t != null) { 81 throw (Exception )t; 82 } 83 else { 84 throw ex; 85 } 86 } 87 } 88 89 private void invokeAdminEventListener( 90 AdminEventListener listener, 91 AdminEvent e) { 92 93 AdminEventMulticaster aem = AdminEventMulticaster.getInstance(); 94 AdminEventResult result = AdminEventResult.getAdminEventResult(e); 95 96 try { 97 invokeNotification(listener, GENERIC_PROCESS_EVENT_METHOD, e); 98 return; 99 }catch (Throwable t) { 100 if( !(t instanceof NoSuchMethodException )) 101 aem.handleListenerError(e, t, result); 102 } 103 104 try { 105 invokeNotification(listener, GENERIC_PROCESS_EVENT_METHOD, e, AdminEvent.class); 106 return; 107 }catch (Throwable t) { 108 if( !(t instanceof NoSuchMethodException )) 109 aem.handleListenerError(e, t, result); 110 } 111 112 try { 113 ElementChangeEvent event = (ElementChangeEvent)e; 114 int code = event.getActionType(); 115 if ( code == ElementChangeEvent.ACTION_ELEMENT_CREATE ) { 116 invokeNotification(listener,ACTION_CREATE_METHOD, e); 117 } else if ( code == ElementChangeEvent.ACTION_ELEMENT_UPDATE) { 118 invokeNotification(listener,ACTION_UPDATE_METHOD, e); 119 } else if (code == ElementChangeEvent.ACTION_ELEMENT_DELETE) { 120 invokeNotification(listener,ACTION_DELETE_METHOD, e); 121 } else { 122 throw new RuntimeException ("not valid error code"); 123 } 124 } catch (Throwable t) { 125 aem.handleListenerError(e, t, result); 126 } 127 } 128 129 private AdminEvent _event; 130 131 private static String ACTION_UPDATE_METHOD = "handleUpdate"; 134 135 private static String ACTION_DELETE_METHOD = "handleDelete"; 138 139 private static String ACTION_CREATE_METHOD = "handleCreate"; 142 143 private static String GENERIC_PROCESS_EVENT_METHOD = "processEvent"; 145 } 146 | Popular Tags |