KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > JResources


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 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 any 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  * --------------------------------------------------------------------------
22  * $Id: JResources.java,v 1.1 2004/05/28 14:02:56 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.security;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29
30 import org.objectweb.jonas.security.realm.factory.JResource;
31 import org.objectweb.jonas.security.realm.factory.JResourceDS;
32 import org.objectweb.jonas.security.realm.factory.JResourceLDAP;
33 import org.objectweb.jonas.security.realm.factory.JResourceMemory;
34
35
36 /**
37  *
38  *
39  * @author Guillaume Sauthier
40  */

41 public class JResources {
42
43     /**
44      * List of Resource
45      */

46     private Hashtable JavaDoc jResources = null;
47
48     /**
49      * SecurityService
50      */

51     private SecurityService service = null;
52
53     /**
54      * Xml Header
55      */

56     public static final String JavaDoc HEADER_XML =
57         "<?xml version='1.0' encoding='utf-8'?>\n"
58         + "<!DOCTYPE jonas-realm PUBLIC\n"
59         + " \"-//ObjectWeb//DTD JOnAS realm 1.0//EN\"\n"
60         + " \"http://www.objectweb.org/jonas/dtds/jonas-realm_1_0.dtd\">\n";
61
62     /**
63      * Create a JResoures list attached to a given SecurityService.
64      * @param s the parent SecurityService
65      */

66     public JResources(SecurityService s) {
67         jResources = new Hashtable JavaDoc();
68         service = s;
69     }
70
71     /**
72      * Add the Resource (memory, ldap, datasource,...)
73      * @param jResource an instance of JResource or subclasses (JResourceMemory, JResourceDS,...)
74      * @throws Exception if the resource name already exists
75      */

76     public void addJResource(JResource jResource) throws Exception JavaDoc {
77         if (jResources.get(jResource.getName()) != null) {
78             throw new Exception JavaDoc("The resource name " + jResource.getName() + " already exists !");
79         }
80
81         jResources.put(jResource.getName(), jResource);
82
83         service.bindResource(jResource.getName(), jResource);
84     }
85
86     /**
87      * Removes the named JResource from the JResource list.
88      * @param resourceName JResource name to be removed
89      * @return the removed JResource
90      * @throws Exception when JResource is not found with given name.
91      */

92     public JResource remove(String JavaDoc resourceName) throws Exception JavaDoc {
93         JResource jResource = (JResource) jResources.get(resourceName);
94         if (jResource == null) {
95             throw new Exception JavaDoc("The resource name " + resourceName + " doesn't exist !");
96         }
97        jResources.remove(resourceName);
98        return jResource;
99     }
100
101     /**
102      * @param name the name of the JResource to get.
103      * @return Returns the named JResource
104      */

105     public JResource getJResource(String JavaDoc name) {
106         return (JResource) jResources.get(name);
107     }
108
109     /**
110      * @return Returns the JResources Enumeration
111      */

112     public Enumeration JavaDoc getResources() {
113         return jResources.elements();
114     }
115
116     /**
117      * String representation of the JOnAS realm
118      * @return the xml representation of the JOnAS realm
119      */

120     public String JavaDoc toXML() {
121
122         // Required values
123

124         StringBuffer JavaDoc xml = new StringBuffer JavaDoc(HEADER_XML);
125         xml.append("<!--\n");
126         xml.append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
127         xml.append(" - Define a jonas-realm.xml file for JOnAS realms\n");
128         xml.append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
129         xml.append(" -->\n");
130
131         xml.append("<jonas-realm>\n");
132
133         // Memory realms
134
xml.append(" <!--\n");
135         xml.append(" -= MEMORY REALM =-\n");
136         xml.append(" Define the users, groups and roles\n");
137         xml.append(" -->\n");
138         xml.append(" <jonas-memoryrealm>\n");
139         for (Enumeration JavaDoc e = jResources.elements(); e.hasMoreElements();) {
140                 Object JavaDoc o = e.nextElement();
141                 if (o instanceof JResourceMemory) {
142                     xml.append(o.toString());
143                     xml.append("\n");
144                 }
145         }
146         xml.append(" </jonas-memoryrealm>\n");
147
148
149         // Datasource realms
150
xml.append(" <!--\n");
151         xml.append(" -= DATASOURCE REALM =-\n");
152         xml.append(" Define the configuration to use datas from a datasource\n");
153         xml.append(" -->\n");
154         xml.append(" <jonas-dsrealm>\n");
155         for (Enumeration JavaDoc e = jResources.elements(); e.hasMoreElements();) {
156                 Object JavaDoc o = e.nextElement();
157                 if (o instanceof JResourceDS) {
158                     xml.append(o.toString());
159                     xml.append("\n");
160                 }
161         }
162         xml.append(" </jonas-dsrealm>\n");
163
164         // Ldap realms
165
xml.append(" <!--\n");
166         xml.append(" -= LDAP REALM =-\n");
167         xml.append(" Define the configuration to use datas from an ldap server\n");
168         xml.append(" -->\n");
169         xml.append(" <jonas-ldaprealm>\n");
170         for (Enumeration JavaDoc e = jResources.elements(); e.hasMoreElements();) {
171                 Object JavaDoc o = e.nextElement();
172                 if (o instanceof JResourceLDAP) {
173                     xml.append(o.toString());
174                     xml.append("\n");
175                 }
176         }
177         xml.append(" </jonas-ldaprealm>\n");
178
179
180
181         xml.append("</jonas-realm>\n");
182
183
184         return xml.toString();
185     }
186
187 }
188
Popular Tags