001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or 1any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: Florent BENOIT
022: * --------------------------------------------------------------------------
023: * $Id: JResource.java 6661 2005-04-28 08:43:27Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.realm.factory;
026:
027: import java.io.Serializable;
028: import java.util.ArrayList;
029: import java.util.Hashtable;
030: import javax.management.MBeanServer;
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import javax.naming.NamingException;
034: import javax.naming.Reference;
035: import javax.naming.Referenceable;
036:
037: import org.objectweb.jonas.security.SecurityService;
038: import org.objectweb.jonas.service.ServiceManager;
039: import org.objectweb.jonas.management.ReconfigDispatcher;
040: import org.objectweb.jonas.management.ReconfiguredXml;
041:
042: //import objectweb.util
043: import org.objectweb.util.monolog.api.Logger;
044: import org.objectweb.util.monolog.api.BasicLevel;
045:
046: import org.objectweb.jonas.common.Log;
047: import org.objectweb.jonas.jmx.JmxService;
048: import org.objectweb.jonas.security.realm.principals.User;
049:
050: /**
051: * This class implements JOnAS realm factory objects. It is build from an xml
052: * file by the security service of JOnAS.
053: * @author Florent Benoit
054: */
055: public abstract class JResource extends ReconfigDispatcher implements
056: Serializable, Referenceable, JResourceMBean {
057:
058: /**
059: * Name of this resource
060: */
061: private String name = null;
062:
063: /**
064: * The logger used in JOnAS
065: */
066: private static Logger logger = Log
067: .getLogger(Log.JONAS_SECURITY_PREFIX);
068:
069: /**
070: * Hashtable used to cache the users (speed up search)
071: */
072: private Hashtable users = null;
073:
074: /**
075: * Value used as sequence number by reconfiguration notifications
076: */
077: private long sequenceNumber;
078:
079: /**
080: * Reference to the security service
081: */
082: private SecurityService securityService = null;
083:
084: /**
085: * Reference to a MBean server.
086: */
087: private MBeanServer mbeanServer = null;
088:
089: /**
090: * Constructor
091: * @throws Exception if a service can't be retrieved
092: */
093: public JResource() throws Exception {
094: users = new Hashtable();
095: sequenceNumber = 0;
096: try {
097: ServiceManager sm = ServiceManager.getInstance();
098: securityService = (SecurityService) sm.getSecurityService();
099: mbeanServer = ((JmxService) sm.getJmxService())
100: .getJmxServer();
101: } catch (Exception e) {
102: // no save available
103: // no MBeans
104: if (logger.isLoggable(BasicLevel.DEBUG)) {
105: logger
106: .log(BasicLevel.DEBUG,
107: "Cannot get MBean server, client mode ? no jmx registration");
108: }
109: } catch (Error err) {
110: // ServiceManager with JProp class not found
111: if (logger.isLoggable(BasicLevel.DEBUG)) {
112: logger.log(BasicLevel.DEBUG,
113: "No services can be retrieved, client mode ?");
114: }
115: }
116:
117: }
118:
119: /**
120: * Set the name of this resource
121: * @param name Name of the resource
122: */
123: public void setName(String name) {
124: this .name = name;
125: }
126:
127: /**
128: * Get the name of this resource
129: * @return the name of this resource
130: */
131: public String getName() {
132: return name;
133: }
134:
135: /**
136: * Get the MBean server
137: * @return the MBean server
138: */
139: protected MBeanServer getMBeanServer() {
140: return mbeanServer;
141: }
142:
143: /**
144: * Retrieves the Reference of the object. The Reference contains the factory
145: * used to create this object and the optional parameters used to configure
146: * the factory.
147: * @return the non-null Reference of the object.
148: * @throws NamingException if a naming exception was encountered while
149: * retrieving the reference.
150: */
151: public abstract Reference getReference() throws NamingException;
152:
153: /**
154: * Remove all the Mbeans used by this resource
155: * @throws JResourceException if the MBeans can not be removed
156: */
157: public abstract void removeMBeans() throws JResourceException;
158:
159: /**
160: * Check if a user is found and return it
161: * @param name the wanted user name
162: * @return the user found or null
163: * @throws JResourceException if there is an error during the search
164: */
165: public abstract User findUser(String name)
166: throws JResourceException;
167:
168: /**
169: * Check if the given credential is the right credential for the given user
170: * @param user user to check its credentials
171: * @param credentials the given credentials
172: * @return true if the credential is valid for this user
173: */
174: public abstract boolean isValidUser(User user, String credentials);
175:
176: /**
177: * Get all the roles (from the roles and from the groups) of the given user
178: * @param user the given user
179: * @return the array list of all the roles for a given user
180: * @throws JResourceException if it fails
181: */
182: public abstract ArrayList getArrayListCombinedRoles(User user)
183: throws JResourceException;
184:
185: /**
186: * Return users
187: * @return Return users
188: */
189: public Hashtable getUsers() {
190: return users;
191: }
192:
193: /**
194: * Return a sequence number and increase this number
195: * @return a sequence number
196: */
197: protected long getSequenceNumber() {
198: return ++sequenceNumber;
199: }
200:
201: /**
202: * Update and save configuration
203: */
204: public void saveConfig() {
205:
206: if (securityService != null) {
207: // Retrieve the xml value by the Security service
208: String xml = securityService.toXML();
209: ReconfiguredXml reconfiguredXml = new ReconfiguredXml(xml);
210:
211: // Send a reconfiguration notification to the listener MBean
212: sendReconfigNotification(getSequenceNumber(), name,
213: reconfiguredXml);
214: sendSaveNotification(getSequenceNumber(), name);
215:
216: // rebind this resource into the jndi
217: try {
218: Context ictx = new InitialContext();
219: ictx.rebind(getName(), this );
220: if (logger.isLoggable(BasicLevel.DEBUG)) {
221: logger.log(BasicLevel.DEBUG, "jResource "
222: + getName()
223: + " is rebind into the registry.");
224: }
225: } catch (NamingException e) {
226: logger.log(BasicLevel.ERROR,
227: "Cannot rebind factory to the jndi : '"
228: + e.getMessage() + "'");
229: }
230:
231: }
232: }
233:
234: /**
235: * @return Returns the logger.
236: */
237: public static Logger getLogger() {
238: return logger;
239: }
240:
241: /**
242: * @param logger The logger to set.
243: */
244: public static void setLogger(Logger logger) {
245: JResource.logger = logger;
246: }
247:
248: /**
249: * @param users The users to set.
250: */
251: public void setUsers(Hashtable users) {
252: this .users = users;
253: }
254:
255: /**
256: * Clear the cache
257: */
258: public void clearCache() {
259: setUsers(new Hashtable());
260: }
261: }
|