1 23 package com.sun.enterprise.admin.alert; 24 25 import javax.management.NotificationListener ; 26 import javax.management.NotificationFilter ; 27 import javax.management.MBeanServer ; 28 import javax.management.ObjectName ; 29 30 import java.lang.reflect.Method ; 31 import java.lang.reflect.Field ; 32 import java.security.AccessController ; 33 import java.security.PrivilegedAction ; 34 import java.util.List ; 35 import java.util.ArrayList ; 36 import java.util.StringTokenizer ; 37 import java.util.logging.ErrorManager ; 38 import java.util.logging.Logger ; 39 import java.util.logging.Level ; 40 41 import com.sun.enterprise.admin.server.core.AdminService; 42 import com.sun.enterprise.server.ServerContext; 43 import com.sun.enterprise.server.ApplicationServer; 44 import com.sun.enterprise.config.serverbeans.ElementProperty; 45 import com.sun.enterprise.config.serverbeans.AlertService; 46 import com.sun.enterprise.config.serverbeans.AlertSubscription; 47 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 48 import com.sun.enterprise.config.serverbeans.ListenerConfig; 49 import com.sun.enterprise.config.serverbeans.FilterConfig; 50 51 import com.sun.enterprise.server.logging.LogMBean; 52 import com.sun.appserv.management.alert.LogDomains; 53 54 55 62 public class AlertConfigurator { 63 64 private static final String MBEAN_SERVER_DELEGATE_OBJECT_NAME = 66 "JMImplementation:type=MBeanServerDelegate"; 67 68 private static final String DEFAULT_FILTER_CLASS_NAME = 70 "com.sun.appserv.management.alert.MailFilter"; 71 72 private static final String LOG_MBEAN_NAME = "LogMBean"; 74 75 private static final AlertConfigurator instance = new AlertConfigurator( ); 76 77 80 public static AlertConfigurator getAlertConfigurator( ) { 81 return instance; 82 } 83 84 private AlertConfigurator( ) { } 85 86 89 private AlertService getAlertService( ) { 90 try { 91 ServerContext sc = ApplicationServer.getServerContext(); 92 if( sc == null ) { 93 return null; 94 } 95 return ServerBeansFactory.getConfigBean( 96 sc.getConfigContext()).getAlertService( ); 97 } catch( Exception e ) { 98 new ErrorManager ().error( "Error In getAlertService ", e, 99 ErrorManager.GENERIC_FAILURE ); 100 } 101 return null; 102 } 103 104 111 public void configure( ) { 112 Logger logger = LogDomains.getAlertLogger( ); 113 if( logger.isLoggable( Level.FINE ) ) { 114 logger.log( Level.FINE, "AlertConfigurator.configure called.." ); 115 } 116 AlertService alertService = getAlertService( ); 117 if( alertService != null ) { 118 int count = alertService.sizeAlertSubscription( ); 119 if( count == 0 ) return; 120 121 List alertSubscriptionList = new ArrayList ( ); 122 for( int i = 0; i < count; i++ ) { 123 AlertSubscription subscription = 124 alertService.getAlertSubscription( i ); 125 NotificationListener listener = configureNotificationListener( 126 subscription.getListenerConfig( ) ); 127 NotificationFilter filter = configureNotificationFilter( 128 subscription.getFilterConfig( ) ); 129 alertSubscriptionList.add( new AlertSubscriptionInfo( 130 subscription.getListenerConfig().getSubscribeListenerWith( ), 131 listener, filter )); 132 String monitorNames = subscription.getListenerConfig( 133 ).getSubscribeListenerWith( ); 134 if( logger.isLoggable( Level.FINE ) ) { 135 logger.log( Level.FINE, 136 "AlertConfigurator.configure monitorNames.." + 137 monitorNames ); 138 } 139 StringTokenizer tokenizer = new StringTokenizer ( monitorNames, 140 "," ); 141 while( tokenizer.hasMoreTokens( ) ) { 146 String token = tokenizer.nextToken().trim(); 147 if( token.equals( LOG_MBEAN_NAME ) ) { 148 LogMBean.getInstance( ).addNotificationListener( 149 listener, filter, null ); 150 } 151 } 152 } 153 MBeanRegistrationEventListener registrationListener = 159 new MBeanRegistrationEventListener( alertSubscriptionList ); 160 readyForMBeanRegistrationEvent( registrationListener ); 161 } 162 } 163 164 168 private NotificationListener configureNotificationListener( 169 ListenerConfig listenerConfig ) 170 { 171 Logger alertLogger = LogDomains.getAlertLogger( ); 172 if( alertLogger.isLoggable( Level.FINE ) ) { 173 alertLogger.log( Level.FINE, 174 "ConfigureNotificationListener called with className ..." + 175 listenerConfig.getListenerClassName( ) ); 176 } 177 NotificationListener listener = null; 178 try { 179 listener = (NotificationListener ) instantiateAndConfigure( 180 listenerConfig.getListenerClassName( ), 181 listenerConfig.getElementProperty( ) ); 182 } catch( Exception e ) { 183 new ErrorManager ().error( 184 "Error In Notification Listener Config ", e, 185 ErrorManager.GENERIC_FAILURE ); 186 } 187 return listener; 188 } 189 190 193 private NotificationFilter configureNotificationFilter( 194 FilterConfig filterConfig ) 195 { 196 String filterClassName = DEFAULT_FILTER_CLASS_NAME; 197 ElementProperty[] properties = null; 198 if( filterConfig != null ) { 199 filterClassName = filterConfig.getFilterClassName(); 200 properties = filterConfig.getElementProperty(); 201 } 202 NotificationFilter filter = null; 203 try { 204 filter = (NotificationFilter ) instantiateAndConfigure( 205 filterClassName, properties ); 206 } catch( Exception e ) { 207 new ErrorManager ().error( 208 "Error In Notification Filter Config ", e, 209 ErrorManager.GENERIC_FAILURE ); 210 } 211 return filter; 212 } 213 214 215 218 private void setProperties( Object o, ElementProperty[] properties ) { 219 if( properties == null ) return; 220 Method [] methods = null; 221 try { 222 methods = getDeclaredMethods( o.getClass( ) ); 223 for( int i = 0; i < properties.length; i++ ) { 224 ElementProperty property = properties[i]; 225 String propertyName = property.getName( ).toLowerCase( ); 226 String propertyValue = property.getValue( ); 227 for( int j = 0; j < methods.length; j++ ) { 228 String methodName = methods[j].getName().toLowerCase(); 229 if ( ( methodName.startsWith( "set" ) ) 230 && ( methodName.endsWith( propertyName ) ) ) 231 { 232 Class [] parameterTypes = methods[j].getParameterTypes( ); 233 if( parameterTypes.length != 1 ) { 234 new ErrorManager ().error( 235 "Only one Parameter is allowed for the setter " + 236 " Method: " + methodName + 237 " has invalid signature", new Exception (), 238 ErrorManager.GENERIC_FAILURE ); 239 } 240 241 String parameterType = parameterTypes[0].getName(); 242 Object [] parameters = new Object [1]; 243 244 if( parameterType.equals( "java.lang.String") ) { 245 parameters[0] = propertyValue; 246 } else if( parameterType.equals( "byte" ) ) { 247 parameters[0] = 248 new Byte ( propertyValue.getBytes()[0]); 249 } else if( parameterType.equals( "int" ) ) { 250 parameters[0] = new Integer (propertyValue); 251 } else if( parameterType.equals( "float" ) ) { 252 parameters[0] = new Float (propertyValue); 253 } else if( parameterType.equals( "double") ) { 254 parameters[0] = new Double (propertyValue); 255 } else if( parameterType.equals( "char" ) ) { 256 parameters[0] = 257 new Character (propertyValue.charAt(0)); 258 } else if( parameterType.equals("boolean") ) { 259 parameters[0] = new Boolean (propertyValue); 260 } else if( parameterType.equals("long") ) { 261 parameters[0] = new Long (propertyValue); 262 } else if( parameterType.equals("short") ) { 263 parameters[0] = new Short (propertyValue); 264 } else { 265 new ErrorManager ().error( 266 "Only the basic primitive types can be set " + 267 "as properties to NotificationListener and " + 268 " NotificationFilter ", new Exception (), 269 ErrorManager.GENERIC_FAILURE ); 270 continue; 271 } 272 methods[j].invoke( o, parameters ); 273 } 274 } 275 } 276 } catch( Exception e ) { 277 new ErrorManager ().error( 278 "Error While Setting properties to Notification Listener or " + 279 " Filter ", e, ErrorManager.GENERIC_FAILURE ); 280 } 281 } 282 283 284 287 private Object instantiateAndConfigure( String className, 288 ElementProperty[] properties ) 289 { 290 Logger alertLogger = LogDomains.getAlertLogger(); 291 if( alertLogger.isLoggable( Level.FINE ) ) { 292 alertLogger.log( Level.FINE, 293 "instantiateAndConfigure called with className.." + 294 className ); 295 } 296 Object o = getInstance( className ); 297 if( ( o != null ) 298 &&( properties != null ) ) 299 { 300 if( alertLogger.isLoggable( Level.FINE ) ) { 301 alertLogger.log( Level.FINE, 302 "instantiateAndConfigure setting Properties.." ); 303 } 304 setProperties( o, properties ); 305 } 306 return o; 307 } 308 309 314 private Object getInstance( final String className ) { 315 if( className == null ) return null; 316 return AccessController.doPrivileged( new PrivilegedAction () { 317 public Object run() { 318 try { 319 ClassLoader cl = 320 Thread.currentThread().getContextClassLoader(); 321 if (cl == null) 322 cl = ClassLoader.getSystemClassLoader(); 323 return Class.forName( className, true, cl).newInstance(); } catch( Exception e ) { 324 new ErrorManager ().error( 325 "Error In Instantiating Class " + className, e, 326 ErrorManager.GENERIC_FAILURE ); 327 } 328 return null; 329 } 330 } 331 ); 332 } 333 334 337 private final Method [] getDeclaredMethods(final Class clz) { 338 return (Method []) AccessController.doPrivileged(new PrivilegedAction () { public Object run() { 339 return clz.getDeclaredMethods(); 340 } 341 }); 342 } 343 344 345 348 private void readyForMBeanRegistrationEvent( 349 NotificationListener registrationListener ) 350 { 351 try { 352 MBeanServer mbeanServer = 353 AdminService.getAdminService().getAdminContext( 354 ).getMBeanServer(); 355 mbeanServer.addNotificationListener( 356 new ObjectName ( MBEAN_SERVER_DELEGATE_OBJECT_NAME ), 357 registrationListener, (NotificationFilter ) null,(Object ) null ); 358 } catch( Exception e ) { 359 new ErrorManager ().error( 360 "Error In registerning MBeanServerNotificationListener ", e, 361 ErrorManager.GENERIC_FAILURE ); 362 } 363 } 364 } 365 | Popular Tags |