Package com.din.din.webapp.beans

Source Code of com.din.din.webapp.beans.LoginBean

package com.din.din.webapp.beans;

import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.din.din.webapp.beans.util.LoginUtil;
import com.din.din.webapp.listeners.LoginPhaseListener.LoginListener;

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {
  private static final long serialVersionUID = -8173146749595183650L;

  public static final String ACCESSOR = "#{loginBean}";
 
  private boolean loggedIn = false;
  private List<SoftReference<LoginListener>> listeners = new ArrayList<SoftReference<LoginListener>>();
 
  public boolean isLoggedIn() {
    return LoginUtil.isLoggedIn();
  }
 
  public void performCheck() {
    boolean currentLoginStatus = isLoggedIn();
    if(currentLoginStatus && !loggedIn) {
      triggerLoginEvent(currentLoginStatus);
    } else if (!currentLoginStatus && loggedIn) {
      triggerLoginEvent(currentLoginStatus);
    }
    this.loggedIn = currentLoginStatus;
  }
 
  public void addListener(LoginListener listener) {
    listeners.add(new SoftReference<LoginListener>(listener));
    if(loggedIn) {
      listener.onLogin();
    }
  }
 
  private void triggerLoginEvent(boolean loggedIn) {
    Iterator<SoftReference<LoginListener>> iterator = listeners.iterator();
    while(iterator.hasNext()) {
      SoftReference<LoginListener> listenerRef = iterator.next();
      LoginListener listener = listenerRef.get();
      if(listener != null) {
        if(loggedIn) {
          listener.onLogin();
        } else {
          listener.onLogoff();
        }
      } else {
        iterator.remove();
      }
    }
  } 
}
TOP

Related Classes of com.din.din.webapp.beans.LoginBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.