changeset 3476:4a6321dd5186

Implement a class representation of features corresponding to roles flys-client/trunk@5171 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 12:51:18 +0000
parents 9b29facddbd1
children 2c63994ceffb
files flys-client/src/main/java/de/intevation/flys/client/server/features/FeatureServletContextListener.java flys-client/src/main/java/de/intevation/flys/client/server/features/Features.java flys-client/src/main/java/de/intevation/flys/client/server/features/FeaturesNamespaceContext.java flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java flys-client/src/main/webapp/WEB-INF/features.xml
diffstat 5 files changed, 247 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/FeatureServletContextListener.java	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,60 @@
+package de.intevation.flys.client.server.features;
+
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.client.server.LoggingConfigurator;
+
+/**
+ * ServletContextListenter to initalize the Features globally for
+ * all Servlets
+ */
+public class FeatureServletContextListener implements ServletContextListener {
+
+    public static final String LOG4J_PROPERTIES = "FLYS_CLIENT_LOG4J_PROPERIES";
+
+    public static final Logger logger = Logger.getLogger(FeatureServletContextListener.class);
+
+    @Override
+    public void  contextInitialized(ServletContextEvent sce) {
+        ServletContext sc = sce.getServletContext();
+
+        this.initLogging(sc);
+
+        String filename = sc.getInitParameter("features-file");
+
+        logger.debug("Initializing ServletContext");
+        try {
+            XMLFileFeatures features = new XMLFileFeatures(sc.getRealPath(filename));
+            sc.setAttribute(Features.CONTEXT_ATTRIBUTE, features);
+        } catch(IOException e) {
+            logger.error(e);
+        }
+    }
+
+    @Override
+    public void contextDestroyed(ServletContextEvent sce) {
+        //DO NOTHING
+    }
+
+
+    private void initLogging(ServletContext sc) {
+        String log4jProperties = System.getenv(LOG4J_PROPERTIES);
+
+        if (log4jProperties == null || log4jProperties.length() == 0) {
+            String file = sc.getInitParameter("log4j-properties");
+
+            if (file != null && file.length() > 0) {
+                log4jProperties = sc.getRealPath(file);
+            }
+        }
+        System.out.println(log4jProperties);
+
+        LoggingConfigurator.init(log4jProperties);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/Features.java	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,13 @@
+package de.intevation.flys.client.server.features;
+
+import java.util.List;
+
+public interface Features {
+
+    public static final String CONTEXT_ATTRIBUTE = "de.intevation.flys.client.server.features";
+
+    /**
+     * Returns all allowed features to a list of roles
+     */
+    public List<String> getFeatures(List<String> roles);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/FeaturesNamespaceContext.java	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,80 @@
+package de.intevation.flys.client.server.features;
+
+import java.util.Iterator;
+
+import javax.xml.XMLConstants;
+
+import javax.xml.namespace.NamespaceContext;
+
+public class FeaturesNamespaceContext
+implements   NamespaceContext {
+
+    /**
+     * The URI of the namespace of the features.
+     */
+    public final static String NAMESPACE_URI =
+        "http://www.intevation.de/2012/flys/features";
+
+    /**
+     * The XML prefix for the features namespace.
+     */
+    public final static String NAMESPACE_PREFIX = "ftr";
+
+    /**
+     * Final instance to be easily used to avoid creation
+     * of instances.
+     */
+    public static final FeaturesNamespaceContext INSTANCE =
+        new FeaturesNamespaceContext();
+
+    /**
+     * The default constructor.
+     */
+    public FeaturesNamespaceContext() {
+    }
+
+    /**
+     * @see javax.xml.namespace.NamespaceContext#getNamespaceURI(String)
+     * @param prefix The prefix
+     * @return The corresponing URI
+     */
+    @Override
+    public String getNamespaceURI(String prefix) {
+
+        if (prefix == null) {
+            throw new NullPointerException("Null prefix");
+        }
+
+        if (NAMESPACE_PREFIX.equals(prefix)) {
+            return NAMESPACE_URI;
+        }
+
+        if ("xml".equals(prefix)) {
+            return XMLConstants.XML_NS_URI;
+        }
+
+        return XMLConstants.NULL_NS_URI;
+    }
+
+    /**
+     * @see javax.xml.namespace.NamespaceContext#getPrefix(String)
+     * @param uri The URI
+     * @return nothing.
+     * @throws java.lang.UnsupportedOperationException
+     */
+    @Override
+    public String getPrefix(String uri) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * @see javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String)
+     * @param uri The URI
+     * @return nothing
+     * @throws java.lang.UnsupportedOperationException
+     */
+    @Override
+    public Iterator getPrefixes(String uri) {
+        throw new UnsupportedOperationException();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,84 @@
+package de.intevation.flys.client.server.features;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import java.util.Hashtable;
+import java.util.List;
+import java.util.LinkedList;
+
+import javax.xml.xpath.XPathConstants;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+
+public class XMLFileFeatures implements Features {
+
+    private static final Logger logger = Logger.getLogger(XMLFileFeatures.class);
+
+    private Hashtable<String, List<String>> featuremap = new Hashtable<String, List<String>>();
+
+    public XMLFileFeatures(String filename) throws IOException {
+        FileInputStream finput = new FileInputStream(filename);
+        Document doc = XMLUtils.parseDocument(finput);
+
+        String XPATH_FEATURES = "ftr:feature/child::text()";
+        String XPATH_ROLES    = "/ftr:features/ftr:role";
+
+        NodeList roles = (NodeList) XMLUtils.xpath(
+            doc,
+            XPATH_ROLES,
+            XPathConstants.NODESET,
+            FeaturesNamespaceContext.INSTANCE);
+
+        for(int i=0; i < roles.getLength(); i++) {
+            Node rolenode = roles.item(i);
+
+            String name = XMLUtils.xpathString(
+                rolenode, "@name", FeaturesNamespaceContext.INSTANCE);
+
+            logger.debug("Found role: " + name);
+
+            NodeList features = (NodeList) XMLUtils.xpath(
+                rolenode,
+                XPATH_FEATURES,
+                XPathConstants.NODESET,
+                FeaturesNamespaceContext.INSTANCE);
+
+            if (features.getLength() > 0) {
+                List<String> allowed = new LinkedList<String>();
+                for (int j=0; j < features.getLength(); j++) {
+                    Node featurenode = features.item(j);
+                    String featurename = featurenode.getNodeValue();
+
+                    logger.debug("found feature: " + featurename);
+
+                    allowed.add(featurename);
+                }
+                featuremap.put(name, allowed);
+            }
+        }
+        logger.debug("Loaded all features");
+
+        finput.close();
+    }
+
+    @Override
+    public List<String> getFeatures(List<String> roles) {
+        List<String> features = new LinkedList<String>();
+
+        for (String role: roles) {
+            List<String> allowed = this.featuremap.get(role);
+            if (!allowed.isEmpty()) {
+                features.addAll(allowed);
+            }
+        }
+        return features;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/webapp/WEB-INF/features.xml	Wed Aug 08 12:51:18 2012 +0000
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ftr:features xmlns:ftr="http://www.intevation.de/2012/flys/features">
+    <ftr:role name="bar">
+        <ftr:feature>foo</ftr:feature>
+    </ftr:role>
+</ftr:features>
+
+
+
+

http://dive4elements.wald.intevation.org