KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > audit > AuditManager


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 /*
25  * AuditManager.java
26  *
27  * Created on July 28, 2003, 1:56 PM
28  */

29
30 package com.sun.enterprise.security.audit;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
54  *
55  * @author Harpreet Singh
56  * @author Shing Wai Chan
57  */

58 public final class AuditManager {
59     static final String JavaDoc NAME = "name";
60     static final String JavaDoc CLASSNAME = "classname";
61
62     private static final String JavaDoc AUDIT_MGR_WS_INVOCATION_KEY =
63         "auditmgr.webServiceInvocation";
64     private static final String JavaDoc AUDIT_MGR_EJB_AS_WS_INVOCATION_KEY =
65         "auditmgr.ejbAsWebServiceInvocation";
66     private static final String JavaDoc AUDIT_MGR_SERVER_STARTUP_KEY =
67         "auditmgr.serverStartup";
68     private static final String JavaDoc AUDIT_MGR_SERVER_SHUTDOWN_KEY =
69         "auditmgr.serverShutdown";
70
71     private static final Logger JavaDoc _logger =
72              LogDomains.getLogger(LogDomains.SECURITY_LOGGER);
73
74     private static final LocalStringManagerImpl _localStrings =
75     new LocalStringManagerImpl(AuditManager.class);
76
77     private List JavaDoc instances = Collections.synchronizedList(new ArrayList JavaDoc());
78     // just a copy of names of the audit classes - helpful for log messages
79
// since we will not have a lot of audit classes, keeping a duplicate copy
80
// seems reasonable.
81
private Map JavaDoc moduleToNameMap = new HashMap JavaDoc();
82     private Map JavaDoc nameToModuleMap = new HashMap JavaDoc();
83     // make this accessible to the containers so that the cost of non-audit case,
84
// is just a comparision.
85
private static boolean auditOn = false;
86     /** Creates a new instance of AuditManager */
87     AuditManager() {
88     }
89     
90     /**
91      * This method initializes AuditManager which load audit modules and
92      * audit enabled flag
93      */

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             // @todo will be removed to incorporate the new structure.
107
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 JavaDoc name = am[i].getName();
116                     String JavaDoc classname = am[i].getClassname();
117                     Properties JavaDoc p = new Properties JavaDoc();
118                     //XXX should we remove this two extra properties
119
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 JavaDoc nme = ep[j].getName();
125                         String JavaDoc 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 JavaDoc ex){
133                      String JavaDoc msg = _localStrings.getLocalString(
134                          "auditmgr.loaderror",
135                          "Audit: Cannot load AuditModule = {0}",
136                          new Object JavaDoc[]{ am[i].getName() });
137                      _logger.log(Level.WARNING, msg, ex);
138                 }
139             }
140         } catch (Exception JavaDoc e) {
141             String JavaDoc 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     /**
148      * Add the given audit module to the list of loaded audit module.
149      * Adding the same name twice will override previous one.
150      * @param name of auditModule
151      * @param am an instance of a class extending AuditModule that has been
152      * successfully loaded into the system.
153      * @exception
154      */

155     void addAuditModule(String JavaDoc name, String JavaDoc classname, Properties JavaDoc props)
156             throws Exception JavaDoc {
157         // make sure only a name corresponding to only one auditModule
158
removeAuditModule(name);
159         AuditModule am = loadAuditModule(classname, props);
160
161         moduleToNameMap.put(am, name);
162         nameToModuleMap.put(name, am);
163         // clone list to resolve multi-thread issues in looping instances
164
List JavaDoc list = new ArrayList JavaDoc();
165         Collections.copy(instances, list);
166         list.add(am);
167         instances = Collections.synchronizedList(list);
168     }
169
170     /**
171      * Remove the audit module of given name from the loaded list.
172      * @param name of auditModule
173      */

174     void removeAuditModule(String JavaDoc name) {
175         Object JavaDoc am = nameToModuleMap.get(name);
176         if (am != null) {
177             nameToModuleMap.remove(name);
178             moduleToNameMap.remove(am);
179             // clone list to resolve multi-thread issues in looping instances
180
List JavaDoc list = new ArrayList JavaDoc();
181             Collections.copy(instances, list);
182             list.remove(am);
183             instances = Collections.synchronizedList(list);
184         }
185     }
186
187     /**
188      * Get the audit module of given name from the loaded list.
189      * @param name of auditModule
190      */

191     AuditModule getAuditModule(String JavaDoc name) {
192         return (AuditModule)nameToModuleMap.get(name);
193     }
194
195
196     /**
197      * This method return auditModule with given classname and properties.
198      * @param classname
199      * @param props
200      * @exception
201      */

202     private AuditModule loadAuditModule(String JavaDoc classname,
203             Properties JavaDoc props) throws Exception JavaDoc {
204         AuditModule auditModule = null;
205         Class JavaDoc am = Class.forName(classname);
206         Object JavaDoc obj = am.newInstance();
207         auditModule = (AuditModule) obj;
208         auditModule.init(props);
209         return auditModule;
210     }
211     
212     /**
213      * logs the authentication call for all the loaded modules.
214      * @see com.sun.appserv.security.AuditModule.authentication
215      */

216     public void authentication(String JavaDoc user, String JavaDoc realm, boolean success){
217         if(auditOn){
218             List JavaDoc 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 JavaDoc e){
226                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
227                     String JavaDoc msg =
228                     _localStrings.getLocalString("auditmgr.authentication",
229                     " Audit Module {0} threw the followin exception during authentication:",
230                         new Object JavaDoc[] {name});
231                     _logger.log(Level.INFO, msg, e);
232                 }
233             }
234         }
235     }
236     /**
237      * logs the web authorization call for all loaded modules
238      * @see com.sun.appserv.security.AuditModule.webInvocation
239      */

