AbsSecurityContextHelper.java

00001 
00025 package org.objectweb.jonas.security;
00026 
00027 import java.util.ArrayList;
00028 
00029 import org.objectweb.jonas.common.JProp;
00030 import org.objectweb.jonas.security.realm.factory.JResource;
00031 import org.objectweb.jonas.security.realm.factory.JResourceException;
00032 import org.objectweb.jonas.security.realm.principals.User;
00033 import org.objectweb.jonas.service.ServiceManager;
00034 import org.objectweb.security.context.SecurityContext;
00035 import org.objectweb.security.context.SecurityCurrent;
00036 import org.objectweb.util.monolog.api.BasicLevel;
00037 import org.objectweb.util.monolog.api.Logger;
00038 
00045 public abstract class AbsSecurityContextHelper {
00046 
00050     private static JResource jResource = null;
00051     
00055     abstract protected Logger getLogger();
00056     
00060     abstract protected String getRealmKey();
00061     
00065     abstract protected String getRealmDefault();
00066 
00072     public void login(String principalName, String credential) {
00073 
00074         // No authentication can be made with a null username
00075         if (principalName == null) {
00076             getLogger().log(BasicLevel.ERROR, "No username so no authentication");
00077             return;
00078         }
00079 
00080         // Does a user with this username exist?
00081         User user = null;
00082         try {
00083             user = getJResource().findUser(principalName);
00084         } catch (Exception jre) {
00085             // could not retrieve user
00086             getLogger().log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage());
00087             return;
00088         }
00089 
00090         // User was not found
00091         if (user == null) {
00092             getLogger().log(BasicLevel.DEBUG, "User " + principalName + " not found.");
00093             return;
00094         }
00095 
00096         boolean validated = getJResource().isValidUser(user, credential);
00097         if (!validated) {
00098             getLogger().log(BasicLevel.ERROR, "The password for the user " + principalName + " is not valid");
00099             return;
00100         }
00101 
00102         ArrayList combinedRoles = null;
00103         try {
00104             combinedRoles = getJResource().getArrayListCombinedRoles(user);
00105         } catch (JResourceException jre) {
00106             getLogger().log(BasicLevel.ERROR, jre.getMessage());
00107             return;
00108         }
00109 
00110         SecurityContext ctx = new SecurityContext(principalName, combinedRoles);
00111         SecurityCurrent current = SecurityCurrent.getCurrent();
00112         current.setSecurityContext(ctx);
00113         getLogger().log(BasicLevel.DEBUG, "Login of principalName '" + principalName + "' succeeded.");
00114 
00115     }
00116 
00120     private JResource getJResource() {
00121 
00122         if (jResource != null) {
00123             return jResource;
00124         }
00125 
00126         SecurityService securityService = null;
00127         // Get the Security Service
00128         try {
00129             securityService = (SecurityService) ServiceManager.getInstance().getSecurityService();
00130         } catch (Exception e) {
00131             // Can't retrieve Security service
00132             throw new IllegalStateException("can't retrieve Security service");
00133         }
00134 
00135         String resName = null;
00136         try {
00137             resName = JProp.getInstance().getValue(getRealmKey());
00138         } catch (Exception e) {
00139             getLogger().log(BasicLevel.ERROR, "Cannot read properties in jonas.properties file.");
00140         }
00141         if (resName == null) {
00142             getLogger().log(BasicLevel.DEBUG, "Cannot read property '" + getRealmKey() + "' in jonas.properties file. Use default value = '" + getRealmDefault() + "'.");
00143             resName = getRealmDefault();
00144         }
00145 
00146         // Get the resource from the security service
00147         jResource = securityService.getJResource(resName);
00148         if (jResource == null) {
00149             throw new IllegalStateException("Can't retrieve resource '" + resName + "' from the security service");
00150         }
00151         return jResource;
00152     }
00153 
00154 
00155 }

Generated on Tue Feb 15 15:05:21 2005 for JOnAS by  doxygen 1.3.9.1