00001
00025 package org.objectweb.jonas.security;
00026
00027 import java.util.Enumeration;
00028 import java.util.Hashtable;
00029
00030 import org.objectweb.jonas.security.realm.factory.JResource;
00031 import org.objectweb.jonas.security.realm.factory.JResourceDS;
00032 import org.objectweb.jonas.security.realm.factory.JResourceLDAP;
00033 import org.objectweb.jonas.security.realm.factory.JResourceMemory;
00034
00035
00041 public class JResources {
00042
00046 private Hashtable jResources = null;
00047
00051 private SecurityService service = null;
00052
00056 public static final String HEADER_XML =
00057 "<?xml version='1.0' encoding='utf-8'?>\n"
00058 + "<!DOCTYPE jonas-realm PUBLIC\n"
00059 + " \"-//ObjectWeb//DTD JOnAS realm 1.0//EN\"\n"
00060 + " \"http://www.objectweb.org/jonas/dtds/jonas-realm_1_0.dtd\">\n";
00061
00066 public JResources(SecurityService s) {
00067 jResources = new Hashtable();
00068 service = s;
00069 }
00070
00076 public void addJResource(JResource jResource) throws Exception {
00077 if (jResources.get(jResource.getName()) != null) {
00078 throw new Exception("The resource name " + jResource.getName() + " already exists !");
00079 }
00080
00081 jResources.put(jResource.getName(), jResource);
00082
00083 service.bindResource(jResource.getName(), jResource);
00084 }
00085
00092 public JResource remove(String resourceName) throws Exception {
00093 JResource jResource = (JResource) jResources.get(resourceName);
00094 if (jResource == null) {
00095 throw new Exception("The resource name " + resourceName + " doesn't exist !");
00096 }
00097 jResources.remove(resourceName);
00098 return jResource;
00099 }
00100
00105 public JResource getJResource(String name) {
00106 return (JResource) jResources.get(name);
00107 }
00108
00112 public Enumeration getResources() {
00113 return jResources.elements();
00114 }
00115
00120 public String toXML() {
00121
00122
00123
00124 StringBuffer xml = new StringBuffer(HEADER_XML);
00125 xml.append("<!--\n");
00126 xml.append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
00127 xml.append(" - Define a jonas-realm.xml file for JOnAS realms\n");
00128 xml.append(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
00129 xml.append(" -->\n");
00130
00131 xml.append("<jonas-realm>\n");
00132
00133
00134 xml.append(" <!--\n");
00135 xml.append(" -= MEMORY REALM =-\n");
00136 xml.append(" Define the users, groups and roles\n");
00137 xml.append(" -->\n");
00138 xml.append(" <jonas-memoryrealm>\n");
00139 for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
00140 Object o = e.nextElement();
00141 if (o instanceof JResourceMemory) {
00142 xml.append(o.toString());
00143 xml.append("\n");
00144 }
00145 }
00146 xml.append(" </jonas-memoryrealm>\n");
00147
00148
00149
00150 xml.append(" <!--\n");
00151 xml.append(" -= DATASOURCE REALM =-\n");
00152 xml.append(" Define the configuration to use datas from a datasource\n");
00153 xml.append(" -->\n");
00154 xml.append(" <jonas-dsrealm>\n");
00155 for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
00156 Object o = e.nextElement();
00157 if (o instanceof JResourceDS) {
00158 xml.append(o.toString());
00159 xml.append("\n");
00160 }
00161 }
00162 xml.append(" </jonas-dsrealm>\n");
00163
00164
00165 xml.append(" <!--\n");
00166 xml.append(" -= LDAP REALM =-\n");
00167 xml.append(" Define the configuration to use datas from an ldap server\n");
00168 xml.append(" -->\n");
00169 xml.append(" <jonas-ldaprealm>\n");
00170 for (Enumeration e = jResources.elements(); e.hasMoreElements();) {
00171 Object o = e.nextElement();
00172 if (o instanceof JResourceLDAP) {
00173 xml.append(o.toString());
00174 xml.append("\n");
00175 }
00176 }
00177 xml.append(" </jonas-ldaprealm>\n");
00178
00179
00180
00181 xml.append("</jonas-realm>\n");
00182
00183
00184 return xml.toString();
00185 }
00186
00187 }