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: JResourceMemoryFactory.java 5459 2004-09-17 22:33:33Z ehardesty $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas.security.realm.factory;
026:
027: //import java
028: import java.util.Hashtable;
029:
030: //import javax
031: import javax.naming.Context;
032: import javax.naming.Name;
033: import javax.naming.RefAddr;
034: import javax.naming.Reference;
035:
036: import org.objectweb.jonas.common.JNDIUtils;
037:
038: //import objectweb.util
039: import org.objectweb.util.monolog.api.BasicLevel;
040:
041: /**
042: * This class provides an implementation of a JResource Memory factory for
043: * managing users
044: * @author Florent Benoit
045: */
046: public class JResourceMemoryFactory extends JResourceFactory {
047:
048: /**
049: * The Java type for which this factory knows how to create objects.
050: */
051: private static final String FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceMemory";
052:
053: /**
054: * Creates a javax.mail.Session object using the location or reference
055: * information specified.
056: * @param obj the possibly null object containing location or reference
057: * information that can be used in creating an object.
058: * @param name the name of this object relative to nameCtx, or null if no
059: * name is specified.
060: * @param nameCtx the context relative to which the name parameter is
061: * specified, or null if name is relative to the default initial
062: * context.
063: * @param environment the possibly null environment that is used in creating
064: * the object.
065: * @return a newly created javax.mail.Session object with the specific
066: * configuration; null if an object cannot be created.
067: * @throws Exception if this object factory encountered an exception while
068: * attempting to create an object, and no other object factories are
069: * to be tried.
070: */
071: public Object getObjectInstance(Object obj, Name name,
072: Context nameCtx, Hashtable environment) throws Exception {
073:
074: //Get the reference
075: Reference ref = (Reference) obj;
076:
077: //Get the class name
078: String clname = ref.getClassName();
079:
080: //Check the class name
081: if (!ref.getClassName().equals(FACTORY_TYPE)) {
082: getLogger().log(
083: BasicLevel.ERROR,
084: "Cannot create object : required type is '"
085: + FACTORY_TYPE + "', but found type is '"
086: + clname + "'.");
087: return null;
088: }
089:
090: Hashtable users = new Hashtable();
091: Hashtable groups = new Hashtable();
092: Hashtable roles = new Hashtable();
093:
094: String jResName = (String) ref.get("name").getContent();
095:
096: RefAddr refAddr = null;
097: refAddr = ref.get("users");
098: if (refAddr != null) {
099: users = (Hashtable) JNDIUtils.getObjectFromBytes(
100: (byte[]) refAddr.getContent(), getLogger());
101: }
102:
103: refAddr = ref.get("groups");
104: if (refAddr != null) {
105: groups = (Hashtable) JNDIUtils.getObjectFromBytes(
106: (byte[]) refAddr.getContent(), getLogger());
107: }
108:
109: refAddr = ref.get("roles");
110: if (refAddr != null) {
111: roles = (Hashtable) JNDIUtils.getObjectFromBytes(
112: (byte[]) refAddr.getContent(), getLogger());
113: }
114:
115: // Create and return a new object
116: JResourceMemory jResourceMemory = new JResourceMemory();
117: jResourceMemory.setName(jResName);
118: jResourceMemory.setUsers(users);
119: jResourceMemory.setGroups(groups);
120: jResourceMemory.setRoles(roles);
121:
122: return jResourceMemory;
123: }
124:
125: }
|