KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > MessagesQueryFilter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.filter;
19
20 import javax.management.remote.JMXConnector JavaDoc;
21 import javax.management.remote.JMXServiceURL JavaDoc;
22 import javax.management.remote.JMXConnectorFactory JavaDoc;
23 import javax.management.MBeanServerConnection JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import javax.management.openmbean.CompositeData JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 public class MessagesQueryFilter extends AbstractQueryFilter {
33
34     private JMXServiceURL JavaDoc jmxServiceUrl;
35     private ObjectName JavaDoc destName;
36
37     /**
38      * Create a JMS message query filter
39      * @param jmxServiceUrl - JMX service URL to connect to
40      * @param destName - object name query to retrieve the destination
41      */

42     public MessagesQueryFilter(JMXServiceURL JavaDoc jmxServiceUrl, ObjectName JavaDoc destName) {
43         super(null);
44         this.jmxServiceUrl = jmxServiceUrl;
45         this.destName = destName;
46     }
47
48     /**
49      * Queries the specified destination using the message selector format query
50      * @param queries - message selector queries
51      * @return list messages that matches the selector
52      * @throws Exception
53      */

54     public List JavaDoc query(List JavaDoc queries) throws Exception JavaDoc {
55         String JavaDoc selector = "";
56
57         // Convert to message selector
58
for (Iterator JavaDoc i=queries.iterator(); i.hasNext();) {
59             selector = selector + "(" + i.next().toString() + ") AND ";
60         }
61
62         // Remove last AND
63
if (selector != "") {
64             selector = selector.substring(0, selector.length() - 5);
65         }
66
67         return queryMessages(selector);
68     }
69
70     /**
71      * Query the messages of a queue destination using JMX
72      * @param selector - message selector
73      * @return list of messages that matches the selector
74      * @throws Exception
75      */

76     protected List JavaDoc queryMessages(String JavaDoc selector) throws Exception JavaDoc {
77         JMXConnector JavaDoc connector = createJmxConnector();
78         MBeanServerConnection JavaDoc server = connector.getMBeanServerConnection();
79         CompositeData JavaDoc[] messages = (CompositeData JavaDoc[])server.invoke(destName, "browse", new Object JavaDoc[] {}, new String JavaDoc[] {});
80         connector.close();
81
82         return Arrays.asList(messages);
83     }
84
85     /**
86      * Get the JMX service URL the query is connecting to.
87      * @return JMX service URL
88      */

89     public JMXServiceURL JavaDoc getJmxServiceUrl() {
90         return jmxServiceUrl;
91     }
92
93     /**
94      * Sets the JMX service URL the query is going to connect to.
95      * @param jmxServiceUrl - new JMX service URL
96      */

97     public void setJmxServiceUrl(JMXServiceURL JavaDoc jmxServiceUrl) {
98         this.jmxServiceUrl = jmxServiceUrl;
99     }
100
101     /**
102      * Sets the JMX service URL the query is going to connect to.
103      * @param jmxServiceUrl - new JMX service URL
104      */

105     public void setJmxServiceUrl(String JavaDoc jmxServiceUrl) throws MalformedURLException JavaDoc {
106         setJmxServiceUrl(new JMXServiceURL JavaDoc(jmxServiceUrl));
107     }
108
109     /**
110      * Creates a JMX connector
111      * @return JMX connector
112      * @throws java.io.IOException
113      */

114     protected JMXConnector JavaDoc createJmxConnector() throws IOException JavaDoc {
115         return JMXConnectorFactory.connect(getJmxServiceUrl());
116     }
117 }
118
Popular Tags