KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > classsecurity > ClassAuthenticationWrapper


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security.classsecurity;
11
12 import java.util.*;
13
14 import org.mmbase.security.*;
15 import org.mmbase.security.SecurityException;
16 import org.mmbase.util.*;
17 import org.mmbase.util.logging.*;
18 import org.mmbase.util.xml.DocumentReader;
19 import org.w3c.dom.*;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXException JavaDoc;
22
23
24 /**
25  * ClassAuthenticationWrapper wraps another Authentication implemention, and adds an extra
26  * configuration file. In this configuration file the wrapped Authentication can be specified (and
27  * <em>its</em> configuration file if it needs one). Besides that, also authentication credentials
28  * can be linked to classes in this XML configuration file.
29  *
30  * @author Michiel Meeuwissen
31  * @version $Id: ClassAuthenticationWrapper.java,v 1.7 2005/01/30 16:46:39 nico Exp $
32  * @since MMBase-1.8
33  */

34 public class ClassAuthenticationWrapper extends Authentication {
35     private static final Logger log = Logging.getLoggerInstance(ClassAuthenticationWrapper.class);
36
37     private Authentication wrappedAuthentication;
38
39     /**
40      * Instantiates an Authentication implementation
41      */

42     private Authentication getAuthenticationInstance(String JavaDoc className) throws SecurityException JavaDoc {
43         Authentication result;
44         try {
45             Class JavaDoc classType = Class.forName(className);
46             Object JavaDoc o = classType.newInstance();
47             result = (Authentication) o;
48         } catch(ClassNotFoundException JavaDoc cnfe) {
49             throw new SecurityException JavaDoc(cnfe);
50         } catch(IllegalAccessException JavaDoc iae) {
51             throw new SecurityException JavaDoc(iae);
52         } catch(InstantiationException JavaDoc ie) {
53             throw new SecurityException JavaDoc(ie);
54         }
55         return result;
56     }
57
58     /**
59      * {@inheritDoc}
60      * Reads the configuration file and instantiates and loads the wrapped Authentication.
61      */

62     protected void load() throws SecurityException JavaDoc {
63         try {
64             InputSource JavaDoc in = MMBaseCopConfig.securityLoader.getInputSource(configResource);
65             Document document = DocumentReader.getDocumentBuilder(
66                true, // validate aggresively, because no further error-handling will be done
67
new XMLErrorHandler(false, 0), // don't log, throw exception if not valid, otherwise big chance on NPE and so on
68
new XMLEntityResolver(true, getClass()) // validate
69
).parse(in);
70             
71             
72             Node authentication = document.getElementsByTagName("authentication").item(0);
73             
74             String JavaDoc wrappedClass = authentication.getAttributes().getNamedItem("class").getNodeValue();
75             String JavaDoc wrappedUrl = authentication.getAttributes().getNamedItem("url").getNodeValue();
76             
77             wrappedAuthentication = getAuthenticationInstance(wrappedClass);
78             wrappedAuthentication.load(manager, configWatcher, wrappedUrl);
79             ClassAuthentication.stopWatching();
80             ClassAuthentication.load(configResource);
81
82         } catch (java.io.IOException JavaDoc ioe) {
83             throw new SecurityException JavaDoc(ioe);
84         } catch (SAXException JavaDoc se) {
85             throw new SecurityException JavaDoc(se);
86         }
87
88     }
89
90     /**
91      * logs-in using the first match on the class from the configuration file.
92      * @param loginInfo If there are possible credentials already, they can be in this map. The new
93      * one will be added. If it is null, a new Map is instantiated.
94      * @param parameters Required by the login method of Authentication. I think noone ever uses it.
95      */

96     protected UserContext login(Map loginInfo, Object JavaDoc[] parameters) throws SecurityException JavaDoc {
97         ClassAuthentication.Login l = ClassAuthentication.classCheck(null);
98         if (l != null) {
99             if (loginInfo == null) loginInfo = new HashMap();
100             loginInfo.putAll(l.map);
101             return wrappedAuthentication.login(l.application, loginInfo, parameters);
102         }
103         return null;
104     }
105
106     /**
107      * {@inheritDoc}
108      */

109     public UserContext login(String JavaDoc application, Map loginInfo, Object JavaDoc[] parameters) throws SecurityException JavaDoc {
110
111         // first try 'cleanly':
112
try {
113             return wrappedAuthentication.login(application, loginInfo, parameters);
114         } catch (UnknownAuthenticationMethodException uam) { // no luck
115
return login(loginInfo, parameters);
116         } catch (SecurityException JavaDoc se) { // perhaps not recognized
117
log.warn("Authentication did not succeed " + se.getMessage() + " trying self");
118             return login(loginInfo, parameters);
119         }
120     }
121
122     /**
123      * {@inheritDoc}
124      */

125     public boolean isValid(UserContext userContext) throws SecurityException JavaDoc {
126         return wrappedAuthentication.isValid(userContext);
127     }
128
129
130 }
131
Popular Tags