1 23 24 29 30 package com.sun.enterprise.security.audit; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.ArrayList ; 34 import java.util.Collections ; 35 import java.util.Map ; 36 import java.util.HashMap ; 37 import java.util.Properties ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 import javax.servlet.http.HttpServletRequest ; 42 43 import com.sun.appserv.security.AuditModule; 44 import com.sun.enterprise.config.serverbeans.Server; 45 import com.sun.enterprise.config.serverbeans.SecurityService; 46 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 47 import com.sun.enterprise.config.serverbeans.ElementProperty; 48 import com.sun.enterprise.config.ConfigContext; 49 import com.sun.enterprise.server.ApplicationServer; 50 import com.sun.logging.LogDomains; 51 import com.sun.enterprise.util.LocalStringManagerImpl; 52 53 58 public final class AuditManager { 59 static final String NAME = "name"; 60 static final String CLASSNAME = "classname"; 61 62 private static final String AUDIT_MGR_WS_INVOCATION_KEY = 63 "auditmgr.webServiceInvocation"; 64 private static final String AUDIT_MGR_EJB_AS_WS_INVOCATION_KEY = 65 "auditmgr.ejbAsWebServiceInvocation"; 66 private static final String AUDIT_MGR_SERVER_STARTUP_KEY = 67 "auditmgr.serverStartup"; 68 private static final String AUDIT_MGR_SERVER_SHUTDOWN_KEY = 69 "auditmgr.serverShutdown"; 70 71 private static final Logger _logger = 72 LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 73 74 private static final LocalStringManagerImpl _localStrings = 75 new LocalStringManagerImpl(AuditManager.class); 76 77 private List instances = Collections.synchronizedList(new ArrayList ()); 78 private Map moduleToNameMap = new HashMap (); 82 private Map nameToModuleMap = new HashMap (); 83 private static boolean auditOn = false; 86 87 AuditManager() { 88 } 89 90 94 public void loadAuditModules() { 95 try { 96 ConfigContext configContext = 97 ApplicationServer.getServerContext().getConfigContext(); 98 assert(configContext != null); 99 100 Server configBean = ServerBeansFactory.getServerBean(configContext); 101 assert(configBean != null); 102 103 SecurityService securityBean = 104 ServerBeansFactory.getSecurityServiceBean(configContext); 105 assert(securityBean != null); 106 boolean auditFlag = securityBean.isAuditEnabled(); 108 109 setAuditOn(auditFlag); 110 com.sun.enterprise.config.serverbeans.AuditModule[] am = 111 securityBean.getAuditModule(); 112 113 for (int i = 0; i < am.length; i++){ 114 try { 115 String name = am[i].getName(); 116 String classname = am[i].getClassname(); 117 Properties p = new Properties (); 118 p.setProperty(NAME, name); 120 p.setProperty(CLASSNAME, classname); 121 ElementProperty[] ep = am[i].getElementProperty(); 122 int epsize = am[i].sizeElementProperty(); 123 for (int j = 0; j < epsize; j++){ 124 String nme = ep[j].getName(); 125 String val = ep[j].getValue(); 126 p.setProperty(nme, val); 127 } 128 AuditModule auditModule = loadAuditModule(classname, p); 129 instances.add(auditModule); 130 moduleToNameMap.put(auditModule, name); 131 nameToModuleMap.put(name, auditModule); 132 } catch(Exception ex){ 133 String msg = _localStrings.getLocalString( 134 "auditmgr.loaderror", 135 "Audit: Cannot load AuditModule = {0}", 136 new Object []{ am[i].getName() }); 137 _logger.log(Level.WARNING, msg, ex); 138 } 139 } 140 } catch (Exception e) { 141 String msg = _localStrings.getLocalString("auditmgr.badinit", 142 "Audit: Cannot load Audit Module Initialization information. AuditModules will not be loaded."); 143 _logger.log(Level.WARNING, msg, e); 144 } 145 } 146 147 155 void addAuditModule(String name, String classname, Properties props) 156 throws Exception { 157 removeAuditModule(name); 159 AuditModule am = loadAuditModule(classname, props); 160 161 moduleToNameMap.put(am, name); 162 nameToModuleMap.put(name, am); 163 List list = new ArrayList (); 165 Collections.copy(instances, list); 166 list.add(am); 167 instances = Collections.synchronizedList(list); 168 } 169 170 174 void removeAuditModule(String name) { 175 Object am = nameToModuleMap.get(name); 176 if (am != null) { 177 nameToModuleMap.remove(name); 178 moduleToNameMap.remove(am); 179 List list = new ArrayList (); 181 Collections.copy(instances, list); 182 list.remove(am); 183 instances = Collections.synchronizedList(list); 184 } 185 } 186 187 191 AuditModule getAuditModule(String name) { 192 return (AuditModule)nameToModuleMap.get(name); 193 } 194 195 196 202 private AuditModule loadAuditModule(String classname, 203 Properties props) throws Exception { 204 AuditModule auditModule = null; 205 Class am = Class.forName(classname); 206 Object obj = am.newInstance(); 207 auditModule = (AuditModule) obj; 208 auditModule.init(props); 209 return auditModule; 210 } 211 212 216 public void authentication(String user, String realm, boolean success){ 217 if(auditOn){ 218 List list = instances; 219 int size = list.size(); 220 for (int i = 0; i < size; i++) { 221 AuditModule am = null; 222 try{ 223 am = (AuditModule)list.get(i); 224 am.authentication(user, realm, success); 225 } catch (Exception e){ 226 String name = (String )moduleToNameMap.get(am); 227 String msg = 228 _localStrings.getLocalString("auditmgr.authentication", 229 " Audit Module {0} threw the followin exception during authentication:", 230 new Object [] {name}); 231 _logger.log(Level.INFO, msg, e); 232 } 233 } 234 } 235 } 236 240 public void webInvocation(String user, HttpServletRequest req, 241 String type, boolean success){ 242 if(auditOn){ 243 List list = instances; 244 int size = list.size(); 245 for (int i = 0; i < size; i++) { 246 AuditModule am = (AuditModule)list.get(i); 247 try{ 248 am.webInvocation(user, req, type, success); 249 } catch (Exception e){ 250 String name = (String )moduleToNameMap.get(am); 251 String msg = 252 _localStrings.getLocalString("auditmgr.webinvocation", 253 " Audit Module {0} threw the followin exception during web invocation :", 254 new Object [] {name}); 255 _logger.log(Level.INFO, msg, e); 256 } 257 } 258 } 259 } 260 264 public void ejbInvocation(String user, String ejb, String method, 265 boolean success){ 266 if(auditOn){ 267 List list = instances; 268 int size = list.size(); 269 for (int i = 0; i < size; i++) { 270 AuditModule am = (AuditModule)list.get(i); 271 try{ 272 am.ejbInvocation(user, ejb, method, success); 273 } catch (Exception e){ 274 String name = (String )moduleToNameMap.get(am); 275 String msg = 276 _localStrings.getLocalString("auditmgr.ejbinvocation", 277 " Audit Module {0} threw the followin exception during ejb invocation :", 278 new Object [] {name}); 279 _logger.log(Level.INFO, msg, e); 280 } 281 282 } 283 } 284 } 285 286 291 public void webServiceInvocation(String uri, String endpoint, 292 boolean validRequest){ 293 if(auditOn){ 294 List list = instances; 301 int size = list.size(); 302 for (int i = 0; i < size; i++) { 303 AuditModule am = (AuditModule)list.get(i); 304 try{ 305 am.webServiceInvocation(uri, endpoint, validRequest); 306 } catch (Exception e){ 307 String name = (String )moduleToNameMap.get(am); 308 String msg = 309 _localStrings.getLocalString(AUDIT_MGR_WS_INVOCATION_KEY, 310 " Audit Module {0} threw the following exception during "+ 311 "web service invocation :", 312 new Object [] {name}); 313 _logger.log(Level.INFO, msg, e); 314 } 315 } 316 } 317 } 318 319 320 325 public void ejbAsWebServiceInvocation(String endpoint, boolean validRequest){ 326 if(auditOn){ 327 328 List list = instances; 329 int size = list.size(); 330 for (int i = 0; i < size; i++) { 331 AuditModule am = (AuditModule)list.get(i); 332 try{ 333 am.ejbAsWebServiceInvocation(endpoint, validRequest); 334 } catch (Exception e){ 335 String name = (String )moduleToNameMap.get(am); 336 String msg = 337 _localStrings.getLocalString(AUDIT_MGR_EJB_AS_WS_INVOCATION_KEY, 338 " Audit Module {0} threw the following exception during "+ 339 "ejb as web service invocation :", 340 new Object [] {name}); 341 _logger.log(Level.INFO, msg, e); 342 } 343 } 344 } 345 } 346 347 public void serverStarted(){ 348 if(auditOn){ 349 List list = instances; 356 int size = list.size(); 357 for (int i = 0; i < size; i++) { 358 AuditModule am = (AuditModule)list.get(i); 359 try{ 360 am.serverStarted(); 361 } catch (Exception e){ 362 String name = (String )moduleToNameMap.get(am); 363 String msg = 364 _localStrings.getLocalString(AUDIT_MGR_SERVER_STARTUP_KEY, 365 " Audit Module {0} threw the following exception during "+ 366 "server startup :", 367 new Object [] {name}); 368 _logger.log(Level.INFO, msg, e); 369 } 370 } 371 } 372 } 373 374 public void serverShutdown(){ 375 if(auditOn){ 376 List list = instances; 383 int size = list.size(); 384 for (int i = 0; i < size; i++) { 385 AuditModule am = (AuditModule)list.get(i); 386 try{ 387 am.serverShutdown(); 388 } catch (Exception e){ 389 String name = (String )moduleToNameMap.get(am); 390 String msg = 391 _localStrings.getLocalString(AUDIT_MGR_SERVER_SHUTDOWN_KEY, 392 " Audit Module {0} threw the following exception during "+ 393 "server shutdown :", 394 new Object [] {name}); 395 _logger.log(Level.INFO, msg, e); 396 } 397 } 398 } 399 } 400 401 void setAuditOn(boolean auditOn) { 402 this.auditOn = auditOn; 403 } 404 405 public boolean isAuditOn() { 406 return auditOn; 407 } 408 409 } 410 | Popular Tags |