KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > auth > realm > AuthRealmEventListenerImpl


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.auth.realm;
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.AuthRealm;
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.AuthRealmEvent;
36 import com.sun.enterprise.admin.event.AuthRealmEventListener;
37 import com.sun.enterprise.security.SecurityUtil;
38 import com.sun.enterprise.server.pluggable.SecuritySupport;
39
40 /**
41  * Implements interface AuthRealmEventListener.
42  * So that realms can be dynamically created/updated/deleted.
43  * @author Shing Wai Chan
44  */

45 public class AuthRealmEventListenerImpl implements AuthRealmEventListener {
46
47     /**
48      * New auth realm created.
49      * It is called whenever a AuthRealmEvent with action of
50      * AuthRealmEvent.ACTION_CREATE is received.
51      * @throws AdminEventListenerException when the listener is unable to
52      * process the event.
53      */

54     public void authRealmCreated(AuthRealmEvent event)
55              throws AdminEventListenerException {
56         try {
57             createRealm(event);
58         } catch(Exception JavaDoc ex) {
59             throw new AdminEventListenerException(ex);
60         }
61     }
62
63     /**
64      * Auth realm deleted.
65      * It is called whenever a AuthRealmEvent with action of
66      * AuthRealmEvent.ACTION_DELETE is received.
67      * @throws AdminEventListenerException when the listener is unable to
68      * process the event.
69      */

70     public void authRealmDeleted(AuthRealmEvent event)
71              throws AdminEventListenerException {
72         try {
73             //only unload the realm, keep any auxiliary file for sanity
74
Realm.unloadInstance(event.getAuthRealmName());
75         } catch(Exception JavaDoc ex) {
76             throw new AdminEventListenerException(ex);
77         }
78     }
79
80     /**
81      * Auth realm updated (attributes change).
82      * It is called whenever a AuthRealmEvent with action of
83      * AuthRealmEvent.ACTION_UPDATE is received.
84      * @throws AdminEventListenerException when the listener is unable to
85      * process the event.
86      */

87     public void authRealmUpdated(AuthRealmEvent event)
88              throws AdminEventListenerException {
89         try {
90             //XXX replace with a new realm, need to revisit in JSR 196
91
createRealm(event);
92         } catch(Exception JavaDoc ex) {
93             throw new AdminEventListenerException(ex);
94         }
95     }
96
97     /**
98      * This method will create or replace existing realm with a new one
99      * in cache.
100      * @param event
101      * @exception for instance, BadRealmException, ConfigException,
102      * SynchronizationException
103      */

104     private void createRealm(AuthRealmEvent event) throws Exception JavaDoc {
105         ConfigContext configContext = event.getConfigContext();
106         String JavaDoc realmName = event.getAuthRealmName();
107         SecurityService security =
108             ServerBeansFactory.getSecurityServiceBean(configContext);
109         AuthRealm authRealm = security.getAuthRealmByName(realmName);
110         //authRealm cannot be null here
111
String JavaDoc className = authRealm.getClassname();
112         ElementProperty[] elementProps = authRealm.getElementProperty();
113         int size = (elementProps != null) ? elementProps.length : 0;
114         Properties JavaDoc props = new Properties JavaDoc();
115         for (int i = 0; i < size; i++) {
116             props.setProperty(elementProps[i].getName(),
117                     elementProps[i].getValue());
118         }
119
120         if ("com.sun.enterprise.security.auth.realm.file.FileRealm".equals(className)) {
121             SecuritySupport secSupp = SecurityUtil.getSecuritySupport();
122             secSupp.synchronizeKeyFile(configContext, realmName);
123         }
124         Realm.instantiate(realmName, className, props);
125     }
126 }
127
Popular Tags