users@glassfish.java.net

Local Stateless SessionBean Create Exception

From: <glassfish_at_javadesktop.org>
Date: Wed, 20 Aug 2008 08:43:21 PDT

Title: Local Stateless SessionBean ONLY will be available to STANDALONE CLIENT after some other in-container component invoked it successfully
 
1.Application Structure:
 
Standalone Test Client and Webtier Servlets access the Session Bean: SecuritySessionFacade
 

Standalone client accesses Remote interface
Servlet access Local interface
 
Standalone test client uses ServiceLocator to get Remote interface via service locator like this:
 
ServiceLocator.java
....
public ServiceLocator() {
        try {Properties property = System.getProperties();
            property.setProperty('org.omg.CORBA.ORBInitialHost', 'localhost');
            property.setProperty('org.omg.CORBA.ORBInitialPort', '3700');
            ic = new InitialContext(property);
        } catch (NamingException ne) {
            throw new RuntimeException(ne);
        }
    }
....
public Object getEJBObject(String jndiName){
        try {
            return lookup(jndiName);
        } catch (NamingException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, 'exception caught', ex);
            throw new RuntimeException(ex);
        }
    }
 
Servlets uses resource injection like this:
 @EJB(name = 'ejb/SecuritySessionFacade', beanName = 'SecuritySessionFacade', beanInterface = SecuritySessionFacadeLocal.class)
    private SecuritySessionFacadeLocal securitySessionFacadeBean;
 
 
SecuritySessionFacade use other local stateless beans :
SecuritySessionFacadeBean.java:
 
@Stateless(name = 'SecuritySessionFacade')
@EJBs({
    @EJB(name = 'ejb/AccountFacade', beanInterface = AccountFacadeLocal.class),
    @EJB(name = 'ejb/EventFacade', beanInterface = EventFacadeLocal.class),
    @EJB(name = 'ejb/EventTypeFacade', beanInterface = EventTypeFacadeLocal.class),
    @EJB(name = 'ejb/FeatureFacade', beanInterface = FeatureFacadeLocal.class),
    @EJB(name = 'ejb/RoleFacade', beanInterface = RoleFacadeLocal.class),
    @EJB(name = 'ejb/SecurityLogFacade', beanInterface = SecurityLogFacadeLocal.class),
    @EJB(name = 'ejb/UserGroupFacade', beanInterface = UserGroupFacadeLocal.class),
    @EJB(name = 'ejb/SecurityPolicyFacade', beanInterface = SecurityPolicyFacadeLocal.class)
})
 
 
......
 
SecuritySessionFacadeBean invokes AccountBO, and then AccountBO invokes ejb/AccountFacade.
 
 
2.Problem description
 
Problem 1:
Step 1: Start glassfishV2-2
Step 2: Use netbean 6.1 to Undeploy and deploy
Step 3: Client accesses AccountFacadeLocal via securitySessionFacadeBean's remote inteface =============Failed Exception thrown
Step 4: Servlet ccesses AccountFacadeLocal via securitySessionFacadeBean's local inteface============= Success
Step 5: Client accesses AccountFacadeLocal via securitySessionFacadeBean's remote inteface=========== Success
 
Problem 2:
If AccountFacadeLocal stateless session bean came across a runtime Exception (Such as: IllegalArgumentException), then the AccountFacade local stateless session bean won't be available until server's restart, NamingException will be thrown always.
 Test Client
=====================================================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package securitytestclient;

import net.vicp.madz.security.appservice.SecurityEvent;
import net.vicp.madz.security.entity.Account;
import net.vicp.madz.security.facade.SecuritySessionFacadeRemote;

/**
 *
 * @author cn1zd042
 */
public class Main {

    private static SecuritySessionFacadeRemote securityFacade;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        SecuritySessionDelegate instance = new SecuritySessionDelegate();
        long start = System.currentTimeMillis();
        System.out.println();
        for (int i = 0; i < 1000; i++) {
            Account account = new Account();
            account.setAccountName("Tracy" + i);
            account.setPassword("111111");
            account.setQuestion("Pet");
            account.setAnswer("Tracy's Pet");
            account.setEmail("tracy_at_126.com");
            instance.createAccount(account);
        }
        System.out.println("Task took " + (System.currentTimeMillis() - start) + " millis");


