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: JResourceManager.java 4840 2004-05-28 14:02:57Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.security.lib;
025:
026: import java.io.File;
027: import java.io.Reader;
028: import java.net.MalformedURLException;
029: import java.net.URL;
030:
031: import org.apache.commons.digester.Digester;
032:
033: import org.objectweb.jonas.common.JProp;
034: import org.objectweb.jonas.common.Log;
035: import org.objectweb.jonas.security.JResources;
036: import org.objectweb.jonas.security.SecurityServiceException;
037: import org.objectweb.jonas.security.rules.JDSRuleSet;
038: import org.objectweb.jonas.security.rules.JLDAPRuleSet;
039: import org.objectweb.jonas.security.rules.JMemoryRuleSet;
040:
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: *
046: *
047: * @author Guillaume Sauthier
048: */
049: public class JResourceManager {
050:
051: /**
052: * Logger which is used
053: */
054: private static Logger logger = Log
055: .getLogger(Log.JONAS_SECURITY_PREFIX);
056:
057: /**
058: * JONAS_ROOT/xml
059: */
060: private String xmlDir = null;
061:
062: /**
063: * DTD of the realm configuration file
064: */
065: protected static final String CONFIG_FILE_DTD = "jonas-realm_1_0.dtd";
066:
067: /**
068: * JONAS_ROOT
069: */
070: private static final String JONAS_ROOT = JProp.getInstallRoot();
071:
072: /**
073: * Unique JresourceManager instance
074: */
075: private static JResourceManager instance = null;
076:
077: /**
078: * Private Constructor for Singleton
079: */
080: private JResourceManager() {
081: xmlDir = JONAS_ROOT + File.separator + "xml" + File.separator;
082: }
083:
084: /**
085: * @return returns the unique JResourceManager instance
086: */
087: public static JResourceManager getInstance() {
088: if (instance == null) {
089: instance = new JResourceManager();
090: }
091: return instance;
092: }
093:
094: /**
095: * Create and configure the Digester that will be used for the xml
096: * parsing of the JOnAS realm file.
097: * @return Digester the digester containing the rules for the xml parsing
098: * of the JOnAS realm file (jonas-realm.xml)
099: */
100: protected Digester createRealmDigester() {
101:
102: // Initialize the digester
103: Digester digester = new Digester();
104: File realmDtd = new File(xmlDir + CONFIG_FILE_DTD);
105: URL realmDtdURL = null;
106: boolean validating = true;
107: try {
108: realmDtdURL = realmDtd.toURL();
109: digester.register("-//ObjectWeb//DTD JOnAS realm 1.0//EN",
110: realmDtdURL.toExternalForm());
111: } catch (MalformedURLException e) {
112: // Can't locate URL for DTD, no validation
113: logger.log(BasicLevel.INFO,
114: "Can not locate URL for DTD validation, no validation will be done."
115: + e.getMessage());
116: validating = false;
117: }
118:
119: digester.setValidating(validating);
120: digester.setErrorHandler(new JErrorHandler());
121: digester.addRuleSet(new JMemoryRuleSet(
122: "jonas-realm/jonas-memoryrealm/"));
123: digester
124: .addRuleSet(new JDSRuleSet("jonas-realm/jonas-dsrealm/"));
125: digester.addRuleSet(new JLDAPRuleSet(
126: "jonas-realm/jonas-ldaprealm/"));
127:
128: return (digester);
129: }
130:
131: /**
132: * Add JResource(s) created from parsing of the reader contents inside the JResources given instance.
133: *
134: * @param res JResources element where JResource will be addded.
135: * @param reader XML Content Reader
136: * @param xml filename / xml : used in Error messages
137: *
138: * @throws SecurityServiceException When parsing fails
139: */
140: public void addResources(JResources res, Reader reader, String xml)
141: throws SecurityServiceException {
142: // Create the digester for the parsing of the jonas-realm.xml file.
143: Digester realmDigester = createRealmDigester();
144:
145: try {
146: realmDigester.push(res);
147: realmDigester.parse(reader);
148: reader.close();
149: } catch (org.xml.sax.SAXException saxe) {
150: logger.log(BasicLevel.ERROR,
151: "Error when parsing the XML of the file " + xml);
152: throw new SecurityServiceException(saxe.getMessage(), saxe);
153: } catch (Exception e) {
154: logger.log(BasicLevel.ERROR,
155: "Error when reading config file from the xml file "
156: + xml);
157: throw new SecurityServiceException(
158: "Error when reading config file from the xml file "
159: + xml + " : " + e.getMessage(), e);
160: }
161:
162: }
163:
164: }
|