KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > realm > factory > JResource


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: JResource.java,v 1.9 2005/04/28 08:43:25 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.realm.factory;
28
29 import java.io.Serializable JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.naming.Reference JavaDoc;
37 import javax.naming.Referenceable JavaDoc;
38
39 import org.objectweb.jonas.security.SecurityService;
40 import org.objectweb.jonas.service.ServiceManager;
41 import org.objectweb.jonas.management.ReconfigDispatcher;
42 import org.objectweb.jonas.management.ReconfiguredXml;
43
44 //import objectweb.util
45
import org.objectweb.util.monolog.api.Logger;
46 import org.objectweb.util.monolog.api.BasicLevel;
47
48 import org.objectweb.jonas.common.Log;
49 import org.objectweb.jonas.jmx.JmxService;
50 import org.objectweb.jonas.security.realm.principals.User;
51
52 /**
53  * This class implements JOnAS realm factory objects. It is build from an xml
54  * file by the security service of JOnAS.
55  * @author Florent Benoit
56  */

57 public abstract class JResource extends ReconfigDispatcher implements Serializable JavaDoc, Referenceable JavaDoc, JResourceMBean {
58
59     /**
60      * Name of this resource
61      */

62     private String JavaDoc name = null;
63
64     /**
65      * The logger used in JOnAS
66      */

67     private static Logger logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
68
69     /**
70      * Hashtable used to cache the users (speed up search)
71      */

72     private Hashtable JavaDoc users = null;
73
74     /**
75      * Value used as sequence number by reconfiguration notifications
76      */

77     private long sequenceNumber;
78
79     /**
80      * Reference to the security service
81      */

82     private SecurityService securityService = null;
83
84     /**
85      * Reference to a MBean server.
86      */

87     private MBeanServer JavaDoc mbeanServer = null;
88
89     /**
90      * Constructor
91      * @throws Exception if a service can't be retrieved
92      */

93     public JResource() throws Exception JavaDoc {
94         users = new Hashtable JavaDoc();
95         sequenceNumber = 0;
96         try {
97             ServiceManager sm = ServiceManager.getInstance();
98             securityService = (SecurityService) sm.getSecurityService();
99             mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer();
100         } catch (Exception JavaDoc e) {
101             // no save available
102
// no MBeans
103
if (logger.isLoggable(BasicLevel.DEBUG)) {
104                 logger.log(BasicLevel.DEBUG, "Cannot get MBean server, client mode ? no jmx registration");
105             }
106         } catch (Error JavaDoc err) {
107             // ServiceManager with JProp class not found
108
if (logger.isLoggable(BasicLevel.DEBUG)) {
109                 logger.log(BasicLevel.DEBUG, "No services can be retrieved, client mode ?");
110             }
111         }
112
113     }
114
115     /**
116      * Set the name of this resource
117      * @param name Name of the resource
118      */

119     public void setName(String JavaDoc name) {
120         this.name = name;
121     }
122
123     /**
124      * Get the name of this resource
125      * @return the name of this resource
126      */

127     public String JavaDoc getName() {
128         return name;
129     }
130
131     /**
132      * Get the MBean server
133      * @return the MBean server
134      */

135     protected MBeanServer JavaDoc getMBeanServer() {
136         return mbeanServer;
137     }
138
139     /**
140      * Retrieves the Reference of the object. The Reference contains the factory
141      * used to create this object and the optional parameters used to configure
142      * the factory.
143      * @return the non-null Reference of the object.
144      * @throws NamingException if a naming exception was encountered while
145      * retrieving the reference.
146      */

147     public abstract Reference JavaDoc getReference() throws NamingException JavaDoc;
148
149     /**
150      * Remove all the Mbeans used by this resource
151      * @throws JResourceException if the MBeans can not be removed
152      */

153     public abstract void removeMBeans() throws JResourceException;
154
155     /**
156      * Check if a user is found and return it
157      * @param name the wanted user name
158      * @return the user found or null
159      * @throws JResourceException if there is an error during the search
160      */

161     public abstract User findUser(String JavaDoc name) throws JResourceException;
162
163     /**
164      * Check if the given credential is the right credential for the given user
165      * @param user user to check its credentials
166      * @param credentials the given credentials
167      * @return true if the credential is valid for this user
168      */

169     public abstract boolean isValidUser(User user, String JavaDoc credentials);
170
171     /**
172      * Get all the roles (from the roles and from the groups) of the given user
173      * @param user the given user
174      * @return the array list of all the roles for a given user
175      * @throws JResourceException if it fails
176      */

177     public abstract ArrayList JavaDoc getArrayListCombinedRoles(User user) throws JResourceException;
178
179     /**
180      * Return users
181      * @return Return users
182      */

183     public Hashtable JavaDoc getUsers() {
184         return users;
185     }
186
187     /**
188      * Return a sequence number and increase this number
189      * @return a sequence number
190      */

191     protected long getSequenceNumber() {
192         return ++sequenceNumber;
193     }
194
195     /**
196      * Update and save configuration
197      */

198     public void saveConfig() {
199
200         if (securityService != null) {
201             // Retrieve the xml value by the Security service
202
String JavaDoc xml = securityService.toXML();
203             ReconfiguredXml reconfiguredXml = new ReconfiguredXml(xml);
204
205             // Send a reconfiguration notification to the listener MBean
206
sendReconfigNotification(getSequenceNumber(), name, reconfiguredXml);
207             sendSaveNotification(getSequenceNumber(), name);
208
209             // rebind this resource into the jndi
210
try {
211                 Context JavaDoc ictx = new InitialContext JavaDoc();
212                 ictx.rebind(getName(), this);
213                 if (logger.isLoggable(BasicLevel.DEBUG)) {
214                     logger.log(BasicLevel.DEBUG, "jResource " + getName() + " is rebind into the registry.");
215                 }
216             } catch (NamingException JavaDoc e) {
217                 logger.log(BasicLevel.ERROR, "Cannot rebind factory to the jndi : '" + e.getMessage() + "'");
218             }
219
220         }
221     }
222
223     /**
224      * @return Returns the logger.
225      */

226     public static Logger getLogger() {
227         return logger;
228     }
229     /**
230      * @param logger The logger to set.
231      */

232     public static void setLogger(Logger logger) {
233         JResource.logger = logger;
234     }
235     /**
236      * @param users The users to set.
237      */

238     public void setUsers(Hashtable JavaDoc users) {
239         this.users = users;
240     }
241
242     /**
243      * Clear the cache
244      */

245     public void clearCache() {
246         setUsers(new Hashtable JavaDoc());
247     }
248 }
Popular Tags