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 org.apache.activemq.broker.region.MessageReference;
020    import org.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    
023    import java.io.IOException;
024    import java.util.HashMap;
025    import java.util.Iterator;
026    import java.util.LinkedList;
027    
028    
029    /**
030     * An eviction strategy which evicts the oldest message within messages with the same property value
031     *
032     *
033     * @org.apache.xbean.XBean
034     *
035     */
036    public class UniquePropertyMessageEvictionStrategy extends MessageEvictionStrategySupport {
037    
038        private static final Logger LOG = LoggerFactory.getLogger(UniquePropertyMessageEvictionStrategy.class);
039    
040        protected String propertyName;
041    
042        public String getPropertyName() {
043            return propertyName;
044        }
045    
046        public void setPropertyName(String propertyName) {
047            this.propertyName = propertyName;
048        }
049    
050        @Override
051        public MessageReference[] evictMessages(LinkedList messages) throws IOException {
052            MessageReference oldest = (MessageReference)messages.getFirst();
053            HashMap<Object, MessageReference> pivots = new HashMap<Object, MessageReference>();
054            Iterator iter = messages.iterator();
055    
056            for (int i = 0; iter.hasNext(); i++) {
057                MessageReference reference = (MessageReference) iter.next();
058                if (propertyName != null && reference.getMessage().getProperty(propertyName) != null) {
059                    Object key = reference.getMessage().getProperty(propertyName);
060                    if (pivots.containsKey(key)) {
061                        MessageReference pivot = pivots.get(key);
062                        if (reference.getMessage().getTimestamp() > pivot.getMessage().getTimestamp()) {
063                             pivots.put(key, reference);
064                        }
065                    } else {
066                        pivots.put(key, reference);
067                    }
068                }
069            }
070    
071            if (!pivots.isEmpty()) {
072                for (MessageReference ref : pivots.values()) {
073                    messages.remove(ref);
074                }
075    
076                if (messages.size() != 0) {
077                    return (MessageReference[])messages.toArray(new MessageReference[messages.size()]);
078                }
079            }
080            return new MessageReference[] {oldest};
081    
082        }
083    }