[seam-commits] Seam SVN: r7734 - branches/Seam_2_0/src/main/org/jboss/seam/async.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Mar 28 08:52:39 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-03-28 08:52:39 -0400 (Fri, 28 Mar 2008)
New Revision: 7734

Modified:
   branches/Seam_2_0/src/main/org/jboss/seam/async/AbstractDispatcher.java
   branches/Seam_2_0/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java
   branches/Seam_2_0/src/main/org/jboss/seam/async/TimerServiceDispatcher.java
Log:
API safe backport of r773, Use object orientation properly

Modified: branches/Seam_2_0/src/main/org/jboss/seam/async/AbstractDispatcher.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/async/AbstractDispatcher.java	2008-03-28 12:39:28 UTC (rev 7733)
+++ branches/Seam_2_0/src/main/org/jboss/seam/async/AbstractDispatcher.java	2008-03-28 12:52:39 UTC (rev 7734)
@@ -22,6 +22,58 @@
 public abstract class AbstractDispatcher<T, S> implements Dispatcher<T, S>
 {
    
+   public class DispatcherParameters
+   {
+      private Date expiration;
+      private Date finalExpiration;
+      private Long duration;
+      private Long intervalDuration;
+      private String intervalCron;
+      
+      public String getIntervalCron()
+      {
+         return intervalCron;
+      }
+      public Long getDuration()
+      {
+         return duration;
+      }
+      public Date getExpiration()
+      {
+         return expiration;
+      }
+      public Date getFinalExpiration()
+      {
+         return finalExpiration;
+      }
+      public Long getIntervalDuration()
+      {
+         return intervalDuration;
+      }
+      public void setIntervalCron(String cron)
+      {
+         this.intervalCron = cron;
+      }
+      public void setDuration(Long duration)
+      {
+         this.duration = duration;
+      }
+      public void setExpiration(Date expiration)
+      {
+         this.expiration = expiration;
+      }
+      public void setFinalExpiration(Date finalExpiration)
+      {
+         this.finalExpiration = finalExpiration;
+      }
+      public void setIntervalDuration(Long intervalDuration)
+      {
+         this.intervalDuration = intervalDuration;
+      }
+      
+      
+   }
+   
    public static final String EXECUTING_ASYNCHRONOUS_CALL = "org.jboss.seam.core.executingAsynchronousCall";
       
    public static Dispatcher instance()
@@ -45,58 +97,108 @@
 
    protected Schedule createSchedule(InvocationContext invocation)
    {
-      Long duration = null;
-      Date expiration = null;
-      Date finalExpiration = null;
-
-      Long intervalDuration = null;
-      String cron = null;
-      
-      int intervalParamCount = 0;
-
-      Annotation[][] parameterAnnotations = invocation.getMethod().getParameterAnnotations();
-      for ( int i=0; i<parameterAnnotations.length; i++ )
+      DispatcherParameters dispatcherParameters = extractAndValidateParameters(invocation);
+      if (dispatcherParameters.getIntervalCron() == null)
       {
-         Annotation[] annotations = parameterAnnotations[i];
+         return new TimerSchedule(dispatcherParameters.getDuration(), dispatcherParameters.getExpiration(), dispatcherParameters.getIntervalDuration(), dispatcherParameters.getFinalExpiration());
+      }
+      else
+      {
+         return new CronSchedule(dispatcherParameters.getDuration(), dispatcherParameters.getExpiration(), dispatcherParameters.getIntervalCron(), dispatcherParameters.getFinalExpiration());
+      }
+   }
+   
+   protected TimerSchedule createTimerSchedule(InvocationContext invocation)
+   {
+      DispatcherParameters dispatcherParameters = extractAndValidateParameters(invocation);
+      return createTimerSchedule(dispatcherParameters);
+   }
+   
+   private TimerSchedule createTimerSchedule(DispatcherParameters dispatcherParameters)
+   {
+      return new TimerSchedule(dispatcherParameters.getDuration(), dispatcherParameters.getExpiration(), dispatcherParameters.getIntervalDuration(), dispatcherParameters.getFinalExpiration());
+   }
+   
+   protected DispatcherParameters extractAndValidateParameters(InvocationContext invocation)
+   {
+      DispatcherParameters dispatcherParameters = new DispatcherParameters();
+      for ( int i=0; i < invocation.getMethod().getParameterAnnotations().length; i++ )
+      {
+         Annotation[] annotations = invocation.getMethod().getParameterAnnotations()[i];
          for (Annotation annotation: annotations)
          {
             if ( annotation.annotationType().equals(Duration.class) )
             {
-               duration = (Long) invocation.getParameters()[i];
+               if (invocation.getParameters()[i] instanceof Long)
+               {
+                  dispatcherParameters.setDuration((Long) invocation.getParameters()[i]);
+               }
+               else if (invocation.getParameters()[i] != null)
+               {
+                  throw new IllegalArgumentException("@Duration on " + invocation.getTarget().getClass() + ":" + invocation.getMethod().getName() + " must be a Long");
+               }
             }
-            else if ( annotation.annotationType().equals(IntervalDuration.class) )
-            {
-               intervalDuration = (Long) invocation.getParameters()[i];
-               intervalParamCount++;
-            }
             else if ( annotation.annotationType().equals(Expiration.class) )
             {
-               expiration = (Date) invocation.getParameters()[i];
+               if (invocation.getParameters()[i] instanceof Date)
+               {
+                  dispatcherParameters.setExpiration((Date) invocation.getParameters()[i]);
+               }
+               else if (invocation.getParameters()[i] != null)
+               {
+                  throw new IllegalArgumentException("@Expiration on " + invocation.getTarget().getClass() + ":" + invocation.getMethod().getName() + " must be a java.util.Date");
+               }
             }
             else if ( annotation.annotationType().equals(FinalExpiration.class) )
             {
-               finalExpiration = (Date) invocation.getParameters()[i];
+               if (!( this instanceof QuartzDispatcher ))
+               {
+                  throw new IllegalArgumentException("Can only use @FinalExpiration with the QuartzDispatcher");
+               }
+               else if (invocation.getParameters()[i] instanceof Date)
+               {
+                  dispatcherParameters.setFinalExpiration((Date) invocation.getParameters()[i]);
+               }
+               else if (invocation.getParameters()[i] != null)
+               {
+                  throw new IllegalArgumentException("@FinalExpiration on " + invocation.getTarget().getClass() + ":" + invocation.getMethod().getName() + " must be a java.util.Date");
+               }
             }
             else if ( annotation.annotationType().equals(IntervalCron.class) )
             {
-               cron = (String) invocation.getParameters()[i];
-               intervalParamCount++;
+               if (!( this instanceof QuartzDispatcher ))
+               {
+                  throw new IllegalArgumentException("Can only use @IntervalCron with the QuartzDispatcher");
+               }
+               else if (invocation.getParameters()[i] instanceof String)
+               {
+                  dispatcherParameters.setIntervalCron((String) invocation.getParameters()[i]);
+               }
+               else if (invocation.getParameters()[i] != null)
+               {
+                  throw new IllegalArgumentException("@IntervalCron on " + invocation.getTarget().getClass() + ":" + invocation.getMethod().getName() + " must be a String");
+               }
             }
+            else if ( annotation.annotationType().equals(IntervalDuration.class) )
+            {
+               if (invocation.getParameters()[i] instanceof Long)
+               {
+                  dispatcherParameters.setIntervalDuration((Long) invocation.getParameters()[i]);
+               }
+               else if (invocation.getParameters()[i] != null)
+               {
+                  throw new IllegalArgumentException("@IntervalDuration on " + invocation.getTarget().getClass() + ":" + invocation.getMethod().getName() + " must be a Long");
+               }
+            }
          }
       }
-      
-      if (intervalParamCount > 1) {
-        throw new RuntimeException ("Cannot have more than one @Interval arguments in asynchrnous method");
+
+      if ( dispatcherParameters.getIntervalCron() != null && dispatcherParameters.getIntervalDuration() != null )
+      {
+         throw new IllegalArgumentException("Can only use one of @IntervalCron and @IntervalDuration");
       }
       
-      if ( cron!=null ) 
-      {
-        return new CronSchedule(duration, expiration, cron, finalExpiration);
-      } 
-      else 
-      {
-        return new TimerSchedule(duration, expiration, intervalDuration, finalExpiration);
-      }
+      return dispatcherParameters;
    }
    
 }

Modified: branches/Seam_2_0/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java	2008-03-28 12:39:28 UTC (rev 7733)
+++ branches/Seam_2_0/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java	2008-03-28 12:52:39 UTC (rev 7734)
@@ -45,7 +45,7 @@
    public Future scheduleInvocation(InvocationContext invocation, Component component)
    {
       return scheduleWithExecutorService( 
-               (TimerSchedule) createSchedule(invocation), 
+               createTimerSchedule(invocation), 
                new RunnableAsynchronous( new AsynchronousInvocation(invocation, component) ) 
             );
    }

Modified: branches/Seam_2_0/src/main/org/jboss/seam/async/TimerServiceDispatcher.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/async/TimerServiceDispatcher.java	2008-03-28 12:39:28 UTC (rev 7733)
+++ branches/Seam_2_0/src/main/org/jboss/seam/async/TimerServiceDispatcher.java	2008-03-28 12:52:39 UTC (rev 7734)
@@ -63,7 +63,7 @@
    public Timer scheduleInvocation(InvocationContext invocation, Component component)
    {
       return new TimerProxy( 
-              scheduleWithTimerService( (TimerSchedule) createSchedule(invocation), 
+              scheduleWithTimerService( createTimerSchedule(invocation), 
               new AsynchronousInvocation(invocation, component) ) );
       
    }




More information about the seam-commits mailing list