Some refactoring. XmlHelper removed. Method "generate" returns string.

This commit is contained in:
Zhen Tang 2013-06-16 21:09:31 +08:00
parent 27a397edbd
commit 1a18180845
11 changed files with 16 additions and 239 deletions

View File

@ -4,9 +4,7 @@ import haflow.entity.Node;
import java.util.Map;
import org.w3c.dom.Document;
public interface ModuleMetadata {
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs);
}

View File

@ -1,17 +1,7 @@
package haflow.module.oozie;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import haflow.entity.Node;
import haflow.module.Module;
import haflow.module.ModuleConfiguration;
@ -21,27 +11,11 @@ import haflow.module.ModuleMetadata;
@Module(id = "a0d027c3-a4bd-61b5-5063-134ff71f8122", name = "End", category = "Oozie", configurations = { @ModuleConfiguration(key = "name", displayName = "Name") }, inputs = { @ModuleEndpoint(name = "from", minNumber = 1, maxNumber = 1) }, outputs = {})
public class EndModule implements ModuleMetadata {
// TODO: Fix it
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
String name = configurations.get("name");
String xml = "<end name=\"" + name + "\"/>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document doc = db.parse(is);
return doc;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
return xml;
}
}

View File

@ -2,8 +2,6 @@ package haflow.module.oozie;
import java.util.Map;
import org.w3c.dom.Document;
import haflow.entity.Node;
import haflow.module.Module;
import haflow.module.ModuleConfiguration;
@ -19,7 +17,7 @@ import haflow.module.ModuleMetadata;
@ModuleEndpoint(name = "error", minNumber = 1, maxNumber = 1) })
public class FileSystem implements ModuleMetadata {
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
// TODO Auto-generated method stub
return null;

View File

@ -6,18 +6,8 @@ import haflow.module.ModuleConfiguration;
import haflow.module.ModuleEndpoint;
import haflow.module.ModuleMetadata;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@Module(id = "ada600a8-aa63-968a-ca46-9085e0e0bd2e", name = "Java", category = "Oozie", configurations = {
@ModuleConfiguration(key = "job-tracker", displayName = "Job Tracker"),
@ModuleConfiguration(key = "name-node", displayName = "Name Node"),
@ -35,7 +25,7 @@ import org.xml.sax.SAXException;
@ModuleEndpoint(name = "error", minNumber = 1, maxNumber = 1) })
public class JavaModule implements ModuleMetadata {
// TODO: Fix it
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
String name = configurations.get("name");
String arg = configurations.get("arg");
@ -60,24 +50,7 @@ public class JavaModule implements ModuleMetadata {
"<arg>" + "-eee " + arg + "</arg>" + "\n" + "</java>" + "\n"
+ "<ok to=\"" + ok + "\"/>" + "\n" + "<error to=\"fail\"/>"
+ "\n" + "</action>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(actionXML);
InputSource is = new InputSource(sr);
Document doc = db.parse(is);
return doc;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
return actionXML;
}
}

View File

@ -1,17 +1,7 @@
package haflow.module.oozie;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import haflow.entity.Node;
import haflow.module.Module;
import haflow.module.ModuleConfiguration;
@ -24,28 +14,13 @@ import haflow.module.ModuleMetadata;
public class KillModule implements ModuleMetadata {
// TODO: Fix it
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
String name = configurations.get("name");
String xml = "<kill name=\""
+ name
+ "\"><message>Work flow failed, error message[${wf:errorMessage(wf:lastErrorNode())}]</message></kill>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document doc = db.parse(is);
return doc;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
return xml;
}
}

View File

