01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.openejb.core.timer;
17:
18: import javax.ejb.Timer;
19: import javax.ejb.TimerHandle;
20: import javax.ejb.NoSuchObjectLocalException;
21:
22: import java.io.Serializable;
23: import java.util.Date;
24:
25: import org.apache.openejb.core.BaseContext;
26: import org.apache.openejb.core.ThreadContext;
27:
28: public class TimerImpl implements Timer {
29: private final TimerData timerData;
30:
31: public TimerImpl(TimerData timerData) {
32: this .timerData = timerData;
33: }
34:
35: public void cancel() throws IllegalStateException,
36: NoSuchObjectLocalException {
37: checkState();
38: timerData.cancel();
39: }
40:
41: public long getTimeRemaining() throws IllegalStateException,
42: NoSuchObjectLocalException {
43: checkState();
44: long now = System.currentTimeMillis();
45: long then = timerData.getExpiration().getTime();
46: return then - now;
47: }
48:
49: public Date getNextTimeout() throws IllegalStateException,
50: NoSuchObjectLocalException {
51: checkState();
52: return timerData.getExpiration();
53: }
54:
55: public Serializable getInfo() throws IllegalStateException,
56: NoSuchObjectLocalException {
57: checkState();
58: return (Serializable) timerData.getInfo();
59: }
60:
61: public TimerHandle getHandle() throws IllegalStateException,
62: NoSuchObjectLocalException {
63: checkState();
64: return new TimerHandleImpl(timerData.getId(), timerData
65: .getDeploymentId());
66: }
67:
68: /**
69: * Insure that timer methods can be invoked for the current operation on this Context.
70: */
71: private void checkState() throws IllegalStateException,
72: NoSuchObjectLocalException {
73: if (!BaseContext.isTimerMethodAllowed()) {
74: throw new IllegalStateException(
75: "Timer method not permitted for current operation "
76: + ThreadContext.getThreadContext()
77: .getCurrentOperation().name());
78: }
79: if (timerData.isCancelled()) {
80: throw new NoSuchObjectLocalException(
81: "Timer has been cancelled");
82: }
83: }
84:
85: }
|