Author: dkulp
Date: Thu Feb 17 16:29:41 2011
New Revision: 1071681
URL: http://svn.apache.org/viewvc?rev=1071681&view=rev
Log:
Change AssertionBuilderFactory to an interface (with an impl) to make
it easier to extend. Make some other changes to make it easier
to extend the Neethi classes with extra functionality and such.
Added:
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactoryImpl.java
- copied, changed from r1071040,
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
Modified:
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyEngine.java
Modified:
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java?rev=1071681&r1=1071680&r2=1071681&view=diff
==============================================================================
---
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
(original)
+++
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
Thu Feb 17 16:29:41 2011
@@ -19,16 +19,10 @@
package org.apache.neethi;
-import java.lang.reflect.ParameterizedType;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
import javax.xml.namespace.QName;
import org.apache.neethi.builders.AssertionBuilder;
import org.apache.neethi.builders.converters.ConverterRegistry;
-import org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder;
-import org.apache.neethi.util.Service;
/**
* AssertionFactory is used to create an Assertion from an Element. It uses an
@@ -36,38 +30,21 @@ import org.apache.neethi.util.Service;
* QName of the given element. Domain Policy authors could right custom
* AssertionBuilders to build Assertions for domain specific assertions.
*/
-public class AssertionBuilderFactory {
-
- public static final String POLICY_NAMESPACE =
"http://schemas.xmlsoap.org/ws/2004/09/policy";
-
- public static final String POLICY = "Policy";
+public interface AssertionBuilderFactory {
- public static final String EXACTLY_ONE = "ExactlyOne";
+ /**
+ * Returns the PolicyEngine associated with this factory
+ * @return
+ */
+ PolicyEngine getPolicyEngine();
- public static final String ALL = "All";
-
- private Map<QName, AssertionBuilder> registeredBuilders
- = new ConcurrentHashMap<QName, AssertionBuilder>();
- private AssertionBuilder defaultBuilder;
- private final ConverterRegistry converters;
- private final PolicyEngine engine;
-
- public AssertionBuilderFactory(PolicyEngine eng, ConverterRegistry reg) {
- converters = reg;
- engine = eng;
-
- for (AssertionBuilder builder :
Service.providers(AssertionBuilder.class)) {
- QName[] knownElements = builder.getKnownElements();
- for (int i = 0; i < knownElements.length; i++) {
- registeredBuilders.put(knownElements[i], builder);
- }
- }
- defaultBuilder = new XMLPrimitiveAssertionBuilder();
- }
-
- public PolicyEngine getPolicyEngine() {
- return engine;
- }
+ /**
+ * Returns the ConverterRegistry that the builder
+ * uses for converting the object to the types
+ * needed for the AssertionBuilders
+ * @return
+ */
+ ConverterRegistry getConverterRegistry();
/**
* Registers an AssertionBuilder with a specified QName.
@@ -76,9 +53,15 @@ public class AssertionBuilderFactory {
* @param builder the AssertionBuilder that can build an Assertion from
* an element of specified type
*/
- public void registerBuilder(QName key, AssertionBuilder builder) {
- registeredBuilders.put(key, builder);
- }
+ void registerBuilder(QName key, AssertionBuilder builder);
+
+ /**
+ * Registers an AssertionBuilder with all the builder's known elements.
+ *
+ * @param builder the AssertionBuilder that can build an Assertion from
+ * an element of specified type
+ */
+ void registerBuilder(AssertionBuilder builder);
/**
@@ -88,40 +71,7 @@ public class AssertionBuilderFactory {
* Assertion.
* @return an Assertion that is built using the specified element.
*/
- public Assertion build(Object element) {
- AssertionBuilder builder;
-
- QName qname = converters.findQName(element);
- builder = registeredBuilders.get(qname);
- if (builder == null) {
- /*
- * if we can't locate an appropriate AssertionBuilder, we always
use the
- * XMLPrimitiveAssertionBuilder
- */
- builder = defaultBuilder;
- }
- return invokeBuilder(element, builder);
- }
-
- @SuppressWarnings("unchecked")
- private Assertion invokeBuilder(Object element, AssertionBuilder builder) {
- Class<?> type = findAssertionBuilderTarget(builder.getClass());
- return builder.build(converters.convert(element, type), this);
- }
-
- private Class<?> findAssertionBuilderTarget(Class<?> c) {
- Class interfaces[] = c.getInterfaces();
- for (int x = 0; x < interfaces.length; x++) {
- if (interfaces[x] == AssertionBuilder.class) {
- ParameterizedType pt =
(ParameterizedType)c.getGenericInterfaces()[x];
- return (Class)pt.getActualTypeArguments()[0];
- }
- }
- if (c.getClass().getSuperclass() != null) {
- return findAssertionBuilderTarget(c.getSuperclass());
- }
- return null;
- }
+ Assertion build(Object element);
/**
@@ -132,7 +82,5 @@ public class AssertionBuilderFactory {
* Assertion from
* @return an AssertionBuilder that understands qname type
*/
- public AssertionBuilder getBuilder(QName qname) {
- return registeredBuilders.get(qname);
- }
+ public AssertionBuilder getBuilder(QName qname);
}
Copied:
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactoryImpl.java
(from r1071040,
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java)
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactoryImpl.java?p2=webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactoryImpl.java&p1=webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java&r1=1071040&r2=1071681&rev=1071681&view=diff
==============================================================================
---
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactory.java
(original)
+++
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/AssertionBuilderFactoryImpl.java
Thu Feb 17 16:29:41 2011
@@ -36,24 +36,16 @@ import org.apache.neethi.util.Service;
* QName of the given element. Domain Policy authors could right custom
* AssertionBuilders to build Assertions for domain specific assertions.
*/
-public class AssertionBuilderFactory {
+public class AssertionBuilderFactoryImpl implements AssertionBuilderFactory {
- public static final String POLICY_NAMESPACE =
"http://schemas.xmlsoap.org/ws/2004/09/policy";
-
- public static final String POLICY = "Policy";
-
- public static final String EXACTLY_ONE = "ExactlyOne";
-
- public static final String ALL = "All";
-
private Map<QName, AssertionBuilder> registeredBuilders
= new ConcurrentHashMap<QName, AssertionBuilder>();
- private AssertionBuilder defaultBuilder;
- private final ConverterRegistry converters;
- private final PolicyEngine engine;
- public AssertionBuilderFactory(PolicyEngine eng, ConverterRegistry reg) {
- converters = reg;
+ protected AssertionBuilder defaultBuilder;
+ protected ConverterRegistry converters = new ConverterRegistry();
+ protected PolicyEngine engine;
+
+ public AssertionBuilderFactoryImpl(PolicyEngine eng) {
engine = eng;
for (AssertionBuilder builder :
Service.providers(AssertionBuilder.class)) {
@@ -65,9 +57,20 @@ public class AssertionBuilderFactory {
defaultBuilder = new XMLPrimitiveAssertionBuilder();
}
+ public ConverterRegistry getConverterRegistry() {
+ return converters;
+ }
+
public PolicyEngine getPolicyEngine() {
return engine;
}
+
+ /**
+ * Touch point for subclasses that would like to dynamically load
+ * builders when policies are first encountered
+ */
+ protected void loadDynamic() {
+ }
/**
* Registers an AssertionBuilder with a specified QName.
@@ -77,9 +80,16 @@ public class AssertionBuilderFactory {
* an element of specified type
*/
public void registerBuilder(QName key, AssertionBuilder builder) {
+ loadDynamic();
registeredBuilders.put(key, builder);
}
+ public void registerBuilder(AssertionBuilder builder) {
+ loadDynamic();
+ for (QName q : builder.getKnownElements()) {
+ registeredBuilders.put(q, builder);
+ }
+ }
/**
* Returns an assertion that is built using the specified element.
@@ -89,20 +99,30 @@ public class AssertionBuilderFactory {
* @return an Assertion that is built using the specified element.
*/
public Assertion build(Object element) {
+ loadDynamic();
AssertionBuilder builder;
QName qname = converters.findQName(element);
builder = registeredBuilders.get(qname);
if (builder == null) {
- /*
- * if we can't locate an appropriate AssertionBuilder, we always
use the
- * XMLPrimitiveAssertionBuilder
- */
- builder = defaultBuilder;
+ builder = handleNoRegisteredBuilder(qname);
}
return invokeBuilder(element, builder);
}
+ /**
+ * A subclass may want to log when an unknown assertion is encountered
+ * or possibly throw an exception or similar. Another option is to
+ * provide a unique AssertionBuilder specific for that QName.
+ * @param qname
+ * @return a AssertionBuilder to use for the element.
+ */
+ protected AssertionBuilder handleNoRegisteredBuilder(QName qname) {
+ // if we can't locate an appropriate AssertionBuilder, we always use
the
+ // XMLPrimitiveAssertionBuilder
+ return defaultBuilder;
+ }
+
@SuppressWarnings("unchecked")
private Assertion invokeBuilder(Object element, AssertionBuilder builder) {
Class<?> type = findAssertionBuilderTarget(builder.getClass());
@@ -133,6 +153,7 @@ public class AssertionBuilderFactory {
* @return an AssertionBuilder that understands qname type
*/
public AssertionBuilder getBuilder(QName qname) {
+ loadDynamic();
return registeredBuilders.get(qname);
}
}
Modified:
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyEngine.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyEngine.java?rev=1071681&r1=1071680&r2=1071681&view=diff
==============================================================================
---
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyEngine.java
(original)
+++
webservices/commons/trunk/modules/neethi/src/main/java/org/apache/neethi/PolicyEngine.java
Thu Feb 17 16:29:41 2011
@@ -44,9 +44,17 @@ public class PolicyEngine {
private static final Log LOG = LogFactory.getLog(PolicyEngine.class);
- private ConverterRegistry converters = new ConverterRegistry();
- private AssertionBuilderFactory factory = new
AssertionBuilderFactory(this, converters);
- private PolicyRegistry defaultPolicyRegistry;
+ protected AssertionBuilderFactory factory = new
AssertionBuilderFactoryImpl(this);
+ protected PolicyRegistry defaultPolicyRegistry;
+
+ public PolicyEngine() {
+ factory = new AssertionBuilderFactoryImpl(this);
+ }
+
+ public PolicyEngine(AssertionBuilderFactory factory) {
+ this.factory = factory;
+ }
+
/**
* Registers an AssertionBuilder instances and associates it with a QName.
@@ -78,6 +86,10 @@ public class PolicyEngine {
public void setPolicyRegistry(PolicyRegistry reg) {
defaultPolicyRegistry = reg;
}
+
+ public AssertionBuilderFactory getAssertionBuilderFactory() {
+ return factory;
+ }
/**
* Creates a Policy object from an InputStream.
@@ -90,12 +102,11 @@ public class PolicyEngine {
try {
XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
return getPolicy(reader);
+ } catch (RuntimeException ex) {
+ throw ex;
} catch (Exception ex) {
- ex.printStackTrace();
+ throw new RuntimeException("Could not load policy.", ex);
}
-
- // TODO throw an IllegalArgumentException
- return null;
}
public Policy getPolicy(Element el) {
@@ -129,11 +140,11 @@ public class PolicyEngine {
try {
XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
return getPolicyReference(reader);
+ } catch (RuntimeException ex) {
+ throw ex;
} catch (Exception ex) {
- ex.printStackTrace();
+ throw new RuntimeException("Could not load policy reference.",
ex);
}
- // TODO throw an IllegalArgumentException
- return null;
}
/**
@@ -144,7 +155,7 @@ public class PolicyEngine {
* @return a PolicyReference object of the PolicyReference element
*/
public PolicyReference getPolicyReference(Object element) {
- QName qn = converters.findQName(element);
+ QName qn = factory.getConverterRegistry().findQName(element);
if (!Constants.isPolicyRef(qn)) {
throw new RuntimeException(
@@ -153,7 +164,7 @@ public class PolicyEngine {
PolicyReference reference = new PolicyReference(this);
- Map<QName, String> attributes = converters.getAttributes(element);
+ Map<QName, String> attributes =
factory.getConverterRegistry().getAttributes(element);
// setting the URI value
reference.setURI(attributes.get(new QName("URI")));
@@ -161,7 +172,7 @@ public class PolicyEngine {
}
private Policy getPolicyOperator(Object element) {
- String ns = converters.findQName(element).getNamespaceURI();
+ String ns =
factory.getConverterRegistry().findQName(element).getNamespaceURI();
return (Policy) processOperationElement(element, new
Policy(defaultPolicyRegistry, ns));
}
@@ -179,7 +190,7 @@ public class PolicyEngine {
if (Constants.TYPE_POLICY == operator.getType()) {
Policy policyOperator = (Policy) operator;
- Map<QName, String> attributes =
converters.getAttributes(operationElement);
+ Map<QName, String> attributes =
factory.getConverterRegistry().getAttributes(operationElement);
for (Map.Entry<QName, String> ent : attributes.entrySet()) {
policyOperator.addAttribute(ent.getKey(), ent.getValue());
@@ -187,11 +198,11 @@ public class PolicyEngine {
}
- for (Iterator iterator =
converters.getChildElements(operationElement);
+ for (Iterator iterator =
factory.getConverterRegistry().getChildElements(operationElement);
iterator.hasNext();) {
Object childElement = iterator.next();
- QName qn = converters.findQName(childElement);
+ QName qn = factory.getConverterRegistry().findQName(childElement);
if (childElement == null || qn == null
|| qn.getNamespaceURI() == null) {