View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   package net.kwfgrid.gwes.operationmapper;
8   
9   import net.kwfgrid.gworkflowdl.structure.Operation;
10  import org.apache.log4j.Logger;
11  import net.kwfgrid.gwes.GenericWorkflowHandler;
12  import net.kwfgrid.gwes.Constants;
13  import net.kwfgrid.gwes.exception.OperationMapperException;
14  import net.kwfgrid.gwes.exception.StateTransitionException;
15  import net.kwfgrid.gworkflowdl.structure.Transition;
16  import net.kwfgrid.gworkflowdl.structure.Workflow;
17  import net.kwfgrid.gworkflowdl.structure.OperationClass;
18  import de.fraunhofer.first.resmatch.client.ResourceMatcherClient;
19  
20  import java.util.ArrayList;
21  
22  /**
23   * Maps red operations to yellow ones, using the Resource Matcher service.
24   * @author Andreas Hoheisel
25   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
26   * @version $Id: Yellow2BlueResourceMatcherOperationMapper.java 1520 2011-02-28 16:44:35Z hoheisel $
27   */
28  public class Yellow2BlueResourceMatcherOperationMapper extends OperationMapper {
29  
30      /**
31       * log4j logger.
32       */
33      final static Logger logger = Logger.getLogger(Yellow2BlueResourceMatcherOperationMapper.class);
34  
35      /**
36       * Maps the operation contained in the specified transition to a less abstract level.
37       *
38       * @param transition
39       * @return <code>true</code> if a mapping was possible, <code>false</code> otherwise.
40       */
41      public boolean processTransition(GenericWorkflowHandler handler, Transition transition) throws OperationMapperException {
42          if (transition.getAbstractionLevel() != Operation.YELLOW) {
43              logger.warn("Invoked Yellow2BlueResourceMatcherOperationMapper for operation on transition \""+transition.getID()+"\" that is not Yellow! ");
44              return false;
45          }
46          boolean modification = false;
47          if (!(handler.getWorkflow() instanceof Workflow))
48              throw new OperationMapperException("Workflow class not supported.");
49  
50          Workflow workflow = (Workflow) handler.getWorkflow();
51          String operationClass = ((OperationClass) transition.getOperation().get()).getName();
52          logger.info("processing yellow transition \"" + transition.getID() + "\" ...");
53  
54          if (transition.getProperties().get(Constants.PROP_TRANSITION_RESOURCEMATCHER_REFINEMENT_FAILED) != null
55                  || transition.getProperties().get(Constants.PROP_TRANSITION_YELLOW2BLUE_REFINEMENT_FAILED) != null) {
56              logger.warn("This workflow contains yellow transitions that cannot be refined automatically. There are no matching operation and resource instances registered for operation class \"" + operationClass + "\"");
57              workflow.getProperties().put(Constants.PROP_WARN_ + handler.createNewErrorID(),
58                      "This workflow contains yellow transitions that cannot be refined automatically. There are no matching operation and resource instances registered for operation class \"" + operationClass + "\"");
59              try {
60                  handler.suspendWorkflowAsync();
61              } catch (StateTransitionException e) {
62                  logger.warn("exception:\n" + e, e);
63              }
64              // reset refinement failed in order transition be able transition refine again on next try
65              transition.getProperties().remove("resourcematcher.refinement.failed");
66              transition.getProperties().remove("yellow2blue.refinement.failed");
67          } else {
68              try {
69                  logger.info("Workflow refinement: invoking ResourceMatcher (yellow -> blue) ...");
70  
71                  String newWorkflow = ResourceMatcherClient.getInstance().match(handler.getWorkflowDescription());
72                  if (newWorkflow == null) {
73                      logger.warn("This workflow contains yellow transitions that cannot be refined automatically. The resource matcher returned \"null\" for operation class \"" + operationClass + "\"");
74                      workflow.getProperties().put(Constants.PROP_WARN_ + handler.createNewErrorID(),
75                              "This workflow contains yellow transitions that cannot be refined automatically. The resource matcher returned \"null\" for operation class \"" + operationClass + "\"");
76                      handler.suspendWorkflowAsync();
77  //                } else if (handler.getWorkflowDescription().equals(newWorkflow)) {
78  //                        logger.error("The ResourceMatcher was not able transition refine the workflow description");
79  //                        workflow.getProperties().put(GWorkflowDLHandler.PROP_ERROR_ + handler.createNewErrorID(), "The ResourceMatcher was not able transition refine the workflow description");
80  //                        handler.suspendWorkflowAsync();
81                  } else if (newWorkflow.startsWith("<soapenv:Fault")) {
82                      logger.warn("This workflow contains yellow transitions that cannot be refined automatically due transition an error with the resource matcher.");
83                      workflow.getProperties().put(Constants.PROP_WARN_ + handler.createNewErrorID(),
84                              "This workflow contains yellow transitions that cannot be refined automatically due transition an error with the resource matcher.");
85                      handler.suspendWorkflowAsync();
86                  } else {
87                      // ToDo: FixMe: Problem when exchanging whole workflow while other transitions are still active! 
88                      handler.setWorkflowDescription(newWorkflow);
89                      modification = true;
90                      logger.info("Workflow refinement by ResourceMatcher done.");
91                      if (logger.isDebugEnabled())
92                          logger.debug("==========> new ResourceMatcher Workflow <==========\n" + newWorkflow);
93                  }
94              } catch (Exception e) {
95                  logger.error("Invocation of ResourceMatcher failed: " + e, e);
96                  try {
97                      handler.abortWorkflowAsync();
98                  } catch (StateTransitionException e1) {
99                      logger.error("exception:\n" + e1, e1);
100                 }
101                 workflow.getProperties().put(Constants.PROP_ERROR_ + handler.createNewErrorID(), "Invocation of ResourceMatcher failed: " + e);
102             }
103         }
104 
105         return modification;
106     }
107 
108     /**
109      * Maps the operation contained in the specified list of transitions to a less abstract level.
110      *
111      * @param handler     The parent workflow handler.
112      * @param transitions The list of transitions.
113      * @return <code>true</code> if a mapping was possible, <code>false</code> otherwise.
114      * @throws net.kwfgrid.gwes.exception.OperationMapperException
115      *
116      */
117     public boolean processTransitions(GenericWorkflowHandler handler, ArrayList<Transition> transitions) throws OperationMapperException {
118         /// not impelemented.
119         return false;
120     }
121 }