001: package com.bm.utils.substitues;
002:
003: import java.io.Serializable;
004: import java.lang.reflect.InvocationTargetException;
005: import java.lang.reflect.Method;
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.Date;
009:
010: import javax.annotation.Resource;
011: import javax.ejb.EJBException;
012: import javax.ejb.Timeout;
013: import javax.ejb.Timer;
014: import javax.ejb.TimerService;
015: import javax.persistence.EntityManager;
016: import javax.persistence.EntityTransaction;
017:
018: import com.bm.ejb3guice.annotations.InjectedIn;
019: import com.bm.ejb3guice.annotations.NotifyOnInject;
020:
021: /**
022: * @author Daniel Wiese
023: * @author Marcus Nilsson Date: May 16, 2007 Time: 9:50:53 AM
024: */
025: @NotifyOnInject
026: public class MockedTimerService implements TimerService {
027: private Object caller;
028:
029: @Resource
030: private EntityManager entityManager;
031:
032: private final Collection<TimerMock> timers = new ArrayList<TimerMock>();
033:
034: public MockedTimerService() {
035: }
036:
037: @InjectedIn
038: public void injectedIn(Object injectectedIn) {
039: this .caller = injectectedIn;
040: }
041:
042: public Timer createTimer(long l, Serializable info)
043: throws IllegalArgumentException, IllegalStateException,
044: EJBException {
045: final Class callerClass = caller.getClass();
046:
047: TimerMock t = new TimerMock(l, info) {
048: public void timerExpired() {
049: Method[] methods = callerClass.getMethods();
050: for (Method m : methods) {
051: if (m.isAnnotationPresent(Timeout.class)) {
052: try {
053: EntityTransaction t = entityManager
054: .getTransaction();
055: t.begin();
056: m.invoke(caller, this );
057: t.commit();
058: } catch (IllegalAccessException e) {
059: throw new RuntimeException(e);
060: } catch (InvocationTargetException e) {
061: throw new RuntimeException(e);
062: }
063: }
064:
065: timers.remove(this );
066: }
067: }
068: };
069:
070: synchronized (timers) {
071: timers.add(t);
072: }
073:
074: t.start();
075:
076: return null;
077: }
078:
079: public Timer createTimer(long l, long l1, Serializable serializable)
080: throws IllegalArgumentException, IllegalStateException,
081: EJBException {
082: throw new RuntimeException("Not implemented");
083: }
084:
085: public Timer createTimer(Date date, Serializable serializable)
086: throws IllegalArgumentException, IllegalStateException,
087: EJBException {
088: throw new RuntimeException("Not implemented");
089: }
090:
091: public Timer createTimer(Date date, long l,
092: Serializable serializable) throws IllegalArgumentException,
093: IllegalStateException, EJBException {
094: throw new RuntimeException("Not implemented");
095: }
096:
097: @SuppressWarnings("unchecked")
098: public Collection getTimers() throws IllegalStateException,
099: EJBException {
100: Collection res = new ArrayList();
101:
102: synchronized (timers) {
103: for (TimerMock t : timers) {
104: res.add(t);
105: }
106: }
107:
108: return res;
109: }
110: }
|