changeset 2:5e94403971af

infrastructur for Communicationg with the ArtifactDatabase added. gnv/trunk@71 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 11 Sep 2009 15:17:38 +0000
parents 3481f68e6d2a
children d7b4a77ba892
files gnv/Changelog gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientFactory.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/exception/ArtifactDatabaseClientException.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java
diffstat 8 files changed, 568 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Thu Sep 10 15:04:33 2009 +0000
+++ b/gnv/Changelog	Fri Sep 11 15:17:38 2009 +0000
@@ -1,3 +1,15 @@
+2009-09-11  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java Added, 
+      src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java Added: 
+      ObjectStructor for Representing delivered Objects from the ArtifactDatabase
+      
+    * src/main/java/de/intevation/gnv/artifactdatabase/client/exception/ArtifactDatabaseClientException.java Added,
+      src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Added, 
+      src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientFactory.java Added,  
+      src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java Added:
+       Integrated Interface-Structure to handle the communication to the ArtifactDatabase
+
 2009-09-10  Tim Englich  <tim.englich@intevation.de>
     * .svnignore Added:  Added Eclipse specific Files and Folders to the Ignorelist
 2009-09-10  Tim Englich  <tim.englich@intevation.de>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,25 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.client;
+
+import java.util.Collection;
+
+import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public interface ArtifactDatabaseClient {
+    
+    
+    /**
+     * Return the Artifactfactories which can be resolved.
+     * @return the Artifactfactories which can be resolved.
+     * @throws ArtifactDatabaseClientException
+     */
+    public Collection<ArtifactObject> getArtifactFactories() throws ArtifactDatabaseClientException;
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientFactory.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,56 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.client;
+
+import org.apache.log4j.Logger;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class ArtifactDatabaseClientFactory {
+
+
+    
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(ArtifactDatabaseClientFactory.class);
+
+    
+    /**
+     * The singleton Instance of this Factory.
+     */
+    private static ArtifactDatabaseClientFactory instance = null;
+
+    /**
+     * Constructor
+     */
+    private ArtifactDatabaseClientFactory() {
+        super();
+    }
+
+    /**
+     * This Method provides an singleton Instance of this Class.
+     * @return an singleton Instance of this Class
+     */
+    public static ArtifactDatabaseClientFactory getInstance(){
+        if (instance == null){
+            instance = new ArtifactDatabaseClientFactory();
+        }
+        return instance;
+    }
+    
+    
+    /**
+     * Getting the ArtifactDatabaseClient
+     * @return the ArtifactDatabaseClient
+     */
+    public ArtifactDatabaseClient getArtifactDatabaseClient(){
+        //TODO Read from Configuration?
+        return new DefaultArtifactDatabaseClient();
+    }
+   
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,132 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.client;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.log4j.Logger;
+import org.restlet.Client;
+import org.restlet.data.Method;
+import org.restlet.data.Protocol;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.representation.Representation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
+import de.intevation.gnv.util.XMLUtils;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient {
+    /**
+     * the logger, used to log exceptions and additonaly information
+     */
+    private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class);
+    
+    // TODO Container for ArtifactDatabases should be used.
+    private static Map<String, String> artifactDatabases = null;
+    
+    private static boolean initialized = false;
+    
+    /**
+     * Constructor
+     */
+    public DefaultArtifactDatabaseClient() {
+        super();
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories()
+     */
+    public Collection<ArtifactObject> getArtifactFactories() throws ArtifactDatabaseClientException {
+        Collection<ArtifactObject> resultValues = null;
+        if (!initialized){
+            this.initialize();
+        }
+        try {
+           XMLUtils xmlUtils = new XMLUtils();
+            log.debug("DefaultArtifactDatabaseClient.getArtifactFactories");
+            Iterator<String> it = artifactDatabases.values().iterator();
+            while (it.hasNext()){
+                String server = it.next();
+                String url =  server+ "/factories";
+                InputStream inputStream = this.doGetRequest(url);
+                Document document = xmlUtils.readDocument(inputStream);
+                log.debug(xmlUtils.writeDocument2String(document));
+                if (resultValues == null){
+                    resultValues = this.getArtifactFactories(document,server);
+                }else{
+                    resultValues.addAll(this.getArtifactFactories(document,server));
+                }
+            }
+        } catch (IOException e) {
+            log.error(e,e);
+        }
+        return resultValues;
+    }
+
+    private Collection<ArtifactObject>  getArtifactFactories(Document document, String server){
+        XMLUtils xmlUtils = new XMLUtils();
+        NodeList artifactFactories = xmlUtils.getNodeSetXPath(document, "/result/factories/factory");
+        Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>(artifactFactories.getLength());
+        if (artifactFactories != null){
+            for (int i = 0; i < artifactFactories.getLength(); i++){
+                Node  artifactFactoryNode = artifactFactories.item(i);
+                String name = xmlUtils.getStringXPath(artifactFactoryNode, "@name");
+                String description = xmlUtils.getStringXPath(artifactFactoryNode, "@description");
+                ArtifactFactory artifactFactory = new ArtifactFactory(name, description, server);
+                resultValues.add(artifactFactory);
+            }
+        }
+        return resultValues;
+    }
+    
+    /**
+     * @throws IOException
+     */
+    private InputStream doGetRequest(String requestUrl) throws IOException {
+        Client client = new Client(Protocol.HTTP);
+        Request request = new Request(Method.GET, requestUrl);  
+        Response response = client.handle(request);
+        Representation output = response.getEntity();
+        return output.getStream();
+    }
+    
+    private synchronized void initialize(){
+        if (!initialized){
+            this.artifactDatabases = new HashMap<String, String>();
+            this.artifactDatabases.put("test", "http://localhost:8181");            // TODO Read from Config
+            initialized = true;
+        }
+        
+    }
+    
+   
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/exception/ArtifactDatabaseClientException.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,48 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.client.exception;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class ArtifactDatabaseClientException extends Exception {
+
+    /**
+     * The UID of this Class
+     */
+    private static final long serialVersionUID = -8979622024425251952L;
+
+    /**
+     * Constructor
+     */
+    public ArtifactDatabaseClientException() {
+        super();
+    }
+
+    /**
+     * Constructor
+     * @param arg0
+     */
+    public ArtifactDatabaseClientException(String arg0) {
+        super(arg0);
+    }
+
+    /**
+     * Constructor
+     * @param arg0
+     */
+    public ArtifactDatabaseClientException(Throwable arg0) {
+        super(arg0);
+    }
+
+    /**
+     * Constructor
+     * @param arg0
+     * @param arg1
+     */
+    public ArtifactDatabaseClientException(String arg0, Throwable arg1) {
+        super(arg0, arg1);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactFactory.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,77 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.objects;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class ArtifactFactory implements ArtifactObject {
+
+    private String name = null;
+    
+    private String description = null;
+    
+    private boolean selected  = false;
+    
+    private String dataBaseUrl = null;
+    /**
+     * @return the dataBaseUrl
+     */
+    public String getDataBaseUrl() {
+        return dataBaseUrl;
+    }
+
+
+    /**
+     * Constructor
+     * @param name
+     * @param description
+     * @param dataBaseUrl
+     */
+    public ArtifactFactory(String name, String description, String dataBaseUrl) {
+        super();
+        this.name = name;
+        this.description = description;
+        this.dataBaseUrl = dataBaseUrl;
+    }
+
+
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getId()
+     */
+    public String getId() {
+        return this.name;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#isSelected()
+     */
+    public boolean isSelected() {
+
+        return this.selected;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#setSelected(boolean)
+     */
+    public void setSelected(boolean selected) {
+        this.selected = selected;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getName()
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.objects.ArtifactObject#getDescription()
+     */
+    public String getDescription() {
+        return description;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/objects/ArtifactObject.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,21 @@
+/**
+ *
+ */
+package de.intevation.gnv.artifactdatabase.objects;
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public interface ArtifactObject {
+    
+    
+    public String getId();
+    
+    public boolean isSelected();
+    
+    public void setSelected(boolean selected);
+    
+    public String getName();
+    public String getDescription();
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/util/XMLUtils.java	Fri Sep 11 15:17:38 2009 +0000
@@ -0,0 +1,197 @@
+package de.intevation.gnv.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.TransformerFactoryConfigurationError;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.log4j.Logger;
+
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathConstants;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+/**
+ *  @author Sascha L. Teichmann
+ */
+public class XMLUtils
+{
+    private static Logger logger = Logger.getLogger(XMLUtils.class);
+
+    public XMLUtils() {
+    }
+
+    public static class ElementCreator
+    {
+        protected Document document;
+        protected String   ns;
+        protected String   prefix;
+
+        public ElementCreator(Document document, String ns, String prefix) {
+            this.document = document;
+            this.ns       = ns;
+            this.prefix   = prefix;
+        }
+
+        public Element create(String name) {
+            Element element = document.createElementNS(ns, name);
+            element.setPrefix(prefix);
+            return element;
+        }
+
+        public void addAttr(Element element, String name, String value) {
+            Attr attr = document.createAttributeNS(ns, name);
+            attr.setValue(value);
+            attr.setPrefix(prefix);
+            element.setAttributeNode(attr);
+        }
+    } // class ElementCreator
+
+    public Document newDocument() {
+        try {
+            return DocumentBuilderFactory
+                .newInstance()
+                .newDocumentBuilder()
+                .newDocument();
+        }
+        catch (ParserConfigurationException pce) {
+            logger.error(pce.getLocalizedMessage(), pce);
+        }
+        return null;
+    }
+
+    public XPath newXPath() {
+        return newXPath(null);
+    }
+
+    public XPath newXPath(NamespaceContext namespaceContext) {
+        XPathFactory factory = XPathFactory.newInstance();
+        XPath        xpath   = factory.newXPath();
+        if (namespaceContext != null) {
+            xpath.setNamespaceContext(namespaceContext);
+        }
+        return xpath;
+    }
+
+    public Object xpath(Object root, String query, QName returnTyp) {
+        return xpath(root, query, returnTyp, null);
+    }
+
+    public final String xpathString(
+        Object root, String query, NamespaceContext namespaceContext
+    ) {
+        return (String)xpath(root, query, XPathConstants.STRING, namespaceContext);
+    }
+
+    public static final Object xpath(
+        Object           root,
+        String           query,
+        QName            returnType,
+        NamespaceContext namespaceContext
+    ) {
+        if (root == null) {
+            return null;
+        }
+
+        try {
+            XPath xpath = new XMLUtils().newXPath(namespaceContext);
+            if (xpath != null) {
+                return xpath.evaluate(query, root, returnType);
+            }
+        }
+        catch (XPathExpressionException xpee) {
+            logger.error(xpee.getLocalizedMessage(), xpee);
+        }        
+
+        return null;
+    }
+    
+    public Object getXPath(
+        Object root, String query, QName returnType
+    ) {
+        return xpath(root, query, returnType);
+    }
+
+    public String getStringXPath(String xpath) {
+        return getStringXPath(xpath, null);
+    }
+
+    public NodeList getNodeSetXPath(Object root, String query) {
+        return (NodeList)getXPath(root, query, XPathConstants.NODESET);
+    }
+
+    public Node getNodeXPath(Object root, String query) {
+        return (Node)getXPath(root, query, XPathConstants.NODE);
+    }
+
+    public String getStringXPath(Object root, String xpath) {
+        return getStringXPath(root, xpath, null);
+    }
+
+    public String getStringXPath(
+        Object root, String query, String def
+    ) {
+        String s = (String)getXPath(root, query, XPathConstants.STRING);
+        return s == null || s.length() == 0
+            ? def
+            : s;
+    }
+    
+    public Document readDocument(InputStream inputStream){
+        Document returnValue = null;
+        try {
+            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+            returnValue = docBuilder.parse (inputStream);
+        } catch (ParserConfigurationException e) {
+            logger.error(e,e);
+        } catch (SAXException e) {
+            logger.error(e,e);
+        } catch (IOException e) {
+            logger.error(e,e);
+        }
+        return returnValue;
+    }
+    
+    public String writeDocument2String(Document document){
+        try {
+            TransformerFactory transformerFactory = TransformerFactory.newInstance();
+            Transformer transformer = transformerFactory.newTransformer();
+            DOMSource source = new DOMSource(document);
+            StringWriter sw = new StringWriter();
+            StreamResult result =  new StreamResult(sw);
+            transformer.transform(source, result);
+            return sw.getBuffer().toString();
+        } catch (TransformerConfigurationException e) {
+            logger.error(e,e);
+        } catch (TransformerFactoryConfigurationError e) {
+            logger.error(e,e);
+        } catch (TransformerException e) {
+            logger.error(e,e);
+        }
+        return null;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org