View Javadoc

1   /*
2    * Copyright 2011 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   
8   package net.kwfgrid.gwes;
9   
10  import net.kwfgrid.gwes.exception.LoggingException;
11  import net.kwfgrid.gwes.exception.WorkflowSecurityException;
12  import net.kwfgrid.gworkflowdl.conversion.EPML2GWorkflowDLConverter;
13  import net.kwfgrid.gworkflowdl.conversion.EPNML2GWorkflowDLConverter;
14  import net.kwfgrid.gworkflowdl.conversion.GWorkflowDLConverter;
15  import net.kwfgrid.gworkflowdl.structure.WorkflowFormatException;
16  import org.apache.log4j.Logger;
17  import org.jdom.JDOMException;
18  import org.jdom.transform.XSLTransformException;
19  
20  import java.io.IOException;
21  import java.util.regex.Pattern;
22  
23  public class GenericWorkflowHandlerFactory {
24  
25      static final Logger logger = Logger.getLogger(GenericWorkflowHandlerFactory.class);
26  
27      private GWESEngine engine;
28  
29      /**
30       * Regex pattern that identifies a GWorkflowDL XML string.
31       */
32      static final Pattern GWorkflowDL_1_0 = Pattern.compile("(?s).*<workflow.+gworkflowdl_1_0\\.xsd.+", Pattern.DOTALL);
33      static final Pattern GWorkflowDL_1_1 = Pattern.compile("(?s).*<workflow.+gworkflowdl_1_1\\.xsd.+", Pattern.DOTALL);
34      static final Pattern GWorkflowDL_2_0 = Pattern.compile("(?s).*<workflow.+gworkflowdl_2_0\\.xsd.+", Pattern.DOTALL);
35      static final Pattern GWorkflowDL_2_1 = Pattern.compile("(?s).*<workflow.+gworkflowdl_2_1\\.xsd.+", Pattern.DOTALL);
36      static final Pattern EPNML_1_1 = Pattern.compile("(?s).*<net.+type.+epnml-?1\\.?1.+", Pattern.DOTALL);
37      static final Pattern EPML = Pattern.compile("(?s).*epml xmlns:.+=\"http://www.epml.de\".+", Pattern.DOTALL);
38  
39      public GenericWorkflowHandlerFactory(GWESEngine engine) {
40          this.engine = engine;
41      }
42  
43      GenericWorkflowHandler createWorkflowHandler(String workflowDescription, String userIdCredential) throws WorkflowFormatException, WorkflowSecurityException, LoggingException {
44          GenericWorkflowHandler handler;
45          String originalDescriptionType = Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT;
46  
47          try {
48              // GWorkflowDL 2.1 -> GWorkflowDLHandler
49              if (GWorkflowDL_2_1.matcher(workflowDescription).matches()) {
50                  logger.info("Workflow description of type \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" detected.");
51                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescription, userIdCredential);
52              }
53  
54              // GWorkflowDL 2.0 -> GWorkflowDLHandler
55              else if (GWorkflowDL_2_0.matcher(workflowDescription).matches()) {
56                  originalDescriptionType = "GWorkflowDL version 2.0";
57                  logger.info("Workflow description of type \"" + originalDescriptionType + "\" detected. Converting to \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" ...");
58                  GWorkflowDLConverter converter = new GWorkflowDLConverter("gworkflowdl_2_0-to-2_1.xsl");
59                  String workflowDescriptionNew = converter.convert(workflowDescription);
60                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescriptionNew, userIdCredential);
61              }
62  
63              // GWorkflowDL 1.1 -> GWorkflowDLHandler
64              else if (GWorkflowDL_1_1.matcher(workflowDescription).matches()) {
65                  originalDescriptionType = "GWorkflowDL version 1.1";
66                  logger.info("Workflow description of type \"" + originalDescriptionType + "\" detected. Converting to \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" ...");
67                  GWorkflowDLConverter converter = new GWorkflowDLConverter("gworkflowdl_1_1-to-2_1.xsl");
68                  String workflowDescriptionNew = converter.convert(workflowDescription);
69                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescriptionNew, userIdCredential);
70              }
71  
72              // GWorkflowDL 1.0 -> GWorkflowDLHandler
73              else if (GWorkflowDL_1_0.matcher(workflowDescription).matches()) {
74                  originalDescriptionType = "GWorkflowDL version 1.0";
75                  logger.info("Workflow description of type \"" + originalDescriptionType + "\" detected. Converting to \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" ...");
76                  GWorkflowDLConverter converter = new GWorkflowDLConverter("gworkflowdl_1_0-to-2_1.xsl");
77                  String workflowDescriptionNew = converter.convert(workflowDescription);
78                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescriptionNew, userIdCredential);
79              }
80  
81              // EPNML 1.1 -> GWorkflowDLHandler
82              else if (EPNML_1_1.matcher(workflowDescription).matches()) {
83                  originalDescriptionType = "EPNML version 1.1";
84                  logger.info("Workflow description of type \"" + originalDescriptionType + "\" detected. Converting to \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" ...");
85                  EPNML2GWorkflowDLConverter converter = new EPNML2GWorkflowDLConverter();
86                  String workflowDescriptionNew = converter.convertFromEPNMLToGWorkflowDL(workflowDescription);
87                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescriptionNew, userIdCredential);
88              }
89  
90              // EPML 1.2 -> GWorkflowDLHandler
91              else if (EPML.matcher(workflowDescription).matches()) {
92                  originalDescriptionType = "EPML version 1.2";
93                  logger.info("Workflow description of type \"" + originalDescriptionType + "\" detected. Converting to \"" + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + "\" ...");
94                  EPML2GWorkflowDLConverter converter = new EPML2GWorkflowDLConverter();
95                  String workflowDescriptionNew = converter.convertFromEPMLToGWorkflowDL(workflowDescription);
96                  handler = new GWorkflowDLHandler(engine, originalDescriptionType, workflowDescriptionNew, userIdCredential);
97              }
98  
99              // not supported workflow description language.
100             else {
101                 logger.error("Unknown workflow description language. Currently, GWorkflowDL version 1.0, 1.1, 2.0, 2.1, (E)PNML 1.1 as well as EPML 1.2 are supported.\n" +
102                         "The workflow description must contain a reference to the XML schema 'gworkflowdl_1_0.xsd', 'gworkflowdl_1_1.xsd', 'gworkflowdl_2_0.xsd', 'gworkflowdl_2_1.xsd', 'epnml-1.1', or to the namespace 'http://www.epml.de'\n" +
103                         workflowDescription);
104                 throw new WorkflowFormatException("Unknown workflow description language. Currently, GWorkflowDL version 1.0, 1.1, 2.0, 2.1, (E)PNML 1.1 as well as EPML 1.2 are supported.\n" +
105                         "The workflow description must contain a reference to the XML schema 'gworkflowdl_1_0.xsd', 'gworkflowdl_1_1.xsd', 'gworkflowdl_2_0.xsd', 'gworkflowdl_2_1.xsd', 'epnml-1.1', or to the namespace 'http://www.epml.de'\n" +
106                         workflowDescription);
107             }
108         } catch (XSLTransformException e) {
109             logger.error("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
110             throw new WorkflowFormatException("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
111         } catch (IOException e) {
112             logger.error("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
113             throw new WorkflowFormatException("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
114         } catch (JDOMException e) {
115             logger.error("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
116             throw new WorkflowFormatException("Exception during conversion from " + originalDescriptionType + " to " + Constants.WORKFLOW_DESCRIPTION_TYPE_DEFAULT + ":\n" + e, e);
117         }
118         return handler;
119     }
120 }