Package org.huihoo.willow.client

Source Code of org.huihoo.willow.client.ObjectClientImpl

//----------------------------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.client;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.willow.Context;
import org.huihoo.willow.Engine;
import org.huihoo.willow.Globals;
import org.huihoo.willow.Server;
import org.huihoo.willow.ServerFactory;
import org.huihoo.willow.Service;
import org.huihoo.willow.session.SessionManager;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.client.ObjectClient;
import org.huihoo.workflow.client.WorkflowClient;
import org.huihoo.workflow.client.object.model.ObjectService;
import org.huihoo.workflow.runtime.WorkflowService;
import org.huihoo.workflow.runtime.WorkflowSession;
import org.huihoo.workflow.store.RealmDatabase;
import org.huihoo.workflow.store.UserDatabase;
import org.huihoo.workflow.usermodel.WorkflowParticipant;
/**
* @author reic
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class ObjectClientImpl implements ObjectClient
{
  private static Log log = LogFactory.getLog(ObjectClientImpl.class);
  protected Engine willow_engine;
  protected Hashtable env;
  protected WorkflowSession session;
  //-----------------------------------------------------------constructor
  public ObjectClientImpl(Hashtable environment) throws WorkflowException
  {
    init(environment);
  }
  public WorkflowSession getSession() throws WorkflowException
  {
    return session;
  }
  public void disconnect() throws WorkflowException
  {
    session.invalidate();
  }
  public ObjectService findService(String service_name) throws WorkflowException
  {
    WorkflowParticipant participant = session.getParticipant();
    String userid = participant.getUUID();
    String password = participant.getPassword();
    if (willow_engine != null)
    {
      Context context = willow_engine.findChild(service_name);
      if (context != null && (context instanceof WorkflowService))
      {
        WorkflowService workflowService = (WorkflowService) context;
        if (!authenticate(workflowService, userid, password))
        {
          throw new WorkflowException("login org.huihoo.workflow service unsuccessfully.userid=" + userid + ",password=" + password);
        }
       
        return new ObjectService(this,workflowService);
      }
    }
    return null;
  }
  public ObjectService[] findServices() throws WorkflowException
  {
    Vector vector = new Vector(0);
    WorkflowParticipant participant = session.getParticipant();
    String userid = participant.getUUID();
    String password = participant.getPassword();
    if (willow_engine != null)
    {
      Context[] contexts = willow_engine.findChildren();
      for(int i=0;i<contexts.length;++i)
      {
        Context context=contexts[i];
       
        if (context != null && (context instanceof WorkflowService))
        {
          WorkflowService workflowService = (WorkflowService) context;
          if (authenticate(workflowService, userid, password))
          {
            vector.add(new ObjectService(this,workflowService));
          }
        }
      }

    }
   
    int size = vector.size();
    ObjectService[] result = new ObjectService[size];
    for (int i = 0; i < size; ++i)
    {
      result[i] = (ObjectService) vector.elementAt(i);
    }
    return result;
  }
  private void init(Hashtable refEnv) throws WorkflowException
  {
    if (refEnv == null)
    {
      refEnv = new Hashtable();
    }
    this.env = (Hashtable) refEnv.clone();
    String engine_name = null;
    String provider_url = (String) refEnv.get(WorkflowClient.PROVIDER_URL);
    if (provider_url != null && provider_url.length() > 0)
    {
      engine_name = parseEngineName(provider_url);
    }
    Server server = ServerFactory.getServer();
    Service[] services = server.findServices();
    if (engine_name == null)
    {
      if (services.length > 0)
      {
        willow_engine = services[0].getEngine();
      }
    }
    else
    {
      for (int i = 0; i < services.length; ++i)
      {
        if (engine_name.equals(services[i].getEngine().getName()))
        {
          willow_engine = services[i].getEngine();
        }
      }
    }
    if (willow_engine != null)
    {
      String username = (String) env.get(WorkflowClient.SECURITY_PRINCIPAL);
      String password = (String) env.get(WorkflowClient.SECURITY_CREDENTIALS);
      SessionManager manager = willow_engine.getSessionManager();
      RealmDatabase realmDatabase = willow_engine.getRealmDatabase();
      WorkflowParticipant participant = realmDatabase.authenticate(username, password);
      if (participant == null)
      {
        throw new WorkflowException("login workflow  engine unsuccessfully.username=" + username + ",password=" + password);
      }
      this.session = manager.createSession();
      this.session.setParticipant(participant);
    }
  }
  private boolean authenticate(WorkflowService workflowService, String userid, String password) throws WorkflowException
  {
    UserDatabase userDatabase = workflowService.getUserDatabase();
    WorkflowParticipant user = userDatabase.findParticipant(userid);
    if (user != null && user.getPassword().equals(password))
    {
      return true;
    }
    return false;
  }
  static String parseEngineName(String provider_url)
  {
    String engineName = null;
    if (provider_url != null && provider_url.length() > 0)
    {
      int schemeLength = 0;
      String schemePrefix = Globals.PROTOCOL_WORKFLOW_LOCAL + ":";
      if (provider_url.startsWith(schemePrefix))
      {
        schemeLength = schemePrefix.length();
      }
      if (schemeLength > 0)
      {
        engineName = provider_url.substring(schemeLength);
        if (engineName != null && engineName.length() > 0)
        {
          while (engineName.charAt(0) == '/')
          {
            if (engineName.length() == 1)
            {
              engineName = null;
              break;
            }
            engineName = engineName.substring(1);
          }
          if (engineName != null && engineName.length() > 0)
          {
            int pos = engineName.indexOf("/");
            if (pos > 0)
            {
              engineName = engineName.substring(0, pos);
            }
          }
        }
      }
    }
    return engineName;
  }
}
TOP

Related Classes of org.huihoo.willow.client.ObjectClientImpl

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.