[jboss-cvs] JBossAS SVN: r80144 - in projects/demos/microcontainer/trunk/ioc/src/main: resources/META-INF and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Oct 28 09:00:57 EDT 2008
Author: alesj
Date: 2008-10-28 09:00:56 -0400 (Tue, 28 Oct 2008)
New Revision: 80144
Added:
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/PrototypeCreator.java
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/SetterProxy.java
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/UnmodifiablePrototype.java
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/ValueInvoker.java
Modified:
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java
projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/Prototype.java
projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/annotations-beans.xml
projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/bean-factory-beans.xml
Log:
Impl enhanced bean factory.
Modified: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java 2008-10-28 12:58:40 UTC (rev 80143)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -21,6 +21,13 @@
*/
package org.jboss.demos.ioc.factory;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.HashSet;
+
import org.jboss.beans.metadata.plugins.factory.GenericBeanFactory;
import org.jboss.kernel.spi.config.KernelConfigurator;
@@ -37,7 +44,49 @@
public Object createBean() throws Throwable
{
Object bean = super.createBean();
- // TODO enhance bean - using javassist
- return bean;
+ Class<?> clazz = bean.getClass();
+ if (clazz.isAnnotationPresent(SetterProxy.class))
+ {
+ Set<Class> interfaces = new HashSet<Class>();
+ addInterfaces(clazz, interfaces);
+
+ return Proxy.newProxyInstance(
+ clazz.getClassLoader(),
+ interfaces.toArray(new Class<?>[interfaces.size()]),
+ new SetterInterceptor(bean)
+ );
+ }
+ else
+ {
+ return bean;
+ }
}
+
+ protected static void addInterfaces(Class<?> clazz, Set<Class> interfaces)
+ {
+ if (clazz == null)
+ return;
+
+ interfaces.addAll(Arrays.asList(clazz.getInterfaces()));
+ addInterfaces(clazz.getSuperclass(), interfaces);
+ }
+
+ private class SetterInterceptor implements InvocationHandler
+ {
+ private Object target;
+
+ private SetterInterceptor(Object target)
+ {
+ this.target = target;
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ String methodName = method.getName();
+ if (methodName.startsWith("set"))
+ throw new IllegalArgumentException("Cannot invoke setters.");
+
+ return method.invoke(target, args);
+ }
+ }
}
\ No newline at end of file
Modified: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/Prototype.java
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/Prototype.java 2008-10-28 12:58:40 UTC (rev 80143)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/Prototype.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -24,7 +24,7 @@
/**
* @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
*/
-public class Prototype
+public class Prototype implements ValueInvoker
{
private Object value;
@@ -46,4 +46,9 @@
{
this.value = value;
}
+
+ public Object getValue()
+ {
+ return value;
+ }
}
Copied: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/PrototypeCreator.java (from rev 79973, projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java)
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/PrototypeCreator.java (rev 0)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/PrototypeCreator.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -0,0 +1,68 @@
+/*
+* 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.demos.ioc.factory;
+
+import org.jboss.beans.metadata.spi.factory.BeanFactory;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class PrototypeCreator
+{
+ private BeanFactory bfDefault;
+ private BeanFactory enhanced;
+ private BeanFactory proxied;
+
+ public void create() throws Throwable
+ {
+ ValueInvoker vi1 = (ValueInvoker)bfDefault.createBean();
+ vi1.setValue("default");
+
+ ValueInvoker vi2 = (ValueInvoker)enhanced.createBean();
+ vi2.setValue("enhanced");
+
+ ValueInvoker vi3 = (ValueInvoker)proxied.createBean();
+ try
+ {
+ vi3.setValue("default");
+ throw new Error("Should not be here.");
+ }
+ catch (Exception ignored)
+ {
+ }
+ }
+
+ public void setDefault(BeanFactory bfDefault)
+ {
+ this.bfDefault = bfDefault;
+ }
+
+ public void setEnhanced(BeanFactory enhanced)
+ {
+ this.enhanced = enhanced;
+ }
+
+ public void setProxied(BeanFactory proxied)
+ {
+ this.proxied = proxied;
+ }
+}
\ No newline at end of file
Copied: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/SetterProxy.java (from rev 79973, projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/EnhancedBeanFactory.java)
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/SetterProxy.java (rev 0)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/SetterProxy.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -0,0 +1,36 @@
+/*
+* 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.demos.ioc.factory;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at Target(ElementType.TYPE)
+ at Retention(RetentionPolicy.RUNTIME)
+public @interface SetterProxy
+{
+}
\ No newline at end of file
Copied: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/UnmodifiablePrototype.java (from rev 79091, projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/Prototype.java)
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/UnmodifiablePrototype.java (rev 0)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/UnmodifiablePrototype.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -0,0 +1,38 @@
+/*
+* 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.demos.ioc.factory;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+ at SetterProxy
+public class UnmodifiablePrototype extends Prototype
+{
+ public UnmodifiablePrototype()
+ {
+ }
+
+ public UnmodifiablePrototype(Object value)
+ {
+ super(value);
+ }
+}
\ No newline at end of file
Added: projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/ValueInvoker.java
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/ValueInvoker.java (rev 0)
+++ projects/demos/microcontainer/trunk/ioc/src/main/java/org/jboss/demos/ioc/factory/ValueInvoker.java 2008-10-28 13:00:56 UTC (rev 80144)
@@ -0,0 +1,34 @@
+/*
+* 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.demos.ioc.factory;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface ValueInvoker
+{
+ Object invoke();
+
+ void setValue(Object value);
+
+ Object getValue();
+}
Modified: projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/annotations-beans.xml
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/annotations-beans.xml 2008-10-28 12:58:40 UTC (rev 80143)
+++ projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/annotations-beans.xml 2008-10-28 13:00:56 UTC (rev 80144)
@@ -14,16 +14,10 @@
</constructor>
</bean>
- <bean name="SimpleExecutorOnType" class="org.jboss.demos.ioc.annotations.SimpleExecutor">
+ <bean name="SimpleExecutor" class="org.jboss.demos.ioc.annotations.SimpleExecutor">
<annotation>@org.jboss.demos.ioc.annotations.StopWatchLog</annotation>
</bean>
- <bean name="SimpleExecutorOnMethod" class="org.jboss.demos.ioc.annotations.SimpleExecutor">
- <install method="execute">
- <annotation>@org.jboss.demos.ioc.annotations.StopWatchLog</annotation>
- </install>
- </bean>
-
<bean name="ExecutorInvoker" class="org.jboss.demos.ioc.annotations.ExecutorInvoker">
<incallback method="addExecutor"/>
</bean>
Modified: projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/bean-factory-beans.xml
===================================================================
--- projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/bean-factory-beans.xml 2008-10-28 12:58:40 UTC (rev 80143)
+++ projects/demos/microcontainer/trunk/ioc/src/main/resources/META-INF/bean-factory-beans.xml 2008-10-28 13:00:56 UTC (rev 80144)
@@ -10,4 +10,14 @@
<property name="value"><inject bean="Object"/></property>
</beanfactory>
+ <beanfactory name="ProxiedPrototype" class="org.jboss.demos.ioc.factory.UnmodifiablePrototype" factoryClass="org.jboss.demos.ioc.factory.EnhancedBeanFactory">
+ <property name="value"><inject bean="Object"/></property>
+ </beanfactory>
+
+ <bean name="PrototypeCreator" class="org.jboss.demos.ioc.factory.PrototypeCreator">
+ <property name="default"><inject bean="DefaultPrototype"/></property>
+ <property name="enhanced"><inject bean="EnhancedPrototype"/></property>
+ <property name="proxied"><inject bean="ProxiedPrototype"/></property>
+ </bean>
+
</deployment>
More information about the jboss-cvs-commits
mailing list