[jboss-cvs] JBossAS SVN: r68197 - in projects/microcontainer/trunk: kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 12 15:04:20 EST 2007
Author: alesj
Date: 2007-12-12 15:04:20 -0500 (Wed, 12 Dec 2007)
New Revision: 68197
Added:
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ExecutionWrapper.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PropertyDispatchWrapper.java
projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryAnnotationTestCase.properties
projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryTestCase.properties
projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryXMLTestCase.properties
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/InstanceFactory.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/CompositeValueInvocationHandler.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/ListInstanceFactory.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SetInstanceFactory.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SortedSetInstanceFactory.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/SimpleCompositeInterface.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleCompositeInterface.java
Modified:
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/KernelControllerContextAction.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
Log:
Interface support in Collection/CompositeValue.
Dispatch notion from KernelControllerContextAction introduced to ConfigureAction.
Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ConfigureAction.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -24,11 +24,8 @@
import java.util.Set;
import org.jboss.beans.info.spi.BeanInfo;
-import org.jboss.beans.info.spi.PropertyInfo;
-import org.jboss.beans.info.plugins.BeanInfoUtil;
import org.jboss.beans.metadata.spi.BeanMetaData;
import org.jboss.beans.metadata.spi.PropertyMetaData;
-import org.jboss.beans.metadata.spi.ValueMetaData;
import org.jboss.kernel.plugins.config.Configurator;
import org.jboss.kernel.spi.dependency.KernelControllerContext;
@@ -45,7 +42,7 @@
Object object = context.getTarget();
BeanInfo info = context.getBeanInfo();
BeanMetaData metaData = context.getBeanMetaData();
- setAttributes(object, info, metaData, false);
+ setAttributes(context, object, info, metaData, false);
installKernelControllerContextAware(context);
}
@@ -59,7 +56,7 @@
BeanMetaData metaData = context.getBeanMetaData();
try
{
- setAttributes(object, info, metaData, true);
+ setAttributes(context, object, info, metaData, true);
}
catch (Throwable t)
{
@@ -70,13 +67,14 @@
/**
* Set attributes/properties.
*
+ * @param context the context
* @param target the target
* @param info the bean info
* @param metaData the bean metadata
* @param nullify should we nullify attributes/properties
* @throws Throwable for any error
*/
- protected void setAttributes(Object target, BeanInfo info, BeanMetaData metaData, boolean nullify) throws Throwable
+ protected void setAttributes(KernelControllerContext context, Object target, BeanInfo info, BeanMetaData metaData, boolean nullify) throws Throwable
{
Set<PropertyMetaData> propertys = metaData.getProperties();
if (propertys != null && propertys.isEmpty() == false)
@@ -87,7 +85,7 @@
for(PropertyMetaData property : propertys)
{
- dispatchSetProperty(property, nullify, info, target, cl);
+ dispatchSetProperty(context, property, nullify, info, target, cl);
}
}
}
@@ -95,6 +93,7 @@
/**
* Dispatch property set
*
+ * @param context the context
* @param property the property
* @param nullify should we nullify
* @param info the bean info
@@ -102,29 +101,10 @@
* @param cl classloader
* @throws Throwable for any error
*/
- // TODO - wrap with MetaDataStack push and ContextCL change?
- protected void dispatchSetProperty(PropertyMetaData property, boolean nullify, BeanInfo info, Object target, ClassLoader cl)
+ protected void dispatchSetProperty(KernelControllerContext context, PropertyMetaData property, boolean nullify, BeanInfo info, Object target, ClassLoader cl)
throws Throwable
{
- String name = property.getName();
- if (nullify)
- {
- try
- {
- info.setProperty(target, name, null);
- }
- catch (Throwable t)
- {
- if (log.isTraceEnabled())
- log.trace("Ignored for " + target + "." + name, t);
- }
- }
- else
- {
- PropertyInfo propertyInfo = BeanInfoUtil.getPropertyInfo(info, target, name);
- ValueMetaData valueMetaData = property.getValue();
- Object value = valueMetaData.getValue(propertyInfo.getType(), cl);
- info.setProperty(target, name, value);
- }
+ ExecutionWrapper wrapper = new PropertyDispatchWrapper(property, nullify, info, target, cl);
+ dispatchExecutionWrapper(context, wrapper);
}
}
\ No newline at end of file
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ExecutionWrapper.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ExecutionWrapper.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/ExecutionWrapper.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,76 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.kernel.plugins.dependency;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+/**
+ * Simple execution wrapper
+ */
+abstract class ExecutionWrapper
+{
+ /**
+ * Execute.
+ *
+ * @param access the access controller context
+ * @return execution's value
+ * @throws Throwable for any error
+ */
+ public Object execute(AccessControlContext access) throws Throwable
+ {
+ if (access == null)
+ {
+ return execute();
+ }
+ else
+ {
+ PrivilegedExceptionAction<Object> action = getAction();
+ try
+ {
+ return AccessController.doPrivileged(action, access);
+ }
+ catch (PrivilegedActionException e)
+ {
+ throw e.getCause();
+ }
+ }
+ }
+
+ /**
+ * Execute w/o access controller context.
+ *
+ * @return execution's value
+ * @throws Throwable for any error
+ */
+ protected abstract Object execute() throws Throwable;
+
+ /**
+ * Get the privileged action.
+ *
+ * @return the privileged exception action
+ */
+ protected abstract PrivilegedExceptionAction<Object> getAction();
+}
+
Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/KernelControllerContextAction.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/KernelControllerContextAction.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/KernelControllerContextAction.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -22,8 +22,7 @@
package org.jboss.kernel.plugins.dependency;
import java.security.AccessControlContext;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import org.jboss.beans.metadata.spi.BeanMetaData;
import org.jboss.dependency.plugins.action.SimpleControllerContextAction;
@@ -66,6 +65,20 @@
*/
static Object dispatchJoinPoint(final KernelControllerContext context, final Joinpoint joinpoint) throws Throwable
{
+ ExecutionWrapper wrapper = new JoinpointDispatchWrapper(joinpoint);
+ return dispatchExecutionWrapper(context, wrapper);
+ }
+
+ /**
+ * Dispatch a wrapper
+ *
+ * @param context the context
+ * @param wrapper the wrapper
+ * @return the result
+ * @throws Throwable for any error
+ */
+ static Object dispatchExecutionWrapper(final KernelControllerContext context, final ExecutionWrapper wrapper) throws Throwable
+ {
BeanMetaData metaData = context.getBeanMetaData();
ClassLoader cl = Configurator.getClassLoader(metaData);
AccessControlContext access = null;
@@ -92,22 +105,7 @@
if (cl != null && access == null)
Thread.currentThread().setContextClassLoader(cl);
- if (access == null)
- {
- return joinpoint.dispatch();
- }
- else
- {
- DispatchJoinPoint action = new DispatchJoinPoint(joinpoint);
- try
- {
- return AccessController.doPrivileged(action, access);
- }
- catch (PrivilegedActionException e)
- {
- throw e.getCause();
- }
- }
+ return wrapper.execute(access);
}
finally
{
@@ -196,4 +194,29 @@
}
}
}
+
+ /**
+ * Joinpoint dispatch execution wrapper.
+ */
+ private static class JoinpointDispatchWrapper extends ExecutionWrapper
+ {
+ private Joinpoint joinpoint;
+
+ public JoinpointDispatchWrapper(Joinpoint joinpoint)
+ {
+ if (joinpoint == null)
+ throw new IllegalArgumentException("Null joinpoint");
+ this.joinpoint = joinpoint;
+ }
+
+ protected Object execute() throws Throwable
+ {
+ return joinpoint.dispatch();
+ }
+
+ protected PrivilegedExceptionAction<Object> getAction()
+ {
+ return new DispatchJoinPoint(joinpoint);
+ }
+ }
}
\ No newline at end of file
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PropertyDispatchWrapper.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PropertyDispatchWrapper.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/dependency/PropertyDispatchWrapper.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,117 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.kernel.plugins.dependency;
+
+import java.security.PrivilegedExceptionAction;
+
+import org.jboss.logging.Logger;
+import org.jboss.beans.metadata.spi.PropertyMetaData;
+import org.jboss.beans.metadata.spi.ValueMetaData;
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.beans.info.spi.PropertyInfo;
+import org.jboss.beans.info.plugins.BeanInfoUtil;
+
+/**
+ * PropertyDispatchWrapper.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+class PropertyDispatchWrapper extends ExecutionWrapper
+{
+ private static Logger log = Logger.getLogger(ConfigureAction.class);
+
+ private PropertyMetaData property;
+ private boolean nullify;
+ private BeanInfo beanInfo;
+ private Object target;
+ private ClassLoader cl;
+
+ public PropertyDispatchWrapper(PropertyMetaData property, boolean nullify, BeanInfo beanInfo, Object target, ClassLoader cl)
+ {
+ if (property == null)
+ throw new IllegalArgumentException("Null property");
+ this.property = property;
+ this.nullify = nullify;
+ if (beanInfo == null)
+ throw new IllegalArgumentException("Null bean info");
+ this.beanInfo = beanInfo;
+ if (target == null)
+ throw new IllegalArgumentException("Null target");
+ this.target = target;
+ this.cl = cl;
+ }
+
+ public Object execute() throws Throwable
+ {
+ String name = property.getName();
+ if (nullify)
+ {
+ try
+ {
+ beanInfo.setProperty(target, name, null);
+ }
+ catch (Throwable t)
+ {
+ if (log.isTraceEnabled())
+ log.trace("Ignored for " + target + "." + name, t);
+ }
+ }
+ else
+ {
+ PropertyInfo propertyInfo = BeanInfoUtil.getPropertyInfo(beanInfo, target, name);
+ ValueMetaData valueMetaData = property.getValue();
+ Object value = valueMetaData.getValue(propertyInfo.getType(), cl);
+ beanInfo.setProperty(target, name, value);
+ }
+ return null;
+ }
+
+ protected PrivilegedExceptionAction<Object> getAction()
+ {
+ return new PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ try
+ {
+ return execute();
+ }
+ catch (RuntimeException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
+ {
+ throw e;
+ }
+ catch (Error e)
+ {
+ throw e;
+ }
+ catch (Throwable t)
+ {
+ throw new RuntimeException(t);
+ }
+ }
+ };
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryAnnotationTestCase.properties
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryAnnotationTestCase.properties (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryAnnotationTestCase.properties 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1 @@
+test.Permission.1=java.lang.RuntimePermission, getClassLoader
\ No newline at end of file
Added: projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryTestCase.properties
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryTestCase.properties (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryTestCase.properties 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1 @@
+test.Permission.1=java.lang.RuntimePermission, getClassLoader
\ No newline at end of file
Added: projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryXMLTestCase.properties
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryXMLTestCase.properties (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/xml-test/org/jboss/test/kernel/config/test/ValueFactoryXMLTestCase.properties 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1 @@
+test.Permission.1=java.lang.RuntimePermission, getClassLoader
\ No newline at end of file
Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/CollectionValueSupport.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -29,7 +29,6 @@
/**
* CollectionValueSupport.
*
- * TODO tests
* @author <a href="ales.justin at jboss.com">Ales Justin</a>
*/
public class CollectionValueSupport extends AbstractMetaValue implements CollectionValue
@@ -184,6 +183,5 @@
{
throw new UnsupportedOperationException();
}
-
}
}
Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/InstanceFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/InstanceFactory.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/InstanceFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,42 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metatype.api.values;
+
+import org.jboss.beans.info.spi.BeanInfo;
+
+/**
+ * The recreate instance factory.
+ *
+ * @param <T> the actual instance type
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface InstanceFactory<T>
+{
+ /**
+ * Create new instance.
+ *
+ * @param beanInfo the bean info
+ * @return new instance
+ * @throws Throwable for any error
+ */
+ T instantiate(BeanInfo beanInfo) throws Throwable;
+}
Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/api/values/MetaValueFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -83,6 +83,15 @@
public abstract void setBuilder(Class<?> clazz, MetaValueBuilder builder);
/**
+ * Set an instance factory.
+ *
+ * @param <T> exact instance type
+ * @param clazz the class
+ * @param factory the factory
+ */
+ public abstract <T> void setInstanceFactory(Class<T> clazz, InstanceFactory<T> factory);
+
+ /**
* Unwrap meta value.
* Supports simple and generic meta value.
*
Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/CompositeValueInvocationHandler.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/CompositeValueInvocationHandler.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/CompositeValueInvocationHandler.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,154 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metatype.plugins.values;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import java.lang.reflect.Proxy;
+import java.util.Map;
+import java.util.Collections;
+import java.util.WeakHashMap;
+
+import org.jboss.util.UnreachableStatementException;
+import org.jboss.metatype.api.values.CompositeValue;
+import org.jboss.metatype.api.values.MetaValueFactory;
+import org.jboss.metatype.api.values.MetaValue;
+
+/**
+ * CompositeValueInvocationHandler.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ */
+public class CompositeValueInvocationHandler implements InvocationHandler
+{
+ /** The MetaValueFactory */
+ private static MetaValueFactory factory = MetaValueFactory.getInstance();
+
+ /** A cache of methods to keys */
+ private static final Map<Method, String> compositeDataKeyCache = Collections.synchronizedMap(new WeakHashMap<Method, String>());
+
+ /** The composite value */
+ private CompositeValue compositeValue;
+
+ /**
+ * Create a new CompositeValueInvocationHandler.
+ *
+ * @param compositeValue the composite value
+ */
+ public CompositeValueInvocationHandler(CompositeValue compositeValue)
+ {
+ if (compositeValue == null)
+ throw new IllegalArgumentException("Null compositeValue");
+ this.compositeValue = compositeValue;
+ }
+
+ /**
+ * Get the compositeData.
+ *
+ * @return the compositeData.
+ */
+ public CompositeValue getCompositeValue()
+ {
+ return compositeValue;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ if (Object.class.equals(method.getDeclaringClass()))
+ return handleObjectInvocation(method.getName(), args);
+
+ MetaValue value = compositeValue.get(getCompositeDataKey(method));
+ Type returnType = method.getGenericReturnType();
+ return factory.unwrap(value, returnType);
+ }
+
+ private Object handleObjectInvocation(String name, Object[] args) throws Throwable
+ {
+ if ("equals".equals(name))
+ {
+ Object object = args[0];
+ if (object == null || object instanceof Proxy == false)
+ return false;
+ InvocationHandler handler = Proxy.getInvocationHandler(object);
+ if (handler == this)
+ return true;
+ if (handler == null || handler instanceof CompositeValueInvocationHandler == false)
+ return false;
+
+ CompositeValueInvocationHandler other = (CompositeValueInvocationHandler) handler;
+ return getCompositeValue().equals(other.getCompositeValue());
+ }
+ else if ("hashCode".equals(name))
+ return getCompositeValue().hashCode();
+ else if ("toString".equals(name))
+ return getCompositeValue().toString();
+ throw new UnreachableStatementException();
+ }
+
+ /**
+ * Get the key for a composite data getter method
+ *
+ * @param method the method
+ * @return the key
+ */
+ public static String getCompositeDataKey(Method method)
+ {
+ String key = compositeDataKeyCache.get(method);
+ if (key != null)
+ return key;
+
+ StringBuilder fieldName = null;
+
+ Class returnType = method.getReturnType();
+ Class[] paramTypes = method.getParameterTypes();
+ if (Void.TYPE.equals(returnType) == false && paramTypes.length == 0)
+ {
+ String name = method.getName();
+ if (name.startsWith("is") && name.length() > 2)
+ {
+ if (Boolean.TYPE.equals(returnType))
+ {
+ fieldName = new StringBuilder();
+ fieldName.append(Character.toLowerCase(name.charAt(2)));
+ if (name.length() > 3)
+ fieldName.append(name.substring(3));
+ }
+ }
+ else if (name.startsWith("get") && name.length() > 3)
+ {
+ fieldName = new StringBuilder();
+ fieldName.append(Character.toLowerCase(name.charAt(3)));
+ if (name.length() > 4)
+ fieldName.append(name.substring(4));
+ }
+ }
+
+ if (fieldName == null)
+ return null;
+
+ String result = fieldName.toString();
+ compositeDataKeyCache.put(method, result);
+ return result;
+ }
+}
Modified: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/DefaultMetaValueFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -26,6 +26,8 @@
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
@@ -34,6 +36,9 @@
import java.util.Map;
import java.util.Stack;
import java.util.WeakHashMap;
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
import org.jboss.beans.info.spi.BeanInfo;
import org.jboss.beans.info.spi.PropertyInfo;
@@ -64,6 +69,7 @@
import org.jboss.metatype.api.values.TableValueSupport;
import org.jboss.metatype.api.values.CollectionValue;
import org.jboss.metatype.api.values.CollectionValueSupport;
+import org.jboss.metatype.api.values.InstanceFactory;
import org.jboss.metatype.plugins.types.DefaultMetaTypeFactory;
import org.jboss.metatype.spi.values.MetaValueBuilder;
import org.jboss.reflect.spi.ArrayInfo;
@@ -112,6 +118,17 @@
/** The Object type info */
private static final TypeInfo OBJECT_TYPE_INFO = configuration.getTypeInfo(Object.class);
+ /** The instance factory builders */
+ private Map<Class, InstanceFactory> instanceFactoryMap = new WeakHashMap<Class, InstanceFactory>();
+
+ public DefaultMetaValueFactory()
+ {
+ // set default collection instance factories
+ setInstanceFactory(List.class, ListInstanceFactory.INSTANCE);
+ setInstanceFactory(Set.class, SetInstanceFactory.INSTANCE);
+ setInstanceFactory(SortedSet.class, SortedSetInstanceFactory.INSTANCE);
+ }
+
public void setBuilder(Class<?> clazz, MetaValueBuilder builder)
{
synchronized (builders)
@@ -121,7 +138,18 @@
builders.put(clazz, new WeakReference<MetaValueBuilder>(builder));
}
}
-
+
+ public <T> void setInstanceFactory(Class<T> clazz, InstanceFactory<T> factory)
+ {
+ synchronized(instanceFactoryMap)
+ {
+ if (factory == null)
+ instanceFactoryMap.remove(clazz);
+ else
+ instanceFactoryMap.put(clazz, factory);
+ }
+ }
+
/**
* Create a simple value
*
@@ -642,6 +670,14 @@
try
{
BeanInfo beanInfo = configuration.getBeanInfo(typeName, cl);
+ ClassInfo classInfo = beanInfo.getClassInfo();
+ if (classInfo.isInterface())
+ {
+ InvocationHandler handler = createCompositeValueInvocationHandler(compositeValue);
+ Class clazz = classInfo.getType();
+ Class[] interfaces = new Class[]{clazz};
+ return Proxy.newProxyInstance(clazz.getClassLoader(), interfaces, handler);
+ }
Object bean = createNewInstance(beanInfo);
for (String name : compositeMetaType.keySet())
{
@@ -659,6 +695,17 @@
}
/**
+ * Create composite invocation handler.
+ *
+ * @param compositeValue the composite value
+ * @return composite invocation handler
+ */
+ protected InvocationHandler createCompositeValueInvocationHandler(CompositeValue compositeValue)
+ {
+ return new CompositeValueInvocationHandler(compositeValue);
+ }
+
+ /**
* Unwrap collection meta value.
*
* @param collectionValue the collection value
@@ -672,7 +719,7 @@
{
BeanInfo collectionInfo;
// null is not instance of
- if (type instanceof ClassInfo && ((ClassInfo)type).isInterface() == false)
+ if (type instanceof ClassInfo)
{
collectionInfo = configuration.getBeanInfo(type);
}
@@ -681,12 +728,13 @@
MetaType metaType = collectionValue.getMetaType();
collectionInfo = configuration.getBeanInfo(metaType.getTypeName(), Thread.currentThread().getContextClassLoader());
}
+ ClassInfo classInfo = collectionInfo.getClassInfo();
Collection collection = (Collection)createNewInstance(collectionInfo);
Iterator<MetaValue> iter = collectionValue.iterator();
while (iter.hasNext())
{
MetaValue metaValue = iter.next();
- TypeInfo componentType = collectionInfo.getClassInfo().getComponentType();
+ TypeInfo componentType = classInfo.getComponentType();
// try better
if (OBJECT_TYPE_INFO.equals(componentType))
componentType = getTypeInfo(metaValue.getMetaType(), null);
@@ -722,7 +770,15 @@
*/
protected Object createNewInstance(BeanInfo beanInfo) throws Throwable
{
- // TODO - some 'instantiator' map, which knows how to instantiate beaninfo (non default constructor)?
+ ClassInfo classInfo = beanInfo.getClassInfo();
+ if (classInfo.isInterface())
+ {
+ InstanceFactory instanceFactory = instanceFactoryMap.get(classInfo.getType());
+ if (instanceFactory == null)
+ throw new IllegalArgumentException("Cannot instantiate interface BeanInfo, missing InstanceFactory: " + classInfo);
+
+ return instanceFactory.instantiate(beanInfo);
+ }
return beanInfo.newInstance();
}
Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/ListInstanceFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/ListInstanceFactory.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/ListInstanceFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,47 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metatype.plugins.values;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.jboss.metatype.api.values.InstanceFactory;
+import org.jboss.beans.info.spi.BeanInfo;
+
+/**
+ * List instance factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ListInstanceFactory implements InstanceFactory<List>
+{
+ public static ListInstanceFactory INSTANCE = new ListInstanceFactory();
+
+ private ListInstanceFactory()
+ {
+ }
+
+ public List instantiate(BeanInfo beanInfo) throws Throwable
+ {
+ return new ArrayList();
+ }
+}
Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SetInstanceFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SetInstanceFactory.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SetInstanceFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,47 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metatype.plugins.values;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.metatype.api.values.InstanceFactory;
+
+/**
+ * Set instance factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SetInstanceFactory implements InstanceFactory<Set>
+{
+ public static SetInstanceFactory INSTANCE = new SetInstanceFactory();
+
+ private SetInstanceFactory()
+ {
+ }
+
+ public Set instantiate(BeanInfo beanInfo) throws Throwable
+ {
+ return new HashSet();
+ }
+}
Added: projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SortedSetInstanceFactory.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SortedSetInstanceFactory.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/main/org/jboss/metatype/plugins/values/SortedSetInstanceFactory.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,47 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.metatype.plugins.values;
+
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.metatype.api.values.InstanceFactory;
+
+/**
+ * Sorted set instance factory.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class SortedSetInstanceFactory implements InstanceFactory<SortedSet>
+{
+ public static SortedSetInstanceFactory INSTANCE = new SortedSetInstanceFactory();
+
+ private SortedSetInstanceFactory()
+ {
+ }
+
+ public SortedSet instantiate(BeanInfo beanInfo) throws Throwable
+ {
+ return new TreeSet();
+ }
+}
Added: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/SimpleCompositeInterface.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/SimpleCompositeInterface.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/SimpleCompositeInterface.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,44 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.test.metatype.values.factory.support;
+
+/**
+ * SimpleCompositeInterface.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface SimpleCompositeInterface
+{
+ /**
+ * Get the something.
+ *
+ * @return the something.
+ */
+ String getSomething();
+
+ /**
+ * Set the something.
+ *
+ * @param something the something.
+ */
+ void setSomething(String something);
+}
Added: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleCompositeInterface.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleCompositeInterface.java (rev 0)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/support/TestSimpleCompositeInterface.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -0,0 +1,76 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt 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.test.metatype.values.factory.support;
+
+/**
+ * TestSimpleCompositeInterface.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class TestSimpleCompositeInterface implements SimpleCompositeInterface
+{
+ private String something;
+
+ /**
+ * Default constructor.
+ * Needed with unwrap.
+ */
+ public TestSimpleCompositeInterface()
+ {
+ }
+
+ /**
+ * Create a new TestSimpleComposite.
+ *
+ * @param something the something
+ */
+ public TestSimpleCompositeInterface(String something)
+ {
+ this.something = something;
+ }
+
+ public String getSomething()
+ {
+ return something;
+ }
+
+ public void setSomething(String something)
+ {
+ this.something = something;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (obj == this)
+ return true;
+ if (obj == null || obj instanceof SimpleCompositeInterface == false)
+ return false;
+
+ SimpleCompositeInterface other = (SimpleCompositeInterface) obj;
+ return something.equals(other.getSomething());
+ }
+
+ public int hashCode()
+ {
+ return something.hashCode();
+ }
+}
Modified: projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java 2007-12-12 19:43:51 UTC (rev 68196)
+++ projects/microcontainer/trunk/metatype/src/tests/org/jboss/test/metatype/values/factory/test/UnwrapValueUnitTestCase.java 2007-12-12 20:04:20 UTC (rev 68197)
@@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
import java.util.Date;
import junit.framework.Test;
@@ -33,6 +35,8 @@
import org.jboss.test.metatype.values.factory.support.TestGeneric;
import org.jboss.test.metatype.values.factory.support.TestSimpleComposite;
import org.jboss.test.metatype.values.factory.support.TestRecursiveSimpleComposite;
+import org.jboss.test.metatype.values.factory.support.TestSimpleCompositeInterface;
+import org.jboss.test.metatype.values.factory.support.SimpleCompositeInterface;
/**
* UnwrapValueUnitTestCase.
@@ -87,8 +91,10 @@
checkSingle(composite, true);
checkSingle(composite, false);
- checkSingle(new TestRecursiveSimpleComposite("something", composite), true);
+ checkSingle(new TestRecursiveSimpleComposite("something", composite), true);
checkSingle(new TestRecursiveSimpleComposite("something", composite), false);
+
+ checkSingle(new TestSimpleCompositeInterface("something"), SimpleCompositeInterface.class);
}
public void testCollectionUnwrap() throws Exception
@@ -96,23 +102,31 @@
Integer i1 = 123;
Integer i2 = 123;
checkCollection(new ArrayList<Integer>(), i1, i2);
+ checkCollection(new ArrayList<Integer>(), List.class, i1, i2);
checkCollection(new HashSet<Integer>(), i1, i2);
+ checkCollection(new HashSet<Integer>(), Set.class, i1, i2);
TestEnum one = TestEnum.ONE;
TestEnum two = TestEnum.TWO;
TestEnum three = TestEnum.THREE;
checkCollection(new ArrayList<TestEnum>(), one, two, three, one);
+ checkCollection(new ArrayList<TestEnum>(), List.class, one, two, three, one);
checkCollection(new HashSet<TestEnum>(), one, two, three, one);
+ checkCollection(new HashSet<TestEnum>(), Set.class, one, two, three, one);
TestGeneric g1 = new TestGeneric("123");
TestGeneric g2 = new TestGeneric("123");
checkCollection(new ArrayList<TestGeneric>(), g1, g2);
+ checkCollection(new ArrayList<TestGeneric>(), List.class, g1, g2);
checkCollection(new HashSet<TestGeneric>(), g1, g2);
+ checkCollection(new HashSet<TestGeneric>(), Set.class, g1, g2);
TestSimpleComposite c1 = new TestSimpleComposite("123");
TestSimpleComposite c2 = new TestSimpleComposite("123");
checkCollection(new ArrayList<TestSimpleComposite>(), c1, c2);
+ checkCollection(new ArrayList<TestSimpleComposite>(), List.class, c1, c2);
checkCollection(new HashSet<TestSimpleComposite>(), c1, c2);
+ checkCollection(new HashSet<TestSimpleComposite>(), Set.class, c1, c2);
}
public void testArrayUnwrap() throws Exception
@@ -237,6 +251,14 @@
assertEquals(object, unwrapped);
}
+ protected void checkSingle(Object object, Class<?> clazz)
+ {
+ MetaValue metaValue = createMetaValue(object, clazz);
+ assertNotNull(metaValue);
+ Object unwrapped = unwrapMetaValue(metaValue, clazz);
+ assertEquals(object, unwrapped);
+ }
+
protected <T> void checkCollection(Collection<T> collection, T... params) throws Exception
{
collection.addAll(Arrays.asList(params));
@@ -244,6 +266,12 @@
checkSingle(collection, false);
}
+ protected <T> void checkCollection(Collection<T> collection, Class<? extends Collection> clazz, T... params) throws Exception
+ {
+ collection.addAll(Arrays.asList(params));
+ checkSingle(collection, clazz);
+ }
+
protected void checkArray(Object object, boolean typeInfoFromObject, Asserter asserter)
{
MetaValue metaValue = createMetaValue(object);
More information about the jboss-cvs-commits
mailing list