1 25 package org.objectweb.jonas.security.lib; 26 27 import java.io.File ; 28 import java.io.Reader ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 32 import org.apache.commons.digester.Digester; 33 34 import org.objectweb.jonas.common.JProp; 35 import org.objectweb.jonas.common.Log; 36 import org.objectweb.jonas.security.JResources; 37 import org.objectweb.jonas.security.SecurityServiceException; 38 import org.objectweb.jonas.security.rules.JDSRuleSet; 39 import org.objectweb.jonas.security.rules.JLDAPRuleSet; 40 import org.objectweb.jonas.security.rules.JMemoryRuleSet; 41 42 import org.objectweb.util.monolog.api.BasicLevel; 43 import org.objectweb.util.monolog.api.Logger; 44 45 46 51 public class JResourceManager { 52 53 56 private static Logger logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX); 57 58 61 private String xmlDir = null; 62 63 66 protected static final String CONFIG_FILE_DTD = "jonas-realm_1_0.dtd"; 67 68 71 private static final String JONAS_ROOT = JProp.getInstallRoot(); 72 73 76 private static JResourceManager instance = null; 77 78 79 82 private JResourceManager() { 83 xmlDir = JONAS_ROOT + File.separator + "xml" + File.separator; 84 } 85 86 89 public static JResourceManager getInstance() { 90 if (instance == null) { 91 instance = new JResourceManager(); 92 } 93 return instance; 94 } 95 96 102 protected Digester createRealmDigester() { 103 104 Digester digester = new Digester(); 106 File realmDtd = new File (xmlDir + CONFIG_FILE_DTD); 107 URL realmDtdURL = null; 108 boolean validating = true; 109 try { 110 realmDtdURL = realmDtd.toURL(); 111 digester.register("-//ObjectWeb//DTD JOnAS realm 1.0//EN", realmDtdURL.toExternalForm()); 112 } catch (MalformedURLException e) { 113 logger.log(BasicLevel.INFO, "Can not locate URL for DTD validation, no validation will be done." + e.getMessage()); 115 validating = false; 116 } 117 118 digester.setValidating(validating); 119 digester.setErrorHandler(new JErrorHandler()); 120 digester.addRuleSet(new JMemoryRuleSet("jonas-realm/jonas-memoryrealm/")); 121 digester.addRuleSet(new JDSRuleSet("jonas-realm/jonas-dsrealm/")); 122 digester.addRuleSet(new JLDAPRuleSet("jonas-realm/jonas-ldaprealm/")); 123 124 return (digester); 125 } 126 127 136 public void addResources(JResources res, Reader reader, String xml) throws SecurityServiceException { 137 Digester realmDigester = createRealmDigester(); 139 140 try { 141 realmDigester.push(res); 142 realmDigester.parse(reader); 143 reader.close(); 144 } catch (org.xml.sax.SAXException saxe) { 145 logger.log(BasicLevel.ERROR, "Error when parsing the XML of the file " + xml); 146 throw new SecurityServiceException(saxe.getMessage(), saxe); 147 } catch (Exception e) { 148 logger.log(BasicLevel.ERROR, "Error when reading config file from the xml file " + xml); 149 throw new SecurityServiceException("Error when reading config file from the xml file " + xml + " : " + e.getMessage(), e); 150 } 151 152 } 153 154 155 } 156 | Popular Tags |