240     public void webInvocation(String JavaDoc user, HttpServletRequest JavaDoc req,
241         String JavaDoc type, boolean success){
242         if(auditOn){
243             List JavaDoc 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 JavaDoc e){
250                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
251                     String JavaDoc msg =
252                     _localStrings.getLocalString("auditmgr.webinvocation",
253                     " Audit Module {0} threw the followin exception during web invocation :",
254                         new Object JavaDoc[] {name});
255                     _logger.log(Level.INFO, msg, e);
256                 }
257             }
258         }
259     }
260     /**
261      * logs the ejb authorization call for all ejb modules
262      * @see com.sun.appserv.security.AuditModule.ejbInvocation
263      */

264     public void ejbInvocation(String JavaDoc user, String JavaDoc ejb, String JavaDoc method,
265             boolean success){
266         if(auditOn){
267             List JavaDoc 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 JavaDoc e){
274                         String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
275                         String JavaDoc msg =
276                         _localStrings.getLocalString("auditmgr.ejbinvocation",
277                         " Audit Module {0} threw the followin exception during ejb invocation :",
278                             new Object JavaDoc[] {name});
279                         _logger.log(Level.INFO, msg, e);
280                 }
281
282             }
283         }
284     }
285     
286     /**
287      * This method is called for the web service calls with MLS set
288      * and the endpoints deployed as servlets
289      * @see com.sun.appserv.security.AuditModule.webServiceInvocation
290      */

291     public void webServiceInvocation(String JavaDoc uri, String JavaDoc endpoint,
292                                      boolean validRequest){
293         if(auditOn){
294             // This surely is not the most optimal way of iterating through
295
// the list of audit modules since I think the list is static
296
// For now just do as its done for ejb/web audits - TODO later
297
// Another thing to do would be make the list of audit modules
298
// generic, preventing type casting at runtime
299
// like: List<AuditModule> list
300
List JavaDoc 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 JavaDoc e){
307                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
308                     String JavaDoc 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 JavaDoc[] {name});
313                     _logger.log(Level.INFO, msg, e);
314                 }
315             }
316         }
317     }
318
319
320     /**
321      * This method is called for the web service calls with MLS set
322      * and the endpoints deployed as servlets
323      * @see com.sun.appserv.security.AuditModule.webServiceInvocation
324      */

325     public void ejbAsWebServiceInvocation(String JavaDoc endpoint, boolean validRequest){
326         if(auditOn){
327
328             List JavaDoc 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 JavaDoc e){
335                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
336                     String JavaDoc 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 JavaDoc[] {name});
341                     _logger.log(Level.INFO, msg, e);
342                 }
343             }
344         }
345     }
346
347     public void serverStarted(){
348         if(auditOn){
349             // This surely is not the most optimal way of iterating through
350
// the list of audit modules since I think the list is static
351
// For now just do as its done for ejb/web audits - TODO later
352
// Another thing to do would be make the list of audit modules
353
// generic, preventing type casting at runtime
354
// like: List<AuditModule> list
355
List JavaDoc 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 JavaDoc e){
362                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
363                     String JavaDoc msg =
364                     _localStrings.getLocalString(AUDIT_MGR_SERVER_STARTUP_KEY,
365                     " Audit Module {0} threw the following exception during "+
366                     "server startup :",
367                         new Object JavaDoc[] {name});
368                     _logger.log(Level.INFO, msg, e);
369                 }
370             }
371         }
372     }
373
374     public void serverShutdown(){
375         if(auditOn){
376             // This surely is not the most optimal way of iterating through
377
// the list of audit modules since I think the list is static
378
// For now just do as its done for ejb/web audits - TODO later
379
// Another thing to do would be make the list of audit modules
380
// generic, preventing type casting at runtime
381
// like: List<AuditModule> list
382
List JavaDoc 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 JavaDoc e){
389                     String JavaDoc name = (String JavaDoc)moduleToNameMap.get(am);
390                     String JavaDoc msg =
391                     _localStrings.getLocalString(AUDIT_MGR_SERVER_SHUTDOWN_KEY,
392                     " Audit Module {0} threw the following exception during "+
393                     "server shutdown :",
394                         new Object JavaDoc[] {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