001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.timer;
019    
020    import java.util.Date;
021    
022    import org.apache.geronimo.timer.ExecutorFeedingTimerTask;
023    import org.apache.geronimo.timer.ExecutorTask;
024    
025    /**
026     *
027     *
028     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
029     *
030     * */
031    public class WorkInfo {
032        //these should be persistent.
033        private long id = -1;
034    
035        //typically object name of ejb.
036        private final String key;
037        //typically entity pk
038        private final Object userId;
039        //typically serializable object ejb timer service supplies to users.
040        private final Object userInfo;
041        //next firing
042        private Date time;
043        //time between firings
044        private final Long period;
045    
046        private final boolean atFixedRate;
047    
048        //these should not be persistent.
049        private ExecutorFeedingTimerTask worker;
050        private ExecutorTask taskWrapper;
051    
052        //wrappers to this timer service can store the wrapper here.
053        private Object clientHandle;
054    
055    
056        public WorkInfo(String key, Object userId, Object userInfo, Date time, Long period, boolean atFixedRate) {
057            this.key = key;
058            this.userId = userId;
059            this.userInfo = userInfo;
060            this.time = time;
061            this.period = period;
062            this.atFixedRate = atFixedRate;
063        }
064    
065        public String getKey() {
066            return key;
067        }
068    
069        public long getId() {
070            return id;
071        }
072    
073        public void setId(long id) {
074            if (this.id != -1) {
075                throw new IllegalStateException("Id can be set only once!");
076            }
077            this.id = id;
078        }
079    
080        public Object getUserId() {
081            return userId;
082        }
083    
084        public Object getUserInfo() {
085            return userInfo;
086        }
087    
088        public Date getTime() {
089            return time;
090        }
091    
092        public Long getPeriod() {
093            return period;
094        }
095    
096        public boolean getAtFixedRate() {
097            return atFixedRate;
098        }
099    
100        public void initialize(ExecutorFeedingTimerTask worker, ExecutorTask taskWrapper) {
101            this.worker = worker;
102            this.taskWrapper = taskWrapper;
103        }
104    
105        public ExecutorFeedingTimerTask getExecutorFeedingTimerTask() {
106            return worker;
107        }
108    
109        public Runnable getExecutorTask() {
110            return taskWrapper;
111        }
112    
113        public Object getClientHandle() {
114            return clientHandle;
115        }
116    
117        public void setClientHandle(Object clientHandle) {
118            this.clientHandle = clientHandle;
119        }
120    
121        public boolean isOneTime() {
122            return period == null;
123        }
124    
125        void nextTime() {
126            if (period == null) {
127                throw new IllegalStateException("This is a one-time timerTask");
128            }
129            time = new Date(time.getTime() + period.longValue());
130        }
131    
132        public void nextInterval() {
133            if (period == null) {
134                throw new IllegalStateException("This is a one-time timerTask");
135            }
136            time = new Date(System.currentTimeMillis() + period.longValue());
137        }
138    }