        start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String accoutName = "Tracy";
            String password = "111111";
            SecurityEvent result = instance.login(accoutName, password);
        }
        System.out.println("Task took " + (System.currentTimeMillis() - start) + " millis");
        {
            Account account = null;
            instance.editAccount(account);
        }
        start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String accoutName = "Tracy";
            String password = "111111";
            SecurityEvent result = instance.login(accoutName, password);
        }
        System.out.println("Task took " + (System.currentTimeMillis() - start) + " millis");
    }
}

=========================================================
EJB Module__ SecuritySessionFacadeBean
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vicp.madz.security.facade;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBs;
import javax.ejb.Stateless;
import net.vicp.madz.security.appservice.LoginAS;
import net.vicp.madz.security.appservice.SecurityEvent;
import net.vicp.madz.security.bo.*;
import net.vicp.madz.security.entity.Account;
import net.vicp.madz.security.entity.Event;
import net.vicp.madz.security.entity.EventType;
import net.vicp.madz.security.entity.Feature;
import net.vicp.madz.security.entity.Role;
import net.vicp.madz.security.entity.SecurityLog;
import net.vicp.madz.security.entity.UserGroup;
import net.vicp.madz.security.facade.entity.*;

/**
 *
 * @author Administrator
 */
@Stateless(name = "SecuritySessionFacade")
@EJBs({
    @EJB(name = "ejb/AccountFacade", beanInterface = AccountFacadeLocal.class),
    @EJB(name = "ejb/EventFacade", beanInterface = EventFacadeLocal.class),
    @EJB(name = "ejb/EventTypeFacade", beanInterface = EventTypeFacadeLocal.class),
    @EJB(name = "ejb/FeatureFacade", beanInterface = FeatureFacadeLocal.class),
    @EJB(name = "ejb/RoleFacade", beanInterface = RoleFacadeLocal.class),
    @EJB(name = "ejb/SecurityLogFacade", beanInterface = SecurityLogFacadeLocal.class),
    @EJB(name = "ejb/UserGroupFacade", beanInterface = UserGroupFacadeLocal.class),
    @EJB(name = "ejb/SecurityPolicyFacade", beanInterface = SecurityPolicyFacadeLocal.class)
})
public class SecuritySessionFacadeBean implements SecuritySessionFacadeRemote, SecuritySessionFacadeLocal {

    private LoginAS las = new LoginAS();
    private AccountBO abo = new AccountBO();
    private EventBO ebo = new EventBO();
    private EventTypeBO eventTypeBO = new EventTypeBO();
    private FeatureBO featureBO = new FeatureBO();
    private RoleBO roleBO = new RoleBO();
    private SecurityLogBO logBO = new SecurityLogBO();
    private SecurityPolicyBO pbo = new SecurityPolicyBO();
    private UserGroupBO groupBO = new UserGroupBO();

    public SecurityEvent createAccount(Account account) {
        return abo.createAccount(account);
    }

    public SecurityEvent login(String accoutName, String password) {
        return las.login(accoutName, password);
    }

    public void editAccount(Account account) {
        abo.editAccount(account);
    }

    public SecurityEvent resetPassword(String accountName, String oldPassword, String newPassword) {
        return abo.resetPassword(accountName, oldPassword, newPassword);
    }

    public boolean checkExist(String accountName) {
        return abo.checkExist(accountName);
    }

    public Account findAccountByName(String accountName) {
        return abo.findAccountByName(accountName);
    } // Add business logic below. (Right-click in editor and choose


    public Account findAccountById(String id) {
        return abo.findAccountById(id);
    }

    public Account findAccountFullById(String id) {
        return abo.findAccountFullById(id);
    }

    public List<Account> findAccountByNameFuzzy(String accountName) {
        return abo.findAccountByNameFuzzy(accountName);
    } // Add business logic below. (Right-click in editor and choose


