KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.admin.event;
25
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.NoSuchMethodException JavaDoc;
31
32
33 /**
34  * This class calls appropriates listener method for the event with a specific
35  * action code
36  *
37  * @author Satish Viswanatham
38  */

39  public class BaseAdminEventHandler {
40
41     /**
42      * Constructor
43      */

44      public BaseAdminEventHandler(AdminEvent event) {
45         _event = event;
46      }
47
48      /**
49       * This method processes the event.
50       */

51       public int processEvent() {
52         List JavaDoc list = AdminEventMulticaster.getInstance().getListeners(
53                             _event.getType());
54                 if (list != null && !list.isEmpty()) {
55             Iterator JavaDoc 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 JavaDoc methodName,
66                           AdminEvent e) throws Exception JavaDoc
67     {
68         invokeNotification(ae, methodName, e, ((Object JavaDoc)e).getClass());
69     }
70     
71     private void invokeNotification(AdminEventListener ae, String JavaDoc methodName,
72                           AdminEvent e, Class JavaDoc eventParamClass) throws Exception JavaDoc
73     {
74         Method JavaDoc meth = null;
75         meth = ae.getClass().getMethod( methodName, new Class JavaDoc[]{eventParamClass});
76         try {
77             meth.invoke(ae, new Object JavaDoc[]{e});
78         } catch (Exception JavaDoc ex) {
79             Throwable JavaDoc t = ex.getCause();
80             if (t != null) {
81                 throw (Exception JavaDoc)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 JavaDoc t) {
100             if( !(t instanceof NoSuchMethodException JavaDoc))
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 JavaDoc t) {
108             if( !(t instanceof NoSuchMethodException JavaDoc))
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 JavaDoc("not valid error code");
123             }
124         } catch (Throwable JavaDoc t) {
125             aem.handleListenerError(e, t, result);
126         }
127     }
128
129     private AdminEvent _event;
130
131     // Method to be called in the listener for action of type
132
// ACTION_UPDATE_METHOD
133
private static String JavaDoc ACTION_UPDATE_METHOD = "handleUpdate";
134
135     // Method to be called in the listener for action of type
136
// ACTION_DELTE_METHOD
137
private static String JavaDoc ACTION_DELETE_METHOD = "handleDelete";
138
139     // Method to be called in the listener for action of type
140
// ACTION_CREATE_METHOD
141
private static String JavaDoc ACTION_CREATE_METHOD = "handleCreate";
142
143     // Generic method to be called in the listener
144
private static String JavaDoc GENERIC_PROCESS_EVENT_METHOD = "processEvent";
145  }
146
Popular Tags