You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2010/08/30 19:57:26 UTC

svn commit: r990883 - in /myfaces/extensions/cdi/trunk: core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/ core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/ jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces...

Author: gpetracek
Date: Mon Aug 30 17:57:26 2010
New Revision: 990883

URL: http://svn.apache.org/viewvc?rev=990883&view=rev
Log:
EXTCDI-47

Added:
    myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/InvocationOrder.java
    myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/InvocationOrderComparator.java
Modified:
    myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java

Added: myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/InvocationOrder.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/InvocationOrder.java?rev=990883&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/InvocationOrder.java (added)
+++ myfaces/extensions/cdi/trunk/core/api/src/main/java/org/apache/myfaces/extensions/cdi/core/api/InvocationOrder.java Mon Aug 30 17:57:26 2010
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.extensions.cdi.core.api;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.TYPE;
+
+/**
+ * @author Gerhard Petracek
+ */
+@Target(TYPE)
+@Retention(RUNTIME)
+@Documented
+public @interface InvocationOrder
+{
+    int value() default 1000;
+}

Added: myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/InvocationOrderComparator.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/InvocationOrderComparator.java?rev=990883&view=auto
==============================================================================
--- myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/InvocationOrderComparator.java (added)
+++ myfaces/extensions/cdi/trunk/core/impl/src/main/java/org/apache/myfaces/extensions/cdi/core/impl/InvocationOrderComparator.java Mon Aug 30 17:57:26 2010
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.extensions.cdi.core.impl;
+
+import org.apache.myfaces.extensions.cdi.core.api.InvocationOrder;
+
+import java.util.Comparator;
+
+/**
+ * @author Gerhard Petracek
+ */
+public class InvocationOrderComparator<T> implements Comparator<T>
+{
+    public int compare(T t1, T t2)
+    {
+        if (hasPriority(t1) && hasPriority(t2))
+        {
+            return isPriorityHigher(t1.getClass().getAnnotation(InvocationOrder.class),
+                    t2.getClass().getAnnotation(InvocationOrder.class));
+        }
+        if (!hasPriority(t1) && !hasPriority(t2))
+        {
+            return 0;
+        }
+        return hasPriority(t1) ? -1 : 1;
+    }
+
+    private int isPriorityHigher(InvocationOrder priority1, InvocationOrder priority2)
+    {
+        if (priority1.value() == priority2.value())
+        {
+            return 0;
+        }
+
+        return priority1.value() < priority2.value() ? -1 : 1;
+    }
+
+    private boolean hasPriority(Object o)
+    {
+        return o.getClass().isAnnotationPresent(InvocationOrder.class);
+    }
+}

Modified: myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java?rev=990883&r1=990882&r2=990883&view=diff
==============================================================================
--- myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java (original)
+++ myfaces/extensions/cdi/trunk/jee-modules/jsf-module/impl/src/main/java/org/apache/myfaces/extensions/cdi/javaee/jsf/impl/listener/phase/PhaseListenerExtension.java Mon Aug 30 17:57:26 2010
@@ -21,6 +21,7 @@ package org.apache.myfaces.extensions.cd
 import org.apache.myfaces.extensions.cdi.javaee.jsf.api.listener.phase.JsfPhaseListener;
 import org.apache.myfaces.extensions.cdi.javaee.jsf.impl.util.JsfUtils;
 import org.apache.myfaces.extensions.cdi.core.api.util.ClassUtils;
+import org.apache.myfaces.extensions.cdi.core.impl.InvocationOrderComparator;
 
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.Extension;
@@ -94,14 +95,16 @@ public class PhaseListenerExtension impl
     //current workaround some servers
     public static List<PhaseListener> consumePhaseListeners()
     {
-        ClassLoader cl = ClassUtils.getClassLoader(null);
-        List<PhaseListener> plList = phaseListeners.get(cl);
+        ClassLoader classLoader = ClassUtils.getClassLoader(null);
+        List<PhaseListener> foundPhaseListeners = phaseListeners.get(classLoader);
 
-        if(plList != null && ! plList.isEmpty())
+        if(foundPhaseListeners != null && ! foundPhaseListeners.isEmpty())
         {
-            List<PhaseListener> result = new ArrayList<PhaseListener>(plList.size());
-            result.addAll(plList);
-            plList.clear();
+            List<PhaseListener> result = new ArrayList<PhaseListener>(foundPhaseListeners.size());
+            result.addAll(foundPhaseListeners);
+            foundPhaseListeners.clear();
+
+            Collections.sort(result, new InvocationOrderComparator<PhaseListener>());
             return result;
         }
         return Collections.emptyList();