    public List<Account> findAccountByIdFuzzy(String id) {
        return abo.findAccountByIdFuzzy(id);
    } // "EJB Methods > Add Business Method" or "Web Service > Add Operation")


    public List<Account> findAllAccount() {
        return abo.findAllAccount();
    }

    public List<Account> findAllAccount(int pageSize, int page) {
        return abo.findAllAccount(pageSize, page);
    }

    public List<Account> findAllFullAccount(int pageSize, int page) {
        return abo.findAllFullAccount(pageSize, page);
    }

    public void freezeAccount(Account account) {
        abo.freezeAccount(account);
    }

    public void unFreezeAccount(Account account) {
        abo.unFreezeAccount(account);
    }

    public void lockAccount(Account account) {
        abo.lockAccount(account);
    }

    public void unLockAccount(Account account) {
        abo.unLockAccount(account);
    }

    public void resetPasswordFlag(Account account) {
        abo.resetPasswordFlag(account);
    }

    public void addRoleToAccount(Account account, Role role) {
        abo.addRoleToAccount(account, role);
    }

    public void editRoleOfAccount(Account account, List<Role> roles) {
        abo.editRoleOfAccount(account, roles);
    }

    public void removeRoleFromAccount(Account account, Role role) {
        abo.removeRoleFromAccount(account, role);
    }

    public void addAccountToGroup(Account account, UserGroup group) {
        abo.addAccountToGroup(account, group);
    }

    public void editGroupOfAccount(Account account, List<UserGroup> groups) {
        abo.editGroupOfAccount(account, groups);
    }

    public void removeAccountFromGroup(Account account, UserGroup group) {
        abo.removeAccountFromGroup(account, group);
    }

    public void createEvent(Event event) {
        ebo.create(event);
    }

    public void editEvent(Event event) {
        ebo.edit(event);
    }

    public void removeEvent(Event event) {
        ebo.remove(event);
    }

    public Event findEventById(Object id) {
        return ebo.find(id);
    }

    public List<Event> findAllEvent() {
        return ebo.findAll();
    }

    public List<Event> findAllEvent(int pageSize, int page) {
        return ebo.findAll(pageSize, page);
    }

    public void setMaxLoginFailedTimes(int failedTimes) {
        pbo.setMaxLoginFailedTimes(failedTimes);
    }

    public void setNeverAutoUnlockPolicy(boolean value) {
        pbo.setNeverAutoUnlock(value);
    }

    public void setPasswordFormatPolicy(boolean value) {
        pbo.setPasswordFormatPolicy(value);
    }

    public void setPasswordLengthPolicy(boolean value) {
        pbo.setPasswordLengthPolicy(value);
    }

    public void setPasswordMaxLength(int maxLength) {
        pbo.setPasswordMaxLength(maxLength);
    }

    public void setPasswordMinLength(int minLength) {
        pbo.setPasswordMinLength(minLength);
    }

    public void setPasswordMaxReservePolicy(boolean value) {
        pbo.setPasswordMaxReservePolicy(value);
    }

    public void setPassworldMaxReserveDays(int days) {
        pbo.setPasswordMaxReserveDays(days);
    }

    public void setPasswordPattern(String regx) {
        pbo.setPasswordPattern(regx);
    }

    public void setUnlockInterval(int days) {
        pbo.setUnlockInterval(days);
    }

    public void createEventType(EventType eventType) {
        eventTypeBO.create(eventType);
    }

    public void editEventType(EventType eventType) {
        eventTypeBO.edit(eventType);
    }

    public void removeEventType(EventType eventType) {
        eventTypeBO.remove(eventType);
    }

    public EventType findEventTypeById(Object id) {
        return eventTypeBO.find(id);
    }

    public List<EventType> findAllEventType() {
        return eventTypeBO.findAll();
    }

    public List<EventType> findAllEventType(int pageSize, int page) {
        return eventTypeBO.findAll(pageSize, page);
    }

