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.store; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.apache.activemq.command.ActiveMQDestination; 024import org.apache.activemq.command.ActiveMQQueue; 025import org.apache.activemq.command.ActiveMQTopic; 026import org.apache.activemq.command.SubscriptionInfo; 027 028/** 029 * Used to implement common PersistenceAdapter methods. 030 */ 031public class PersistenceAdapterSupport { 032 033 private static final DestinationMatcher MATCH_ALL = new AlwaysMatches(); 034 035 /** 036 * Provides an interface for a Destination matching object that can be used to 037 * search for specific destinations from a persistence adapter. 038 */ 039 public interface DestinationMatcher { 040 041 /** 042 * Given a Destination object, return true if the destination matches some defined 043 * search criteria, false otherwise. 044 * 045 * @param destination 046 * the destination to inspect. 047 * 048 * @return true if the destination matches the target criteria, false otherwise. 049 */ 050 boolean matches(ActiveMQDestination destination); 051 052 } 053 054 /** 055 * Searches the set of subscriptions from the given persistence adapter and returns all those 056 * that belong to the given ClientId value. 057 * 058 * @param adapter 059 * the persistence adapter instance to search within. 060 * @param clientId 061 * the client ID value used to filter the subscription set. 062 * 063 * @return a list of all subscriptions belonging to the given client. 064 * 065 * @throws IOException if an error occurs while listing the stored subscriptions. 066 */ 067 static public List<SubscriptionInfo> listSubscriptions(PersistenceAdapter adapter, String clientId) throws IOException { 068 ArrayList<SubscriptionInfo> rc = new ArrayList<SubscriptionInfo>(); 069 for (ActiveMQDestination destination : adapter.getDestinations()) { 070 if (destination.isTopic()) { 071 TopicMessageStore store = adapter.createTopicMessageStore((ActiveMQTopic) destination); 072 for (SubscriptionInfo sub : store.getAllSubscriptions()) { 073 if (clientId == sub.getClientId() || clientId.equals(sub.getClientId())) { 074 rc.add(sub); 075 } 076 } 077 } 078 } 079 return rc; 080 } 081 082 /** 083 * Provides a means of querying the persistence adapter for a list of ActiveMQQueue instances. 084 * 085 * @param adapter 086 * the persistence adapter instance to query. 087 * 088 * @return a List<ActiveMQQeue> with all the queue destinations. 089 * 090 * @throws IOException if an error occurs while reading the destinations. 091 */ 092 static public List<ActiveMQQueue> listQueues(PersistenceAdapter adapter) throws IOException { 093 return listQueues(adapter, MATCH_ALL); 094 } 095 096 /** 097 * Provides a means of querying the persistence adapter for a list of ActiveMQQueue instances 098 * that match some given search criteria. 099 * 100 * @param adapter 101 * the persistence adapter instance to query. 102 * @param matcher 103 * the DestinationMatcher instance used to find the target destinations. 104 * 105 * @return a List<ActiveMQQeue> with all the matching destinations. 106 * 107 * @throws IOException if an error occurs while reading the destinations. 108 */ 109 static public List<ActiveMQQueue> listQueues(PersistenceAdapter adapter, DestinationMatcher matcher) throws IOException { 110 ArrayList<ActiveMQQueue> rc = new ArrayList<ActiveMQQueue>(); 111 for (ActiveMQDestination destination : adapter.getDestinations()) { 112 if (destination.isQueue() && matcher.matches(destination)) { 113 rc.add((ActiveMQQueue) destination); 114 } 115 } 116 return rc; 117 } 118 119 /** 120 * Provides a means of querying the persistence adapter for a list of ActiveMQTopic instances. 121 * 122 * @param adapter 123 * the persistence adapter instance to query. 124 * 125 * @return a List<ActiveMQTopic> with all the topic destinations. 126 * 127 * @throws IOException if an error occurs while reading the destinations. 128 */ 129 static public List<ActiveMQTopic> listTopics(PersistenceAdapter adapter) throws IOException { 130 return listTopics(adapter, MATCH_ALL); 131 } 132 133 /** 134 * Provides a means of querying the persistence adapter for a list of ActiveMQTopic instances 135 * that match some given search criteria. 136 * 137 * @param adapter 138 * the persistence adapter instance to query. 139 * @param matcher 140 * the DestinationMatcher instance used to find the target destinations. 141 * 142 * @return a List<ActiveMQTopic> with all the matching destinations. 143 * 144 * @throws IOException if an error occurs while reading the destinations. 145 */ 146 static public List<ActiveMQTopic> listTopics(PersistenceAdapter adapter, DestinationMatcher matcher) throws IOException { 147 ArrayList<ActiveMQTopic> rc = new ArrayList<ActiveMQTopic>(); 148 for (ActiveMQDestination destination : adapter.getDestinations()) { 149 if (destination.isTopic() && matcher.matches(destination)) { 150 rc.add((ActiveMQTopic) destination); 151 } 152 } 153 return rc; 154 } 155 156 private static class AlwaysMatches implements DestinationMatcher { 157 158 @Override 159 public boolean matches(ActiveMQDestination destination) { 160 return true; 161 } 162 } 163}