deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject deltaspike git commit: DELTASPIKE-1054 auto-detection and validation of the used job-type
Date Sat, 02 Jan 2016 00:35:20 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 3d8a30629 -> d2f5d7440


DELTASPIKE-1054 auto-detection and validation of the used job-type


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/d2f5d744
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/d2f5d744
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/d2f5d744

Branch: refs/heads/master
Commit: d2f5d74403aa74a93f46e2cbde0e0bf528f3c30b
Parents: 3d8a306
Author: gpetracek <gpetracek@apache.org>
Authored: Sat Jan 2 01:18:09 2016 +0100
Committer: gpetracek <gpetracek@apache.org>
Committed: Sat Jan 2 01:30:48 2016 +0100

----------------------------------------------------------------------
 .../scheduler/impl/SchedulerExtension.java      | 51 +++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/d2f5d744/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/SchedulerExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/SchedulerExtension.java
b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/SchedulerExtension.java
index 907d2c0..33a7489 100644
--- a/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/SchedulerExtension.java
+++ b/deltaspike/modules/scheduler/impl/src/main/java/org/apache/deltaspike/scheduler/impl/SchedulerExtension.java
@@ -86,7 +86,7 @@ public class SchedulerExtension implements Extension, Deactivatable
             return;
         }
 
-        if (!jobClass.isAssignableFrom(beanClass))
+        if (!jobClass.isAssignableFrom(beanClass) && !Runnable.class.isAssignableFrom(beanClass))
         {
             return;
         }
@@ -105,6 +105,18 @@ public class SchedulerExtension implements Extension, Deactivatable
             return;
         }
 
+        Class configuredJobClass = this.jobClass;
+
+        this.jobClass = resolveFinalJobType();
+
+        if (this.jobClass == null)
+        {
+            afterBeanDiscovery.addDefinitionError(new IllegalStateException("Please only
annotate classes with @" +
+                Scheduled.class.getName() + " of type " +
+                configuredJobClass.getName() + " or of type " + Runnable.class.getName()
+ ", but not both!"));
+            return;
+        }
+
         initScheduler(afterBeanDiscovery);
 
         if (this.scheduler == null)
@@ -128,6 +140,43 @@ public class SchedulerExtension implements Extension, Deactivatable
         }
     }
 
+    /**
+     * Allows to support implementations of {@link java.lang.Runnable}
+     * annotated with {@link Scheduled} >without< explicit config.
+     * @return the job-type which will be used to select the scheduler
+     */
+    protected Class resolveFinalJobType()
+    {
+        Set<Class> foundTypes = new HashSet<Class>();
+
+        for (Class foundJobClass : this.foundManagedJobClasses)
+        {
+            if (jobClass.isAssignableFrom(foundJobClass))
+            {
+                foundTypes.add(jobClass);
+            }
+            else if (Runnable.class.isAssignableFrom(foundJobClass))
+            {
+                foundTypes.add(Runnable.class);
+            }
+        }
+
+        if (foundTypes.size() > 1)
+        {
+            return null;
+        }
+        else if (foundTypes.size() == 1)
+        {
+            return foundTypes.iterator().next();
+        }
+        else
+        {
+            //use the configured type
+            //it's still useful if there is no annotated job-class, but a dyn. usage of the
scheduler is still possible
+            return jobClass;
+        }
+    }
+
     public <X> void stopScheduler(@Observes BeforeShutdown beforeShutdown)
     {
         if (!this.isActivated)


Mime
View raw message