[jboss-cvs] JBossAS SVN: r65945 - in projects/microcontainer/trunk: aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy and 16 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Oct 8 19:02:27 EDT 2007
Author: alesj
Date: 2007-10-08 19:02:27 -0400 (Mon, 08 Oct 2007)
New Revision: 65945
Added:
projects/microcontainer/trunk/aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy/
projects/microcontainer/trunk/aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy/JBossAOPLazyInitializer.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractClassMetaData.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractLazyMetaData.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/ClassMetaData.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/LazyMetaData.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassCharactersHandler.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassHandler.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyHandler.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyInterfaceInterceptor.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/AbstractLazyInitializer.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JDKLazyInitializer.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JavassistLazyInitializer.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyInitializer.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyProxyFactory.java
projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/
projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/test/
projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.xml
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/IRare.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JDKLazyFactory.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JavassistLazyFactory.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/RareBean.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JDKLazyInstantiationTestCase.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JavassistLazyInstantiationTestCase.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyInstantiationTestCase.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.java
projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyTestSuite.java
Modified:
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/config/Configurator.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBinding20.java
projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBindingHelper.java
projects/microcontainer/trunk/kernel/src/resources/main/schema/bean-deployer_2_0.xsd
Log:
Lazy bean handling.
Added: projects/microcontainer/trunk/aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy/JBossAOPLazyInitializer.java
===================================================================
--- projects/microcontainer/trunk/aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy/JBossAOPLazyInitializer.java (rev 0)
+++ projects/microcontainer/trunk/aop-mc-int/src/main/org/jboss/aop/microcontainer/lazy/JBossAOPLazyInitializer.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,73 @@
+/*
+* 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.aop.microcontainer.lazy;
+
+import java.util.Set;
+
+import org.jboss.kernel.plugins.lazy.AbstractLazyInitializer;
+import org.jboss.kernel.plugins.config.Configurator;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.aop.proxy.container.AOPProxyFactoryParameters;
+import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory;
+import org.jboss.metadata.spi.MetaData;
+import org.jboss.metadata.spi.stack.MetaDataStack;
+
+/**
+ * JBossAOP lazy initializer.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JBossAOPLazyInitializer extends AbstractLazyInitializer
+{
+ public Object initializeProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ KernelControllerContext context = getKernelControllerContext(kernel, bean);
+ BeanInfo beanInfo = context.getBeanInfo();
+ if (beanInfo == null)
+ throw new IllegalArgumentException("Cannot proxy factory beans.");
+
+ MetaData metaData = MetaDataStack.peek();
+ MetaDataStack.mask();
+ try
+ {
+ AOPProxyFactoryParameters params = new AOPProxyFactoryParameters();
+ params.setMetaData(metaData);
+ if (exposeClass)
+ {
+ params.setProxiedClass(beanInfo.getClassInfo().getType());
+ }
+ if (interfaces != null && interfaces.isEmpty() == false)
+ {
+ ClassLoader cl = Configurator.getClassLoader(context.getBeanMetaData());
+ params.setInterfaces(getClasses(kernel.getConfigurator(), interfaces, cl));
+ }
+ GeneratedAOPProxyFactory factory = new GeneratedAOPProxyFactory();
+ return factory.createAdvisedProxy(params);
+ }
+ finally
+ {
+ MetaDataStack.unmask();
+ }
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractClassMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractClassMetaData.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractClassMetaData.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,58 @@
+/*
+* 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.beans.metadata.plugins;
+
+import java.io.Serializable;
+
+import org.jboss.beans.metadata.spi.ClassMetaData;
+import org.jboss.util.JBossObject;
+
+/**
+ * Class metadata.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class AbstractClassMetaData extends JBossObject implements ClassMetaData, Serializable
+{
+ private static final long serialVersionUID = 1L;
+
+ private String className;
+
+ public AbstractClassMetaData()
+ {
+ }
+
+ public AbstractClassMetaData(String className)
+ {
+ this.className = className;
+ }
+
+ public String getClassName()
+ {
+ return className;
+ }
+
+ public void setClassName(String className)
+ {
+ this.className = className;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractLazyMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractLazyMetaData.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/plugins/AbstractLazyMetaData.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,141 @@
+/*
+* 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.beans.metadata.plugins;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.beans.metadata.spi.LazyMetaData;
+import org.jboss.beans.metadata.spi.MetaDataVisitor;
+import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
+import org.jboss.beans.metadata.spi.ParameterMetaData;
+import org.jboss.dependency.plugins.AbstractDependencyItem;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelController;
+
+/**
+ * Lazy metadata.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class AbstractLazyMetaData extends AbstractBeanMetaData implements LazyMetaData
+{
+ private static final long serialVersionUID = 1L;
+
+ private String beanName;
+ private boolean exposeClass;
+ private Set<String> interfaces;
+
+ private String factoryClassName;
+
+ public AbstractLazyMetaData()
+ {
+ setAutowireCandidate(false);
+ setFactoryClassName("org.jboss.kernel.spi.lazy.LazyProxyFactory");
+ }
+
+ public AbstractLazyMetaData(String beanName)
+ {
+ this();
+ this.beanName = beanName;
+ setName(beanName + "Proxy");
+ }
+
+ public Object getUnderlyingValue()
+ {
+ return beanName;
+ }
+
+ public void initialVisit(MetaDataVisitor vistor)
+ {
+ if (beanName == null)
+ throw new IllegalArgumentException("Null bean name.");
+
+ KernelController controller = (KernelController)vistor.getControllerContext().getController();
+ Kernel kernel = controller.getKernel();
+ AbstractConstructorMetaData constructor = new AbstractConstructorMetaData();
+ constructor.setFactoryClass(factoryClassName);
+ constructor.setFactoryMethod("getProxy");
+ List<ParameterMetaData> parameters = new ArrayList<ParameterMetaData>();
+ parameters.add(new AbstractParameterMetaData(kernel));
+ parameters.add(new AbstractParameterMetaData(beanName));
+ parameters.add(new AbstractParameterMetaData(Boolean.TYPE.getName(), exposeClass));
+ parameters.add(new AbstractParameterMetaData(new AbstractValueMetaData(interfaces)));
+ constructor.setParameters(parameters);
+ setConstructor(constructor);
+
+ vistor.addDependency(new AbstractDependencyItem(name, beanName, ControllerState.INSTANTIATED, ControllerState.DESCRIBED));
+ super.initialVisit(vistor);
+ }
+
+ public Iterator<? extends MetaDataVisitorNode> getChildren()
+ {
+ return null;
+ }
+
+ public List<BeanMetaData> getBeans()
+ {
+ return Collections.singletonList((BeanMetaData)this);
+ }
+
+ protected void setFactoryClassName(String factoryClassName)
+ {
+ this.factoryClassName = factoryClassName;
+ }
+
+ public String getBeanName()
+ {
+ return beanName;
+ }
+
+ public void setBeanName(String beanName)
+ {
+ this.beanName = beanName;
+ if (name == null)
+ setName(beanName + "Proxy");
+ }
+
+ public boolean isExposeClass()
+ {
+ return exposeClass;
+ }
+
+ public void setExposeClass(boolean exposeClass)
+ {
+ this.exposeClass = exposeClass;
+ }
+
+ public Set<String> getInterfaces()
+ {
+ return interfaces;
+ }
+
+ public void setInterfaces(Set<String> interfaces)
+ {
+ this.interfaces = interfaces;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/ClassMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/ClassMetaData.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/ClassMetaData.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -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.beans.metadata.spi;
+
+import org.jboss.util.JBossInterface;
+
+/**
+ * Class/interface metadata.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface ClassMetaData extends JBossInterface
+{
+ String getClassName();
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/LazyMetaData.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/LazyMetaData.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/beans/metadata/spi/LazyMetaData.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,53 @@
+/*
+* 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.beans.metadata.spi;
+
+import java.util.Set;
+
+/**
+ * Metadata about a lazy bean.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface LazyMetaData extends BeanMetaData, BeanMetaDataFactory
+{
+ /**
+ * Get the bean.
+ *
+ * @return the bean
+ */
+ String getBeanName();
+
+ /**
+ * Is class exposed.
+ *
+ * @return true if expose class
+ */
+ boolean isExposeClass();
+
+ /**
+ * Get the intefaces.
+ *
+ * @return
+ */
+ Set<String> getInterfaces();
+}
Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/config/Configurator.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/config/Configurator.java 2007-10-08 21:05:13 UTC (rev 65944)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/config/Configurator.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -176,7 +176,7 @@
}
// Find the constructor
- ConstructorJoinpoint joinPoint = findConstructor(trace, info, metaData, beanMetaData);
+ ConstructorJoinpoint joinPoint = findConstructor(trace, info, metaData);
ConstructorInfo cinfo = joinPoint.getConstructorInfo();
// Set the parameters
@@ -190,7 +190,7 @@
}
// Default constructor
- return findConstructor(trace, info, metaData, beanMetaData);
+ return findConstructor(trace, info, metaData);
}
/**
@@ -198,12 +198,25 @@
*
* @param trace whether trace is enabled
* @param info the bean info
+ * @param metaData the bean metadata
+ * @return the constructor join point
+ * @throws Exception for any error
+ */
+ public static ConstructorJoinpoint findConstructor(boolean trace, BeanInfo info, BeanMetaData metaData) throws Exception
+ {
+ return findConstructor(trace, info, metaData.getConstructor());
+ }
+
+ /**
+ * Find a constructor
+ *
+ * @param trace whether trace is enabled
+ * @param info the bean info
* @param metaData the constructor metadata
- * @param beanMetaData
* @return the constructor join point
* @throws Exception for any error
*/
- public static ConstructorJoinpoint findConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData, BeanMetaData beanMetaData) throws Exception
+ public static ConstructorJoinpoint findConstructor(boolean trace, BeanInfo info, ConstructorMetaData metaData) throws Exception
{
ConstructorInfo cinfo = resolveConstructor(trace, info, metaData);
JoinpointFactory jpf = info.getJoinpointFactory();
Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBinding20.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBinding20.java 2007-10-08 21:05:13 UTC (rev 65944)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBinding20.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -56,6 +56,18 @@
/** The beanfactory element name */
public static final QName beanFactoryQName = new QName(BEAN_DEPLOYER_NS, "beanfactory");
+ /** The lazy binding */
+ public static final QName lazyTypeQName = new QName(BEAN_DEPLOYER_NS, "lazyType");
+
+ /** The lazy element name */
+ public static final QName lazyQName = new QName(BEAN_DEPLOYER_NS, "lazy");
+
+ /** The interface binding */
+ public static final QName interfaceTypeQName = new QName(BEAN_DEPLOYER_NS, "interfaceType");
+
+ /** The interface element name */
+ public static final QName interfaceQName = new QName(BEAN_DEPLOYER_NS, "interface");
+
/** The alias binding */
public static final QName aliasTypeQName = new QName(BEAN_DEPLOYER_NS, "aliasType");
@@ -234,6 +246,7 @@
initDeployment(schemaBinding);
initBean(schemaBinding);
initBeanFactory(schemaBinding);
+ initLazy(schemaBinding);
initArtifacts(schemaBinding);
}
@@ -274,6 +287,18 @@
}
/**
+ * Initialize lazy part of the schema binding
+ *
+ * @param schemaBinding the schema binding
+ */
+ public static void initLazy(SchemaBinding schemaBinding)
+ {
+ // lazy binding
+ TypeBinding beanFactoryType = schemaBinding.getType(lazyTypeQName);
+ BeanSchemaBindingHelper.initLazyHandlers(beanFactoryType);
+ }
+
+ /**
* Initialize other parts of the schema binding
*
* @param schemaBinding the schema binding
@@ -300,6 +325,10 @@
TypeBinding namedAliasType = schemaBinding.getType(namedAliasTypeQName);
BeanSchemaBindingHelper.initNamedAliasHandlers(namedAliasType);
+ // interface binding
+ TypeBinding interfaceType = schemaBinding.getType(interfaceTypeQName);
+ BeanSchemaBindingHelper.initInterfaceHandlers(interfaceType);
+
// alias binding
TypeBinding aliasType = schemaBinding.getType(aliasTypeQName);
BeanSchemaBindingHelper.initAliasHandlers(aliasType);
Modified: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBindingHelper.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBindingHelper.java 2007-10-08 21:05:13 UTC (rev 65944)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/BeanSchemaBindingHelper.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -71,6 +71,9 @@
// deployment has a list beanfactorys
deploymentType.pushInterceptor(BeanSchemaBinding20.beanFactoryQName, DeploymentBeanInterceptor.INTERCEPTOR);
+ // deployment has a list lazys
+ deploymentType.pushInterceptor(BeanSchemaBinding20.lazyQName, DeploymentBeanInterceptor.INTERCEPTOR);
+
// Deployment can take wildcards
deploymentType.getWildcard().setWildcardHandler(DeploymentWildcardHandler.HANDLER);
}
@@ -185,6 +188,19 @@
}
/**
+ * Initialize the handlers for the lazy type
+ *
+ * @param beanFactoryType the lazy type
+ */
+ public static void initLazyHandlers(TypeBinding beanFactoryType)
+ {
+ beanFactoryType.setHandler(LazyHandler.HANDLER);
+
+ // lazy has interfaces
+ beanFactoryType.pushInterceptor(BeanSchemaBinding20.interfaceQName, LazyInterfaceInterceptor.INTERCEPTOR);
+ }
+
+ /**
* Initialize the handlers for the classloader type
*
* @param classloaderType the classloader type
@@ -312,6 +328,19 @@
}
/**
+ * Initialize the handlers for the interface type
+ *
+ * @param interfaceType the interface type
+ */
+ public static void initInterfaceHandlers(TypeBinding interfaceType)
+ {
+ interfaceType.setHandler(ClassHandler.HANDLER);
+
+ // interface can take characters
+ interfaceType.setSimpleType(ClassCharactersHandler.HANDLER);
+ }
+
+ /**
* Initialize the handlers for the alias type
*
* @param aliasType the alias type
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassCharactersHandler.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassCharactersHandler.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassCharactersHandler.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,52 @@
+/*
+* 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.deployment.xml;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.jboss.beans.metadata.plugins.AbstractClassMetaData;
+import org.jboss.xb.binding.sunday.unmarshalling.CharactersHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+
+/**
+ * ClassCharactersHandler.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ClassCharactersHandler extends CharactersHandler
+{
+ /** The interceptor */
+ public static final ClassCharactersHandler HANDLER = new ClassCharactersHandler();
+
+ public Object unmarshal(QName qName, TypeBinding typeBinding, NamespaceContext nsCtx, org.jboss.xb.binding.metadata.ValueMetaData valueMetaData, String value)
+ {
+ return value;
+ }
+
+ public void setValue(QName qname, ElementBinding element, Object owner, Object value)
+ {
+ AbstractClassMetaData clazz = (AbstractClassMetaData) owner;
+ clazz.setClassName((String)value);
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassHandler.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassHandler.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/ClassHandler.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,54 @@
+/*
+* 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.deployment.xml;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.beans.metadata.spi.ClassMetaData;
+import org.jboss.beans.metadata.plugins.AbstractClassMetaData;
+
+/**
+ * ClassHandler.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class ClassHandler extends DefaultElementHandler
+{
+ /** The handler */
+ public static final ClassHandler HANDLER = new ClassHandler();
+
+ public Object startElement(Object parent, QName name, ElementBinding element)
+ {
+ return new AbstractClassMetaData();
+ }
+
+ public Object endElement(Object o, QName qName, ElementBinding element)
+ {
+ ClassMetaData classMetaData = (ClassMetaData) o;
+ String name = classMetaData.getClassName();
+ if (name == null || name.trim().length() == 0)
+ throw new IllegalArgumentException("Null or empty class/interface.");
+ return classMetaData;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyHandler.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyHandler.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyHandler.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,69 @@
+/*
+* 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.deployment.xml;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.jboss.beans.metadata.plugins.AbstractLazyMetaData;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.xml.sax.Attributes;
+
+/**
+ * LazyHandler.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LazyHandler extends DefaultElementHandler
+{
+ /** The handler */
+ public static final LazyHandler HANDLER = new LazyHandler();
+
+ public Object startElement(Object parent, QName name, ElementBinding element)
+ {
+ return new AbstractLazyMetaData();
+ }
+
+ public void attributes(Object o, QName elementName, ElementBinding element, Attributes attrs, NamespaceContext nsCtx)
+ {
+ AbstractLazyMetaData lazy = (AbstractLazyMetaData) o;
+ for (int i = 0; i < attrs.getLength(); ++i)
+ {
+ String localName = attrs.getLocalName(i);
+ if ("name".equals(localName))
+ lazy.setName(attrs.getValue(i));
+ else if ("bean".equals(localName))
+ lazy.setBeanName(attrs.getValue(i));
+ else if ("exposeClass".equals(localName))
+ lazy.setExposeClass(Boolean.parseBoolean(attrs.getValue(i)));
+ }
+ }
+
+ public Object endElement(Object o, QName qName, ElementBinding element)
+ {
+ AbstractLazyMetaData lazy = (AbstractLazyMetaData) o;
+ if (lazy.getBeanName() == null)
+ throw new IllegalArgumentException("Bean name must be set!");
+ return lazy;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyInterfaceInterceptor.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyInterfaceInterceptor.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/deployment/xml/LazyInterfaceInterceptor.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,54 @@
+/*
+* 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.deployment.xml;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.xml.namespace.QName;
+
+import org.jboss.beans.metadata.plugins.AbstractLazyMetaData;
+import org.jboss.beans.metadata.spi.ClassMetaData;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementInterceptor;
+
+/**
+ * LazyInterfaceInterceptor.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LazyInterfaceInterceptor extends DefaultElementInterceptor
+{
+ /** The interceptor */
+ public static final LazyInterfaceInterceptor INTERCEPTOR = new LazyInterfaceInterceptor();
+
+ public void add(Object parent, Object child, QName name)
+ {
+ AbstractLazyMetaData lazy = (AbstractLazyMetaData) parent;
+ ClassMetaData intface = (ClassMetaData) child;
+ Set<String> interfaces = lazy.getInterfaces();
+ if (interfaces == null)
+ {
+ interfaces = new HashSet<String>();
+ lazy.setInterfaces(interfaces);
+ }
+ interfaces.add(intface.getClassName());
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/AbstractLazyInitializer.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/AbstractLazyInitializer.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/AbstractLazyInitializer.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,223 @@
+/*
+* 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.lazy;
+
+import java.lang.reflect.Method;
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.dependency.spi.Controller;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.config.KernelConfigurator;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.lazy.LazyInitializer;
+import org.jboss.kernel.spi.registry.KernelBus;
+import org.jboss.util.JBossStringBuilder;
+
+/**
+ * Abstract lazy initializer.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class AbstractLazyInitializer implements LazyInitializer
+{
+ /**
+ * Abstract invoke handler.
+ */
+ protected abstract class AbstractInvokeHandler
+ {
+ private String bean;
+ private KernelBus bus;
+ private Class proxyClass;
+
+ protected AbstractInvokeHandler(String bean, KernelBus bus, Class proxyClass)
+ {
+ if (bean == null)
+ throw new IllegalArgumentException("Null bean name.");
+ this.bean = bean;
+ if (bus == null)
+ throw new IllegalArgumentException("Null Kernel bus.");
+ this.bus = bus;
+ this.proxyClass = proxyClass;
+ }
+
+ /**
+ * Excute invoke.
+ *
+ * @param proxy the proxy
+ * @param method the method
+ * @param args the arguments
+ * @return result
+ * @throws Throwable for any error
+ */
+ protected Object executeInvoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ String methodName = method.getName();
+
+ if ("hashCode".equals(methodName))
+ return System.identityHashCode(proxy);
+ if ("getClass".equalsIgnoreCase(methodName))
+ return proxyClass;
+ if ("equals".equals(methodName))
+ return proxy == args[0];
+ if ("toString".equals(methodName))
+ return bean + "Proxy";
+
+ if (isGetter(method))
+ {
+ return bus.get(bean, getLowerPropertyName(methodName));
+ }
+ else if (isSetter(method))
+ {
+ bus.set(bean, getLowerPropertyName(methodName), args[0]);
+ return null;
+ }
+ else
+ {
+ int length = args != null ? args.length : 0;
+ return bus.invoke(bean, methodName, args, new String[length]);
+ }
+ }
+ }
+
+ /**
+ * Is getter.
+ *
+ * @param minfo method
+ * @return true if metod is getter
+ */
+ protected static boolean isGetter(Method minfo)
+ {
+ String name = minfo.getName();
+ Class returnType = minfo.getReturnType();
+ Class[] parameters = minfo.getParameterTypes();
+ if ((name.length() > 3 && name.startsWith("get")) || (name.length() > 2 && name.startsWith("is")))
+ {
+ // isBoolean() is not a getter for java.lang.Boolean
+ if (name.startsWith("is") && Boolean.TYPE.equals(returnType) == false)
+ return false;
+ if (parameters.length == 0 && Void.TYPE.equals(returnType) == false)
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Is setter.
+ *
+ * @param minfo method
+ * @return true if method is setter
+ */
+ protected static boolean isSetter(Method minfo)
+ {
+ String name = minfo.getName();
+ Class returnType = minfo.getReturnType();
+ Class[] parameters = minfo.getParameterTypes();
+ if ((name.length() > 3 && name.startsWith("set")))
+ {
+ if (parameters.length == 1 && Void.TYPE.equals(returnType))
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Get lower property name.
+ *
+ * @param name the name
+ * @return lower case property name
+ */
+ protected static String getLowerPropertyName(String name)
+ {
+ int start = name.startsWith("is") ? 2 : 3;
+ name = name.substring(start);
+
+ // If the second character is upper case then we don't make
+ // the first character lower case
+ if (name.length() > 1)
+ {
+ if (Character.isUpperCase(name.charAt(1)))
+ return name;
+ }
+
+ JBossStringBuilder buffer = new JBossStringBuilder(name.length());
+ buffer.append(Character.toLowerCase(name.charAt(0)));
+ if (name.length() > 1)
+ buffer.append(name.substring(1));
+ return buffer.toString();
+ }
+
+ /**
+ * Get kernel controller context.
+ *
+ * @param kernel the kernel
+ * @param bean bean name
+ * @return kernel controller context
+ * @throws IllegalArgumentException if context not found or not KernelControllerContext
+ */
+ protected KernelControllerContext getKernelControllerContext(Kernel kernel, String bean)
+ {
+ Controller controller = kernel.getController();
+ ControllerContext context = controller.getContext(bean, ControllerState.DESCRIBED);
+ if (context == null)
+ throw new IllegalArgumentException("Should not be here, dependency failed.");
+ if (context instanceof KernelControllerContext == false)
+ throw new IllegalArgumentException("Context not KernelControllerContext: " + context);
+ return KernelControllerContext.class.cast(context);
+ }
+
+ /**
+ * Get bean info.
+ *
+ * @param kernel the kernel
+ * @param bean the bean name
+ * @return bean info instance
+ */
+ protected BeanInfo getBeanInfo(Kernel kernel, String bean)
+ {
+ KernelControllerContext context = getKernelControllerContext(kernel, bean);
+ return context.getBeanInfo();
+ }
+
+ /**
+ * Get classes from class names.
+ *
+ * @param configurator the configurator
+ * @param classNames the class names
+ * @param cl classloader
+ * @return array of classes
+ * @throws Throwable for any error
+ */
+ protected static Class[] getClasses(KernelConfigurator configurator, Set<String> classNames, ClassLoader cl)
+ throws Throwable
+ {
+ Class[] classes = new Class[classNames.size()];
+ int i = 0;
+ for (String className : classNames)
+ {
+ classes[i] = configurator.getClassInfo(className, cl).getType();
+ }
+ return classes;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JDKLazyInitializer.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JDKLazyInitializer.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JDKLazyInitializer.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,73 @@
+/*
+* 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.lazy;
+
+import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.config.Configurator;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.registry.KernelBus;
+
+/**
+ * Java JDK lazy initializer.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JDKLazyInitializer extends AbstractLazyInitializer
+{
+ public Object initializeProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ if (exposeClass)
+ throw new IllegalArgumentException("Cannot expose class via JDK LazyInitializer.");
+ if (interfaces == null || interfaces.isEmpty())
+ throw new IllegalArgumentException("Null interfaces.");
+
+ KernelControllerContext context = getKernelControllerContext(kernel, bean);
+ BeanInfo beanInfo = context.getBeanInfo();
+ if (beanInfo == null)
+ throw new IllegalArgumentException("Cannot proxy factory beans.");
+ LazyHandler lazyHandler = new LazyHandler(bean, kernel.getBus(), beanInfo.getClassInfo().getType());
+ ClassLoader cl = Configurator.getClassLoader(context.getBeanMetaData());
+ return Proxy.newProxyInstance(getClass().getClassLoader(), getClasses(kernel.getConfigurator(), interfaces, cl), lazyHandler);
+ }
+
+ /**
+ * Lazy invocation handler.
+ */
+ public class LazyHandler extends AbstractInvokeHandler implements InvocationHandler
+ {
+ public LazyHandler(String bean, KernelBus bus, Class proxyClass)
+ {
+ super(bean, bus, proxyClass);
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ return executeInvoke(proxy, method, args);
+ }
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JavassistLazyInitializer.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JavassistLazyInitializer.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/plugins/lazy/JavassistLazyInitializer.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,112 @@
+/*
+* 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.lazy;
+
+import java.lang.reflect.Method;
+import java.util.Set;
+import java.security.PrivilegedAction;
+import java.security.AccessController;
+
+import javassist.util.proxy.MethodFilter;
+import javassist.util.proxy.MethodHandler;
+import javassist.util.proxy.ProxyFactory;
+import javassist.util.proxy.ProxyObject;
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.config.Configurator;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.registry.KernelBus;
+
+/**
+ * Javassist lazy initializer.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JavassistLazyInitializer extends AbstractLazyInitializer
+{
+ public Object initializeProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ KernelControllerContext context = getKernelControllerContext(kernel, bean);
+ BeanInfo beanInfo = context.getBeanInfo();
+ if (beanInfo == null)
+ throw new IllegalArgumentException("Cannot proxy factory beans.");
+
+ ProxyFactory factory = new ProxyFactory();
+ factory.setFilter(FINALIZE_FILTER);
+ if (exposeClass)
+ {
+ factory.setSuperclass(beanInfo.getClassInfo().getType());
+ }
+ if (interfaces != null && interfaces.size() > 0)
+ {
+ ClassLoader cl = Configurator.getClassLoader(context.getBeanMetaData());
+ factory.setInterfaces(getClasses(kernel.getConfigurator(), interfaces, cl));
+ }
+ Class proxyClass = AccessController.doPrivileged(new ClassCreator(factory));
+ ProxyObject proxy = (ProxyObject)proxyClass.newInstance();
+ proxy.setHandler(new LazyHandler(bean, kernel.getBus(), beanInfo.getClassInfo().getType()));
+ return proxy;
+ }
+
+ private static final MethodFilter FINALIZE_FILTER = new MethodFilter()
+ {
+ public boolean isHandled(Method m)
+ {
+ // skip finalize methods
+ return !("finalize".equals(m.getName()) && m.getParameterTypes().length == 0);
+ }
+ };
+
+ /**
+ * Lazy method handler.
+ */
+ public class LazyHandler extends AbstractInvokeHandler implements MethodHandler
+ {
+ public LazyHandler(String bean, KernelBus bus, Class proxyClass)
+ {
+ super(bean, bus, proxyClass);
+ }
+
+ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+ {
+ return executeInvoke(self, thisMethod, args);
+ }
+ }
+
+ /**
+ * Privileged class creator.
+ */
+ protected class ClassCreator implements PrivilegedAction<Class>
+ {
+ private ProxyFactory factory;
+
+ public ClassCreator(ProxyFactory factory)
+ {
+ this.factory = factory;
+ }
+
+ public Class run()
+ {
+ return factory.createClass();
+ }
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyInitializer.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyInitializer.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyInitializer.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,46 @@
+/*
+* 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.spi.lazy;
+
+import java.util.Set;
+
+import org.jboss.kernel.Kernel;
+
+/**
+ * Lazy initializer.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface LazyInitializer
+{
+ /**
+ * Initialize lazy proxy.
+ *
+ * @param kernel the kernel
+ * @param bean the bean to wrap
+ * @param exposeClass do we expose full class
+ * @param interfaces interfaces to expose
+ * @return the proxy
+ * @throws Throwable for any error
+ */
+ Object initializeProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable;
+}
Added: projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyProxyFactory.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyProxyFactory.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/main/org/jboss/kernel/spi/lazy/LazyProxyFactory.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,130 @@
+/*
+* 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.spi.lazy;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.jboss.beans.info.spi.BeanInfo;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.config.KernelConfigurator;
+
+/**
+ * Create lazy proxy of a bean.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LazyProxyFactory
+{
+ private static LazyInitializer initializer;
+ private static Map<String, String> initializerMap;
+
+ static
+ {
+ initializerMap = new LinkedHashMap<String, String>();
+ // test class name, actual LazyInitializer implementation
+ initializerMap.put("javassist.util.proxy.ProxyObject", "org.jboss.kernel.plugins.lazy.JavassistLazyInitializer");
+ initializerMap.put("org.jboss.aop.proxy.container.AOPProxyFactoryParameters", "org.jboss.aop.microcontainer.lazy.JBossAOPLazyInitializer");
+ initializerMap.put("java.lang.reflect.Proxy", "org.jboss.kernel.plugins.lazy.JDKLazyInitializer");
+ }
+
+ /**
+ * Get the LazyInitializater instance.
+ *
+ * @param configurator the configurator
+ * @return initializer instance
+ */
+ protected static LazyInitializer getInitializer(KernelConfigurator configurator)
+ {
+ if (initializer == null)
+ {
+ for(Map.Entry<String, String> entry : initializerMap.entrySet())
+ {
+ if (testLibExists(entry.getKey()))
+ {
+ initializer = createInitializer(configurator, entry.getValue());
+ if (initializer != null)
+ break;
+ }
+ }
+ }
+ if (initializer == null)
+ throw new IllegalArgumentException("Cannot initialize LazyInitializater, check classpath for missing classes.");
+ return initializer;
+ }
+
+ /**
+ * Check if test class exists.
+ *
+ * @param className class name to test
+ * @return true if test successful, false otherwise
+ */
+ protected static boolean testLibExists(String className)
+ {
+ try
+ {
+ return Class.forName(className) != null;
+ }
+ catch (ClassNotFoundException e)
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Create initializer instance.
+ *
+ * @param configurator the configurator
+ * @param initializerClassName initializer class name
+ * @return initializer instance or null if we fail
+ */
+ protected static LazyInitializer createInitializer(KernelConfigurator configurator, String initializerClassName)
+ {
+ try
+ {
+ BeanInfo beanInfo = configurator.getBeanInfo(initializerClassName, LazyInitializer.class.getClassLoader());
+ Object result = beanInfo.newInstance();
+ return LazyInitializer.class.cast(result);
+ }
+ catch (Throwable ignored)
+ {
+ }
+ return null;
+ }
+
+ /**
+ * Create lazy proxy.
+ *
+ * @param kernel the kernel
+ * @param bean the bean to wrap
+ * @param exposeClass do we expose full class
+ * @param interfaces interfaces to expose
+ * @return the proxy
+ * @throws Throwable for any error
+ */
+ public static Object getProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ LazyInitializer lazyInitializer = getInitializer(kernel.getConfigurator());
+ return lazyInitializer.initializeProxy(kernel, bean, exposeClass, interfaces);
+ }
+}
Modified: projects/microcontainer/trunk/kernel/src/resources/main/schema/bean-deployer_2_0.xsd
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/main/schema/bean-deployer_2_0.xsd 2007-10-08 21:05:13 UTC (rev 65944)
+++ projects/microcontainer/trunk/kernel/src/resources/main/schema/bean-deployer_2_0.xsd 2007-10-08 23:02:27 UTC (rev 65945)
@@ -57,6 +57,7 @@
<xsd:element name="destroy" type="lifecycleType" minOccurs="0"/>
<xsd:element name="bean" type="beanType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="beanfactory" type="beanfactoryType" minOccurs="0" maxOccurs="unbounded"/>
+ <xsd:element name="lazy" type="lazyType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:any namespace="##other" processContents="strict" minOccurs="0">
<xsd:annotation>
<xsd:documentation>An extension value</xsd:documentation>
@@ -183,6 +184,29 @@
<xsd:attribute name="class" type="xsd:token" use="required"/>
</xsd:complexType>
+ <xsd:complexType name="lazyType">
+ <xsd:annotation>
+ <xsd:documentation>
+ <![CDATA[
+ Wrap the exisgin bean into lazy proxy.
+ Expose the full class or just interfaces.
+ Name is optional, and if no name is provided bean+'Proxy' will be used.
+
+ e.g.
+ <lazy name="UserProxy" bean="User">
+ <interface>org.acme.domain.IUser</interface>
+ </lazy>
+ ]]>
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:sequence>
+ <xsd:element name="interface" type="interfaceType" minOccurs="0" maxOccurs="unbounded"/>
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="optional"/>
+ <xsd:attribute name="bean" type="xsd:string" use="required"/>
+ <xsd:attribute name="exposeClass" type="xsd:boolean" default="false" use="optional"/>
+ </xsd:complexType>
+
<xsd:complexType name="constructorType">
<xsd:annotation>
<xsd:documentation>
@@ -221,6 +245,7 @@
<xsd:element name="parameter" type="parameterType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:element name="bean" type="beanType"/>
+ <xsd:element name="lazy" type="lazyType"/>
<xsd:element name="value" type="plainValueType"/>
<xsd:element name="collection" type="collectionType"/>
<xsd:element name="list" type="listType"/>
@@ -602,6 +627,19 @@
</xsd:simpleContent>
</xsd:complexType>
+ <xsd:complexType name="interfaceType">
+ <xsd:annotation>
+ <xsd:documentation>
+ <![CDATA[
+ The interface type represents an interface class.
+ ]]>
+ </xsd:documentation>
+ </xsd:annotation>
+ <xsd:simpleContent>
+ <xsd:extension base="xsd:string" />
+ </xsd:simpleContent>
+ </xsd:complexType>
+
<xsd:simpleType name="classNameType">
<xsd:annotation>
<xsd:documentation> The elements that use this type designate the name
@@ -643,6 +681,7 @@
</xsd:annotation>
<xsd:choice>
<!--<xsd:element name="bean" type="beanType"/>-->
+ <!--<xsd:element name="lazy" type="lazyType"/>-->
<xsd:element name="value" type="plainValueType"/>
<xsd:element name="inject" type="injectionType"/>
<xsd:element name="value-factory" type="valueFactoryType"/>
@@ -1067,4 +1106,14 @@
-->
<xsd:element name="beanfactory" type="beanfactoryType"/>
+ <!--
+ WARN:
+ Do not use this in the bean deployer, it won't work!
+ FIXME:
+ The correct way to do this is to have two separate
+ schemas (one for deployments the other for beans)
+ then use schema imports.
+ -->
+ <xsd:element name="lazy" type="lazyType"/>
+
</xsd:schema>
Added: projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.xml
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.xml (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.xml 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
+ xmlns="urn:jboss:bean-deployer:2.0">
+
+ <bean name="bean" class="org.jboss.test.kernel.lazy.support.RareBean"/>
+
+ <lazy name="proxy" bean="bean" exposeClass="true">
+ <interface>org.jboss.test.kernel.lazy.support.IRare</interface>
+ </lazy>
+
+</deployment>
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/IRare.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/IRare.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/IRare.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -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.test.kernel.lazy.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface IRare
+{
+ int getHits();
+
+ void setHits(int hits);
+
+ int checkHits(int expectedHits);
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JDKLazyFactory.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JDKLazyFactory.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JDKLazyFactory.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,40 @@
+/*
+* 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.kernel.lazy.support;
+
+import java.util.Set;
+
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.lazy.JDKLazyInitializer;
+import org.jboss.kernel.spi.lazy.LazyInitializer;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JDKLazyFactory
+{
+ public static Object getProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ LazyInitializer lazyInitializer = new JDKLazyInitializer();
+ return lazyInitializer.initializeProxy(kernel, bean, exposeClass, interfaces);
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JavassistLazyFactory.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JavassistLazyFactory.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/JavassistLazyFactory.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,40 @@
+/*
+* 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.kernel.lazy.support;
+
+import java.util.Set;
+
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.lazy.JavassistLazyInitializer;
+import org.jboss.kernel.spi.lazy.LazyInitializer;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JavassistLazyFactory
+{
+ public static Object getProxy(Kernel kernel, String bean, boolean exposeClass, Set<String> interfaces) throws Throwable
+ {
+ LazyInitializer lazyInitializer = new JavassistLazyInitializer();
+ return lazyInitializer.initializeProxy(kernel, bean, exposeClass, interfaces);
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/RareBean.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/RareBean.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/support/RareBean.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,45 @@
+/*
+* 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.kernel.lazy.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class RareBean implements IRare
+{
+ private int hits;
+
+ public int getHits()
+ {
+ return hits;
+ }
+
+ public void setHits(int hits)
+ {
+ this.hits = hits;
+ }
+
+ public int checkHits(int expectedHits)
+ {
+ return expectedHits - hits;
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JDKLazyInstantiationTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JDKLazyInstantiationTestCase.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JDKLazyInstantiationTestCase.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,48 @@
+/*
+* 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.kernel.lazy.test;
+
+import junit.framework.Test;
+import org.jboss.test.kernel.lazy.support.JDKLazyFactory;
+
+/**
+ * JDK lazy test.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JDKLazyInstantiationTestCase extends LazyInstantiationTestCase
+{
+ public JDKLazyInstantiationTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return suite(JDKLazyInstantiationTestCase.class);
+ }
+
+ protected String getFactoryClassName()
+ {
+ return JDKLazyFactory.class.getName();
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JavassistLazyInstantiationTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JavassistLazyInstantiationTestCase.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/JavassistLazyInstantiationTestCase.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,48 @@
+/*
+* 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.kernel.lazy.test;
+
+import junit.framework.Test;
+import org.jboss.test.kernel.lazy.support.JavassistLazyFactory;
+
+/**
+ * Javassist lazy test.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class JavassistLazyInstantiationTestCase extends LazyInstantiationTestCase
+{
+ public JavassistLazyInstantiationTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return suite(JavassistLazyInstantiationTestCase.class);
+ }
+
+ protected String getFactoryClassName()
+ {
+ return JavassistLazyFactory.class.getName();
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyInstantiationTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyInstantiationTestCase.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyInstantiationTestCase.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,114 @@
+/*
+* 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.kernel.lazy.test;
+
+import java.util.Collections;
+
+import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
+import org.jboss.beans.metadata.plugins.AbstractLazyMetaData;
+import org.jboss.dependency.spi.ControllerMode;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.test.AbstractTestDelegate;
+import org.jboss.test.kernel.AbstractKernelTest;
+import org.jboss.test.kernel.lazy.support.IRare;
+import org.jboss.test.kernel.lazy.support.RareBean;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class LazyInstantiationTestCase extends AbstractKernelTest
+{
+ public LazyInstantiationTestCase(String name)
+ {
+ super(name);
+ }
+
+ /**
+ * Default setup w/o security manager enabled
+ *
+ * @param clazz the class
+ * @return the delegate
+ * @throws Exception for any error
+ */
+ public static AbstractTestDelegate getDelegate(Class clazz) throws Exception
+ {
+ return new AbstractTestDelegate(clazz);
+ }
+
+ protected abstract String getFactoryClassName();
+
+ public void testLazy() throws Throwable
+ {
+ Kernel kernel = bootstrap();
+ KernelController controller = kernel.getController();
+
+ AbstractBeanMetaData bean = new AbstractBeanMetaData("bean", RareBean.class.getName());
+ bean.setMode(ControllerMode.MANUAL);
+
+ KernelControllerContext beanContext = controller.install(bean);
+ controller.change(beanContext, ControllerState.NOT_INSTALLED);
+
+ ModifiedLazyMetaData lazy = new ModifiedLazyMetaData("bean", getFactoryClassName());
+ lazy.setInterfaces(Collections.singleton(IRare.class.getName()));
+ KernelControllerContext lazyContext = controller.install(lazy);
+
+ assertNotNull(lazyContext);
+ assertEquals(ControllerState.DESCRIBED, lazyContext.getState());
+
+ controller.change(beanContext, ControllerState.DESCRIBED);
+ controller.change(lazyContext, ControllerState.INSTALLED);
+
+ IRare lazyRare = (IRare)lazyContext.getTarget();
+ assertNotNull(lazyRare);
+
+ try
+ {
+ lazyRare.getHits();
+ throw new RuntimeException("Should not be here.");
+ }
+ catch(Throwable t)
+ {
+ assertInstanceOf(t, IllegalArgumentException.class);
+ }
+
+ controller.change(beanContext, ControllerState.INSTALLED);
+
+ assertEquals(0, lazyRare.getHits());
+ lazyRare.setHits(10);
+ assertEquals(5, lazyRare.checkHits(15));
+
+ controller.change(beanContext, ControllerState.PRE_INSTALL);
+ assertEquals(ControllerState.DESCRIBED, lazyContext.getState());
+ }
+
+ private class ModifiedLazyMetaData extends AbstractLazyMetaData
+ {
+ public ModifiedLazyMetaData(String beanName, String factoryClassName)
+ {
+ super(beanName);
+ setFactoryClassName(factoryClassName);
+ }
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyMetaDataTestCase.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,63 @@
+/*
+* 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.kernel.lazy.test;
+
+import junit.framework.Test;
+import org.jboss.test.kernel.junit.MicrocontainerTest;
+import org.jboss.test.kernel.lazy.support.IRare;
+import org.jboss.test.kernel.lazy.support.RareBean;
+
+/**
+ * Test lazy metadata.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LazyMetaDataTestCase extends MicrocontainerTest
+{
+ public LazyMetaDataTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return suite(LazyMetaDataTestCase.class);
+ }
+
+ public void testLazyXMLTest() throws Throwable
+ {
+ Object proxy = getBean("proxy");
+ assertNotNull(proxy);
+ assertInstanceOf(proxy, IRare.class);
+ assertInstanceOf(proxy, RareBean.class);
+ RareBean rare = (RareBean)proxy;
+
+ RareBean bean = (RareBean)getBean("bean");
+ assertNotNull(bean);
+
+ assertEquals(bean.getHits(), rare.getHits());
+ bean.setHits(123);
+ assertEquals(bean.getHits(), rare.getHits());
+ bean.setHits(321);
+ assertEquals(bean.getHits(), rare.getHits());
+ }
+}
Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyTestSuite.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyTestSuite.java (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/lazy/test/LazyTestSuite.java 2007-10-08 23:02:27 UTC (rev 65945)
@@ -0,0 +1,50 @@
+/*
+* 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.kernel.lazy.test;
+
+import junit.framework.TestSuite;
+import junit.framework.Test;
+import junit.textui.TestRunner;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LazyTestSuite extends TestSuite
+{
+ public static void main(String[] args)
+ {
+ TestRunner.run(LazyTestSuite.suite());
+ }
+
+ public static Test suite()
+ {
+ TestSuite suite = new TestSuite("Lazy Proxy Tests");
+
+ // meta data
+ suite.addTest(LazyMetaDataTestCase.suite());
+ // api test
+ suite.addTest(JavassistLazyInstantiationTestCase.suite());
+ suite.addTest(JDKLazyInstantiationTestCase.suite());
+
+ return suite;
+ }
+}
More information about the jboss-cvs-commits
mailing list