1 23 24 package com.sun.enterprise.admin.server.core; 25 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import java.util.Properties ; 32 import java.util.Enumeration ; 33 34 import javax.management.ObjectName ; 35 import javax.management.Attribute ; 36 37 import com.sun.enterprise.admin.AdminContext; 38 import com.sun.enterprise.admin.event.AdminEvent; 39 import com.sun.enterprise.admin.event.AdminEventCache; 40 import com.sun.enterprise.admin.event.AdminEventMulticaster; 41 import com.sun.enterprise.admin.event.AdminEventResult; 42 import com.sun.enterprise.admin.event.ElementChangeHelper; 43 import com.sun.enterprise.admin.util.proxy.CallStack; 44 import com.sun.enterprise.admin.util.proxy.Call; 45 import com.sun.enterprise.admin.util.proxy.InterceptorImpl; 46 import com.sun.enterprise.admin.event.EventStack; 47 import com.sun.enterprise.admin.event.EventContext; 48 import com.sun.enterprise.config.ConfigContext; 49 import com.sun.enterprise.config.ConfigException; 50 import com.sun.enterprise.admin.event.ResourceDeployEvent; 51 import com.sun.enterprise.admin.event.BaseDeployEvent; 52 import com.sun.enterprise.security.SecurityContext; 53 import com.sun.enterprise.security.audit.AuditManager; 54 import com.sun.enterprise.security.audit.AuditManagerFactory; 55 56 import com.sun.enterprise.admin.event.pluggable.RestartEventHelper; 57 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory; 58 import com.sun.enterprise.server.ApplicationServer; 59 import com.sun.enterprise.server.ServerContext; 60 import com.sun.enterprise.admin.event.pluggable.NotificationFactory; 61 62 63 66 public class ConfigInterceptor extends InterceptorImpl { 67 68 private AdminContext adminContext; 69 private Logger _logger; 70 private static AuditManager auditManager = 71 AuditManagerFactory.getAuditManagerInstance(); 72 73 public ConfigInterceptor() { 74 } 75 76 public ConfigInterceptor(AdminContext ctx) { 77 adminContext = ctx; 78 if (adminContext != null) { 79 _logger = adminContext.getAdminLogger(); 80 } else { 81 _logger = Logger.getLogger("global"); 82 } 83 } 84 85 92 public void postInvoke(CallStack callStack) { 93 94 95 if (callStack.getStackSize() == 1) { 96 Call call = callStack.getActiveCall(); 97 if (call.getState().isSuccess() && isFlushNeeded(call)) { 98 try { 99 ConfigContext ctx = adminContext.getAdminConfigContext(); 100 if (ctx.isChanged()) { 101 ctx.flush(); 102 } 103 } catch (ConfigException ce) { 104 _logger.log(Level.WARNING, 105 "core.config_exception_occurred", ce); 106 } 107 } 108 if (isFlushNeeded(call)) { 109 new AdminNotificationHelper(adminContext).sendNotification(); 110 } 111 } 112 auditInvocation(callStack.getActiveCall()); 113 114 115 } 116 117 121 private boolean isFlushNeeded(Call call) { 122 String methodName = call.getMethod().getName(); 123 if( methodName.equals("setAttribute") || 124 methodName.equals("setAttributes") || 125 (methodName.equals("invoke") && !isReadonlyInvokeOperation(call))) { 126 return true; 127 } 128 return false; 129 } 130 131 private boolean isReadonlyInvokeOperation(Call call) { 132 133 try { 134 Object [] args = call.getArguments(); 135 if(args!=null && args.length>1) 136 { 137 String operationName = (String ) (args[1]); 138 139 if("takeNotifications".equals(operationName)) { 141 return true; 142 } 143 } 144 } catch(Throwable t){ } 145 146 return false; 147 } 148 149 public void preInvoke(CallStack callstack) { 150 151 if (callstack.getStackSize() == 1) { 152 EventStack stack = new EventStack(); 153 stack.setConfigContext(adminContext.getAdminConfigContext()); 154 EventContext.setEventStackToThreadLocal(stack); 155 } 156 157 } 158 159 238 239 240 244 private void auditInvocation(Call call) 245 { 246 if(auditManager.isAuditOn()) 247 { 248 if ( isAuditable(call)){ 249 SecurityContext sc = SecurityContext.getCurrent(); 250 262 272 } 273 } 274 } 275 279 private boolean isAuditable(Call call) { 280 if(call==null) 281 return false; 282 String methodName = call.getMethod().getName(); 283 if( !methodName.equals("setAttribute") && 284 !methodName.equals("setAttributes") && 285 !methodName.equals("invoke")) 286 return false; 287 if(methodName.equals("invoke")) 288 { 289 String opName = getOperationName(call); 290 if(opName==null || 291 opName.startsWith("get") || 292 opName.startsWith("list") || 293 opName.startsWith("is") ) 294 return false; 295 } 296 String objectName = getObjectNameForCall(call); 297 if(objectName==null || objectName.indexOf("category=config")<0) 298 return false; 299 return true; 300 } 301 302 303 306 private String getOperationName(Call call) { 307 if(call==null) 308 return null; 309 String methodName = call.getMethod().getName(); 310 if(methodName.equals("invoke")) 311 { 312 Object [] params = call.getArguments(); 313 if(params!=null && params.length>1 && (params[1] instanceof String )) 314 methodName = (String )params[1]; 315 } 316 return methodName; 317 } 318 319 322 private String getAuditedParamsString(Call call) { 323 if(call==null) 324 return null; 325 Object [] params = call.getArguments(); 326 if(params==null) 327 return null; 328 int i=0; 329 String methodName = call.getMethod().getName(); 330 if(methodName.equals("invoke")) 331 { 332 if(params.length>2) 333 return paramToString(params[2]); 334 } 335 else 336 if(methodName.equals("setAttribute") || 337 methodName.equals("setAttributes") ) 338 { 339 if(params.length>1) 340 return paramToString(params[1]); 341 } 342 return null; 343 } 344 345 348 private String paramToString(Object obj) { 349 if(obj==null) 350 return null; 351 String res = null; 352 if(obj instanceof Object []) 353 { 354 res = "{"; 355 for(int i=0; i<((Object [])obj).length; i++) 356 res += paramToString(((Object [])obj)[i]) + ";"; 357 res += "}"; 358 } 359 else if(obj instanceof ArrayList ) 360 { 361 res = "{"; 362 for(int i=0; i<((ArrayList )obj).size(); i++) 363 res += paramToString(((ArrayList )obj).get(i)) + ";"; 364 res += "}"; 365 } 366 else if(obj instanceof Properties ) 367 { 368 res = "{"; 369 Enumeration keys = ((Properties )obj).keys(); 370 while (keys.hasMoreElements()) 371 { 372 final String key = (String )keys.nextElement(); 373 res += key + "=" + ((Properties )obj).get(key) + ";"; 374 } 375 res += "}"; 376 } 377 else if(obj instanceof Attribute ) 378 { 379 res = ""+((Attribute )obj).getName()+"="+((Attribute )obj).getValue(); 380 } 381 else if(obj instanceof String || 382 obj instanceof Number || 383 obj instanceof ObjectName ) 384 { 385 res = obj.toString(); 386 } 387 else 388 res = "<?>"; 389 return res; 390 } 391 392 395 private String getObjectNameForCall(Call call) { 396 if(call==null) 397 return null; 398 Object [] params = call.getArguments(); 399 if( params==null || 400 params.length<1 || 401 !(params[0] instanceof ObjectName )) 402 return null; 403 return params[0].toString(); 404 } 405 406 } 407 | Popular Tags |