Author: kabir.khan(a)jboss.com
Date: 2009-09-04 10:28:43 -0400 (Fri, 04 Sep 2009)
New Revision: 3645
Added:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/DeploymentItem.java
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/EjbDeploymentItem.java
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/Sorter.java
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/WBClassDeploymentItem.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/BlueDecorator.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Doctor.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Employee.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/PlasticSurgeon.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/RedDecorator.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/SorterTest.java
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Surgeon.java
Modified:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/bootstrap/AbstractBeanDeployer.java
Log:
Start implement sort
Modified:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/bootstrap/AbstractBeanDeployer.java
===================================================================
---
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/bootstrap/AbstractBeanDeployer.java 2009-09-03
15:47:15 UTC (rev 3644)
+++
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/bootstrap/AbstractBeanDeployer.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -46,7 +46,6 @@
private final BeanManagerImpl manager;
private final BeanDeployerEnvironment environment;
- private final List<Throwable> definitionErrors = new
ArrayList<Throwable>();
private final InternalBeanBuilderFactory beanBuilderFactory;
Added:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/DeploymentItem.java
===================================================================
---
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/DeploymentItem.java
(rev 0)
+++
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/DeploymentItem.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,141 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.builder.sorter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import javax.enterprise.inject.Specializes;
+
+import org.jboss.webbeans.introspector.WBClass;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class DeploymentItem<T>
+{
+ private final Set<DeploymentItem<?>> allItems;
+
+ private boolean initialized;
+
+ private List<DeploymentItem<?>> dependencies = Collections.emptyList();
+
+ private WBClass<?> clazz;
+
+ private volatile int hashCode;
+
+ // Cached string representation
+ private String toString;
+
+
+ DeploymentItem(Set<DeploymentItem<?>> allItems, WBClass<?> clazz)
+ {
+ this.allItems = allItems;
+ this.clazz = clazz;
+ }
+
+ void initialize()
+ {
+ if (!initialized)
+ {
+ if (isSpecializing())
+ {
+ addDependency(getSuperClassDependency());
+ }
+ initialized = true;
+ }
+ }
+
+ void addDependency(DeploymentItem<?> dependency)
+ {
+ if (dependencies == Collections.EMPTY_LIST)
+ dependencies = new ArrayList<DeploymentItem<?>>();
+
+ dependencies.add(dependency);
+ }
+
+ public abstract T getUnderlyingItem();
+
+ DeploymentItem<T> findDeploymentItem(DeploymentItem<T> item)
+ {
+ for (DeploymentItem<?> candidate : allItems)
+ {
+ if (candidate.equals(item))
+ return (DeploymentItem<T>)candidate;
+ }
+ throw new IllegalStateException("Could not find item");
+ }
+
+ abstract DeploymentItem<T> getSuperClassDependency();
+
+ public WBClass<?> getWBClass()
+ {
+ return clazz;
+ }
+
+ List<DeploymentItem<?>> getDependencies()
+ {
+ initialize();
+ return dependencies;
+ }
+
+ boolean isSpecializing()
+ {
+ return clazz.isAnnotationPresent(Specializes.class);
+ }
+
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj == null)
+ return false;
+ if (obj.getClass() != this.getClass())
+ return false;
+ if (this.getClass().isAssignableFrom(obj.getClass()) == false)
+ return false;
+ return
this.getWBClass().getName().equals(((DeploymentItem<T>)obj).getWBClass().getName());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ if (hashCode == 0)
+ hashCode = getWBClass().getName().hashCode();
+ return hashCode;
+ }
+
+ @Override
+ public String toString()
+ {
+ if (toString != null)
+ {
+ return toString;
+ }
+ toString = "WBClassDeploymentItem{" + clazz.toString() + "}";
+ return toString;
+ }
+}
Added:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/EjbDeploymentItem.java
===================================================================
---
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/EjbDeploymentItem.java
(rev 0)
+++
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/EjbDeploymentItem.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,68 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.builder.sorter;
+
+import java.util.Set;
+
+import org.jboss.webbeans.ejb.EjbDescriptors;
+import org.jboss.webbeans.ejb.InternalEjbDescriptor;
+import org.jboss.webbeans.introspector.WBClass;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+class EjbDeploymentItem extends DeploymentItem<InternalEjbDescriptor<?>>
+{
+ private InternalEjbDescriptor<?> ejbDescriptor;
+ private EjbDescriptors allDescriptors;
+
+ EjbDeploymentItem(Set<DeploymentItem<?>> allItems, EjbDescriptors
allDescriptors, WBClass<?> clazz, InternalEjbDescriptor<?> ejbDescriptor)
+ {
+ super(allItems, clazz);
+ this.ejbDescriptor = ejbDescriptor;
+ this.allDescriptors = allDescriptors;
+ }
+
+ public InternalEjbDescriptor<?> getUnderlyingItem()
+ {
+ return ejbDescriptor;
+ }
+
+ DeploymentItem<InternalEjbDescriptor<?>> getSuperClassDependency()
+ {
+ //This needs revisiting
+ Class<?> superClass = getWBClass().getWBSuperclass().getJavaClass();
+ for (InternalEjbDescriptor ejb : allDescriptors)
+ {
+ if (ejb.getBeanClass().equals(superClass))
+ {
+ DeploymentItem<InternalEjbDescriptor<?>> item = new
EjbDeploymentItem(null, null, getWBClass().getWBSuperclass(), ejb);
+ return findDeploymentItem(item);
+
+ }
+ }
+ //TODO search wbclasses too?
+ throw new IllegalStateException("Could not find super class for " +
ejbDescriptor);
+ }
+}
Added:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/Sorter.java
===================================================================
---
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/Sorter.java
(rev 0)
+++
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/Sorter.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,181 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.builder.sorter;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.decorator.Decorator;
+import javax.inject.Inject;
+
+import org.jboss.webbeans.BeanManagerImpl;
+import org.jboss.webbeans.ejb.EJBApiAbstraction;
+import org.jboss.webbeans.ejb.EjbDescriptors;
+import org.jboss.webbeans.ejb.InternalEjbDescriptor;
+import org.jboss.webbeans.introspector.WBClass;
+import org.jboss.webbeans.jsf.JsfApiAbstraction;
+import org.jboss.webbeans.resources.ClassTransformer;
+import org.jboss.webbeans.servlet.ServletApiAbstraction;
+import org.jboss.webbeans.util.Reflections;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Sorter
+{
+ private final Set<WBClass<?>> classes;
+
+ private final EjbDescriptors ejbDescriptors;
+
+ private final ClassTransformer classTransformer;
+
+ private final EJBApiAbstraction ejbApiAbstraction;
+
+ private final JsfApiAbstraction jsfApiAbstraction;
+
+ private final ServletApiAbstraction servletApiAbstraction;
+
+ private final List<DeploymentItem<?>> decoratorItems = new
ArrayList<DeploymentItem<?>>();
+
+ private final LinkedHashSet<DeploymentItem<?>> unsortedItems = new
LinkedHashSet<DeploymentItem<?>>();
+
+ private final List<DeploymentItem<?>> sortedItems = new
ArrayList<DeploymentItem<?>>();
+
+ private final List<DeploymentItem<?>> visitedItems = new
ArrayList<DeploymentItem<?>>();
+
+ private Sorter(Set<WBClass<?>> classes, EjbDescriptors ejbDescriptors,
ClassTransformer classTransformer,
+ EJBApiAbstraction ejbApiAbstraction, JsfApiAbstraction jsfApiAbstraction,
ServletApiAbstraction servletApiAbstraction)
+ {
+ this.classes = classes;
+ this.classTransformer = classTransformer;
+ this.ejbDescriptors = ejbDescriptors;
+ this.ejbApiAbstraction = ejbApiAbstraction;
+ this.jsfApiAbstraction = jsfApiAbstraction;
+ this.servletApiAbstraction = servletApiAbstraction;
+ }
+
+ public static Sorter createSorter(BeanManagerImpl beanManager,
Set<WBClass<?>> classes, EjbDescriptors ejbDescriptors)
+ {
+ ClassTransformer classTransformer =
beanManager.getServices().get(ClassTransformer.class);
+ EJBApiAbstraction ejbApiAbstraction =
beanManager.getServices().get(EJBApiAbstraction.class);
+ JsfApiAbstraction jsfApiAbstraction =
beanManager.getServices().get(JsfApiAbstraction.class);
+ ServletApiAbstraction servletApiAbstraction =
beanManager.getServices().get(ServletApiAbstraction.class);
+
+ Sorter sorter = new Sorter(classes, ejbDescriptors, classTransformer,
ejbApiAbstraction, jsfApiAbstraction, servletApiAbstraction);
+ sorter.createDeploymentItems();
+
+ return sorter;
+ }
+
+ private void createDeploymentItems()
+ {
+ for (WBClass<?> clazz : classes)
+ {
+ WBClassDeploymentItem item = new
WBClassDeploymentItem(Collections.unmodifiableSet(unsortedItems), clazz);
+ boolean managedBeanOrDecorator = !ejbDescriptors.contains(clazz.getJavaClass())
&& isTypeManagedBeanOrDecorator(clazz);
+ if (managedBeanOrDecorator &&
clazz.isAnnotationPresent(Decorator.class))
+ {
+ decoratorItems.add(item);
+ }
+ else
+ {
+ unsortedItems.add(item);
+ }
+ }
+ for (InternalEjbDescriptor<?> ejb : ejbDescriptors)
+ {
+ WBClass<?> clazz = classTransformer.loadClass(ejb.getBeanClass());
+ unsortedItems.add(new
EjbDeploymentItem(Collections.unmodifiableSet(unsortedItems), ejbDescriptors, clazz,
ejb));
+ }
+ }
+
+ public List<DeploymentItem<?>> sort()
+ {
+ //No need to sort the disposal method beans anymore since the disposal method must
be in the same bean
+
+ for (DeploymentItem<?> item : unsortedItems)
+ {
+ Set<DeploymentItem<?>> cycles = new
HashSet<DeploymentItem<?>>();
+ visit(item, cycles);
+ }
+
+ //Decorators should come first
+ List<DeploymentItem<?>> result = new
ArrayList<DeploymentItem<?>>();
+ result.addAll(decoratorItems);
+ result.addAll(sortedItems);
+ return result;
+ }
+
+ private void visit(DeploymentItem<?> item, Set<DeploymentItem<?>>
cycles)
+ {
+ if (cycles.contains(item))
+ throw new RuntimeException("Cycle for " + item);
+ cycles.add(item);
+
+ if (!visitedItems.contains(item))
+ {
+ visitedItems.add(item);
+
+ for (DeploymentItem<?> dependencyItem : item.getDependencies())
+ {
+ visit(dependencyItem, cycles);
+ }
+ sortedItems.add(item);
+ }
+ }
+
+ /**
+ * Indicates if the type is a simple Web Bean
+ *
+ * @param type
+ * The type to inspect
+ * @return True if simple Web Bean, false otherwise
+ */
+ protected boolean isTypeManagedBeanOrDecorator(WBClass<?> clazz)
+ {
+ //FIXME duplicates AbstractBeanDeployer.isTypeManagedBeanOrDecorator() - extract
utility method
+ Class<?> javaClass = clazz.getJavaClass();
+
+ return !clazz.isNonStaticMemberClass() &&
+ !Reflections.isParamerterizedTypeWithWildcard(javaClass) &&
+ !servletApiAbstraction.SERVLET_CLASS.isAssignableFrom(javaClass) &&
+ !servletApiAbstraction.FILTER_CLASS.isAssignableFrom(javaClass) &&
+
!servletApiAbstraction.SERVLET_CONTEXT_LISTENER_CLASS.isAssignableFrom(javaClass)
&&
+
!servletApiAbstraction.HTTP_SESSION_LISTENER_CLASS.isAssignableFrom(javaClass) &&
+
!servletApiAbstraction.SERVLET_REQUEST_LISTENER_CLASS.isAssignableFrom(javaClass)
&&
+ !ejbApiAbstraction.ENTERPRISE_BEAN_CLASS.isAssignableFrom(javaClass)
&&
+ !jsfApiAbstraction.UICOMPONENT_CLASS.isAssignableFrom(javaClass) &&
+ hasSimpleWebBeanConstructor(clazz);
+ }
+
+ private static boolean hasSimpleWebBeanConstructor(WBClass<?> type)
+ {
+ //FIXME duplicates AbstractBeanDeployer.hasSimpleWebBeanConstructor() - extract
utility method
+ return type.getNoArgsWBConstructor() != null ||
type.getAnnotatedWBConstructors(Inject.class).size() > 0;
+ }
+}
Added:
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/WBClassDeploymentItem.java
===================================================================
---
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/WBClassDeploymentItem.java
(rev 0)
+++
ri/branches/kabir-builder/impl/src/main/java/org/jboss/webbeans/builder/sorter/WBClassDeploymentItem.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,51 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.builder.sorter;
+
+import java.util.Set;
+
+import org.jboss.webbeans.introspector.WBClass;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+class WBClassDeploymentItem extends DeploymentItem<WBClass<?>>
+{
+ WBClassDeploymentItem(Set<DeploymentItem<?>> allItems, WBClass<?>
clazz)
+ {
+ super(allItems, clazz);
+ }
+
+ public WBClass<?> getUnderlyingItem()
+ {
+ return getWBClass();
+ }
+
+ DeploymentItem<WBClass<?>> getSuperClassDependency()
+ {
+ DeploymentItem<WBClass<?>> item = new WBClassDeploymentItem(null,
getWBClass().getWBSuperclass());
+ return findDeploymentItem(item);
+ }
+
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/BlueDecorator.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/BlueDecorator.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/BlueDecorator.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,42 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+import javax.decorator.Decorates;
+import javax.decorator.Decorator;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+@Decorator
+public class BlueDecorator
+{
+ @Decorates
+ Employee employee;
+
+ public String getName()
+ {
+ return employee.getName();
+ }
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Doctor.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Doctor.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Doctor.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,32 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Doctor extends Employee
+{
+
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Employee.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Employee.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Employee.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,37 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Employee
+{
+ String name;
+
+ public String getName()
+ {
+ return name;
+ }
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/PlasticSurgeon.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/PlasticSurgeon.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/PlasticSurgeon.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,35 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+import javax.enterprise.inject.Specializes;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+@Specializes
+public class PlasticSurgeon extends Surgeon
+{
+
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/RedDecorator.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/RedDecorator.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/RedDecorator.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,42 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+import javax.decorator.Decorates;
+import javax.decorator.Decorator;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+@Decorator
+public class RedDecorator
+{
+ @Decorates
+ Employee employee;
+
+ public String getName()
+ {
+ return employee.getName();
+ }
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/SorterTest.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/SorterTest.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/SorterTest.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,156 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.jboss.webbeans.BeanManagerImpl;
+import org.jboss.webbeans.builder.sorter.DeploymentItem;
+import org.jboss.webbeans.builder.sorter.Sorter;
+import org.jboss.webbeans.ejb.EjbDescriptors;
+import org.jboss.webbeans.introspector.WBClass;
+import org.jboss.webbeans.introspector.jlr.WBClassImpl;
+import org.jboss.webbeans.mock.MockEELifecycle;
+import org.jboss.webbeans.resources.ClassTransformer;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class SorterTest
+{
+ MockEELifecycle lifecycle;
+ BeanManagerImpl manager;
+
+ final int ITERATIONS = 100;
+
+ @BeforeClass
+ public void beforeMethod() throws Exception
+ {
+ lifecycle = new MockEELifecycle();
+ lifecycle.getDeployment().getArchive().setBeanClasses(new
ArrayList<Class<?>>());
+ lifecycle.initialize();
+ lifecycle.beginApplication();
+ lifecycle.beginSession();
+ lifecycle.beginRequest();
+ manager =
lifecycle.getBootstrap().getManager(lifecycle.getDeployment().getArchive());
+ }
+
+ @AfterClass
+ public void afterClass()
+ {
+ lifecycle.endRequest();
+ lifecycle.endSession();
+ lifecycle.endApplication();
+ }
+
+ @Test
+ public void testSimpleSortNoDependencies()
+ {
+ assertArrays(sortClasses(Employee.class, Doctor.class), Employee.class,
Doctor.class);
+ assertArrays(sortClasses(Doctor.class, Employee.class), Doctor.class,
Employee.class);
+ }
+
+ @Test
+ public void testSimpleSortSpecializedDependencies()
+ {
+ assertArrays(sortClasses(Doctor.class, Surgeon.class), Doctor.class,
Surgeon.class);
+ assertArrays(sortClasses(Surgeon.class, Doctor.class), Doctor.class,
Surgeon.class);
+
+ assertArrays(sortClasses(Doctor.class, Surgeon.class, PlasticSurgeon.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+ assertArrays(sortClasses(Doctor.class, PlasticSurgeon.class, Surgeon.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+ assertArrays(sortClasses(Surgeon.class, PlasticSurgeon.class, Doctor.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+ assertArrays(sortClasses(Surgeon.class, Doctor.class, PlasticSurgeon.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+ assertArrays(sortClasses(PlasticSurgeon.class, Surgeon.class, Doctor.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+ assertArrays(sortClasses(PlasticSurgeon.class, Doctor.class, Surgeon.class),
Doctor.class, Surgeon.class, PlasticSurgeon.class);
+
+ //TODO Not sure we can guarantee the order of Employee in these cases?
+ assertArrays(sortClasses(Employee.class, Doctor.class, Surgeon.class,
PlasticSurgeon.class), Employee.class, Doctor.class, Surgeon.class,
PlasticSurgeon.class);
+ assertArrays(sortClasses(PlasticSurgeon.class, Surgeon.class, Doctor.class,
Employee.class), Doctor.class, Surgeon.class, PlasticSurgeon.class, Employee.class);
+ assertArrays(sortClasses(Doctor.class, Surgeon.class, Employee.class,
PlasticSurgeon.class), Doctor.class, Surgeon.class, Employee.class,
PlasticSurgeon.class);
+ assertArrays(sortClasses(PlasticSurgeon.class, Employee.class, Surgeon.class,
Doctor.class), Doctor.class, Surgeon.class, PlasticSurgeon.class, Employee.class);
+ }
+
+ @Test
+ public void testDecoratorsFirst()
+ {
+ assertArrays(sortClasses(RedDecorator.class, BlueDecorator.class, Doctor.class,
Surgeon.class), RedDecorator.class, BlueDecorator.class, Doctor.class, Surgeon.class);
+ assertArrays(sortClasses(RedDecorator.class, Doctor.class, Surgeon.class,
BlueDecorator.class), RedDecorator.class, BlueDecorator.class, Doctor.class,
Surgeon.class);
+ assertArrays(sortClasses(Surgeon.class, Doctor.class, RedDecorator.class,
BlueDecorator.class), RedDecorator.class, BlueDecorator.class, Doctor.class,
Surgeon.class);
+
+ }
+
+ @Test
+ public void testEjbs()
+ {
+ assert 1 == 0 : "NYI";
+ }
+
+ private Class<?>[] sortClasses(Class<?>...classes)
+ {
+ if (classes == null || classes.length == 0)
+ return new Class<?>[0];
+
+ ClassTransformer transformer = manager.getServices().get(ClassTransformer.class);
+ Set<WBClass<?>> wbClasses = new
LinkedHashSet<WBClass<?>>();
+
+ for (Class<?> clazz : classes)
+ {
+ wbClasses.add(WBClassImpl.of(clazz, transformer));
+ }
+
+ Sorter sorter = Sorter.createSorter(manager, wbClasses, new EjbDescriptors());
+ List<DeploymentItem<?>> sorted = sorter.sort();
+
+ System.out.println("======>" + sorted);
+
+ assert sorted.size() == classes.length;
+
+ Class<?>[] result = new Class<?>[classes.length];
+ int i = 0;
+
+ for (DeploymentItem<?> item : sorted)
+ {
+ result[i++] = item.getWBClass().getJavaClass();
+ }
+
+ return result;
+ }
+
+ private void assertArrays(Class<?>[] actual, Class<?>...expected)
+ {
+ assert Arrays.equals(expected, actual) : printArrays(expected, actual);
+ }
+
+ private String printArrays(Object[] expected, Object[] actual)
+ {
+ return "Expected: " + Arrays.toString(expected) + ", was: " +
Arrays.toString(actual);
+ }
+}
Added:
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Surgeon.java
===================================================================
---
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Surgeon.java
(rev 0)
+++
ri/branches/kabir-builder/tests/src/test/java/org/jboss/webbeans/test/unit/builder/sorter/Surgeon.java 2009-09-04
14:28:43 UTC (rev 3645)
@@ -0,0 +1,35 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.webbeans.test.unit.builder.sorter;
+
+import javax.enterprise.inject.Specializes;
+
+/**
+ *
+ * @author <a href="kabir.khan(a)jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+@Specializes
+public class Surgeon extends Doctor
+{
+
+}