KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > security > AbsSecurityContextHelper


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2004 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: AbsSecurityContextHelper.java,v 1.2 2005/04/28 08:43:26 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.security;
26
27 import java.util.ArrayList JavaDoc;
28
29 import org.objectweb.jonas.common.JProp;
30 import org.objectweb.jonas.security.realm.factory.JResource;
31 import org.objectweb.jonas.security.realm.factory.JResourceException;
32 import org.objectweb.jonas.security.realm.principals.User;
33 import org.objectweb.jonas.service.ServiceManager;
34 import org.objectweb.security.context.SecurityContext;
35 import org.objectweb.security.context.SecurityCurrent;
36 import org.objectweb.util.monolog.api.BasicLevel;
37 import org.objectweb.util.monolog.api.Logger;
38
39 /**
40  * This class allows to authenticate users.
41  * It's a singleton to allow inherence and "static" methods.
42  * @author Florent Benoit : Initial Developper
43  * @author Helene Joanin : Refactoring
44  */

45 public abstract class AbsSecurityContextHelper {
46
47     /**
48      * JResource
49      */

50     private static JResource jResource = null;
51
52     /**
53      * @return return the associated logger
54      */

55     abstract protected Logger getLogger();
56
57     /**
58      * @return return the Realm Key
59      */

60     abstract protected String JavaDoc getRealmKey();
61
62     /**
63      * @return return the default realm value
64      */

65     abstract protected String JavaDoc getRealmDefault();
66
67     /**
68      * Login with given principal and given credential
69      * @param principalName the login
70      * @param credential the password
71      */

72     public void login(String JavaDoc principalName, String JavaDoc credential) {
73
74         // No authentication can be made with a null username
75
if (principalName == null) {
76             getLogger().log(BasicLevel.ERROR, "No username so no authentication");
77             return;
78         }
79
80         // Does a user with this username exist?
81
User user = null;
82         try {
83             user = getJResource().findUser(principalName);
84         } catch (Exception JavaDoc jre) {
85             // could not retrieve user
86
getLogger().log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage());
87             return;
88         }
89
90         // User was not found
91
if (user == null) {
92             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
93                 getLogger().log(BasicLevel.DEBUG, "User " + principalName + " not found.");
94             }
95             return;
96         }
97
98         boolean validated = getJResource().isValidUser(user, credential);
99         if (!validated) {
100             getLogger().log(BasicLevel.ERROR, "The password for the user " + principalName + " is not valid");
101             return;
102         }
103
104         ArrayList JavaDoc combinedRoles = null;
105         try {
106             combinedRoles = getJResource().getArrayListCombinedRoles(user);
107         } catch (JResourceException jre) {
108             getLogger().log(BasicLevel.ERROR, jre.getMessage());
109             return;
110         }
111
112         SecurityContext ctx = new SecurityContext(principalName, combinedRoles);
113         SecurityCurrent current = SecurityCurrent.getCurrent();
114         current.setSecurityContext(ctx);
115         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
116             getLogger().log(BasicLevel.DEBUG, "Login of principalName '" + principalName + "' succeeded.");
117         }
118
119     }
120
121     /**
122      * @return the Resource for the authentication (Realm based)
123      */

124     private JResource getJResource() {
125
126         if (jResource != null) {
127             return jResource;
128         }
129
130         SecurityService securityService = null;
131         // Get the Security Service
132
try {
133             securityService = (SecurityService) ServiceManager.getInstance().getSecurityService();
134         } catch (Exception JavaDoc e) {
135             // Can't retrieve Security service
136
throw new IllegalStateException JavaDoc("can't retrieve Security service");
137         }
138
139         String JavaDoc resName = null;
140         try {
141             resName = JProp.getInstance().getValue(getRealmKey());
142         } catch (Exception JavaDoc e) {
143             getLogger().log(BasicLevel.ERROR, "Cannot read properties in jonas.properties file.");
144         }
145         if (resName == null) {
146             if (getLogger().isLoggable(BasicLevel.DEBUG)) {
147                 getLogger().log(BasicLevel.DEBUG, "Cannot read property '" + getRealmKey() + "' in jonas.properties file. Use default value = '" + getRealmDefault() + "'.");
148             }
149             resName = getRealmDefault();
150         }
151
152         // Get the resource from the security service
153
jResource = securityService.getJResource(resName);
154         if (jResource == null) {
155             throw new IllegalStateException JavaDoc("Can't retrieve resource '" + resName + "' from the security service");
156         }
157         return jResource;
158     }
159
160
161 }
162
Popular Tags