    public void createFeature(Feature feature) {
        featureBO.create(feature);
    }

    public void editFeature(Feature feature) {
        featureBO.edit(feature);
    }

    public void removeFeature(Feature feature) {
        featureBO.remove(feature);
    }

    public Feature findFeature(Object id) {
        return featureBO.find(id);
    }

    public List<Feature> findAllFeature() {
        return featureBO.findAll();
    }

    public List<Feature> getChildFeatures(Object id) {
        return featureBO.getChildFeatures(id);
    }

    public Feature findFullFeature(Object id) {
        return featureBO.findFull(id);
    }

    public List<Feature> findAllFullFeature(int pageSize, int page) {
        return featureBO.findAllFull(pageSize, page);
    }

    public void createRole(Role role) {
        roleBO.create(role);
    }

    public void editRole(Role role) {
        roleBO.edit(role);
    }

    public void removeRole(Role role) {
        roleBO.remove(role);
    }

    public Role findRole(Object id) {
        return roleBO.find(id);
    }

    public List<Role> findAllRole() {
        return roleBO.findAll();
    }

    public Role findFullRole(Object id) {
        return roleBO.findFull(id);
    }

    public List<Role> findAllRole(int pageSize, int page) {
        return roleBO.findAll(pageSize, page);
    }

    public List<Role> findAllFullRole(int pageSize, int page) {
        return roleBO.findAllFull(pageSize, page);
    }

    public void addFeatureToRole(Role role, Feature feature) {
        roleBO.addFeatureToRole(role, feature);
    }

    public void removeFeatureFromRole(Role role, Feature feature) {
        roleBO.removeFeatureFromRole(role, feature);
    }

    public void editFeatureOfRole(Role role, List<Feature> features) {
        roleBO.editFeatureOfRole(role, features);
    }

    public void createUserGroup(UserGroup group) {
        groupBO.create(group);
    }

    public void editUserGroup(UserGroup group) {
        groupBO.edit(group);
    }

    public void removeUserGroup(UserGroup group) {
        groupBO.remove(group);
    }

    public UserGroup findUserGroup(Object id) {
        return groupBO.find(id);
    }

    public UserGroup findFullUserGroup(Object id) {
        return groupBO.findFull(id);
    }

    public List<UserGroup> findAllUserGroup() {
        return groupBO.findAll();
    }

    public List<UserGroup> findAllUserGroup(int pageSize, int page) {
        return groupBO.findAll(pageSize, page);
    }

    public List<UserGroup> findAllFullUserGroup(int pageSize, int page) {
        return groupBO.findAllFull(pageSize, page);
    }

    public void addRoleToGroup(UserGroup group, Role role) {
        groupBO.addRoleToGroup(group, role);
    }

    public void editRolesOfGroup(UserGroup group, List<Role> roles) {
        groupBO.editRolesOfGroup(group, roles);
    }

    public void removeRoleFromGroup(UserGroup group, Role role) {
        groupBO.removeRoleFromGroup(group, role);
    }

    public void createSecurityLog(SecurityLog securityLog) {
        logBO.create(securityLog);
    }

    public void editSecurityLog(SecurityLog securityLog) {
        logBO.edit(securityLog);
    }

    public void removeSecurityLog(SecurityLog securityLog) {
        logBO.remove(securityLog);
    }

    public SecurityLog findSecurityLog(Object id) {
        return logBO.find(id);
    }

    public List<SecurityLog> findAllSecurityLog() {
        return logBO.findAll();
    }

    public List<SecurityLog> findAllSecurityLog(int pageSize, int page) {
        return logBO.findAll(pageSize, page);
    }
}


========================================================
AccountBO.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vicp.madz.security.bo;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;
import net.vicp.madz.security.appservice.LoggingHelper;
import net.vicp.madz.security.appservice.SecurityEvent;
import net.vicp.madz.security.appservice.SecurityPolicies;
import net.vicp.madz.security.appservice.helper.EJBLocator;
import net.vicp.madz.security.entity.Account;
import net.vicp.madz.security.entity.Role;
import net.vicp.madz.security.entity.SecurityPolicy;
import net.vicp.madz.security.entity.UserGroup;
import net.vicp.madz.security.facade.entity.AccountFacadeLocal;
import net.vicp.madz.security.facade.entity.SecurityPolicyFacadeLocal;

