KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > TimerHandleImpl


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb.txtimer;
23
24 // $Id: TimerHandleImpl.java 37858 2005-11-05 17:06:17Z dimitris $
25

26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.NoSuchObjectLocalException JavaDoc;
28 import javax.ejb.Timer JavaDoc;
29 import javax.ejb.TimerHandle JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.text.ParseException JavaDoc;
33 import java.text.SimpleDateFormat JavaDoc;
34 import java.util.Date JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 /**
38  * An implementation of the TimerHandle
39  *
40  * @author Thomas.Diesler@jboss.org
41  * @version $Revision: 37858 $
42  * @since 07-Apr-2004
43  */

44 public class TimerHandleImpl implements TimerHandle JavaDoc
45 {
46    /**
47     * The date pattern used by this handle
48     */

49    public static final String JavaDoc DATE_PATTERN = "dd-MMM-yyyy HH:mm:ss.SSS";
50
51    // The initial txtimer properties
52
private String JavaDoc timerId;
53    private TimedObjectId timedObjectId;
54    private Date JavaDoc firstTime;
55    private long periode;
56    private Serializable JavaDoc info;
57    private int hashCode;
58
59    /**
60     * Construct a handle from a timer
61     */

62    TimerHandleImpl(TimerImpl timer)
63    {
64       timerId = timer.getTimerId();
65       timedObjectId = timer.getTimedObjectId();
66       firstTime = timer.getFirstTime();
67       periode = timer.getPeriode();
68       info = timer.getInfoInternal();
69    }
70
71    /**
72     * Construct a handle from individual parameters
73     */

74    TimerHandleImpl(String JavaDoc timerId, TimedObjectId timedObjectId, Date JavaDoc firstTime, long periode, Serializable JavaDoc info)
75    {
76       this.timerId = timerId;
77       this.timedObjectId = timedObjectId;
78       this.firstTime = firstTime;
79       this.periode = periode;
80       this.info = info;
81    }
82
83    /**
84     * Construct a handle from external form
85     */

86    private TimerHandleImpl(String JavaDoc externalForm)
87    {
88       if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false)
89          throw new IllegalArgumentException JavaDoc("Square brackets expected arround: " + externalForm);
90
91       try
92       {
93          // take first and last char off
94
String JavaDoc inStr = externalForm.substring(1, externalForm.length() - 1);
95
96          if (inStr.startsWith("id=") == false)
97             throw new IllegalArgumentException JavaDoc("Cannot parse: " + externalForm);
98
99          int targetIndex = inStr.indexOf(",target=");
100          int firstIndex = inStr.indexOf(",first=");
101
102          String JavaDoc idStr = inStr.substring(3, targetIndex);
103          String JavaDoc targetStr = inStr.substring(targetIndex + 8, firstIndex);
104          String JavaDoc restStr = inStr.substring(firstIndex + 1);
105
106          timerId = idStr;
107          timedObjectId = TimedObjectId.parse(targetStr);
108
109          StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(restStr, ",=");
110          if (st.countTokens() % 2 != 0)
111             throw new IllegalArgumentException JavaDoc("Cannot parse: " + externalForm);
112
113          SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_PATTERN);
114
115          periode = -1;
116
117          while (st.hasMoreTokens())
118          {
119             String JavaDoc key = st.nextToken();
120             String JavaDoc value = st.nextToken();
121             if (key.equals("first"))
122                firstTime = sdf.parse(value);
123             if (key.equals("periode"))
124                periode = new Long JavaDoc(value).longValue();
125          }
126
127          if (firstTime == null || periode < 0)
128             throw new IllegalArgumentException JavaDoc("Cannot parse: " + externalForm);
129       }
130       catch (ParseException JavaDoc e)
131       {
132          throw new IllegalArgumentException JavaDoc("Cannot parse date/time in: " + externalForm);
133       }
134    }
135
136    /**
137     * Parse the handle from external form.
138     * "[toid=timedObjectId,first=firstTime,periode=periode]"
139     */

140    public static TimerHandleImpl parse(String JavaDoc externalForm)
141    {
142       return new TimerHandleImpl(externalForm);
143    }
144
145    /**
146     * Returns the external representation of the handle.
147     * "[toid=timedObjectId,first=firstTime,periode=periode]"
148     */

149    public String JavaDoc toExternalForm()
150    {
151       SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc(DATE_PATTERN);
152       String JavaDoc firstEvent = sdf.format(firstTime);
153       return "[id=" + timerId + ",target=" + timedObjectId + ",first=" + firstEvent + ",periode=" + periode + "]";
154    }
155
156    public String JavaDoc getTimerId()
157    {
158       return timerId;
159    }
160
161    public TimedObjectId getTimedObjectId()
162    {
163       return timedObjectId;
164    }
165
166    public Date JavaDoc getFirstTime()
167    {
168       return firstTime;
169    }
170
171    public long getPeriode()
172    {
173       return periode;
174    }
175
176    public Serializable JavaDoc getInfo()
177    {
178       return info;
179    }
180
181    /**
182     * Obtain a reference to the txtimer represented by this handle.
183     *
184     * @return Timer which this handle represents
185     * @throws IllegalStateException If this method is invoked while the instance is in
186     * a state that does not allow access to this method.
187     * @throws javax.ejb.NoSuchObjectLocalException
188     * If invoked on a txtimer that has expired or has been cancelled.
189     * @throws javax.ejb.EJBException If this method could not complete due to a system-level failure.
190     */

191    public Timer JavaDoc getTimer() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc
192    {
193
194       EJBTimerService ejbTimerService = EJBTimerServiceLocator.getEjbTimerService();
195       ObjectName JavaDoc containerId = timedObjectId.getContainerId();
196       Object JavaDoc instancePk = timedObjectId.getInstancePk();
197       TimerServiceImpl timerService = (TimerServiceImpl)ejbTimerService.getTimerService(containerId, instancePk);
198       if (timerService == null)
199          throw new NoSuchObjectLocalException JavaDoc("TimerService not available: " + timedObjectId);
200
201       TimerImpl timer = (TimerImpl)timerService.getTimer(this);
202       if (timer == null || timer.isActive() == false)
203          throw new NoSuchObjectLocalException JavaDoc("Timer not available: " + timedObjectId);
204
205       return timer;
206    }
207
208    /**
209     * Return true if objectId, createDate, periode are equal
210     */

211    public boolean equals(Object JavaDoc obj)
212    {
213       if (obj == this) return true;
214       if (obj instanceof TimerHandleImpl)
215       {
216          TimerHandleImpl other = (TimerHandleImpl)obj;
217          return hashCode() == other.hashCode();
218       }
219       return false;
220    }
221
222    /**
223     * Hash code based on objectId, createDate, periode
224     */

225    public int hashCode()
226    {
227       if (hashCode == 0)
228       {
229          hashCode = toExternalForm().hashCode();
230       }
231       return hashCode;
232    }
233
234    /**
235     * Returns a string representation of the object.
236     */

237    public String JavaDoc toString()
238    {
239       return toExternalForm();
240    }
241 }
242
Popular Tags