KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.security.audit;
25
26 import java.util.Properties JavaDoc;
27
28 import com.sun.enterprise.config.ConfigContext;
29 import com.sun.enterprise.config.ConfigException;
30 import com.sun.enterprise.config.serverbeans.AuditModule;
31 import com.sun.enterprise.config.serverbeans.ElementProperty;
32 import com.sun.enterprise.config.serverbeans.SecurityService;
33 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
34 import com.sun.enterprise.admin.event.AdminEventListenerException;
35 import com.sun.enterprise.admin.event.AuditModuleEvent;
36 import com.sun.enterprise.admin.event.AuditModuleEventListener;
37
38 /**
39  * Listener interface to handle audit module events.
40  * So that audit module can be dynamically created/update/deleted.
41  * @author Shing Wai Chan
42  */

43 public class AuditModuleEventListenerImpl implements AuditModuleEventListener {
44
45     /**
46      * New audit module created.
47      * It is called whenever a AuditModuleEvent with action of
48      * AuditModuleEvent.ACTION_CREATE is received.
49      * @throws AdminEventListenerException when the listener is unable to
50      * process the event.
51      */

52     public void auditModuleCreated(AuditModuleEvent event)
53              throws AdminEventListenerException {
54         try {
55             String JavaDoc moduleName = event.getModuleName();
56             AuditModule am = getAuditModule(moduleName,
57                     event.getConfigContext());
58             String JavaDoc classname = am.getClassname();
59             Properties JavaDoc props = getAuditModuleProperties(am);
60
61             AuditManager manager =
62                     AuditManagerFactory.getInstance().getAuditManagerInstance();
63             manager.addAuditModule(moduleName, classname, props);
64         } catch(Exception JavaDoc ex) {
65             throw new AdminEventListenerException(ex);
66         }
67     }
68
69     /**
70      * Audit module deleted.
71      * It is called whenever a AuditModuleEvent with action of
72      * AuditModuleEvent.ACTION_DELETE is received.
73      * @throws AdminEventListenerException when the listener is unable to
74      * process the event.
75      */

76     public void auditModuleDeleted(AuditModuleEvent event)
77              throws AdminEventListenerException {
78         try {
79             String JavaDoc moduleName = event.getModuleName();
80             AuditManager manager =
81                     AuditManagerFactory.getInstance().getAuditManagerInstance();
82             manager.removeAuditModule(moduleName);
83         } catch(Exception JavaDoc ex) {
84             throw new AdminEventListenerException(ex);
85         }
86     }
87
88     /**
89      * Audit module updated (attributes change).
90      * It is called whenever a AuditModuleEvent with action of
91      * AuditModuleEvent.ACTION_UPDATE is received.
92      * @throws AdminEventListenerException when the listener is unable to
93      * process the event.
94      */

95     public void auditModuleUpdated(AuditModuleEvent event)
96              throws AdminEventListenerException {
97         try {
98             String JavaDoc moduleName = event.getModuleName();
99
100             AuditModule am = getAuditModule(moduleName,
101                     event.getConfigContext());
102             String JavaDoc classname = am.getClassname();
103             Properties JavaDoc props = getAuditModuleProperties(am);
104
105             AuditModule oldAm = getAuditModule(moduleName,
106                     event.getOldConfigContext());
107             String JavaDoc oldClassname = oldAm.getClassname();
108             Properties JavaDoc oldProps = getAuditModuleProperties(oldAm);
109
110             AuditManager manager =
111                     AuditManagerFactory.getInstance().getAuditManagerInstance();
112             if (!classname.equals(oldClassname)) {
113                 manager.addAuditModule(moduleName, classname, props);
114             } else if (!props.equals(oldProps)) {
115                 com.sun.appserv.security.AuditModule auditModule =
116                         manager.getAuditModule(moduleName);
117                 auditModule.init(props);
118             }
119         } catch(Exception JavaDoc ex) {
120             throw new AdminEventListenerException(ex);
121         }
122     }
123
124     private AuditModule getAuditModule(String JavaDoc moduleName,
125             ConfigContext configContext) throws ConfigException {
126         SecurityService security =
127             ServerBeansFactory.getSecurityServiceBean(configContext);
128         return security.getAuditModuleByName(moduleName);
129
130     }
131
132     private Properties JavaDoc getAuditModuleProperties(AuditModule am) {
133         ElementProperty[] elementProps = am.getElementProperty();
134         int size = (elementProps != null) ? elementProps.length : 0;
135         Properties JavaDoc props = new Properties JavaDoc();
136         //XXX should we set this?
137
props.setProperty(AuditManager.NAME, am.getName());
138         props.setProperty(AuditManager.CLASSNAME, am.getClassname());
139         for (int i = 0; i < size; i++) {
140             props.setProperty(elementProps[i].getName(),
141                     elementProps[i].getValue());
142         }
143         return props;
144     }
145 }
146
Popular Tags