/**
 * @Preconditinons Local EJB Reference MUST BE defined from the calling EJB/Servlet Component
 * @author Administrator
 */
public class AccountBO {

    private AccountFacadeLocal accountFacade;
    private SecurityPolicyFacadeLocal policyFacade;
    private LoggingHelper loggingHelper;

    public AccountBO() {
        init();
    }

    private void init() {
        if (accountFacade == null) {
            accountFacade = EJBLocator.getAccountFacade();
        }
        if (policyFacade == null) {
            policyFacade = EJBLocator.getSecurityPolicyFacade();
        }
    }

    public void resetPasswordFlag(Account account) {
        account.setNeedResetPwd(true);
        editAccount(account);
    }

    public SecurityEvent createAccount(Account account) {
        if (accountFacade.checkAccountExist(account.getAccountName())) {
            return SecurityEvent.ACCOUNT_ALREADY_EXISTS;
        } else if (validateFormatPassword(account.getPassword()) == SecurityEvent.PASSWORD_PATTERN_VALID) {
            Timestamp current = new Timestamp(System.currentTimeMillis());
            account.setAccountCreateTime(current);
            account.setLastChangePwdTime(current);
            accountFacade.create(account);
            return SecurityEvent.ACCOUNT_REGISTER_SUCCESSFULLY;
        } else {
            return SecurityEvent.PASSWORD_PATTERN_INVALID;
        }
    }

    public void editAccount(Account account) {
        accountFacade.edit(account);
    }

    public void freezeAccount(Account account) {
        account.setFreezenFlag(true);
        editAccount(account);
    }

    public void unFreezeAccount(Account account) {
        account.setFreezenFlag(false);
        editAccount(account);
    }

    public void lockAccount(Account account) {
        account.setLockFlag(true);
        editAccount(account);
    }

    public void unLockAccount(Account account) {
        account.setLockFlag(false);
        editAccount(account);
    }

    public SecurityEvent resetPassword(String accountName, String oldPassword, String newPassword) {
        Account account = accountFacade.findByAccountName(accountName);
        if (account.getPassword().equals(oldPassword)) {
            account.setPassword(newPassword);
            SecurityEvent validateResult = validateFormatPassword(newPassword);
            if (validateResult == SecurityEvent.PASSWORD_PATTERN_VALID) {
                account.setNeedResetPwd(false);
                account.setLastChangePwdTime(new Timestamp(System.currentTimeMillis()));
                loggingHelper.logging(SecurityEvent.PASSWORD_RESET_SUCCESSFULLY, accountName);
                editAccount(account);
                return SecurityEvent.PASSWORD_RESET_SUCCESSFULLY;
            } else {
                return validateResult;
            }
        } else {
            return SecurityEvent.PASSWORD_NOT_VALID;
        }
    }

    public boolean checkExist(String accountName) {
        return accountFacade.checkAccountExist(accountName);
    }

    public Account findAccountById(String id) {
        if (id != null) {
            return accountFacade.find(id);
        } else {
            return null;
        }
    }

    public Account findAccountFullById(String id) {
        if (id != null) {
            return accountFacade.findFull(id);
        } else {
            return null;
        }
    }

    public List<Account> findAllAccount() {
        return accountFacade.findAll();
    }

    public List<Account> findAllAccount(int pageSize, int page) {
        if (pageSize <= 0 || page < 0) {
            return new LinkedList<Account>();
        } else {
            return accountFacade.findAll(pageSize, page);
        }
    }

    public List<Account> findAllFullAccount(int pageSize, int page) {
        if (pageSize <= 0 || page < 0) {
            return new LinkedList<Account>();
        } else {
            return accountFacade.findAllFull(pageSize, page);
        }
    }