@ -2,8 +2,6 @@ package haflow.module.oozie;
import java.util.Map;
import org.w3c.dom.Document;
import haflow.entity.Node;
import haflow.module.Module;
import haflow.module.ModuleConfiguration;
@ -24,7 +22,7 @@ import haflow.module.ModuleMetadata;
@ModuleEndpoint(name = "error", minNumber = 1, maxNumber = 1) })
public class MapReduceModule implements ModuleMetadata {
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
// TODO Auto-generated method stub
return null;

View File

@ -2,8 +2,6 @@ package haflow.module.oozie;
import java.util.Map;
import org.w3c.dom.Document;
import haflow.entity.Node;
import haflow.module.Module;
import haflow.module.ModuleConfiguration;
@ -26,7 +24,7 @@ import haflow.module.ModuleMetadata;
@ModuleEndpoint(name = "error", minNumber = 1, maxNumber = 1) })
public class PigModule implements ModuleMetadata {
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
// TODO Auto-generated method stub
return null;

View File

@ -5,30 +5,15 @@ import haflow.module.Module;
import haflow.module.ModuleEndpoint;
import haflow.module.ModuleMetadata;
import java.io.StringReader;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
@Module(id = "a208d7d2-a8ff-2493-64c2-36f50bc95752", name = "Start", category = "Oozie", configurations = {}, inputs = {}, outputs = { @ModuleEndpoint(name = "to", maxNumber = 1, minNumber = 1) })
public class StartModule implements ModuleMetadata {
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
try {
String xml = "<start to=\"" + outputs.get("ok").getName() + "\"/>";
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(is);
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
String xml = "<start to=\"" + outputs.get("ok").getName() + "\"/>";
return xml;
}
}

View File

@ -5,25 +5,15 @@ import haflow.module.Module;
import haflow.module.ModuleConfiguration;
import haflow.module.ModuleMetadata;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@Module(id = "6e744dc4-edc6-eca2-07d5-28ff55a75b2d", name = "Preprocess", category = "zrace", configurations = {
@ModuleConfiguration(key = "input_path", displayName = "input path"),
@ModuleConfiguration(key = "output_path", displayName = "output path") }, inputs = {}, outputs = {})
public class ZracePreprocess implements ModuleMetadata {
// TODO: Fix it
public Document generate(Map<String, String> configurations,
public String generate(Map<String, String> configurations,
Map<String, Node> inputs, Map<String, Node> outputs) {
String inputPath = configurations.get("input_path");
String outputPath = configurations.get("output_path");
@ -45,24 +35,7 @@ public class ZracePreprocess implements ModuleMetadata {
+ "\n" + "</java>" + "\n" +
// "<ok to=\"end\"/>" + "\n" + //TODO
"<error to=\"fail\"/>" + "\n" + "</action>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(actionXML);
InputSource is = new InputSource(sr);
Document doc = db.parse(is);
return doc;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
return actionXML;
}
}

View File

@ -12,7 +12,6 @@ import haflow.ui.model.RunFlowResultModel;
import haflow.utility.ClusterConfiguration;
import haflow.utility.ModuleLoader;
import haflow.utility.SessionHelper;
import haflow.utility.XmlHelper;
import java.util.ArrayList;
import java.util.HashMap;
@ -65,17 +64,6 @@ public class FlowExecuteService {
this.nodeConfigurationProfileService = nodeConfigurationProfileService;
}
private XmlHelper xmlHelper;
public XmlHelper getXmlHelper() {
return xmlHelper;
}
@Autowired
public void setXmlHelper(XmlHelper xmlHelper) {
this.xmlHelper = xmlHelper;
}
private FlowDeployService flowDeployService;
public FlowDeployService getFlowDeployService() {
@ -311,8 +299,7 @@ public class FlowExecuteService {
}
}
// TODO: Fix it
Document doc = null;// module.generate(configurations);
String part = this.xmlHelper.getXmlString(doc);
String part = null;// module.generate(configurations);
workflowXml.append(part + "\n");
}
workflowXml.append("</workflow-app>" + "\n");

View File

@ -1,82 +0,0 @@
package haflow.utility;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@Component
public class XmlHelper {
private DocumentBuilderFactory dbf;
private DocumentBuilder documentBuilder;
private TransformerFactory tf;
private Transformer transform;
private static XmlHelper instance;
public static XmlHelper getInstance(){
if(instance == null){
instance = new XmlHelper();
}
return instance;
}
public XmlHelper() {
dbf = DocumentBuilderFactory.newInstance();
try {
documentBuilder = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
tf = TransformerFactory.newInstance();
try {
transform = tf.newTransformer();
transform.setOutputProperty("encoding", "UTF-8");
transform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
}
public String getXmlString(Document doc) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transform.transform(new DOMSource(doc), new StreamResult(bos));
String xmlStr = bos.toString();
return xmlStr;
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
public Document getDocument(String xml) {
try {
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Document doc = documentBuilder.parse(is);
return doc;
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}