1 22 package org.jboss.management.mejb; 23 24 import org.jboss.logging.Logger; 25 26 import javax.ejb.CreateException ; 27 import javax.management.InstanceNotFoundException ; 28 import javax.management.ListenerNotFoundException ; 29 import javax.management.NotificationFilter ; 30 import javax.management.NotificationListener ; 31 import javax.management.ObjectName ; 32 import javax.management.j2ee.ManagementHome ; 33 import java.rmi.RemoteException ; 34 import java.security.InvalidParameterException ; 35 import java.util.ArrayList ; 36 import java.util.List ; 37 38 45 public class ListenerRegistration 46 implements javax.management.j2ee.ListenerRegistration 47 { 48 50 public static final int NOTIFICATION_TYPE_RMI = 0; 51 public static final int NOTIFICATION_TYPE_JMS = 1; 52 public static final int NOTIFICATION_TYPE_POLLING = 2; 53 54 56 private ManagementHome mHome; 57 private int mEventType = NOTIFICATION_TYPE_RMI; 58 private String [] mOptions; 59 private List mListeners = new ArrayList (); 60 61 63 private static final Logger log = Logger.getLogger(ListenerRegistration.class); 64 65 67 public ListenerRegistration(ManagementHome pHome, String [] pOptions) 68 { 69 if (pHome == null) 70 { 71 throw new InvalidParameterException ("Home Interface must be specified"); 72 } 73 mHome = pHome; 74 mOptions = pOptions; 75 } 76 77 79 81 public void addNotificationListener(ObjectName pName, 82 NotificationListener pListener, 83 NotificationFilter pFilter, 84 Object pHandback) 85 throws 86 InstanceNotFoundException , 87 RemoteException 88 { 89 MEJB lManagement = null; 90 try 92 { 93 lManagement = getMEJB(); 95 ClientNotificationListener lListener = null; 96 switch (mEventType) 97 { 98 case NOTIFICATION_TYPE_RMI: 99 lListener = new RMIClientNotificationListener(pName, 100 pListener, 101 pHandback, 102 pFilter, 103 lManagement); 104 break; 105 case NOTIFICATION_TYPE_JMS: 106 lListener = new JMSClientNotificationListener(pName, 107 pListener, 108 pHandback, 109 pFilter, 110 mOptions[0], 111 mOptions[1], lManagement); 113 break; 114 case NOTIFICATION_TYPE_POLLING: 115 lListener = new PollingClientNotificationListener(pName, 116 pListener, 117 pHandback, 118 pFilter, 119 5000, 2500, lManagement); 122 } 123 mListeners.add(lListener); 125 } 126 catch (Exception e) 127 { 128 if (e instanceof RuntimeException ) 129 { 130 throw (RuntimeException ) e; 131 } 132 if (e instanceof InstanceNotFoundException ) 133 { 134 throw (InstanceNotFoundException ) e; 135 } 136 throw new RuntimeException ("Remote access to perform this operation failed: " + e.getMessage()); 137 } 138 finally 139 { 140 if (lManagement != null) 141 { 142 try 143 { 144 lManagement.remove(); 145 } 146 catch (Exception e) 147 { 148 log.error("operation failed", e); 149 } 150 } 151 } 152 } 153 154 public void removeNotificationListener(ObjectName pName, 155 NotificationListener pListener) 156 throws 157 InstanceNotFoundException , 158 ListenerNotFoundException , 159 RemoteException 160 { 161 MEJB lManagement = null; 162 try 163 { 164 lManagement = getMEJB(); 166 167 ClientNotificationListener lCheck = new SearchClientNotificationListener(pName, pListener); 168 int i = mListeners.indexOf(lCheck); 169 if (i >= 0) 170 { 171 ClientNotificationListener lListener = (ClientNotificationListener) mListeners.get(i); 172 lListener.removeNotificationListener(lManagement); 173 } 174 } 175 catch (Exception e) 176 { 177 if (e instanceof RuntimeException ) 178 { 179 throw (RuntimeException ) e; 180 } 181 if (e instanceof InstanceNotFoundException ) 182 { 183 throw (InstanceNotFoundException ) e; 184 } 185 throw new RuntimeException ("Remote access to perform this operation failed: " + e.getMessage()); 186 } 187 finally 188 { 189 if (lManagement != null) 190 { 191 try 192 { 193 lManagement.remove(); 194 } 195 catch (Exception e) 196 { 197 log.error("operation failed", e); 198 } 199 } 200 } 201 } 202 203 205 207 209 211 private MEJB getMEJB() 212 throws 213 CreateException , 214 RemoteException 215 { 216 Object lTemp = mHome.create(); 217 MEJB lReturn = (MEJB) lTemp; 218 return lReturn; 219 } 220 221 } 223 | Popular Tags |