# HG changeset patch # User Bjoern Ricks # Date 1344430278 0 # Node ID 4a6321dd5186946a21fb2439c1433a667d7eba3f # Parent 9b29facddbd1cc8329b5c8aee58e864a57b8f63e Implement a class representation of features corresponding to roles flys-client/trunk@5171 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 9b29facddbd1 -r 4a6321dd5186 flys-client/src/main/java/de/intevation/flys/client/server/features/FeatureServletContextListener.java --- /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); + } +} diff -r 9b29facddbd1 -r 4a6321dd5186 flys-client/src/main/java/de/intevation/flys/client/server/features/Features.java --- /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 getFeatures(List roles); +} diff -r 9b29facddbd1 -r 4a6321dd5186 flys-client/src/main/java/de/intevation/flys/client/server/features/FeaturesNamespaceContext.java --- /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(); + } +} diff -r 9b29facddbd1 -r 4a6321dd5186 flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java --- /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> featuremap = new Hashtable>(); + + 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 allowed = new LinkedList(); + 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 getFeatures(List roles) { + List features = new LinkedList(); + + for (String role: roles) { + List allowed = this.featuremap.get(role); + if (!allowed.isEmpty()) { + features.addAll(allowed); + } + } + return features; + } +} diff -r 9b29facddbd1 -r 4a6321dd5186 flys-client/src/main/webapp/WEB-INF/features.xml --- /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 @@ + + + + foo + + + + + +