    public Account findAccountByName(String accountName) {
        return accountFacade.findByAccountName(accountName);
    } // Add business logic below. (Right-click in editor and choose


    public List<Account> findAccountByIdFuzzy(String id) {
        if (id != null) {
            return accountFacade.findByAccountIdFuzzy(id);
        } else {
            return null;
        }
    }

    public List<Account> findAccountByNameFuzzy(String accountName) {
        if (accountName != null) {
            return accountFacade.findByAccountNameFuzzy(accountName);
        } else {
            return null;
        }
    }

    private SecurityEvent validateFormatPassword(String newPassword) {
        //validate length
        SecurityPolicy pwdLengthPolicy = policyFacade.find(SecurityPolicies.PASSWORD_LENGTH_POLICY);
        SecurityPolicy pwdMinLengthPolicy = policyFacade.find(SecurityPolicies.PASSWORD_MIN_LENGTH);
        SecurityPolicy pwdManLengthPolicy = policyFacade.find(SecurityPolicies.PASSWORD_MAX_LENGTH);
        if (pwdLengthPolicy == null) {
            return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
        } else {
            if (pwdLengthPolicy.getValue().equals("ENABLED")) {
                if (pwdMinLengthPolicy == null ||
                        pwdMinLengthPolicy.getValue() == null ||
                        pwdManLengthPolicy == null ||
                        pwdManLengthPolicy.getValue() == null) {
                    return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
                } else {
                    try {
                        int minLength = Integer.parseInt(pwdMinLengthPolicy.getValue());
                        int maxLength = Integer.parseInt(pwdManLengthPolicy.getValue());
                        if (newPassword.length() >= minLength && newPassword.length() <= maxLength) {
                        } else {
                            return SecurityEvent.PASSWORD_LENGTH_INVALID;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
                    }
                }
            } else {
            }
        }
        //validate pattern
        SecurityPolicy pwdFormatPolicy = policyFacade.find(SecurityPolicies.PASSWORD_FORMAT_POLICY);
        if (pwdFormatPolicy == null) {
            return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
        } else {
            if (pwdFormatPolicy.getValue() == null) {
                return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
            } else {
                if (pwdFormatPolicy.getValue().equals("ENABLED")) {
                    SecurityPolicy pwdPattern = policyFacade.find(SecurityPolicies.PASSWORD_PATTERN);
                    if (pwdPattern == null || pwdPattern.getValue() == null) {
                        return SecurityEvent.SECURITY_POLICY_CFG_ERROR;
                    } else {
                        if (newPassword.matches(pwdPattern.getValue())) {
                            return SecurityEvent.PASSWORD_PATTERN_VALID;
                        } else {
                            return SecurityEvent.PASSWORD_PATTERN_INVALID;
                        }
                    }
                }
                return SecurityEvent.PASSWORD_PATTERN_VALID;
            }
        }
    }

    public void addRoleToAccount(Account account, Role role) {
        if (account == null || role == null || account.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        List<Role> roles = account.getRoleCollection();
        roles.size();
        roles.add(role);
    }

    public void editRoleOfAccount(Account account, List<Role> roles) {
        if (account == null || roles == null || account.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        account.setRoleCollection(roles);

    }

    public void removeRoleFromAccount(Account account, Role role) {
        if (account == null || role == null || account.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        List<Role> roles = account.getRoleCollection();
        roles.size();

        if (roles.contains(role)) {
            roles.remove(role);
        }
    }

    public void addAccountToGroup(Account account, UserGroup group) {
        if (account == null || group == null || account.getId() == null || group.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        List<UserGroup> groups = account.getGroupCollection();
        groups.size();
        groups.add(group);
    }

    public void editGroupOfAccount(Account account, List<UserGroup> groups) {
        if (account == null || groups == null || account.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        account.setGroupCollection(groups);
    }

    public void removeAccountFromGroup(Account account, UserGroup group) {
        if (account == null || group == null || account.getId() == null || group.getId() == null) {
            return;
        }

        account = accountFacade.find(account.getId());
        List<UserGroup> groups = account.getGroupCollection();
        groups.size();

        if (groups.contains(group)) {
            groups.remove(group);
        }
    }
}


==========================================================

EntityFacade ___ AccountFacade
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vicp.madz.security.facade.entity;

import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;
import net.vicp.madz.security.entity.Account;
import net.vicp.madz.security.entity.Role;

/**
 *
 * @author Administrator
 */
@Stateless
public class AccountFacade implements AccountFacadeLocal {

    @PersistenceContext(unitName = "Security-ejbPU")
    private EntityManager em;

    public void create(Account account) {
        try {
            em.persist(account);
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (IllegalStateException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (TransactionRequiredException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (EntityExistsException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        }
    }

    public void edit(Account account) {
        try {
            em.merge(account);
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (IllegalStateException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (TransactionRequiredException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        }
    }

    public void remove(Account account) {
        try {
            em.remove(em.merge(account));
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (IllegalStateException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (TransactionRequiredException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        }
    }

    public Account find(Object id) {
        try {
            Account account = em.find(net.vicp.madz.security.entity.Account.class, id);
            return account;
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (IllegalStateException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        }
        return null;
    }

    public List<Account> findAll() {
        try {
            return em.createQuery("select object(o) from Account as o").getResultList();
        } catch (IllegalArgumentException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        } catch (IllegalStateException ex) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ex);
        }
        return new LinkedList<Account>();
    }

    public boolean checkAccountExist(String accountName) {
        Query checkQuery = em.createNamedQuery("checkExist");
        Long result = (Long) checkQuery.setParameter("accountName", accountName).getSingleResult();
        if (result < 1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkAccountLocked(String accountName) {
        Query checkQuery = em.createNamedQuery("checkNotLocked").setParameter("accountName", accountName);
        Long result = (Long) checkQuery.getSingleResult();
        if (result == 1) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkPasswordValid(String accountName, String password) {
        Query checkQuery = em.createNamedQuery("validate");
        Long result = (Long) checkQuery.setParameter("accountName", accountName).setParameter("password", password).getSingleResult();
        if (result < 1) {
            return false;
        } else {
            return true;
        }
    }

    public Account findByAccountName(String accountName) {
        Query findQuery = em.createNamedQuery("findByAccountName");
        Account account = (Account) findQuery.setParameter("accountName", accountName).getSingleResult();
        return account;
    }

    public List<Account> findByAccountIdFuzzy(String accountId) {
        Query findQuery = em.createNamedQuery("findByAccountIdFuzzy");
        List<Account> accounts = findQuery.setParameter("id", accountId).getResultList();
        return accounts;
    }

    public List<Account> findByAccountNameFuzzy(String accountName) {
        Query findQuery = em.createNamedQuery("findByAccountNameFuzzy");
        List<Account> accounts = findQuery.setParameter("accountName", accountName).getResultList();
        return accounts;
    }

    public List<Account> findAll(int pageSize, int page) {
        List<Account> accounts = em.createQuery("select object(o) from Account as o ").setMaxResults(pageSize).setFirstResult(page * pageSize).getResultList();
        return accounts;
    }

    public List<Account> findAllFull(int pageSize, int page) {
        List<Account> accounts = em.createQuery("select object(o) from Account as o JOIN FETCH o.groupCollection JOIN FETCH o.roleCollection JOIN FETCH o.securityLogCollection").setFirstResult(page * pageSize).setMaxResults(pageSize).getResultList();
        return accounts;
    }

    public Account findFull(Object id) {
        Account account = em.find(net.vicp.madz.security.entity.Account.class, id);
        fetchCommonLazyRelationship(account);
        return account;
    }

    private void fetchCommonLazyRelationship(Account a) {
        a.getGroupCollection().size();
        List<Role> roles = (List<Role>) a.getRoleCollection();
        for (Role r : roles) {
            r.getFeatureCollection().size();
        }
    }
}
[Message sent by forum member 'zhongdj' (zhongdj)]

http://forums.java.net/jive/thread.jspa?messageID=294406