Package org.huihoo.willow.store

Source Code of org.huihoo.willow.store.UserDatabaseRealm

//----------------------------BEGIN LICENSE----------------------------
/*
* Willow : the Open Source WorkFlow Project
* Distributable under GNU LGPL license by gun.org
*
* Copyright (C) 2004-2010 huihoo.org
* Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com>
*
* ====================================================================
* Project Homepage : http://www.huihoo.org/willow
* Source Forge     : http://sourceforge.net/projects/huihoo
* Mailing list     : willow@lists.sourceforge.net
*/
//----------------------------END  LICENSE-----------------------------
package org.huihoo.willow.store;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.willow.Engine;
import org.huihoo.willow.Lifecycle;
import org.huihoo.willow.LifecycleException;
import org.huihoo.willow.LifecycleListener;
import org.huihoo.willow.Logger;
import org.huihoo.willow.startup.WorkflowProperties;
import org.huihoo.willow.util.LifecycleSupport;
import org.huihoo.willow.util.StringManager;
import org.huihoo.workflow.store.RealmDatabase;
import org.huihoo.workflow.store.SchemaContext;
import org.huihoo.workflow.store.spi.SpiUserDatabase;
import org.huihoo.workflow.usermodel.WorkflowParticipant;

import org.huihoo.workflow.impl.store.DatabaseRegistry;
import com.zosatapo.commons.store.Store;
/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class UserDatabaseRealm implements RealmDatabase, Lifecycle
{
  private static Log log = LogFactory.getLog(UserDatabaseRealm.class);
  //----------------------------------------------------- Instance Variables
  private SpiUserDatabase userDatabase;
  /**
   * The Engine with which this RealmDatabase is associated.
   */
  protected Engine engine = null;
  /**
     * The debugging detail level for this component.
     */
  protected int debug = 0;
 
  protected Store store;
 
  /**
   * Descriptive information about this RealmDatabase implementation.
   */
  protected static final String info = "org.huihoo.willow.store.UserDatabaseRealm/1.0";
  /**
   * The lifecycle event support for this component.
   */
  protected LifecycleSupport lifecycle = new LifecycleSupport(this);
  /**
   * The string manager for this package.
   */
  protected static StringManager sm = StringManager.getManager(Constants.PACKAGE);
  /**
   * Has this component been started?
   */
  protected boolean started = false;
  /**
   * The property change support for this component.
   */
  protected PropertyChangeSupport support = new PropertyChangeSupport(this);
  //------------------------------------------------------------- Properties
  /**
   * Return the Engine with which this Realm has been associated.
   */
  public Engine getEngine()
  {
    return (engine);
  }
  /**
   * Set the Provider with which this Realm has been associated.
   *
   * @param engine The associated this
   */
  public void setEngine(Engine engine)
  {
    Engine oldEngine = this.engine;
    this.engine = engine;
    support.firePropertyChange("engine", oldEngine, this.engine);
  }
  /**
   * Return the debugging detail level for this component.
   */
  public int getDebug()
  {
    return (this.debug);
  }
  /**
   * Set the debugging detail level for this component.
   *
   * @param debug The new debugging detail level
   */
  public void setDebug(int debug)
  {
    this.debug = debug;
  }
 
//------------------------------------------------------------------------------------
// Store utilities
//------------------------------------------------------------------------------------
  public Store                getStore()
  {
    return this.store;
  }
  public void                 setStore(Store store)
  {
    this.store=store;
  }
  /**
   * Return descriptive information about this Realm implementation and
   * the corresponding version number, in the format
   * <code>&lt;description&gt;/&lt;version&gt;</code>.
   */
  public String getInfo()
  {
    return info;
  }
  // --------------------------------------------------------- Public Methods
  public WorkflowParticipant authenticate(String username, String credentials)
  {
    // Does a user with this username exist?
    WorkflowParticipant user = userDatabase.findParticipant(username);
    if (user == null)
    {
      return (null);
    }
    // Do the credentials specified by the user match?
    // FIXME - Update all realms to support encoded passwords
    boolean validated = false;
    validated = credentials.equals(user.getPassword());
    if (!validated)
    {
      if (debug >= 2)
      {
        log(sm.getString("userDatabaseRealm.authenticateFailure", username));
        return (null);
      }
    }
    // Construct a GenericPrincipal that represents this user
    if (debug >= 2)
    {
      log(sm.getString("userDatabaseRealm.authenticateSuccess", username));
    }
    return user;
  }
  /**
   * Add a property change listener to this component.
   *
   * @param listener The listener to add
   */
  public void addPropertyChangeListener(PropertyChangeListener listener)
  {
    support.addPropertyChangeListener(listener);
  }
  /**
   * Remove a property change listener from this component.
   *
   * @param listener The listener to remove
   */
  public void removePropertyChangeListener(PropertyChangeListener listener)
  {
    support.removePropertyChangeListener(listener);
  }
  // ------------------------------------------------------ Lifecycle Methods
  /**
   * Add a lifecycle event listener to this component.
   *
   * @param listener The listener to add
   */
  public void addLifecycleListener(LifecycleListener listener)
  {
    lifecycle.addLifecycleListener(listener);
  }
  /**
   * Get the lifecycle listeners associated with this lifecycle. If this
   * Lifecycle has no listeners registered, a zero-length array is returned.
   */
  public LifecycleListener[] findLifecycleListeners()
  {
    return lifecycle.findLifecycleListeners();
  }
  /**
   * Remove a lifecycle event listener from this component.
   *
   * @param listener The listener to remove
   */
  public void removeLifecycleListener(LifecycleListener listener)
  {
    lifecycle.removeLifecycleListener(listener);
  }
  /**
   * Prepare for the beginning of active use of the public methods of this
   * component.  This method should be called before any of the public
   * methods of this component are utilized.  It should also send a
   * LifecycleEvent of type START_EVENT to any registered listeners.
   *
   * @exception LifecycleException if this component detects a fatal error
   *  that prevents this component from being used
   */
  public void start() throws LifecycleException
  {
    // Validate and update our current component state
    if (started)
    {
      log.info(sm.getString("userdatabaseRealm.alreadyStarted"));
      return;
    }
    lifecycle.fireLifecycleEvent(START_EVENT, null);
    started = true;
    Throwable throwable = null;
    SpiUserDatabase gudb = DatabaseRegistry.getUserDatabase(DatabaseRegistry.WILLOW_DRIVER_CLASS);
    if (gudb != null)
    {
      try
      {
        userDatabase = (SpiUserDatabase) gudb.clone();
       
        SchemaContext schemaContext = new SchemaContext(WorkflowProperties.getSchemaContextFile());
        userDatabase.setSchemaContext(schemaContext);
       
        Store tmpStore=(this.store==null?WorkflowProperties.getUserStore():this.store);
        userDatabase.setStore(tmpStore);
       
        userDatabase.start();
      }
      catch (Throwable ex)
      {
        throwable = ex;
        userDatabase = null;
      }
    }
    // Create a MessageDigest instance for credentials, if desired
   
    if (userDatabase == null)
    {
      if (throwable != null)
      {
        log.error(sm.getString("userDatabaseRealm.noDatabase"), throwable);
        throw new LifecycleException(sm.getString("userDatabaseRealm.noDatabase"), throwable);
      }
      else
      {
        throw new LifecycleException(sm.getString("userDatabaseRealm.noDatabase"));
      }
    }
  }
  /**
   * Gracefully terminate the active use of the public methods of this
   * component.  This method should be the last one called on a given
   * instance of this component.  It should also send a LifecycleEvent
   * of type STOP_EVENT to any registered listeners.
   *
   * @exception LifecycleException if this component detects a fatal error
   *  that needs to be reported
   */
  public void stop() throws LifecycleException
  {
    // Validate and update our current component state
    if (!started)
    {
      log.info(sm.getString("userdatabaseRealm.notStarted"));
      return;
    }
    lifecycle.fireLifecycleEvent(STOP_EVENT, null);
    started = false;
  }
  protected Logger getLogger()
  {
    return engine.getLogger();
  }
  /**
    * Log a message on the Logger associated with our Engine (if any)
    *
    * @param message Message to be logged
    */
  protected void log(String message)
  {
    Logger logger = getLogger();
    String localName = "UserDatabaseRealm";
    if (logger != null)
    {
      logger.log(localName + " " + message);
    }
    else
    {
      System.out.println(localName + " " + message);
    }
  }
}
TOP

Related Classes of org.huihoo.willow.store.UserDatabaseRealm

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.