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.activemq.broker.region.policy;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import org.apache.activemq.broker.ConnectionContext;
022    import org.apache.activemq.broker.region.MessageReference;
023    import org.apache.activemq.broker.region.SubscriptionRecovery;
024    import org.apache.activemq.broker.region.Topic;
025    import org.apache.activemq.command.ActiveMQDestination;
026    import org.apache.activemq.command.Message;
027    import org.apache.activemq.filter.DestinationFilter;
028    
029    /**
030     * This implementation of {@link SubscriptionRecoveryPolicy} will only keep the
031     * last message.
032     * 
033     * @org.apache.xbean.XBean
034     * @version $Revision$
035     */
036    public class LastImageSubscriptionRecoveryPolicy implements SubscriptionRecoveryPolicy {
037    
038        private volatile MessageReference lastImage;
039    
040        public boolean add(ConnectionContext context, MessageReference node) throws Exception {
041            lastImage = node;
042            return true;
043        }
044    
045        public void recover(ConnectionContext context, Topic topic, SubscriptionRecovery sub) throws Exception {
046            // Re-dispatch the last message seen.
047            MessageReference node = lastImage;
048            if (node != null) {
049                sub.addRecoveredMessage(context, node);
050            }
051        }
052    
053        public void start() throws Exception {
054        }
055    
056        public void stop() throws Exception {
057        }
058    
059        public Message[] browse(ActiveMQDestination destination) throws Exception {
060            List<Message> result = new ArrayList<Message>();
061            DestinationFilter filter = DestinationFilter.parseFilter(destination);
062            if (filter.matches(lastImage.getMessage().getDestination())) {
063                result.add(lastImage.getMessage());
064            }
065            return result.toArray(new Message[result.size()]);
066        }
067    
068        public SubscriptionRecoveryPolicy copy() {
069            return new LastImageSubscriptionRecoveryPolicy();
070        }
071    
072    }