FileDocCategorySizeDatePackage
AuthRealmMBean.javaAPI DocGlassfish v2 API20191Fri May 04 22:24:08 BST 2007com.sun.enterprise.admin.mbeans

AuthRealmMBean

public class AuthRealmMBean extends com.sun.enterprise.admin.config.BaseConfigMBean implements com.sun.enterprise.admin.mbeanapi.IAuthRealmMBean

Fields Summary
static final String
FILE_NAME_PROPERTY
private static final String
ADMIN_REALM
private static final com.sun.enterprise.util.i18n.StringManager
localStrings
Constructors Summary
public AuthRealmMBean()


       
Methods Summary
private voidEmitUserMgmtEvent(int action, java.lang.String user, java.lang.String[] groups)

        try
        {
            AdminContext adminContext = MBeanRegistryFactory.getAdminContext();
            String instanceName = adminContext.getServerName();
            String name = (String)getAttribute(ServerTags.NAME);
            UserMgmtEvent event = new UserMgmtEvent(instanceName, name, action, user, groups);
            event.setTargetDestination(getParentConfigName());
            EventContext.addEvent(event);
        } 
        catch (Exception e) 
        {
            throw new MBeanConfigException(e.getMessage());
        }
    
public voidaddUser(java.lang.String userName, java.lang.String password, java.lang.String[] groupList)
Adds new user to file realm. User cannot exist already.

        checkFileTypeRealm();
        
        FileRealm realm = getRealmKeyFile();
        try
        {
            realm.addUser(userName, password, groupList);
            saveInstanceRealmKeyFile(realm);
        }
        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
        catch(IASSecurityException ise)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.security_exception", mInstanceName, userName, bre.getMessage());
            throw new MBeanConfigException(ise.getMessage());
        }

        EmitUserMgmtEvent(UserMgmtEvent.ACTION_USERADD, userName, groupList);

    
private voidcheckFileTypeRealm()

        String className = null;
        try 
        {
            className = (String)getAttribute(ServerTags.CLASSNAME);
        } 
        catch (Exception e) 
        {}
        if( className==null || 
          !className.equals("com.sun.enterprise.security.auth.realm.file.FileRealm"))
        {
            String msg = localStrings.getString("authRealmMBean.unsupported_type");
            throw new MBeanConfigException(msg);
        }
    
private java.lang.String[]convertEnumerationToStringArray(java.util.Enumeration ee)

        ArrayList list = new ArrayList();
        while(ee.hasMoreElements())
            list.add(ee.nextElement());
        return (String[])list.toArray(new String[list.size()]);
    
public java.lang.String[]getGroupNames()
Returns names of all the groups from the instance realm keyfile

        checkFileTypeRealm();
        
        FileRealm realm = getRealmKeyFile();
        try
        {
            return convertEnumerationToStringArray(realm.getGroupNames());
        }
        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
    
private java.lang.StringgetName()

        return (String)getAttribute(ServerTags.NAME);
    
private java.lang.StringgetParentConfigName()

returns
name of config element which realm belongs to

        return MBeanHelper.getLocation((ModelMBeanInfo)this.getMBeanInfo())[1];
    
private java.lang.StringgetProperty(java.lang.String name)

        try 
        {
            String unresolved = (String)invoke("getPropertyValue", new Object[]{name}, new String[]{"java.lang.String"});
            //This needs to be changed for SE/EE. The instanceName cant
            //be assumed to be the das server instance name.
            final String instanceName = MBeanRegistryFactory.getAdminContext().
                    getServerName();
            final String resolved =  new PropertyResolver(getConfigContext(), 
                    instanceName).resolve(unresolved);
            return resolved;
        } 
        catch (Exception e) 
        {
            return null;
        }
    
