KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > authentication > AuthenticationAC


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3                           Laurent Martelli <laurent@aopsys.com>
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

18
19 package org.objectweb.jac.aspects.authentication;
20
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23 import org.objectweb.jac.core.AspectComponent;
24 import org.objectweb.jac.core.rtti.ClassItem;
25 import org.objectweb.jac.core.rtti.MethodItem;
26 import org.objectweb.jac.util.ExtArrays;
27 import org.objectweb.jac.util.Log;
28
29 /**
30  * This AC weaves the authentication aspect.
31  *
32  * <p>The authentication ensures that the authenticated method are
33  * called only when the user is known in the context. An external
34  * controller (such as the one defined by the user aspect) can by used
35  * to actually grant of refuse the access.
36  *
37  * @see AuthenticationWrapper
38  * @see org.objectweb.jac.aspects.user.UserAC */

39
40 public class AuthenticationAC extends AspectComponent
41     implements AuthenticationConf {
42
43     /** The contextual attribute that contains the authenticated user
44         if any. */

45     public static final String JavaDoc USER = "AuthenticationAC.USER";
46
47     /** Stores the trusted users. */
48     protected HashSet JavaDoc trustedUsers = new HashSet JavaDoc();
49    
50     /**
51      * Tells if a given user is trusted or not.
52      *
53      * @param username the user's name
54      * @return true if the user has been added to the trusted users
55      * list */

56    
57     public boolean isTrustedUser(String JavaDoc username)
58     {
59         Log.trace("authentication","isTrustedUser("+username+")");
60         return trustedUsers.contains(username);
61     }
62
63     /**
64      * Returns all the declared trusted users.
65      *
66      * @see #addTrustedUser(String) */

67
68     public Set JavaDoc getTrustedUsers() {
69         return trustedUsers;
70     }
71
72     AuthenticationWrapper wrapper;
73     AuthenticationWrapper getWrapper() {
74         if (wrapper==null) {
75             wrapper = new AuthenticationWrapper(this,authenticator,null);
76             //wrapper.setAspectComponent(ACManager.get().getName(this));
77
}
78         return wrapper;
79     }
80
81     // AuthenticationConf interface
82

83     public void addTrustedUser(String JavaDoc username) {
84         Log.trace("authentication","addTrustedUser("+username+")");
85         trustedUsers.add(username);
86     }
87
88     public void setController(String JavaDoc classes,
89                               String JavaDoc methods,
90                               MethodItem controller) {
91         Log.trace("authentication","setController("+
92                   classes+","+methods+","+controller+")");
93         getWrapper().setController(controller);
94         //wrapper.setAspectComponent(ACManager.get().getName(this));
95
pointcut("ALL",classes,methods,
96                  wrapper,null);
97     }
98
99     public void setDisplayController(MethodItem controller) {
100         setController("org.objectweb.jac.core.Display",
101                       ".*showCustomized.* || .*fullRefresh.*",
102                       controller);
103     }
104
105     public void setAccessDeniedMessage(String JavaDoc message) {
106         getWrapper().setAccessDeniedMessage(message);
107     }
108
109     public void addRestrictedMethods(String JavaDoc classes,
110                                      String JavaDoc methods,
111                                      String JavaDoc objects ) {
112         Log.trace("authentication","addRestrictedMethods("+
113                   classes+","+methods+","+objects+")");
114         pointcut(objects,classes,methods,
115                  getWrapper(),null);
116     }
117
118     public void addRestrictedObjects(String JavaDoc objects) {
119         pointcut(objects,"ALL","ALL",
120                  getWrapper(),null);
121     }
122
123     public void addRestrictedObjects(String JavaDoc objects, String JavaDoc classes) {
124         pointcut(objects,classes,"ALL",
125                  getWrapper(),null);
126     }
127
128     Authenticator authenticator;
129
130     public void setAuthenticator(ClassItem authenticatorClass) {
131         setAuthenticator(authenticatorClass, ExtArrays.emptyStringArray);
132     }
133
134     public void setAuthenticator(ClassItem authenticatorClass, String JavaDoc[] parameters) {
135         Log.trace("authentication","setAuthenticator("+authenticatorClass+")");
136         try {
137             authenticator = (Authenticator)authenticatorClass.newInstance(parameters);
138         } catch(Exception JavaDoc e) {
139             throw new RuntimeException JavaDoc("Failed to instanciate authenticator "+
140                                        authenticatorClass+": "+e);
141         }
142         getWrapper().setAuthenticator(authenticator);
143     }
144
145 }
146
Popular Tags