[jboss-cvs] JBossAS SVN: r98098 - in projects/kernel/trunk/kernel/src/main/java/org/jboss: kernel/plugins/dependency and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Dec 21 15:52:41 EST 2009


Author: kabir.khan at jboss.com
Date: 2009-12-21 15:52:40 -0500 (Mon, 21 Dec 2009)
New Revision: 98098

Added:
   projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContentValueAdapter.java
   projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/
   projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/Matcher.java
   projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/QualifierMatchers.java
Modified:
   projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContent.java
   projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/ClassAndQualifierKey.java
Log:
[JBKERNEL-70] Initial work on pluggable qualifier matchers. Still needs a bit of work and some testing

Modified: projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContent.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContent.java	2009-12-21 20:50:43 UTC (rev 98097)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContent.java	2009-12-21 20:52:40 UTC (rev 98098)
@@ -21,19 +21,111 @@
 */ 
 package org.jboss.beans.metadata.api.model;
 
-import org.jboss.xb.annotations.JBossXmlEnum;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
 
+import javax.xml.bind.annotation.XmlType;
+
+import org.jboss.util.JBossObject;
+import org.jboss.util.JBossStringBuilder;
+import org.jboss.xb.annotations.JBossXmlAdaptedType;
+
 /**
  * How a qualifier should be parsed
  * 
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
  * @version $Revision: 1.1 $
  */
- at JBossXmlEnum(ignoreCase=true)
-public enum QualifierContent 
+ at XmlType(propOrder = {})
+ at JBossXmlAdaptedType(valueAdapter=QualifierContentValueAdapter.class)
+public class QualifierContent extends JBossObject implements Serializable
 {
    /** The qualifier is a simple string */
-   STRING,
+   public static QualifierContent STRING = new QualifierContent("STRING");
+   
    /** The qualifier is an annotation */
-   ANNOTATION
+   public static QualifierContent ANNOTATION = new QualifierContent("ANNOTATION");
+
+   private static ConcurrentMap<String, QualifierContent> values = new ConcurrentHashMap<String, QualifierContent>();
+
+   static
+   {
+      values.put(STRING.getContentString(), STRING);
+      values.put(ANNOTATION.getContentString(), ANNOTATION);
+   }
+
+   private String contentString;
+   
+   private QualifierContent(String contentString)
+   {
+      this.contentString = checkString(contentString);
+   }
+   
+   /**
+    * Get the content string
+    * 
+    * @return the content string
+    */
+   public String getContentString()
+   {
+      return contentString;
+   }
+   
+   /**
+    * Checks if the other object is also a QualifierContent and has the same 
+    * contentString
+    * @param object The object we want to compare with.
+    */
+   public boolean equals(Object object)
+   {
+      if (object == null || object instanceof QualifierContent == false)
+         return false;
+      QualifierContent other = (QualifierContent) object;
+      return contentString.equals(other.contentString);
+   }
+   
+   public void toString(JBossStringBuilder buffer)
+   {
+      buffer.append(contentString);
+   }
+
+   public void toShortString(JBossStringBuilder buffer)
+   {
+      buffer.append(contentString);
+   }
+
+   protected int getHashCode()
+   {
+      return contentString.hashCode();
+   }
+
+   protected Object readResolve() throws ObjectStreamException
+   {
+      return values.get(contentString);   
+   }
+   
+   private static String checkString(String contentString)
+   {
+      if (contentString == null)
+         throw new IllegalArgumentException("Null content string");
+      if (contentString.trim().length() == 0)
+         throw new IllegalArgumentException("Empty content string");
+      return contentString.trim().toUpperCase();
+   }
+   
+   public static QualifierContent getOrCreateContent(String contentString)
+   {
+      String str = checkString(contentString);
+      QualifierContent content = values.get(str);
+      if (content == null)
+      {
+         content = new QualifierContent(str);
+         QualifierContent old = values.putIfAbsent(str, content);
+         if (old != null)
+            content = old;
+      }
+      return content;
+   }
 }

