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