Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   * 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.airavata.ws.monitor;
 
 import java.net.URI;
 import java.util.Date;
 
 
Manipulate XML Events
 
 public class MonitorUtil {
 
     private static final Logger logger = LoggerFactory.getLogger(MonitorUtil.class);

    
Workflow tracking namespace
 
     public static final XmlNamespace WOR_NS = ..newNamespace("wor",
             "http://airavata.apache.org/schemas/wft/2011/08");

    
XBaya events namespace
 
     public static final XmlNamespace XBAYA_EVENTS_NS = ..newNamespace("xbaya",
             "http://www.extreme.indiana.edu/xgws/xbaya/ns/2006/");

    
gotResult
 
     public static final String GOT_RESULT_EVENT_TAG = "receivedResult";

    
description
 
     public static final String DESCRIPTION_TAG = "description";

    
timestamp
 
     public static final String TIMESTAMP_TAG = "timestamp";

    
request In invokingService
 
     public static final String REQUEST = "request";

    
result In sendingResult
 
     public static final String RESULT = "result";

    
body In sendingResult
 
     public static final String BODY = "body";

    
experimentID In Notification Message
 
     public static final String WORKFLOW_EXPERIMENT_ID = "experimentID";
    
workflowID workflow instance ID.
    private static final String WORKFLOW_ID_ATTRIBUTE = "workflowID";

    
serviceID In workflowInitialized notification, this is the worklfow instance ID.
    private static final String SERVICE_ID_ATTRIBUTE = "serviceID";

    
workflowNodeID Node ID.
    private static final String WORKFLOW_NODE_ID_ATTRIBUTE = "workflowNodeID";

    
receiver Extract a node ID from here when a notification is sent by a workflow when the workflow is sending a message to a service.
    private static final String RECEIVER_TAG = "receiver";

    
responder Extract a node ID from here when a notification is sent by a workflow when the workflow is receiving a message from a service.
    private static final String RESPONDER_TAG = "responder";

    
notificationSource Extract a node ID from here when a service sends notification.
    private static final String NOTIFICATION_SOURCE_TAG = "notificationSource";
    // Followings are specific to some event types.

    
location In publishURL
    private static final String LOCATION_TAG = "location";

    
retryStatusCount In computationDuration
    private static final String RETRY_STATUS_COUNT_TAG = "retryStatusCount";

    
mappedResource In resourceMapping
    private static final String MAPPED_RESOURCE_TAG = "mappedResource";

    
dataProduct In a couple of data-related notifications.
    private static final String DATA_PRODUCT_TAG = "dataProduct";

    
durationInMillis In computationDuration
    @SuppressWarnings("unused")
    private static final String DURATION_IN_MILLS_TAG = "durationInMillis";

    
Type of the notification event.
    public enum EventType {
        
unknown
        UNKNOWN("unknown"),
        // Notification from a workflow

        
workflowInitialized
        WORKFLOW_INITIALIZED("workflowInitialized"),

        
workflowInvoked
        WORKFLOW_INVOKED("workflowInvoked"),

        
workflowTerminated
        WORKFLOW_TERMINATED("workflowTerminated"),

        
invokingService
        INVOKING_SERVICE("invokingService"),

        
invokingServiceSucceeded
        INVOKING_SERVICE_SUCCEEDED("invokingServiceSucceeded"),

        
invokingServiceFailed
        INVOKING_SERVICE_FAILED("invokingServiceFailed"),

        
receivedResult
        RECEIVED_RESULT("receivedResult"),

        
receivedFault
        RECEIVED_FAULT("receivedFault"),
        // Notification from a service

        
serviceInvoked
        SERVICE_INVOKED("serviceInvoked"),

        
sendingResult
        SENDING_RESULT("sendingResult"),

        
sendingResponseFailed
        SENDING_RESPONSE_FAILED("sendingResponseFailed"),

        
sendingFault
        SENDING_FAULT("sendingFault"),
        // Other types of notification from a service.
        
logInfo
        LOG_INFO("logInfo"),

        
logException
        LOG_EXCEPTION("logException"),

        
logWarning
        LOG_WARNING("logWarning"),

        
logDebug
        LOG_DEBUG("logDebug"),

        
dataConsumed
        DATA_CONSUMED("dataConsumed"),

        
dataProduced
        DATA_PRODUCED("dataProduced"),

        
dataReceiveDuration
        DATA_RECEIVE_DURATION("dataReceiveDuration"),

        
applicationAudit
        APPLICATION_AUDIT("applicationAudit"),

        
computationDuration
        COMPUTATION_DURATION("computationDuration"),

        
publishURL
        PUBLISH_URL("publishURL"),

        
resourceMapping
        RESOURCE_MAPPING("resourceMapping");
        String name;
        EventType(String name) {
            this. = name;
        }
    }

    

Parameters:
event
Returns:
The type of the event.
    public static EventType getType(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        XmlNamespace ns = event.getNamespace();
        String name = event.getName();
        if (.equals(ns)) {
            if (.equals(name)) {
                return .;
            } else {
                return .;
            }
        } else if (.equals(ns)) {
            for (EventType type : EventType.values()) {
                if (type.name.equals(name)) {
                    return type;
                }
            }
        }
        return .;
    }

    

Parameters:
event
Returns:
The timestamp.
    public static Date getTimestamp(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        XmlElement timestampEl = event.element();
        if (timestampEl == null)
            return null;
        String timestamp = timestampEl.requiredText();
        DcDate date = DcDate.create(timestamp);
        return new Date(date.getTimeInMillis());
    }

    

Parameters:
event
Returns:
The node ID if the message contains it; "", otherwise
    public static String getNodeID(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        XmlElement idElement = getIDElement(event);
        String nodeID = null;
        if (idElement != null) {
            nodeID = idElement.attributeValue();
        }
        if(nodeID == null){
        }
        if (nodeID == null) {
            nodeID = "";
        }
        return nodeID;
    }

    

Parameters:
event
Returns:
The node ID if the message contains it; "", otherwise
    public static String getExperiementID(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        XmlElement idElement = event.element(,);
        String nodeID = null;
        if (idElement != null) {
            nodeID = idElement.attributeValue();
        }if(nodeID == null){
        }
        if (nodeID == null) {
            nodeID = "";
        }
        return nodeID;
    }

    

Parameters:
event
Returns:
The workflow instance ID. null if there is no workflow instance ID.
    public static URI getWorkflowID(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        EventType type = getType(event);
        XmlElement idElement = getIDElement(event);
        if (idElement == null) {
            return null;
        }
        String workflowID;
        switch (type) {
        case :
        case :
            // Special cases
            workflowID = idElement.attributeValue();
            break;
        default:
            // Default
            workflowID = idElement.attributeValue();
            break;
        }
        if (workflowID == null || workflowID.length() == 0) {
            return null;
        }
        try {
            return new URI(workflowID);
        } catch (URISyntaxException e) {
            .error(e.getMessage(), e);
            return null;
        }
    }

    

Parameters:
event
Returns:
The type of the event to display
    public static String getStatus(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        EventType type = getType(event);
        String status;
        switch (type) {
        case :
            status = "INFO";
            break;
        case :
            status = "EXCEPTION";
            break;
        case :
            status = "WARNING";
            break;
        case :
            status = "DEBUG";
            break;
        default:
            status = event.getName();
        }
        return status;
    }

    

Parameters:
event
Returns:
The message to display.
    public static String getMessage(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        String description = null;
        XmlElement descElement = event.element();
        if (descElement != null) {
            description = descElement.requiredText();
        }
        if (description == null || description.length() == 0) {
            // It might be a data-related notification
            XmlElement dataProduct = event.element();
            if (dataProduct != null) {
                descElement = dataProduct.element();
                if (descElement != null) {
                    description = descElement.requiredText();
                }
            }
        }
        if (description == null) {
            description = "";
        }
        return description;
    }

    

Parameters:
event
Returns:
The location.
    public static String getLocation(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        XmlElement locationEl = event.element();
        if (locationEl != null) {
            String location = locationEl.requiredText();
            return location;
        } else {
            return null;
        }
    }

    
Gets the mapped resource from the event.

Parameters:
event The event, the type of which has to be resourceMapping.
Returns:
The resource
    public static String getMappedResource(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        EventType type = getType(event);
        if (type != .) {
            throw new IllegalArgumentException("Event must have resourceMapping type instead of " + type);
        }
        XmlElement mappedResource = event.element();
        String resource = mappedResource.requiredText();
        return resource;
    }

    
Gets the retry count from the event.

Parameters:
event The event, the type of which has to be resourceMapping.
Returns:
The resource
    public static String getRetryCount(XmlElement event) {
        if (event == null) {
            throw new IllegalArgumentException("null");
        }
        EventType type = getType(event);
        if (type != .) {
            throw new IllegalArgumentException("Event must have resourceMapping type instead of " + type);
        }
        XmlElement retryCountElement = event.element();
        String retryCount = retryCountElement.requiredText();
        return retryCount;
    }

    
Gets the workflow instance ID.

Parameters:
event The event, the type of which has to be workflowInitialized.
Returns:
The workflowInstanceID
    public static URI getWorkflowInstanceID(XmlElement event) {
        EventType type = getType(event);
        if (!(type == . || type == .)) {
            throw new IllegalArgumentException(
                    "Event must be an workflowInitialized type or an workflowTerminated type instead of " + type);
        }
        XmlElement notificationSource = event.element();
        if (notificationSource == null) {
            throw new WorkflowRuntimeException("The notification should have " +  + " element.");
        }
        String workflowInstanceID = notificationSource.attributeValue();
        if (workflowInstanceID == null) {
            throw new WorkflowRuntimeException("The notification should have " +  + " attribute.");
        }
        try {
            return new URI(workflowInstanceID);
        } catch (URISyntaxException e) {
            throw new WorkflowRuntimeException(e);
        }
    }

    

Parameters:
event
Returns:
The element that has workflow ID, node ID, etc.
    private static XmlElement getIDElement(XmlElement event) {
        EventType type = getType(event);
        switch (type) {
        // The following three don't include node ID, but includes workflow ID.
        case :
        case :
        case :
            // The followings include both workflow ID and node ID.
            // TODO they might be used by a workflow too.
        case :
        case :
        case :
        case // TODO make sure
            // The followings are used only in services.
        case :
        case :
        case :
        case :
        case :
        case :
        case :
        case :
        case :
        case :
        case :
        case :
                return event.element();
        case // TODO make sure
        case // TODO make sure
            return event.element();
        case :
        case :
            return event.element();
        case :
            // Most of unknown types are from service.
            return event.element();
        default:
            // Most of unknown types are from service.
            return event.element();
        }
    }
New to GrepCode? Check out our FAQ X