KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > lib > JResourceManager


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JResourceManager.java,v 1.1 2004/05/28 14:02:56 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.security.lib;
26
27 import java.io.File JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
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 /**
47  *
48  *
49  * @author Guillaume Sauthier
50  */

51 public class JResourceManager {
52
53     /**
54      * Logger which is used
55      */

56     private static Logger logger = Log.getLogger(Log.JONAS_SECURITY_PREFIX);
57
58     /**
59      * JONAS_ROOT/xml
60      */

61     private String JavaDoc xmlDir = null;
62
63     /**
64      * DTD of the realm configuration file
65      */

66     protected static final String JavaDoc CONFIG_FILE_DTD = "jonas-realm_1_0.dtd";
67
68     /**
69      * JONAS_ROOT
70      */

71     private static final String JavaDoc JONAS_ROOT = JProp.getInstallRoot();
72
73     /**
74      * Unique JresourceManager instance
75      */

76     private static JResourceManager instance = null;
77
78
79     /**
80      * Private Constructor for Singleton
81      */

82     private JResourceManager() {
83         xmlDir = JONAS_ROOT + File.separator + "xml" + File.separator;
84     }
85
86     /**
87      * @return returns the unique JResourceManager instance
88      */

89     public static JResourceManager getInstance() {
90         if (instance == null) {
91             instance = new JResourceManager();
92         }
93         return instance;
94     }
95
96     /**
97      * Create and configure the Digester that will be used for the xml
98      * parsing of the JOnAS realm file.
99      * @return Digester the digester containing the rules for the xml parsing
100      * of the JOnAS realm file (jonas-realm.xml)
101      */

102     protected Digester createRealmDigester() {
103
104         // Initialize the digester
105
Digester digester = new Digester();
106         File JavaDoc realmDtd = new File JavaDoc(xmlDir + CONFIG_FILE_DTD);
107         URL JavaDoc 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 JavaDoc e) {
113             // Can't locate URL for DTD, no validation
114
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     /**
128      * Add JResource(s) created from parsing of the reader contents inside the JResources given instance.
129      *
130      * @param res JResources element where JResource will be addded.
131      * @param reader XML Content Reader
132      * @param xml filename / xml : used in Error messages
133      *
134      * @throws SecurityServiceException When parsing fails
135      */

136     public void addResources(JResources res, Reader JavaDoc reader, String JavaDoc xml) throws SecurityServiceException {
137         // Create the digester for the parsing of the jonas-realm.xml file.
138
Digester realmDigester = createRealmDigester();
139
140         try {
141             realmDigester.push(res);
142             realmDigester.parse(reader);
143             reader.close();
144         } catch (org.xml.sax.SAXException JavaDoc 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 JavaDoc 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