[jboss-cvs] JBossAS SVN: r100636 - projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Feb 5 18:14:55 EST 2010
Author: anil.saldhana at jboss.com
Date: 2010-02-05 18:14:55 -0500 (Fri, 05 Feb 2010)
New Revision: 100636
Added:
projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/JavaPropertiesConfigParser.java
projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ParserNamespaceSupport.java
Modified:
projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java
projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java
Log:
generalize the embedded xml parsing in module option
Added: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/JavaPropertiesConfigParser.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/JavaPropertiesConfigParser.java (rev 0)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/JavaPropertiesConfigParser.java 2010-02-05 23:14:55 UTC (rev 100636)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.config.parser;
+
+import java.util.Properties;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.events.EndElement;
+import javax.xml.stream.events.StartElement;
+import javax.xml.stream.events.XMLEvent;
+
+/**
+ * Able to read in Java properties into module options
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 5, 2010
+ */
+public class JavaPropertiesConfigParser implements ParserNamespaceSupport
+{
+
+ /**
+ * @see {@code ParserNamespaceSupport#supports(String)}
+ */
+ public boolean supports(String namespaceURI)
+ {
+ return "urn:jboss:java-properties".equals(namespaceURI);
+ }
+
+ /**
+ * @see {@code ParserNamespaceSupport#parse(XMLEventReader)}
+ */
+ public Object parse(XMLEventReader xmlEventReader) throws XMLStreamException
+ {
+ Properties props = new Properties();
+ XMLEvent xmlEvent = null;
+ while(xmlEventReader.hasNext())
+ {
+ xmlEvent = xmlEventReader.peek();
+ if(xmlEvent instanceof StartElement)
+ {
+ StartElement se = (StartElement) xmlEvent;
+ if("module-option".equals(se.getName().getLocalPart()))
+ return props;
+ }
+ if(xmlEvent instanceof EndElement)
+ {
+ xmlEvent = xmlEventReader.nextEvent();
+ continue;
+ }
+
+ xmlEvent = xmlEventReader.nextEvent();
+
+ StartElement peekedStartElement = (StartElement) xmlEvent;
+ String key = null, value = null;
+ if(peekedStartElement.getName().getLocalPart().contains("property") == false)
+ throw new RuntimeException("property element not found");
+ xmlEvent = xmlEventReader.nextEvent();
+ peekedStartElement = (StartElement) xmlEvent;
+ if("key".equals(peekedStartElement.getName().getLocalPart()))
+ {
+ key = xmlEventReader.getElementText();
+ xmlEvent = xmlEventReader.nextEvent();
+ value = xmlEventReader.getElementText();
+ } else if("value".equals(peekedStartElement.getName().getLocalPart()))
+ {
+ throw new RuntimeException("key element not found. Check order of key and value");
+ }
+ props.put(key, value);
+
+ }
+ return props;
+ }
+}
\ No newline at end of file
Modified: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java 2010-02-05 22:36:37 UTC (rev 100635)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ModuleOptionParser.java 2010-02-05 23:14:55 UTC (rev 100636)
@@ -34,8 +34,6 @@
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
-import org.jboss.security.auth.spi.Users;
-
/**
* Parses the Module Option
* @author Anil.Saldhana at redhat.com
@@ -43,6 +41,19 @@
*/
public class ModuleOptionParser
{
+ private static transient Map<String, ParserNamespaceSupport> parsers = new HashMap<String,ParserNamespaceSupport>();
+
+ public ModuleOptionParser()
+ {
+ parsers.put("urn:jboss:user-roles", new UsersConfigParser());
+ parsers.put("urn:jboss:java-properties", new JavaPropertiesConfigParser());
+ }
+
+ public static void addParser(String parserName, ParserNamespaceSupport parser)
+ {
+ parsers.put(parserName, parser);
+ }
+
/**
* Parse the module-option element
* @param xmlEventReader
@@ -82,7 +93,7 @@
XMLEvent embeddedOrText = xmlEventReader.peek();
if(embeddedOrText.getEventType() == XMLStreamConstants.START_ELEMENT)
{
- val = embeddedUsersXMLParsing(xmlEventReader);
+ val = embeddedXMLParsing(xmlEventReader);
}
}
options.put(attribute.getValue(), val );
@@ -92,37 +103,43 @@
return options;
}
+ private Object embeddedXMLParsing(XMLEventReader xmlEventReader) throws XMLStreamException
+ {
+ Object retVal = null;
+
+ XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+ xmlEventReader = xmlInputFactory.createFilteredReader(xmlEventReader, new EventFilter()
+ {
+ public boolean accept(XMLEvent xmlEvent)
+ {
+ return xmlEvent.isStartElement() ;
+ }
+ });
+ while (xmlEventReader.hasNext())
+ {
+ XMLEvent xmlEvent = xmlEventReader.peek();
+ int eventType = xmlEvent.getEventType();
+ switch (eventType)
+ {
+ case XMLStreamConstants.START_ELEMENT:
+ StartElement xmlStartElement = (StartElement) xmlEvent;
+ String nsURI = xmlStartElement.getName().getNamespaceURI();
+ ParserNamespaceSupport parser = getSupportingParser(nsURI);
+ if(parser == null)
+ throw new RuntimeException("Unknown nsuri:" + nsURI);
+ return parser.parse(xmlEventReader);
+ }
+ }
+ return retVal;
+ }
+
/**
- * The {@code XMLLoginModule} may contain an embedded {@code Users} xml representation
- * @param xmlEventReader
+ * Get the parser that supports the particular namespace
+ * @param nsURI
* @return
- * @throws XMLStreamException
*/
- private Users embeddedUsersXMLParsing(XMLEventReader xmlEventReader) throws XMLStreamException
- {
- Users users = new Users();
-
- XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
- xmlEventReader = xmlInputFactory.createFilteredReader(xmlEventReader, new EventFilter()
- {
- public boolean accept(XMLEvent xmlEvent)
- {
- return xmlEvent.isStartElement() ;
- }
- });
- while (xmlEventReader.hasNext())
- {
- XMLEvent xmlEvent = xmlEventReader.peek();
- int eventType = xmlEvent.getEventType();
- switch (eventType)
- {
- case XMLStreamConstants.START_ELEMENT:
-
- UsersConfigParser usersParser = new UsersConfigParser();
- users = usersParser.parse(xmlEventReader);
- return users;
- }
- }
- return users;
- }
+ private ParserNamespaceSupport getSupportingParser(String nsURI)
+ {
+ return parsers.get(nsURI);
+ }
}
\ No newline at end of file
Added: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ParserNamespaceSupport.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ParserNamespaceSupport.java (rev 0)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/ParserNamespaceSupport.java 2010-02-05 23:14:55 UTC (rev 100636)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.security.config.parser;
+
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * Interface to indicate the parser
+ * supports a particular namespace
+ * @author Anil.Saldhana at redhat.com
+ * @since Feb 5, 2010
+ */
+public interface ParserNamespaceSupport
+{
+ /**
+ * Parse the event stream
+ * @param xmlEventReader
+ * @return
+ * @throws XMLStreamException
+ */
+ Object parse(XMLEventReader xmlEventReader) throws XMLStreamException;
+
+ /**
+ * Returns whether the parser supports parsing a particular namespace
+ * @param namespaceURI
+ * @return
+ */
+ boolean supports(String namespaceURI);
+}
\ No newline at end of file
Modified: projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java
===================================================================
--- projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java 2010-02-05 22:36:37 UTC (rev 100635)
+++ projects/security/picketbox/trunk/security-jboss-sx/jbosssx/src/main/java/org/jboss/security/config/parser/UsersConfigParser.java 2010-02-05 23:14:55 UTC (rev 100636)
@@ -41,7 +41,7 @@
* @author Anil.Saldhana at redhat.com
* @since Jan 27, 2010
*/
-public class UsersConfigParser
+public class UsersConfigParser implements ParserNamespaceSupport
{
/**
* Parse the embedded xml in the module option representing
@@ -128,4 +128,12 @@
}
return users;
}
+
+ /**
+ * @see {@code ParserNamespaceSupport#supports(String)}
+ */
+ public boolean supports(String namespaceURI)
+ {
+ return "urn:jboss:user-roles".equals(namespaceURI);
+ }
}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list