KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > AuthorizationFactory


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum;
26
27 import java.util.PropertyResourceBundle JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nemesis.forum.exception.UnauthorizedException;
32
33 /**
34  * An abstract class that defines a framework for providing authorization
35  * services. The static getAuthorization(String,String) and
36  * getAnonymousAuthorization() methods should be called directly from
37  * applications using in order to obtain Authorization tokens.<p>
38  *
39  * Users that wish to change the AuthorizationFactory implementation
40  * used to generate tokens can set the <code>AuthorizationFactory.className</code>
41  * property. For example, if you have altered to use LDAP for user
42  * information, you'd want to write a custom implementation of
43  * AuthorizationFactory to make LDAP authorization queries. After changing the
44  * <code>AuthorizationFactory.className</code>property, you must restart
45  * your application server.
46  */

47 public abstract class AuthorizationFactory {
48     static protected Log log =LogFactory.getLog(AuthorizationFactory.class);
49     
50     private static Object JavaDoc initLock = new Object JavaDoc();
51     
52     private static AuthorizationFactory factory = null;
53
54     /**
55      * Returns the Authorization token associated with the specified username
56      * and password. If the username and password do not match the record of
57      * any user in the system, the method throws an UnauthorizedException.<p>
58      *
59      * When using most implementations of this class, authorization tokens
60      * should be cached. A convenient place to store a token is often in the
61      * HttpSession.
62      *
63      * @param username the username to create an Authorization with.
64      * @param password the password to create an Authorization with.
65      * @return an Authorization token if the username and password are correct.
66      * @throws UnauthorizedException if the username and password do not match
67      * any existing user.
68      */

69     public static Authorization getAuthorization(String JavaDoc username, String JavaDoc password) throws UnauthorizedException {
70         loadAuthorizationFactory();
71         return factory.createAuthorization(username, password);
72     }
73
74     /**
75      * Returns the anonymous user Authorization.
76      *
77      * @return an anonymous Authorization token.
78      */

79     public static Authorization getAnonymousAuthorization() {
80         loadAuthorizationFactory();
81         return factory.createAnonymousAuthorization();
82     }
83
84     /**
85      * Creates Authorization tokens for users. This method is implemented by
86      * concrete subclasses of AuthorizationFactory.
87      *
88      * @param username the username to create an Authorization with.
89      * @param password the password to create an Authorization with.
90      * @return an Authorization token if the username and password are correct.
91      * @throws UnauthorizedException if the username and password do not match
92      * any existing user.
93      */

94     public abstract Authorization createAuthorization(String JavaDoc username, String JavaDoc password) throws UnauthorizedException;
95
96     /**
97      * Creates anonymous Authorization tokens. This method is implemented by
98      * concrete subclasses AuthorizationFactory.
99      *
100      * @return an anonymous Authorization token.
101      */

102     public abstract Authorization createAnonymousAuthorization();
103
104
105     private static String JavaDoc Impl =
106         (String JavaDoc)
107             (
108                 (PropertyResourceBundle JavaDoc) PropertyResourceBundle.getBundle(
109                     "org.nemesis.forum.config")).getObject(
110             "AuthorizationFactory.class");
111
112     /**
113      * Loads a concrete AuthorizationFactory that can be used generate
114      * Authorization tokens for authorized users.<p>
115      *
116      * By default, the implementation used will be an instance of
117      * DbAuthorizationFactory -- the standard database implementation that uses
118      * the user table. A different factory can be specified by setting the
119      * property "AuthorizationFactory.className". However, you must
120      * restart for any change to take effect.
121      */

122     private static void loadAuthorizationFactory() {
123         if (factory == null) {
124             //Use className as a convenient object to get a lock on.
125
synchronized (initLock) {
126                 if (factory == null) {
127                     
128                     try {
129                         Class JavaDoc c = Class.forName(Impl);
130                         factory = (AuthorizationFactory) c.newInstance();
131                     } catch (Exception JavaDoc e) {
132                         log.fatal("Exception loading class: " , e);
133                         
134                     }
135                 }
136             }
137         }
138     }
139 }
140
Popular Tags