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    package org.apache.servicemix.eip.support.resequence;
018    
019    import java.util.LinkedList;
020    import java.util.List;
021    import java.util.Timer;
022    import java.util.TimerTask;
023    
024    /**
025     * A timer task that notifies handlers about scheduled timeouts.
026     * 
027     * @see Timer
028     * @see TimerTask
029     * 
030     * @author Martin Krasser
031     */
032    public class Timeout extends TimerTask {
033        
034        private List<TimeoutHandler> timeoutHandlers;
035        
036        private Timer timer;
037        
038        private long timeout;
039        
040        /**
041         * Creates a new timeout task using the given {@link Timer} instance a timeout value. The
042         * task is not scheduled immediately. It will be scheduled by calling this
043         * task's {@link #schedule()} method.
044         * 
045         * @param timer
046         * @param timeout
047         */
048        public Timeout(Timer timer, long timeout) {
049            this.timeoutHandlers = new LinkedList<TimeoutHandler>();
050            this.timeout = timeout;
051            this.timer = timer;
052        }
053    
054        /**
055         * Returns the list of timeout handlers that have been registered for
056         * notification.
057         * 
058         * @return the list of timeout handlers
059         */
060        public List<TimeoutHandler> getTimeoutHandlers() {
061            return timeoutHandlers;
062        }
063        
064        /**
065         * Appends a new timeout handler at the end of the timeout handler list.
066         * 
067         * @param handler a timeout handler.
068         */
069        public void addTimeoutHandler(TimeoutHandler handler) {
070            timeoutHandlers.add(handler);
071        }
072        
073        /**
074         * inserts a new timeout handler at the beginning of the timeout handler
075         * list.
076         * 
077         * @param handler a timeout handler.
078         */
079        public void addTimeoutHandlerFirst(TimeoutHandler handler) {
080            timeoutHandlers.add(0, handler);
081        }
082        
083        /**
084         * Removes all timeout handlers from the timeout handler list. 
085         */
086        public void clearTimeoutHandlers() {
087            this.timeoutHandlers.clear();
088        }
089        
090        /**
091         * Schedules this timeout task.
092         */
093        public void schedule() {
094            timer.schedule(this, timeout);
095        }
096    
097        /**
098         * Notifies all timeout handlers about the scheduled timeout.
099         */
100        @Override
101        public void run() {
102            for (TimeoutHandler observer : timeoutHandlers) {
103                observer.timeout(this);
104            }
105        }
106    
107    }