Package org.apache.oodt.pcs.util

Source Code of org.apache.oodt.pcs.util.WorkflowManagerUtils

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.oodt.pcs.util;

//JDK imports
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

//OODT imports
import org.apache.oodt.cas.workflow.structs.WorkflowInstance;
import org.apache.oodt.cas.workflow.system.XmlRpcWorkflowManagerClient;
import org.apache.xmlrpc.XmlRpcClient;

/**
*
* A set of utility methods that can be used by PCS that need to communicate
* with the Workflow Manager.
*
* @author mattmann
* @version $Revision$
*/
public class WorkflowManagerUtils {

  /* our workflow manager client */
  private XmlRpcWorkflowManagerClient client;

  /* our log stream */
  private static final Logger LOG = Logger.getLogger(WorkflowManagerUtils.class
      .getName());

  private URL wmUrl;

  public WorkflowManagerUtils(String urlStr) {
    this(safeGetUrlFromString(urlStr));
  }

  public WorkflowManagerUtils(URL url) {
    this.client = new XmlRpcWorkflowManagerClient(url);
    this.wmUrl = url;
  }

  public WorkflowManagerUtils(XmlRpcWorkflowManagerClient client) {
    this.client = client;
  }

  public void updateWorkflowInstanceStatus(String wInstId, String status) {
    try {
      this.client.updateWorkflowInstanceStatus(wInstId, status);
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  public List<WorkflowInstance> safeGetWorkflowInstances() {
    if (!isConnected())
      return Collections.EMPTY_LIST;

    try {
      return this.client.getWorkflowInstances();
    } catch (Throwable ignore) {
      return Collections.EMPTY_LIST;
    }
  }

  public boolean isConnected() {
    try {
      XmlRpcClient c = new XmlRpcClient(this.client.getWorkflowManagerUrl());
      c.execute("workflowmgr.getWorkflowInstances", new Vector());
      return true;
    } catch (Exception ignore) {
      return false;
    }
  }

  public List safeGetWorkflowInstancesByStatus(String status) {
    try {
      return this.client.getWorkflowInstancesByStatus(status);
    } catch (Exception e) {
      LOG.log(Level.WARNING,
          "exception obtaining workflow instances by status: [" + status
              + "]: message: " + e.getMessage());
      return null;
    }
  }

  public int safeGetNumWorkflowInstancesByStatus(String status) {
    try {
      return this.client.getNumWorkflowInstancesByStatus(status);
    } catch (Exception e) {
      LOG.log(Level.WARNING,
          "exception obtaining num workflow instances by status: [" + status
              + "]: message: " + e.getMessage());
      return -1;
    }
  }

  /**
   * @return the client
   */
  public XmlRpcWorkflowManagerClient getClient() {
    return client;
  }

  /**
   * @param client
   *          the client to set
   */
  public void setClient(XmlRpcWorkflowManagerClient client) {
    this.client = client;
    if (this.client != null) {
      this.wmUrl = this.client.getWorkflowManagerUrl();
    }
  }

  private static URL safeGetUrlFromString(String urlStr) {
    URL url = null;

    try {
      url = new URL(urlStr);
    } catch (MalformedURLException e) {
      LOG.log(Level.SEVERE, "PCS: Unable to generate url from url string: ["
          + urlStr + "]: Message: " + e.getMessage());
    }

    return url;
  }

  /**
   *
   * @return The {@link URL} pointer to the Workflow Manager that this
   *         WorkflowManagerUtils communicates with.
   */
  public URL getWmUrl() {
    return this.wmUrl;
  }

}
TOP

Related Classes of org.apache.oodt.pcs.util.WorkflowManagerUtils

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.