Update of /cvsroot/sunxacml/sunxacml/com/sun/xacml/attr
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10845/com/sun/xacml/attr
Modified Files:
AttributeFactory.java StandardAttributeFactory.java
Added Files:
BaseAttributeFactory.java
Log Message:
setup new, more flexible factory scheme
Index: StandardAttributeFactory.java
===================================================================
RCS file: /cvsroot/sunxacml/sunxacml/com/sun/xacml/attr/StandardAttributeFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** StandardAttributeFactory.java 13 Feb 2004 17:54:33 -0000 1.1
--- StandardAttributeFactory.java 15 Mar 2004 21:34:50 -0000 1.2
***************
*** 37,48 ****
package com.sun.xacml.attr;
! import com.sun.xacml.ParsingException;
! import com.sun.xacml.UnknownIdentifierException;
!
! import java.net.URI;
!
! import java.util.HashMap;
- import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
--- 37,55 ----
package com.sun.xacml.attr;
! import com.sun.xacml.attr.proxy.AnyURIAttributeProxy;
! import com.sun.xacml.attr.proxy.Base64BinaryAttributeProxy;
! import com.sun.xacml.attr.proxy.BooleanAttributeProxy;
! import com.sun.xacml.attr.proxy.DateAttributeProxy;
! import com.sun.xacml.attr.proxy.DateTimeAttributeProxy;
! import com.sun.xacml.attr.proxy.DayTimeDurationAttributeProxy;
! import com.sun.xacml.attr.proxy.DoubleAttributeProxy;
! import com.sun.xacml.attr.proxy.HexBinaryAttributeProxy;
! import com.sun.xacml.attr.proxy.IntegerAttributeProxy;
! import com.sun.xacml.attr.proxy.RFC822NameAttributeProxy;
! import com.sun.xacml.attr.proxy.StringAttributeProxy;
! import com.sun.xacml.attr.proxy.TimeAttributeProxy;
! import com.sun.xacml.attr.proxy.YearMonthDurationAttributeProxy;
! import com.sun.xacml.attr.proxy.X500NameAttributeProxy;
import org.w3c.dom.Node;
***************
*** 54,58 ****
* @author Seth Proctor
*/
! public class StandardAttributeFactory extends AttributeFactory
{
--- 61,65 ----
* @author Seth Proctor
*/
! public class StandardAttributeFactory extends BaseAttributeFactory
{
***************
*** 60,66 ****
private static StandardAttributeFactory factoryInstance = null;
- // the map of proxies
- private HashMap attributeMap;
-
// dummy object used as a lock for the getFactory routine
// FIXME: this needs a better mechanism
--- 67,70 ----
***************
*** 72,224 ****
*/
private StandardAttributeFactory() {
! attributeMap = new HashMap();
!
! // boolean
! attributeMap.put(BooleanAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return BooleanAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return BooleanAttribute.getInstance(value);
! }
! });
!
! // date
! attributeMap.put(DateAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return DateAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return DateAttribute.getInstance(value);
! }
! });
!
! // time
! attributeMap.put(TimeAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return TimeAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return TimeAttribute.getInstance(value);
! }
! });
!
! // dateTime
! attributeMap.put(DateTimeAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return DateTimeAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return DateTimeAttribute.getInstance(value);
! }
! });
!
! // dayTimeDuration
! attributeMap.put(DayTimeDurationAttribute.identifier,
! new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return DayTimeDurationAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return DayTimeDurationAttribute.getInstance(value);
! }
! });
!
! // yearMonthDuration
! attributeMap.put(YearMonthDurationAttribute.identifier,
! new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return YearMonthDurationAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return YearMonthDurationAttribute.getInstance(value);
! }
! });
!
! // double
! attributeMap.put(DoubleAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return DoubleAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return DoubleAttribute.getInstance(value);
! }
! });
!
! // integer
! attributeMap.put(IntegerAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return IntegerAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return IntegerAttribute.getInstance(value);
! }
! });
!
! // string
! attributeMap.put(StringAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) {
! return StringAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) {
! return StringAttribute.getInstance(value);
! }
! });
!
! // anyURI
! attributeMap.put(AnyURIAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return AnyURIAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return AnyURIAttribute.getInstance(value);
! }
! });
!
! // hexBinary
! attributeMap.put(HexBinaryAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return HexBinaryAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return HexBinaryAttribute.getInstance(value);
! }
! });
!
! // base64Binary
! attributeMap.put(Base64BinaryAttribute.identifier,
! new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return Base64BinaryAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return Base64BinaryAttribute.getInstance(value);
! }
! });
!
! // x500Name
! attributeMap.put(X500NameAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) throws Exception {
! return X500NameAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) throws Exception {
! return X500NameAttribute.getInstance(value);
! }
! });
!
! // rfc822Name
! attributeMap.put(RFC822NameAttribute.identifier, new AttributeProxy() {
! public AttributeValue getInstance(Node root) {
! return RFC822NameAttribute.getInstance(root);
! }
! public AttributeValue getInstance(String value) {
! return RFC822NameAttribute.getInstance(value);
! }
! });
}
/**
! * Returns the single instance of this factory, creating it if it doesn't
! * exist yet.
*
* @return the factory instance
--- 76,109 ----
*/
private StandardAttributeFactory() {
! addDatatype(BooleanAttribute.identifier, new BooleanAttributeProxy());
! addDatatype(StringAttribute.identifier, new StringAttributeProxy());
! addDatatype(DateAttribute.identifier, new DateAttributeProxy());
! addDatatype(TimeAttribute.identifier, new TimeAttributeProxy());
! addDatatype(DateTimeAttribute.identifier,
! new DateTimeAttributeProxy());
! addDatatype(DayTimeDurationAttribute.identifier,
! new DayTimeDurationAttributeProxy());
! addDatatype(YearMonthDurationAttribute.identifier,
! new YearMonthDurationAttributeProxy());
! addDatatype(DoubleAttribute.identifier, new DoubleAttributeProxy());
! addDatatype(IntegerAttribute.identifier, new IntegerAttributeProxy());
! addDatatype(AnyURIAttribute.identifier, new AnyURIAttributeProxy());
! addDatatype(HexBinaryAttribute.identifier,
! new HexBinaryAttributeProxy());
! addDatatype(Base64BinaryAttribute.identifier,
! new Base64BinaryAttributeProxy());
! addDatatype(X500NameAttribute.identifier,
! new X500NameAttributeProxy());
! addDatatype(RFC822NameAttribute.identifier,
! new RFC822NameAttributeProxy());
}
/**
! * Returns an instance of this factory. This method enforces a singleton
! * model, meaning that this always returns the same instance, creating
! * the factory if it hasn't been requested before. This is the default
! * model used by the <code>AttributeFactory</code>, ensuring quick
! * access to this factory. If you need a new instance of this factory
! * you should use the <code>getNewFactory</code> method.
*
* @return the factory instance
***************
*** 236,359 ****
/**
! * Adds a proxy to the factory, which in turn will allow new attribute
! * types to be created using the factory. Typically the proxy is
! * provided as an anonymous class that simply calls the getInstance
! * methods (or something similar) of some <code>AttributeValue</code>
! * class.
! *
! * @param id the name of the attribute type
! * @param proxy the proxy used to create new attributes of the given type
! */
! public void addDatatype(String id, AttributeProxy proxy) {
! attributeMap.put(id, proxy);
! }
!
! /**
! * Creates a value based on the given DOM root node. The type of the
! * attribute is assumed to be present in the node as an XAML attribute
! * named <code>DataType</code>, as is the case with the
! * AttributeValueType in the policy schema. The value is assumed to be
! * the first child of this node.
! *
! * @param root the DOM root of an attribute value
! *
! * @return a new <code>AttributeValue</code>
! *
! * @throws UnknownIdentifierException if the type in the node isn't
! * known to the factory
! * @throws ParsingException if the node is invalid or can't be parsed
! * by the appropriate proxy
! */
! public AttributeValue createValue(Node root)
! throws UnknownIdentifierException, ParsingException
! {
! Node node = root.getAttributes().getNamedItem("DataType");
!
! return createValue(root, node.getNodeValue());
! }
!
! /**
! * Creates a value based on the given DOM root node and data type.
! *
! * @param root the DOM root of an attribute value
! * @param dataType the type of the attribute
! *
! * @return a new <code>AttributeValue</code>
! *
! * @throws UnknownIdentifierException if the data type isn't known to
! * the factory
! * @throws ParsingException if the node is invalid or can't be parsed
! * by the appropriate proxy
! */
! public AttributeValue createValue(Node root, URI dataType)
! throws UnknownIdentifierException, ParsingException
! {
! return createValue(root, dataType.toString());
! }
!
! /**
! * Creates a value based on the given DOM root node and data type.
! *
! * @param root the DOM root of an attribute value
! * @param type the type of the attribute
! *
! * @return a new <code>AttributeValue</code>
! *
! * @throws UnknownIdentifierException if the type isn't known to
! * the factory
! * @throws ParsingException if the node is invalid or can't be parsed
! * by the appropriate proxy
! */
! public AttributeValue createValue(Node root, String type)
! throws UnknownIdentifierException, ParsingException
! {
! AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
!
! if (proxy != null) {
! try {
! return proxy.getInstance(root);
! } catch (Exception e) {
! throw new ParsingException("couldn't create " + type +
! " attribute based on DOM node");
! }
! } else {
! throw new UnknownIdentifierException("Attributes of type " + type +
! " aren't supported.");
! }
! }
!
! /**
! * Creates a value based on the given data type and text-encoded value.
! * Used primarily by code that does an XPath query to get an
! * attribute value, and then needs to turn the resulting value into
! * an Attribute class.
! *
! * @param dataType the type of the attribute
! * @param value the text-encoded representation of an attribute's value
! *
! * @return a new <code>AttributeValue</code>
*
! * @throws UnknownIdentifierException if the data type isn't known to
! * the factory
! * @throws ParsingException if the text is invalid or can't be parsed
! * by the appropriate proxy
*/
! public AttributeValue createValue(URI dataType, String value)
! throws UnknownIdentifierException, ParsingException
! {
! String type = dataType.toString();
! AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
!
! if (proxy != null) {
! try {
! return proxy.getInstance(value);
! } catch (Exception e) {
! throw new ParsingException("couldn't create " + type +
! " attribute from input: " + value);
! }
! } else {
! throw new UnknownIdentifierException("Attributes of type " + type +
! " aren't supported.");
! }
}
--- 121,131 ----
/**
! * Returns a new instance of <code>AttributeFactory</code> that
! * supports all the standard datatypes.
*
! * @return a new <code>StandardAttributeFactory</code>
*/
! public static AttributeFactory getNewFactory() {
! return new StandardAttributeFactory();
}
--- NEW FILE: BaseAttributeFactory.java ---
/*
* @(#)BaseAttributeFactory.java
*
* Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
package com.sun.xacml.attr;
import com.sun.xacml.ParsingException;
import com.sun.xacml.UnknownIdentifierException;
import java.net.URI;
import java.util.HashMap;
import org.w3c.dom.Node;
/**
* This is a basic implementation of <code>AttributeFactory</code>. It
* implements the insertion and retrieval methods, but doesn't actually
* setup the factory with any datatypes.
*
* @author Seth Proctor
*/
public class BaseAttributeFactory extends AttributeFactory
{
// the map of proxies
private HashMap attributeMap;
/**
* Default constructor.
*/
public BaseAttributeFactory() {
attributeMap = new HashMap();
}
/**
* Adds a proxy to the factory, which in turn will allow new attribute
* types to be created using the factory. Typically the proxy is
* provided as an anonymous class that simply calls the getInstance
* methods (or something similar) of some <code>AttributeValue</code>
* class.
*
* @param id the name of the attribute type
* @param proxy the proxy used to create new attributes of the given type
*/
public void addDatatype(String id, AttributeProxy proxy) {
// make sure this doesn't already exist
if (attributeMap.containsKey(id))
throw new IllegalArgumentException("datatype already exists");
attributeMap.put(id, proxy);
}
/**
* Creates a value based on the given DOM root node. The type of the
* attribute is assumed to be present in the node as an XACML attribute
* named <code>DataType</code>, as is the case with the
* AttributeValueType in the policy schema. The value is assumed to be
* the first child of this node.
*
* @param root the DOM root of an attribute value
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the type in the node isn't
* known to the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root)
throws UnknownIdentifierException, ParsingException
{
Node node = root.getAttributes().getNamedItem("DataType");
return createValue(root, node.getNodeValue());
}
/**
* Creates a value based on the given DOM root node and data type.
*
* @param root the DOM root of an attribute value
* @param dataType the type of the attribute
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the data type isn't known to
* the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root, URI dataType)
throws UnknownIdentifierException, ParsingException
{
return createValue(root, dataType.toString());
}
/**
* Creates a value based on the given DOM root node and data type.
*
* @param root the DOM root of an attribute value
* @param type the type of the attribute
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the type isn't known to
* the factory
* @throws ParsingException if the node is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(Node root, String type)
throws UnknownIdentifierException, ParsingException
{
AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
if (proxy != null) {
try {
return proxy.getInstance(root);
} catch (Exception e) {
throw new ParsingException("couldn't create " + type +
" attribute based on DOM node");
}
} else {
throw new UnknownIdentifierException("Attributes of type " + type +
" aren't supported.");
}
}
/**
* Creates a value based on the given data type and text-encoded value.
* Used primarily by code that does an XPath query to get an
* attribute value, and then needs to turn the resulting value into
* an Attribute class.
*
* @param dataType the type of the attribute
* @param value the text-encoded representation of an attribute's value
*
* @return a new <code>AttributeValue</code>
*
* @throws UnknownIdentifierException if the data type isn't known to
* the factory
* @throws ParsingException if the text is invalid or can't be parsed
* by the appropriate proxy
*/
public AttributeValue createValue(URI dataType, String value)
throws UnknownIdentifierException, ParsingException
{
String type = dataType.toString();
AttributeProxy proxy = (AttributeProxy)(attributeMap.get(type));
if (proxy != null) {
try {
return proxy.getInstance(value);
} catch (Exception e) {
throw new ParsingException("couldn't create " + type +
" attribute from input: " + value);
}
} else {
throw new UnknownIdentifierException("Attributes of type " + type +
" aren't supported.");
}
}
}
Index: AttributeFactory.java
===================================================================
RCS file: /cvsroot/sunxacml/sunxacml/com/sun/xacml/attr/AttributeFactory.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** AttributeFactory.java 13 Feb 2004 17:52:10 -0000 1.4
--- AttributeFactory.java 15 Mar 2004 21:34:50 -0000 1.5
***************
*** 107,110 ****
--- 107,112 ----
* @param id the name of the attribute type
* @param proxy the proxy used to create new attributes of the given type
+ *
+ * @throws IllegalArgumentException if the given id is already in use
*/
public abstract void addDatatype(String id, AttributeProxy proxy);
***************
*** 127,134 ****
* @param id the name of the attribute type
* @param proxy the proxy used to create new attributes of the given type
*/
public static void addAttributeProxy(String id, AttributeProxy proxy) {
- // FIXME: this may have to change when the final mechanism is in
- // place for the default factory...
getInstance().addDatatype(id, proxy);
}
--- 129,136 ----
* @param id the name of the attribute type
* @param proxy the proxy used to create new attributes of the given type
+ *
+ * @throws IllegalArgumentException if the given id is already in use
*/
public static void addAttributeProxy(String id, AttributeProxy proxy) {
getInstance().addDatatype(id, proxy);
}
|