notify-dpml AT lists.ibiblio.org
Subject: DPML Notify
List archive
r1018 - trunk/main/metro/model/src/main/net/dpml/metro/data
- From: mcconnell at BerliOS <mcconnell AT mail.berlios.de>
- To: notify-dpml AT lists.ibiblio.org
- Subject: r1018 - trunk/main/metro/model/src/main/net/dpml/metro/data
- Date: Sat, 4 Feb 2006 17:34:39 +0100
Author: mcconnell
Date: 2006-02-04 17:34:39 +0100 (Sat, 04 Feb 2006)
New Revision: 1018
Added:
trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirective.java
trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirectiveBeanInfo.java
Log:
add array directive
Added: trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirective.java
===================================================================
--- trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirective.java
2006-02-04 05:58:00 UTC (rev 1017)
+++ trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirective.java
2006-02-04 16:34:39 UTC (rev 1018)
@@ -0,0 +1,232 @@
+/*
+ * Copyright 2004-2005 Stephen J. McConnell.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.dpml.metro.data;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.lang.reflect.Array;
+import java.util.Map;
+import java.util.Arrays;
+
+import net.dpml.transit.Value;
+import net.dpml.transit.Construct;
+
+import net.dpml.part.Directive;
+
+/**
+ * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
+ * @version @PROJECT-VERSION@
+ */
+public final class ArrayDirective implements Directive, Value
+{
+
//--------------------------------------------------------------------------
+ // static
+
//--------------------------------------------------------------------------
+
+ /**
+ * Serial version identifier.
+ */
+ static final long serialVersionUID = 1L;
+
+
//--------------------------------------------------------------------------
+ // state
+
//--------------------------------------------------------------------------
+
+ private final String m_classname;
+ private final Value[] m_values;
+
+
//--------------------------------------------------------------------------
+ // constructors
+
//--------------------------------------------------------------------------
+ /**
+ * Create a new array directive.
+ *
+ * @param classname the array type
+ * @param values the construct values
+ */
+ public ArrayDirective( String classname, Value[] values )
+ {
+ if( null == classname )
+ {
+ m_classname = Object.class.getName();
+ }
+ else
+ {
+ m_classname = classname;
+ }
+
+ m_values = values;
+ }
+
+
//--------------------------------------------------------------------------
+ // ArrayDirective
+
//--------------------------------------------------------------------------
+
+ public String getClassname()
+ {
+ return m_classname;
+ }
+
+ public Value[] getValues()
+ {
+ return m_values;
+ }
+
+
//--------------------------------------------------------------------------
+ // Value
+
//--------------------------------------------------------------------------
+
+ /**
+ * Resolve an instance from the value using the context classloader.
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve() throws Exception
+ {
+ return resolve( null, false );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied context map. If
any
+ * target expressions in immediate or nested values contain a symbolic
+ * expression the value will be resolved using the supplied map.
+ *
+ * @param map the context map
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( Map map ) throws Exception
+ {
+ return resolve( map, false );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied isolvation policy.
+ *
+ * @param isolate the isolation policy
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( boolean isolate ) throws Exception
+ {
+ return resolve( null, isolate );
+ }
+
+ /**
+ * Resolve an instance from the value using a supplied context map. If
any
+ * target expressions in immediate or nested values contain a symbolic
+ * expression the value will be resolved using the supplied map.
+ *
+ * @param map the context map
+ * @param isolate the isolation policy
+ * @return the resolved instance
+ * @exception Exception if error occurs during instance resolution
+ */
+ public Object resolve( Map map, boolean isolate ) throws Exception
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Class c = loader.loadClass( m_classname );
+ Object[] instances = (Object[]) Array.newInstance( c,
m_values.length );
+ for( int i=0; i<m_values.length; i++ )
+ {
+ Value value = m_values[i];
+ instances[i] = value.resolve( map, isolate );
+ }
+ return instances;
+ }
+
+
//--------------------------------------------------------------------------
+ // Object
+
//--------------------------------------------------------------------------
+
+ /**
+ * Test if the supplied object is equal to this object.
+ * @param other the object to compare with this instance
+ * @return TRUE if the supplied object is equal to this object
+ */
+ public boolean equals( Object other )
+ {
+ if( null == other )
+ {
+ return false;
+ }
+ if( !( other instanceof ArrayDirective ) )
+ {
+ return false;
+ }
+ else
+ {
+ ArrayDirective directive = (ArrayDirective) other;
+ if( !m_classname.equals( directive.m_classname ) )
+ {
+ return false;
+ }
+ else
+ {
+ return Arrays.equals( m_values, directive.m_values );
+ }
+ }
+ }
+
+ /**
+ * Return the hashcode for the instance.
+ * @return the instance hashcode
+ */
+ public int hashCode()
+ {
+ int hash = m_classname.hashCode();
+ for( int i=0; i<m_values.length; i++ )
+ {
+ hash ^= m_values[i].hashCode();
+ }
+ return hash;
+ }
+
+
//--------------------------------------------------------------------------
+ // Part
+
//--------------------------------------------------------------------------
+
+ /**
+ * Return the part handler uri.
+ * @return the uri of the part handler
+ */
+ public URI getPartHandlerURI()
+ {
+ return PART_HANDLER_URI;
+ }
+
+ private static final URI PART_HANDLER_URI = setupURI(
"@PART-HANDLER-URI@" );
+
+ /**
+ * Internal utility to create a static uri.
+ * @param spec the uri spec
+ * @return the uri
+ */
+ protected static URI setupURI( String spec )
+ {
+ try
+ {
+ return new URI( spec );
+ }
+ catch( URISyntaxException ioe )
+ {
+ return null;
+ }
+ }
+}
Added:
trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirectiveBeanInfo.java
===================================================================
---
trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirectiveBeanInfo.java
2006-02-04 05:58:00 UTC (rev 1017)
+++
trunk/main/metro/model/src/main/net/dpml/metro/data/ArrayDirectiveBeanInfo.java
2006-02-04 16:34:39 UTC (rev 1018)
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2005 Stephen J. McConnell.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.dpml.metro.data;
+
+import java.beans.SimpleBeanInfo;
+import java.beans.BeanDescriptor;
+import java.beans.DefaultPersistenceDelegate;
+import java.beans.Encoder;
+import java.beans.Expression;
+
+/**
+ * BeanInfo for the ContextDescriptor class that declares a persistence
+ * delegate.
+ *
+ * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
+ * @version @PROJECT-VERSION@
+ */
+public final class ArrayDirectiveBeanInfo extends SimpleBeanInfo
+{
+ private static final BeanDescriptor BEAN_DESCRIPTOR =
setupBeanDescriptor();
+
+ /**
+ * Return the bean descriptor.
+ * @return the descriptor
+ */
+ public BeanDescriptor getBeanDescriptor()
+ {
+ return BEAN_DESCRIPTOR;
+ }
+
+ private static BeanDescriptor setupBeanDescriptor()
+ {
+ BeanDescriptor descriptor = new BeanDescriptor( ArrayDirective.class
);
+ descriptor.setValue(
+ "persistenceDelegate",
+ new ArrayDirectivePersistenceDelegate() );
+ return descriptor;
+ }
+
+ /**
+ * Persistence delegate implementation.
+ */
+ private static class ArrayDirectivePersistenceDelegate extends
DefaultPersistenceDelegate
+ {
+ /**
+ * Return the expression value.
+ * @param old the old instance
+ * @param encoder the encoder
+ * @return the expression
+ */
+ public Expression instantiate( Object old, Encoder encoder )
+ {
+ ArrayDirective construct = (ArrayDirective) old;
+ Object[] args = new Object[2];
+ args[0] = construct.getClassname();
+ args[1] = construct.getValues();
+ return new Expression( old, old.getClass(), "new", args );
+ }
+ }
+}
- r1018 - trunk/main/metro/model/src/main/net/dpml/metro/data, mcconnell at BerliOS, 02/04/2006
Archive powered by MHonArc 2.6.24.