private com.sun.enterprise.security.auth.realm.file.FileRealmgetRealmKeyFile()

        try
        {
            /*
            String name = (String)getAttribute(ServerTags.NAME);
            Realm realm =  Realm.getInstance(name);
            if(realm==null)
            {
                String msg = localStrings.getString("authRealmMBean.realm_not_registered", name);
                throw new MBeanConfigException(msg);
            }
            if(!(realm instanceof FileRealm))
            {
                String msg = localStrings.getString("authRealmMBean.unsupported_type");
                throw new MBeanConfigException(msg);
            }
            return (FileRealm)realm;
            */
            return new FileRealm(getRealmKeyFileName());
        }
 //       catch(MBeanConfigException mce)
 //       {
 //           throw mce;
 //       }
        catch(Exception e)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(e.getMessage());
        }
/*        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
        catch(NoSuchRealmException nsr)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.no_such_realm", mInstanceName);
            throw new MBeanConfigException(nsr.getMessage());
        }
*/
    
private java.lang.StringgetRealmKeyFileName()

        return getProperty(FILE_NAME_PROPERTY); 
    
public java.lang.String[]getUserGroupNames(java.lang.String userName)
Returns the name of all the groups that this user belongs to from the instance realm keyfile

        if(userName==null)
           return getGroupNames();

        checkFileTypeRealm();
        
        FileRealm realm = getRealmKeyFile();
        try
        {
            return convertEnumerationToStringArray(realm.getGroupNames(userName));
        }
        catch(NoSuchUserException nse)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.no_such_user", mInstanceName, userName);
            throw new MBeanConfigException(nse.getMessage());
        }
    
public java.lang.String[]getUserNames()
Returns names of all the users from instance realm keyfile

        checkFileTypeRealm();
        
        FileRealm realm = getRealmKeyFile();
        try
        {
            return convertEnumerationToStringArray(realm.getUserNames());
        }
        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
    
private booleanisAdminRealm()

         final String name = (String) getAttribute(ServerTags.NAME);
         if (ADMIN_REALM.equals(name))
             return ( true ) ;
         return ( false );
     
private booleanisBelogsTo(java.util.Enumeration ee, java.lang.Object objectToCheck)

        while(ee.hasMoreElements())
            if(ee.nextElement().equals(objectToCheck))
                return true;
        return false;
    
private booleanisBelogsTo(java.lang.Object[] arr, java.lang.Object objectToCheck)

        for(int i=0; i<arr.length; i++)
            if(objectToCheck.equals(arr[i]))
                return true;
        return false;
    
private booleanisLastAdminUser(java.lang.String userName)

        return (isSystemAdminRealm() &&
            isUserLastInGroup(userName,AdminConstants.DOMAIN_ADMIN_GROUP_NAME));            
    
private booleanisOk(java.lang.String s)

         return ( s != null && s.length() != 0 );
     
private booleanisSystemAdminRealm()
Check if this is the last admin user in DAS admin-auth

        try {
            Server das = ServerHelper.getDAS(getConfigContext());
            String dasConfigName = das.getConfigRef();
            if(!getParentConfigName().equals(dasConfigName))
                return false; //from another config

            JmxConnector jmxc = ServerHelper.getServerSystemConnector(
                    getConfigContext(),
                    das.getName());
            return jmxc.getAuthRealmName().equals(getName());
        } catch (Exception e) {
            throw new MBeanConfigException(e.getMessage());
        }
    
private booleanisUserLastInGroup(java.lang.String userName, java.lang.String groupName)

        checkFileTypeRealm();
        
        FileRealm realm = getRealmKeyFile();

        try
        {
            if(!isBelogsTo(realm.getGroupNames(userName), groupName))
                return false; // user not in the group at all
            
            Enumeration users = realm.getUserNames();
            while(users.hasMoreElements())
            {
                String user = (String)users.nextElement();
                if(!userName.equals(user) &&
                    isBelogsTo(realm.getGroupNames(user), groupName))
                   return false;
            }
        }
        catch(Exception nse)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.no_such_user", mInstanceName, userName);
            throw new MBeanConfigException(nse.getMessage());
        }
        return true;
    