Added: projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContentValueAdapter.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContentValueAdapter.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/beans/metadata/api/model/QualifierContentValueAdapter.java	2009-12-21 20:52:40 UTC (rev 98098)
@@ -0,0 +1,38 @@
+/*
+* 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.beans.metadata.api.model;
+
+import org.jboss.xb.binding.sunday.unmarshalling.ValueAdapter;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class QualifierContentValueAdapter implements ValueAdapter
+{
+   @SuppressWarnings("unchecked")
+   public Object cast(Object o, Class c)
+   {
+      return QualifierContent.getOrCreateContent((String)o);
+   }
+}
\ No newline at end of file

Modified: projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/ClassAndQualifierKey.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/ClassAndQualifierKey.java	2009-12-21 20:50:43 UTC (rev 98097)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/plugins/dependency/ClassAndQualifierKey.java	2009-12-21 20:52:40 UTC (rev 98098)
@@ -24,7 +24,9 @@
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -34,6 +36,8 @@
 import org.jboss.dependency.spi.ControllerState;
 import org.jboss.kernel.spi.dependency.KernelController;
 import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.kernel.spi.qualifier.Matcher;
+import org.jboss.kernel.spi.qualifier.QualifierMatchers;
 import org.jboss.util.JBossObject;
 import org.jboss.util.JBossStringBuilder;
 
@@ -187,30 +191,34 @@
          Set<Object> suppliedQualifiers = QualifiersMdrUtil.mergeSuppliedQualifiersFromMdr(context);
          
          int matches = 0;
-         boolean allTargetQualifiersMatched = true;
-         HashSet<Object> required = requiredQualifiers != null ? new HashSet<Object>(requiredQualifiers) : null;
-         if (suppliedQualifiers != null)
+
+         boolean allRequired = true;
+         if (requiredQualifiers != null && requiredQualifiers.size() > 0 && suppliedQualifiers != null && suppliedQualifiers.size() > 0)
          {
-            allTargetQualifiersMatched = true;
-            
-            for (Object supplied : suppliedQualifiers)
+            for (Object qualifier : requiredQualifiers)
             {
-               if (required != null && required.remove(supplied))
+               if (QualifierMatchers.getInstance().matches(context, suppliedQualifiers, qualifier))
                {
                   matches++;
                   continue;
                }
-               else if (optionalQualifiers != null && optionalQualifiers.contains(supplied))
+               allRequired = false;
+               break;
+            }
+         }
+         if (allRequired && optionalQualifiers != null && optionalQualifiers.size() > 0 && suppliedQualifiers != null && suppliedQualifiers.size() > 0)
+         {
+            for (Object qualifier : optionalQualifiers)
+            {
+               if (QualifierMatchers.getInstance().matches(context, suppliedQualifiers, qualifier))
                {
                   matches++;
                   continue;
                }
-               allTargetQualifiersMatched = false;
-               break;
             }
          }
-         
-         if (allTargetQualifiersMatched && (required == null || required.size() == 0))
+         int size = suppliedQualifiers != null ? suppliedQualifiers.size() : 0;
+         if (matches >= size && allRequired)
          {
             if (matches > max)
             {

Added: projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/Matcher.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/Matcher.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/Matcher.java	2009-12-21 20:52:40 UTC (rev 98098)
@@ -0,0 +1,45 @@
+/*
+* 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.kernel.spi.qualifier;
+
+import java.util.Set;
+
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * Abstraction to check if a qualifier exists in a context's set of supplied qualifiers
+ * or other 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public interface Matcher
+{
+   /**
+    * Check whether the passed in qualifier matches the context and/or the context's supplied qualifiers
+    * 
+    * @param context the context to check if matches the passed in qualifier
+    * @param suppliedQualifiers the standard supplied qualifiers for thecontext
+    * @param qualifier the qualifier to check against the context and/or its qualifiers
+    * @return true if there is a match
+    */
+   boolean matches(ControllerContext context, Set<Object> suppliedQualifiers, Object qualifier);
+}

Added: projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/QualifierMatchers.java
===================================================================
--- projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/QualifierMatchers.java	                        (rev 0)
+++ projects/kernel/trunk/kernel/src/main/java/org/jboss/kernel/spi/qualifier/QualifierMatchers.java	2009-12-21 20:52:40 UTC (rev 98098)
@@ -0,0 +1,97 @@
+/*
+* 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.kernel.spi.qualifier;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * Singleton registry of qualifier matchers and entry point to the qualifier matcher subsystem
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class QualifierMatchers
+{
+   private final static QualifierMatchers SINGLETON = new QualifierMatchers();
+   
+   private Map<Class<?>, Matcher> matchers = new ConcurrentHashMap<Class<?>, Matcher>();
+   
+   private QualifierMatchers()
+   {
+      
+   }
+   
+   /**
+    * Get the singleton instance
+    * 
+    * @return the singleton instance
+    */
+   public static QualifierMatchers getInstance()
+   {
+      return SINGLETON;
+   }
+   
+   /**
+    * Check whether the passed in qualifier matches the context and/or the context's supplied qualifiers
+    * 
+    * @param context the context to check if matches the passed in qualifier
+    * @param suppliedQualifiers the standard supplied qualifiers for thecontext
+    * @param qualifier the qualifier to check against the context and/or its qualifiers
+    * @return true if there is a match
+    */
+   public boolean matches(ControllerContext context, Set<Object> suppliedQualifiers, Object qualifier)
+   {
+      Matcher matcher = getMatcher(qualifier.getClass());
+      return matcher.matches(context, suppliedQualifiers, qualifier);
+   }
+   
+   private Matcher getMatcher(Class<?> clazz)
+   {
+      Matcher matcher =  matchers.get(clazz);
+      if (matcher != null)
+         return matcher;
+      return DefaultEqualsMatcherImpl.SINGLETON; 
+   }
+
+   /**
+    * The default Matcher implementation.
+    * It checks if the qualifier exists in the context's standard supplied qualifiers 
+    * 
+    */
+   private static class DefaultEqualsMatcherImpl implements Matcher
+   {
+      private static final Matcher SINGLETON = new DefaultEqualsMatcherImpl();
+      
+      private DefaultEqualsMatcherImpl()
+      {
+      }
+      
+      public boolean matches(ControllerContext context, Set<Object> suppliedQualifiers, Object qualifier)
+      {
+         return suppliedQualifiers.contains(qualifier);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list