1 25 package org.objectweb.jonas.security; 26 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 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 41 public class JResources { 42 43 46 private Hashtable jResources = null; 47 48 51 private SecurityService service = null; 52 53 56 public static final String 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 66 public JResources(SecurityService s) { 67 jResources = new Hashtable (); 68 service = s; 69 } 70 71 76 public void addJResource(JResource jResource) throws Exception { 77 if (jResources.get(jResource.getName()) != null) { 78 throw new Exception ("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 92 public JResource remove(String resourceName) throws Exception { 93 JResource jResource = (JResource) jResources.get(resourceName); 94 if (jResource == null) { 95 throw new Exception ("The resource name " + resourceName + " doesn't exist !"); 96 } 97 jResources.remove(resourceName); 98 return jResource; 99 } 100 101 105 public JResource getJResource(String name) { 106 return (JResource) jResources.get(name); 107 } 108 109 112 public Enumeration getResources() { 113 return jResources.elements(); 114 } 115 116 120 public String toXML() { 121 122 124 StringBuffer xml = new StringBuffer (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 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 e = jResources.elements(); e.hasMoreElements();) { 140 Object 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 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 e = jResources.elements(); e.hasMoreElements();) { 156 Object 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 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 e = jResources.elements(); e.hasMoreElements();) { 171 Object 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 |