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 JMXServiceURL jmxServiceUrl;
035 private ObjectName destName;
036
037 /**
038 * Create a JMS message query filter
039 *
040 * @param jmxServiceUrl - JMX service URL to connect to
041 * @param destName - object name query to retrieve the destination
042 */
043 public MessagesQueryFilter(JMXServiceURL jmxServiceUrl, ObjectName destName) {
044 super(null);
045 this.jmxServiceUrl = jmxServiceUrl;
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 JMXConnector connector = createJmxConnector();
081 MBeanServerConnection server = connector.getMBeanServerConnection();
082 CompositeData[] messages = (CompositeData[])server.invoke(destName, "browse", new Object[] {}, new String[] {});
083 connector.close();
084
085 return Arrays.asList(messages);
086 }
087
088 /**
089 * Get the JMX service URL the query is connecting to.
090 *
091 * @return JMX service URL
092 */
093 public JMXServiceURL getJmxServiceUrl() {
094 return jmxServiceUrl;
095 }
096
097 /**
098 * Sets the JMX service URL the query is going to connect to.
099 *
100 * @param jmxServiceUrl - new JMX service URL
101 */
102 public void setJmxServiceUrl(JMXServiceURL jmxServiceUrl) {
103 this.jmxServiceUrl = jmxServiceUrl;
104 }
105
106 /**
107 * Sets the JMX service URL the query is going to connect to.
108 *
109 * @param jmxServiceUrl - new JMX service URL
110 */
111 public void setJmxServiceUrl(String jmxServiceUrl) throws MalformedURLException {
112 setJmxServiceUrl(new JMXServiceURL(jmxServiceUrl));
113 }
114
115 /**
116 * Creates a JMX connector
117 *
118 * @return JMX connector
119 * @throws java.io.IOException
120 */
121 protected JMXConnector createJmxConnector() throws IOException {
122 return JMXConnectorFactory.connect(getJmxServiceUrl());
123 }
124 }