[seam-commits] Seam SVN: r7733 - in trunk/src: main/org/jboss/seam/async and 1 other directories.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Fri Mar 28 08:39:28 EDT 2008
Author: pete.muir at jboss.org
Date: 2008-03-28 08:39:28 -0400 (Fri, 28 Mar 2008)
New Revision: 7733
Modified:
trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTaskExecutorDispatcher.java
trunk/src/main/org/jboss/seam/async/AbstractDispatcher.java
trunk/src/main/org/jboss/seam/async/CronSchedule.java
trunk/src/main/org/jboss/seam/async/Dispatcher.java
trunk/src/main/org/jboss/seam/async/QuartzDispatcher.java
trunk/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java
trunk/src/main/org/jboss/seam/async/TimerSchedule.java
trunk/src/main/org/jboss/seam/async/TimerServiceDispatcher.java
trunk/src/main/org/jboss/seam/core/Events.java
Log:
Use object orientation properly
Modified: trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTaskExecutorDispatcher.java
===================================================================
--- trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTaskExecutorDispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/ioc/org/jboss/seam/ioc/spring/SpringTaskExecutorDispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -28,7 +28,7 @@
@Scope(ScopeType.APPLICATION)
@Name("org.jboss.seam.async.dispatcher")
@Install(value=false, precedence=BUILT_IN)
-public class SpringTaskExecutorDispatcher<T, S> extends AbstractDispatcher<T, S>
+public class SpringTaskExecutorDispatcher<T, S extends Schedule> extends AbstractDispatcher<T, S>
{
private ValueExpression<Dispatcher<T, S>> scheduleDispatcher;
Modified: trunk/src/main/org/jboss/seam/async/AbstractDispatcher.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/AbstractDispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/AbstractDispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -19,9 +19,61 @@
* @author Gavin King
*
*/
-public abstract class AbstractDispatcher<T, S> implements Dispatcher<T, S>
+public abstract class AbstractDispatcher<T, S extends Schedule> 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()
@@ -42,61 +94,111 @@
{
Transaction.instance().registerSynchronization( new TransactionCompletionEvent(type, parameters) );
}
-
+
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: trunk/src/main/org/jboss/seam/async/CronSchedule.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/CronSchedule.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/CronSchedule.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -43,7 +43,5 @@
super(duration, expiration, finalExpiration);
this.cron = cron;
}
-
- CronSchedule() {}
}
Modified: trunk/src/main/org/jboss/seam/async/Dispatcher.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/Dispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/Dispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -11,7 +11,7 @@
*
* @param <T> the type of the timer object
*/
-public interface Dispatcher<T, S>
+public interface Dispatcher<T, S extends Schedule>
{
/**
* Schedule an asynchronous method call, examining annotations
Modified: trunk/src/main/org/jboss/seam/async/QuartzDispatcher.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/QuartzDispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/QuartzDispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -100,7 +100,7 @@
{
return scheduleWithQuartzServiceAndWrapExceptions( createSchedule(invocation), new AsynchronousInvocation(invocation, component) );
}
-
+
private static Date calculateDelayedDate (long delay)
{
Date now = new Date ();
@@ -245,7 +245,9 @@
{
return scheduler;
}
+
+
public static QuartzDispatcher instance()
{
return (QuartzDispatcher) AbstractDispatcher.instance();
Modified: trunk/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/ThreadPoolDispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -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: trunk/src/main/org/jboss/seam/async/TimerSchedule.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/TimerSchedule.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/TimerSchedule.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -56,19 +56,19 @@
this.intervalDuration = intervalDuration;
}
- TimerSchedule(Long duration, Date expiration, Long intervalDuration)
+ public TimerSchedule(Long duration, Date expiration, Long intervalDuration)
{
super(duration, expiration);
this.intervalDuration = intervalDuration;
}
- TimerSchedule(Long duration, Date expiration, Long intervalDuration, Date finalExpiration)
+ public TimerSchedule(Long duration, Date expiration, Long intervalDuration, Date finalExpiration)
{
super(duration, expiration, finalExpiration);
this.intervalDuration = intervalDuration;
}
- TimerSchedule() {}
+ private TimerSchedule() {}
Modified: trunk/src/main/org/jboss/seam/async/TimerServiceDispatcher.java
===================================================================
--- trunk/src/main/org/jboss/seam/async/TimerServiceDispatcher.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/async/TimerServiceDispatcher.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -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) ) );
}
@@ -250,4 +250,6 @@
return ( (LocalTimerServiceDispatcher) AbstractDispatcher.instance() );
}
+
+
}
Modified: trunk/src/main/org/jboss/seam/core/Events.java
===================================================================
--- trunk/src/main/org/jboss/seam/core/Events.java 2008-03-28 12:37:11 UTC (rev 7732)
+++ trunk/src/main/org/jboss/seam/core/Events.java 2008-03-28 12:39:28 UTC (rev 7733)
@@ -13,6 +13,7 @@
import org.jboss.seam.async.AbstractDispatcher;
import org.jboss.seam.async.CronSchedule;
import org.jboss.seam.async.Dispatcher;
+import org.jboss.seam.async.Schedule;
import org.jboss.seam.async.TimerSchedule;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.core.Expressions.MethodExpression;
@@ -108,7 +109,7 @@
* @param schedule the schedule object, specific to the dispatcher strategy
* @param parameters parameters to be passes to the listener method
*/
- public void raiseTimedEvent(String type, Object schedule, Object... parameters)
+ public void raiseTimedEvent(String type, Schedule schedule, Object... parameters)
{
getDispatcher().scheduleTimedEvent(type, schedule, parameters);
}
More information about the seam-commits
mailing list