private voidlogDetailsIfAdminRealm()

         try
         {
             if (isAdminRealm()) {
                 //choosing WARNING as the log-level
                 final ConfigContext cc = super.getConfigContext();
                 final String nas = NodeAgentHelper.getNodeAgentsAsString(NodeAgentHelper.getNodeAgentsInDomain(cc));
                 final String sas = ServerHelper.getServersAsString(ServerHelper.getServersInDomainExcludingDAS(cc));
                 String msg, params[];
                 if (isOk(nas) && isOk(sas)) {
                     params = new String[]{ADMIN_REALM, nas, sas};
                     msg = localStrings.getString("admin.password.change_all_msg", params);
                     _sLogger.log(Level.WARNING, msg, params);
                 } else if (isOk(nas) && !isOk(sas)) {
                     params = new String[]{ADMIN_REALM, nas};
                     msg = localStrings.getString("admin.password.change_das_na_msg", params);
                     _sLogger.log(Level.WARNING, msg, params);
                 } else if (!isOk(nas) && !isOk(sas)) {
                     params = new String[]{ADMIN_REALM};
                     msg = localStrings.getString("admin.password.change_das_alone_msg", params);
                     _sLogger.log(Level.WARNING, msg, params);
                 }
             }
         } catch(final Exception e)
         {
            throw new RuntimeException(e);
         }
     
public voidremoveUser(java.lang.String userName)
Remove user from file realm. User must exist.

        checkFileTypeRealm();
        
        if(isLastAdminUser(userName))
        {
            String msg = localStrings.getString("authRealmMBean.removeLastAdminUser",
                                        AdminConstants.DOMAIN_ADMIN_GROUP_NAME);
            throw new MBeanConfigException(msg);
        }
            
        FileRealm realm = getRealmKeyFile();
        try
        {
            realm.removeUser(userName);
            saveInstanceRealmKeyFile(realm);
        }
        catch(NoSuchUserException nse)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.no_such_user", mInstanceName, userName);
            throw new MBeanConfigException(nse.getMessage());
        }
        EmitUserMgmtEvent(UserMgmtEvent.ACTION_USERREMOVE, userName, null);
    
private voidsaveInstanceRealmKeyFile(com.sun.enterprise.security.auth.realm.file.FileRealm realm)

        try
        {
            final String filePath = getRealmKeyFileName();
            _sLogger.log(Level.INFO, "filerealm.write", filePath);
            realm.writeKeyFile(filePath);
        }
        catch(IOException ioe)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.realm_io_error", mInstanceName);
            _sLogger.log(Level.WARNING, "filerealm.writeerror", ioe);
            throw new MBeanConfigException(ioe.getMessage());
        }
/*        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
*/
 
public voidupdateUser(java.lang.String userName, java.lang.String password, java.lang.String[] groupList)
Update data for an existing user. User must exist. This is equivalent to calling removeUser() followed by addUser().

        checkFileTypeRealm();
        
        if(!isBelogsTo(groupList, AdminConstants.DOMAIN_ADMIN_GROUP_NAME) &&
           isLastAdminUser(userName))
        {
            String msg = localStrings.getString("authRealmMBean.removeLastAdminUser",
                                        AdminConstants.DOMAIN_ADMIN_GROUP_NAME);
            throw new MBeanConfigException(msg);
        }

        FileRealm realm = getRealmKeyFile();
        try
        {
            realm.updateUser(userName, userName, password, groupList);
            saveInstanceRealmKeyFile(realm);
	    logDetailsIfAdminRealm();
        }
        catch(BadRealmException bre)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.bad_realm", mInstanceName);
            throw new MBeanConfigException(bre.getMessage());
        }
        catch(NoSuchUserException nse)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.no_such_user", mInstanceName, userName);
            throw new MBeanConfigException(nse.getMessage());
        }
        catch(IASSecurityException ise)
        {
            //String msg =  localStrings.getString( "admin.server.core.mbean.config.security_exception", mInstanceName, userName, bre.getMessage());
            throw new MBeanConfigException(ise.getMessage());
        }
        EmitUserMgmtEvent(UserMgmtEvent.ACTION_USERUPDATE, userName, groupList);