KickJava   Java API By Example, From Geeks To Geeks.

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


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: JResourceMemoryFactory.java,v 1.4 2004/09/17 22:33:32 ehardesty Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas.security.realm.factory;
28
29 //import java
30
import java.util.Hashtable JavaDoc;
31
32 //import javax
33
import javax.naming.Context JavaDoc;
34 import javax.naming.Name JavaDoc;
35 import javax.naming.RefAddr JavaDoc;
36 import javax.naming.Reference JavaDoc;
37
38 import org.objectweb.jonas.common.JNDIUtils;
39
40 //import objectweb.util
41
import org.objectweb.util.monolog.api.BasicLevel;
42
43 /**
44  * This class provides an implementation of a JResource Memory factory for
45  * managing users
46  * @author Florent Benoit
47  */

48 public class JResourceMemoryFactory extends JResourceFactory {
49
50     /**
51      * The Java type for which this factory knows how to create objects.
52      */

53     private static final String JavaDoc FACTORY_TYPE = "org.objectweb.jonas.security.realm.factory.JResourceMemory";
54
55     /**
56      * Creates a javax.mail.Session object using the location or reference
57      * information specified.
58      * @param obj the possibly null object containing location or reference
59      * information that can be used in creating an object.
60      * @param name the name of this object relative to nameCtx, or null if no
61      * name is specified.
62      * @param nameCtx the context relative to which the name parameter is
63      * specified, or null if name is relative to the default initial
64      * context.
65      * @param environment the possibly null environment that is used in creating
66      * the object.
67      * @return a newly created javax.mail.Session object with the specific
68      * configuration; null if an object cannot be created.
69      * @throws Exception if this object factory encountered an exception while
70      * attempting to create an object, and no other object factories are
71      * to be tried.
72      */

73     public Object JavaDoc getObjectInstance(Object JavaDoc obj, Name JavaDoc name, Context JavaDoc nameCtx, Hashtable JavaDoc environment) throws Exception JavaDoc {
74
75         //Get the reference
76
Reference JavaDoc ref = (Reference JavaDoc) obj;
77
78         //Get the class name
79
String JavaDoc clname = ref.getClassName();
80
81         //Check the class name
82
if (!ref.getClassName().equals(FACTORY_TYPE)) {
83             getLogger().log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE
84                     + "', but found type is '" + clname + "'.");
85             return null;
86         }
87
88         Hashtable JavaDoc users = new Hashtable JavaDoc();
89         Hashtable JavaDoc groups = new Hashtable JavaDoc();
90         Hashtable JavaDoc roles = new Hashtable JavaDoc();
91
92         String JavaDoc jResName = (String JavaDoc) ref.get("name").getContent();
93
94         RefAddr JavaDoc refAddr = null;
95         refAddr = ref.get("users");
96         if (refAddr != null) {
97             users = (Hashtable JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent(), getLogger());
98         }
99
100         refAddr = ref.get("groups");
101         if (refAddr != null) {
102             groups = (Hashtable JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent(), getLogger());
103         }
104
105         refAddr = ref.get("roles");
106         if (refAddr != null) {
107             roles = (Hashtable JavaDoc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent(), getLogger());
108         }
109
110         // Create and return a new object
111
JResourceMemory jResourceMemory = new JResourceMemory();
112         jResourceMemory.setName(jResName);
113         jResourceMemory.setUsers(users);
114         jResourceMemory.setGroups(groups);
115         jResourceMemory.setRoles(roles);
116
117         return jResourceMemory;
118     }
119
120 }
Popular Tags