001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 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 any 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: * --------------------------------------------------------------------------
022: * $Id: JResources.java 4840 2004-05-28 14:02:57Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.security;
025:
026: import java.util.Enumeration;
027: import java.util.Hashtable;
028:
029: import org.objectweb.jonas.security.realm.factory.JResource;
030: import org.objectweb.jonas.security.realm.factory.JResourceDS;
031: import org.objectweb.jonas.security.realm.factory.JResourceLDAP;
032: import org.objectweb.jonas.security.realm.factory.JResourceMemory;
033:
034: /**
035: *
036: *
037: * @author Guillaume Sauthier
038: */
039: public class JResources {
040:
041: /**
042: * List of Resource
043: */
044: private Hashtable jResources = null;
045:
046: /**
047: * SecurityService
048: */
049: private SecurityService service = null;
050:
051: /**
052: * Xml Header
053: */
054: public static final String HEADER_XML = "<?xml version='1.0' encoding='utf-8'?>\n"
055: + "<!DOCTYPE jonas-realm PUBLIC\n"
056: + " \"-//ObjectWeb//DTD JOnAS realm 1.0//EN\"\n"
057: + " \"http://www.objectweb.org/jonas/dtds/jonas-realm_1_0.dtd\">\n";
058:
059: /**
060: * Create a JResoures list attached to a given SecurityService.
061: * @param s the parent SecurityService
062: */
063: public JResources(SecurityService s) {
064: jResources = new Hashtable();
065: service = s;
066: }
067:
068: /**
069: * Add the Resource (memory, ldap, datasource,...)
070: * @param jResource an instance of JResource or subclasses (JResourceMemory, JResourceDS,...)
071: * @throws Exception if the resource name already exists
072: */
073: public void addJResource(JResource jResource) throws Exception {
074: if (jResources.get(jResource.getName()) != null) {
075: throw new Exception("The resource name "
076: + jResource.getName() + " already exists !");
077: }
078:
079: jResources.put(jResource.getName(), jResource);
080:
081: service.bindResource(jResource.getName(), jResource);
082: }
083:
084: /**
085: * Removes the named JResource from the JResource list.
086: * @param resourceName JResource name to be removed
087: * @return the removed JResource
088: * @throws Exception when JResource is not found with given name.
089: */
090: public JResource remove(String resourceName) throws Exception {
091: JResource jResource = (JResource) jResources.get(resourceName);
092: if (jResource == null) {
093: throw new Exception("The resource name " + resourceName
094: + " doesn't exist !");
095: }
096: jResources.remove(resourceName);
097: return jResource;
098: }
099:
100: /**
101: * @param name the name of the JResource to get.
102: * @return Returns the named JResource
103: */
104: public JResource getJResource(String name) {
105: return (JResource) jResources.get(name);
106: }
107:
108: /**
109: * @return Returns the JResources Enumeration
110: */
111: public Enumeration getResources() {
112: return jResources.elements();
113: }
114:
115: /**
116: * String representation of the JOnAS realm
117: * @return the xml representation of the JOnAS realm
118: */
119: public String toXML() {
120:
121: // Required values
122:
123: StringBuffer xml = new StringBuffer(HEADER_XML);
124: xml.append("<!--\n");
125: xml
126: .append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
127: xml
128: .append(" - Define a jonas-realm.xml file for JOnAS realms\n");
129: xml
130: .append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
131: xml.append(" -->\n");
132:
133: xml.append("<jonas-realm>\n");
134:
135: // Memory realms
136: xml.append(" <!--\n");
137: xml.append(" -= MEMORY REALM =-\n");
138: xml.append(" Define the users, groups and roles\n");
139: xml.append(" -->\n");
140: xml.append(" <jonas-memoryrealm>\n");
141: for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
142: Object o = e.nextElement();
143: if (o instanceof JResourceMemory) {
144: xml.append(o.toString());
145: xml.append("\n");
146: }
147: }
148: xml.append(" </jonas-memoryrealm>\n");
149:
150: // Datasource realms
151: xml.append(" <!--\n");
152: xml.append(" -= DATASOURCE REALM =-\n");
153: xml
154: .append(" Define the configuration to use datas from a datasource\n");
155: xml.append(" -->\n");
156: xml.append(" <jonas-dsrealm>\n");
157: for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
158: Object o = e.nextElement();
159: if (o instanceof JResourceDS) {
160: xml.append(o.toString());
161: xml.append("\n");
162: }
163: }
164: xml.append(" </jonas-dsrealm>\n");
165:
166: // Ldap realms
167: xml.append(" <!--\n");
168: xml.append(" -= LDAP REALM =-\n");
169: xml
170: .append(" Define the configuration to use datas from an ldap server\n");
171: xml.append(" -->\n");
172: xml.append(" <jonas-ldaprealm>\n");
173: for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
174: Object o = e.nextElement();
175: if (o instanceof JResourceLDAP) {
176: xml.append(o.toString());
177: xml.append("\n");
178: }
179: }
180: xml.append(" </jonas-ldaprealm>\n");
181:
182: xml.append("</jonas-realm>\n");
183:
184: return xml.toString();
185: }
186:
187: }
|