001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.txtimer;
023:
024: // $Id: TimerHandleImpl.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
025:
026: import javax.ejb.EJBException;
027: import javax.ejb.NoSuchObjectLocalException;
028: import javax.ejb.Timer;
029: import javax.ejb.TimerHandle;
030: import javax.management.ObjectName;
031: import java.io.Serializable;
032: import java.text.ParseException;
033: import java.text.SimpleDateFormat;
034: import java.util.Date;
035: import java.util.StringTokenizer;
036:
037: /**
038: * An implementation of the TimerHandle
039: *
040: * @author Thomas.Diesler@jboss.org
041: * @version $Revision: 57209 $
042: * @since 07-Apr-2004
043: */
044: public class TimerHandleImpl implements TimerHandle {
045: /**
046: * The date pattern used by this handle
047: */
048: public static final String DATE_PATTERN = "dd-MMM-yyyy HH:mm:ss.SSS";
049:
050: // The initial txtimer properties
051: private String timerId;
052: private TimedObjectId timedObjectId;
053: private Date firstTime;
054: private long periode;
055: private Serializable info;
056: private int hashCode;
057:
058: /**
059: * Construct a handle from a timer
060: */
061: TimerHandleImpl(TimerImpl timer) {
062: timerId = timer.getTimerId();
063: timedObjectId = timer.getTimedObjectId();
064: firstTime = timer.getFirstTime();
065: periode = timer.getPeriode();
066: info = timer.getInfoInternal();
067: }
068:
069: /**
070: * Construct a handle from individual parameters
071: */
072: TimerHandleImpl(String timerId, TimedObjectId timedObjectId,
073: Date firstTime, long periode, Serializable info) {
074: this .timerId = timerId;
075: this .timedObjectId = timedObjectId;
076: this .firstTime = firstTime;
077: this .periode = periode;
078: this .info = info;
079: }
080:
081: /**
082: * Construct a handle from external form
083: */
084: private TimerHandleImpl(String externalForm) {
085: if (externalForm.startsWith("[") == false
086: || externalForm.endsWith("]") == false)
087: throw new IllegalArgumentException(
088: "Square brackets expected arround: " + externalForm);
089:
090: try {
091: // take first and last char off
092: String inStr = externalForm.substring(1, externalForm
093: .length() - 1);
094:
095: if (inStr.startsWith("id=") == false)
096: throw new IllegalArgumentException("Cannot parse: "
097: + externalForm);
098:
099: int targetIndex = inStr.indexOf(",target=");
100: int firstIndex = inStr.indexOf(",first=");
101:
102: String idStr = inStr.substring(3, targetIndex);
103: String targetStr = inStr.substring(targetIndex + 8,
104: firstIndex);
105: String restStr = inStr.substring(firstIndex + 1);
106:
107: timerId = idStr;
108: timedObjectId = TimedObjectId.parse(targetStr);
109:
110: StringTokenizer st = new StringTokenizer(restStr, ",=");
111: if (st.countTokens() % 2 != 0)
112: throw new IllegalArgumentException("Cannot parse: "
113: + externalForm);
114:
115: SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
116:
117: periode = -1;
118:
119: while (st.hasMoreTokens()) {
120: String key = st.nextToken();
121: String value = st.nextToken();
122: if (key.equals("first"))
123: firstTime = sdf.parse(value);
124: if (key.equals("periode"))
125: periode = new Long(value).longValue();
126: }
127:
128: if (firstTime == null || periode < 0)
129: throw new IllegalArgumentException("Cannot parse: "
130: + externalForm);
131: } catch (ParseException e) {
132: throw new IllegalArgumentException(
133: "Cannot parse date/time in: " + externalForm);
134: }
135: }
136:
137: /**
138: * Parse the handle from external form.
139: * "[toid=timedObjectId,first=firstTime,periode=periode]"
140: */
141: public static TimerHandleImpl parse(String externalForm) {
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 toExternalForm() {
150: SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
151: String firstEvent = sdf.format(firstTime);
152: return "[id=" + timerId + ",target=" + timedObjectId
153: + ",first=" + firstEvent + ",periode=" + periode + "]";
154: }
155:
156: public String getTimerId() {
157: return timerId;
158: }
159:
160: public TimedObjectId getTimedObjectId() {
161: return timedObjectId;
162: }
163:
164: public Date getFirstTime() {
165: return firstTime;
166: }
167:
168: public long getPeriode() {
169: return periode;
170: }
171:
172: public Serializable getInfo() {
173: return info;
174: }
175:
176: /**
177: * Obtain a reference to the txtimer represented by this handle.
178: *
179: * @return Timer which this handle represents
180: * @throws IllegalStateException If this method is invoked while the instance is in
181: * a state that does not allow access to this method.
182: * @throws javax.ejb.NoSuchObjectLocalException
183: * If invoked on a txtimer that has expired or has been cancelled.
184: * @throws javax.ejb.EJBException If this method could not complete due to a system-level failure.
185: */
186: public Timer getTimer() throws IllegalStateException,
187: NoSuchObjectLocalException, EJBException {
188:
189: EJBTimerService ejbTimerService = EJBTimerServiceLocator
190: .getEjbTimerService();
191: ObjectName containerId = timedObjectId.getContainerId();
192: Object instancePk = timedObjectId.getInstancePk();
193: TimerServiceImpl timerService = (TimerServiceImpl) ejbTimerService
194: .getTimerService(containerId, instancePk);
195: if (timerService == null)
196: throw new NoSuchObjectLocalException(
197: "TimerService not available: " + timedObjectId);
198:
199: TimerImpl timer = (TimerImpl) timerService.getTimer(this );
200: if (timer == null || timer.isActive() == false)
201: throw new NoSuchObjectLocalException(
202: "Timer not available: " + timedObjectId);
203:
204: return timer;
205: }
206:
207: /**
208: * Return true if objectId, createDate, periode are equal
209: */
210: public boolean equals(Object obj) {
211: if (obj == this )
212: return true;
213: if (obj instanceof TimerHandleImpl) {
214: TimerHandleImpl other = (TimerHandleImpl) obj;
215: return hashCode() == other.hashCode();
216: }
217: return false;
218: }
219:
220: /**
221: * Hash code based on objectId, createDate, periode
222: */
223: public int hashCode() {
224: if (hashCode == 0) {
225: hashCode = toExternalForm().hashCode();
226: }
227: return hashCode;
228: }
229:
230: /**
231: * Returns a string representation of the object.
232: */
233: public String toString() {
234: return toExternalForm();
235: }
236: }
|