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.console.filter;
018    
019    import java.io.IOException;
020    import java.net.MalformedURLException;
021    import java.util.Arrays;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import javax.management.MBeanServerConnection;
026    import javax.management.ObjectName;
027    import javax.management.openmbean.CompositeData;
028    import javax.management.remote.JMXConnector;
029    import javax.management.remote.JMXConnectorFactory;
030    import javax.management.remote.JMXServiceURL;
031    
032    public class MessagesQueryFilter extends AbstractQueryFilter {
033    
034        private MBeanServerConnection jmxConnection;
035        private ObjectName destName;
036    
037        /**
038         * Create a JMS message query filter
039         * 
040         * @param jmxConnection - JMX connection to use
041         * @param destName - object name query to retrieve the destination
042         */
043        public MessagesQueryFilter(MBeanServerConnection jmxConnection, ObjectName destName) {
044            super(null);
045            this.jmxConnection = jmxConnection;
046            this.destName = destName;
047        }
048    
049        /**
050         * Queries the specified destination using the message selector format query
051         * 
052         * @param queries - message selector queries
053         * @return list messages that matches the selector
054         * @throws Exception
055         */
056        public List query(List queries) throws Exception {
057            String selector = "";
058    
059            // Convert to message selector
060            for (Iterator i = queries.iterator(); i.hasNext();) {
061                selector = selector + "(" + i.next().toString() + ") AND ";
062            }
063    
064            // Remove last AND
065            if (!selector.equals("")) {
066                selector = selector.substring(0, selector.length() - 5);
067            }
068    
069            return queryMessages(selector);
070        }
071    
072        /**
073         * Query the messages of a queue destination using JMX
074         * 
075         * @param selector - message selector
076         * @return list of messages that matches the selector
077         * @throws Exception
078         */
079        protected List queryMessages(String selector) throws Exception {
080            CompositeData[] messages = (CompositeData[]) jmxConnection.invoke(destName, "browse", new Object[] {selector}, new String[] {});
081            return Arrays.asList(messages);
082        }
083    
084    }