Return-Path: Delivered-To: apmail-geronimo-activemq-commits-archive@www.apache.org Received: (qmail 92694 invoked from network); 24 Jan 2006 10:27:44 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 24 Jan 2006 10:27:44 -0000 Received: (qmail 75694 invoked by uid 500); 24 Jan 2006 10:27:44 -0000 Delivered-To: apmail-geronimo-activemq-commits-archive@geronimo.apache.org Received: (qmail 75662 invoked by uid 500); 24 Jan 2006 10:27:44 -0000 Mailing-List: contact activemq-commits-help@geronimo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: activemq-dev@geronimo.apache.org Delivered-To: mailing list activemq-commits@geronimo.apache.org Received: (qmail 75653 invoked by uid 99); 24 Jan 2006 10:27:43 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Jan 2006 02:27:43 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 24 Jan 2006 02:27:40 -0800 Received: (qmail 92247 invoked by uid 65534); 24 Jan 2006 10:27:13 -0000 Message-ID: <20060124102713.92115.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r371880 [2/3] - in /incubator/activemq/trunk: activemq-core/src/main/java/org/apache/activemq/broker/ activemq-core/src/main/java/org/apache/activemq/broker/console/ activemq-core/src/main/java/org/apache/activemq/broker/console/command/ ac... Date: Tue, 24 Jan 2006 10:24:03 -0000 To: activemq-commits@geronimo.apache.org From: aco@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansAttributeQueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansAttributeQueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansAttributeQueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansAttributeQueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,148 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import javax.management.remote.JMXServiceURL; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXConnector; +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import javax.management.MBeanServerConnection; +import javax.management.ReflectionException; +import javax.management.InstanceNotFoundException; +import javax.management.AttributeList; +import javax.management.Attribute; +import javax.management.MBeanAttributeInfo; +import javax.management.IntrospectionException; +import java.util.Set; +import java.util.Collection; +import java.util.Iterator; +import java.util.ArrayList; +import java.util.List; +import java.io.IOException; + +public class MBeansAttributeQueryFilter extends AbstractQueryFilter { + public static final String KEY_OBJECT_NAME_ATTRIBUTE = "Attribute:ObjectName:"; + + private JMXServiceURL jmxServiceUrl; + private Set attribView; + + /** + * Create an mbean attributes query filter that is able to select specific mbean attributes based on the object name to get. + * @param jmxServiceUrl - JMX service url to connect to. + * @param attribView - the attributes to extract + * @param next - the next query filter + */ + public MBeansAttributeQueryFilter(JMXServiceURL jmxServiceUrl, Set attribView, MBeansObjectNameQueryFilter next) { + super(next); + this.jmxServiceUrl = jmxServiceUrl; + this.attribView = attribView; + } + + /** + * Filter the query by retrieving the attributes specified, this will modify the collection to a list of AttributeList + * @param queries - query list + * @return List of AttributeList, which includes the ObjectName, which has a key of MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE + * @throws Exception + */ + public List query(List queries) throws Exception { + return getMBeanAttributesCollection(next.query(queries)); + } + + /** + * Retrieve the specified attributes of the mbean + * @param result - collection of ObjectInstances and/or ObjectNames + * @return List of AttributeList + * @throws IOException + * @throws ReflectionException + * @throws InstanceNotFoundException + * @throws NoSuchMethodException + */ + protected List getMBeanAttributesCollection(Collection result) throws IOException, ReflectionException, InstanceNotFoundException, NoSuchMethodException, IntrospectionException { + List mbeansCollection = new ArrayList(); + + for (Iterator i=result.iterator(); i.hasNext();) { + Object mbean = i.next(); + if (mbean instanceof ObjectInstance) { + mbeansCollection.add(getMBeanAttributes(((ObjectInstance)mbean).getObjectName(), attribView)); + } else if (mbean instanceof ObjectName) { + mbeansCollection.add(getMBeanAttributes((ObjectName)mbean, attribView)); + } else { + throw new NoSuchMethodException("Cannot get the mbean attributes for class: " + mbean.getClass().getName()); + } + } + + return mbeansCollection; + } + + /** + * Retrieve the specified attributes of the mbean + * @param obj - mbean ObjectInstance + * @param attrView - list of attributes to retrieve + * @return AttributeList for the mbean + * @throws ReflectionException + * @throws InstanceNotFoundException + * @throws IOException + */ + protected AttributeList getMBeanAttributes(ObjectInstance obj, Set attrView) throws ReflectionException, InstanceNotFoundException, IOException, IntrospectionException { + return getMBeanAttributes(obj.getObjectName(), attrView); + } + + /** + * Retrieve the specified attributes of the mbean + * @param objName - mbean ObjectName + * @param attrView - list of attributes to retrieve + * @return AttributeList for the mbean + * @throws IOException + * @throws ReflectionException + * @throws InstanceNotFoundException + */ + protected AttributeList getMBeanAttributes(ObjectName objName, Set attrView) throws IOException, ReflectionException, InstanceNotFoundException, IntrospectionException { + JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl); + MBeanServerConnection server = jmxConnector.getMBeanServerConnection(); + + // If no attribute view specified, get all attributes + String[] attribs; + if (attrView == null || attrView.isEmpty()) { + MBeanAttributeInfo[] infos = server.getMBeanInfo(objName).getAttributes(); + attribs = new String[infos.length]; + + for (int i=0; i= 0) { + val = key.substring(pos + 1); + key = key.substring(0, pos); + } + + if (val.startsWith(QUERY_EXP_PREFIX)) { + // do nothing as of the moment + } else if (key != "" && val != "") { + objNameQuery = objNameQuery + key + "=" + val + ","; + } + } + + // Append * to object name + objNameQuery = objNameQuery + "*"; + + return queryMBeans(new ObjectName(DEFAULT_JMX_DOMAIN + ":" + objNameQuery), queryExp); + } + + /** + * Advance query that enables you to specify both the object name and the query expression to use. + * Note: Query expression is currently unsupported. + * @param objName - object name to use for query + * @param queryExpStr - query expression string + * @return set of mbeans that matches the query + * @throws IOException - if there is a problem querying the JMX context + */ + protected List queryMBeans(ObjectName objName, String queryExpStr) throws IOException { + JMXConnector jmxConn = createJmxConnector(); + MBeanServerConnection server = jmxConn.getMBeanServerConnection(); + + QueryExp queryExp = createQueryExp(queryExpStr); + + // Convert mbeans set to list to make it standard throughout the query filter + List mbeans = new ArrayList(server.queryMBeans(objName, queryExp)); + + jmxConn.close(); + + return mbeans; + } + + /** + * Get the JMX service URL the query is connecting to. + * @return JMX service URL + */ + public JMXServiceURL getJmxServiceUrl() { + return jmxServiceUrl; + } + + /** + * Sets the JMX service URL the query is going to connect to. + * @param jmxServiceUrl - new JMX service URL + */ + public void setJmxServiceUrl(JMXServiceURL jmxServiceUrl) { + this.jmxServiceUrl = jmxServiceUrl; + } + + /** + * Sets the JMX service URL the query is going to connect to. + * @param jmxServiceUrl - new JMX service URL + */ + public void setJmxServiceUrl(String jmxServiceUrl) throws MalformedURLException { + setJmxServiceUrl(new JMXServiceURL(jmxServiceUrl)); + } + + /** + * Creates a JMX connector + * @return JMX connector + * @throws IOException + */ + protected JMXConnector createJmxConnector() throws IOException { + return JMXConnectorFactory.connect(getJmxServiceUrl()); + } + + /** + * Creates a query expression based on the query expression string + * Note: currently unsupported + * @param queryExpStr - query expression string + * @return the created query expression + */ + protected QueryExp createQueryExp(String queryExpStr) { + // Currently unsupported + return null; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansRegExQueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansRegExQueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansRegExQueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MBeansRegExQueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,127 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import javax.management.AttributeList; +import javax.management.Attribute; +import java.util.Map; +import java.util.Iterator; +import java.util.regex.Pattern; +import java.lang.reflect.Method; + +public class MBeansRegExQueryFilter extends RegExQueryFilter { + /** + * Creates a regular expression query that is able to match the values of specific mbeans + * @param next - next query filter + */ + public MBeansRegExQueryFilter(QueryFilter next) { + super(next); + } + + /** + * Try to match the object data using the regular expression map. The regex map contains a key-value mapping of an attribute + * key to a regular expression the value of the key should match. The basic rule of matching is that the data must contain + * a property key that is included in the regex map, and that the value of the property key should match the regex specified. + * @param data - object data to match + * @param regex - regex map + * @return true if the data matches the regex map specified + * @throws Exception + */ + protected boolean matches(Object data, Map regex) throws Exception { + // Use reflection to determine where the object should go + try { + Method method = this.getClass().getDeclaredMethod("matches", new Class[] {data.getClass(), Map.class}); + return ((Boolean)method.invoke(this, new Object[] {data, regex})).booleanValue(); + } catch (NoSuchMethodException e) { + return false; + } + } + + /** + * Try to match the object instance using the regular expression map + * @param data - object instance to match + * @param regex - regex map + * @return true if the object instance matches the regex map + */ + protected boolean matches(ObjectInstance data, Map regex) { + return matches(data.getObjectName(), regex); + } + + /** + * Try to match the object name instance using the regular expression map + * @param data - object name to match + * @param regex - regex map + * @return true if the object name matches the regex map + */ + protected boolean matches(ObjectName data, Map regex) { + for (Iterator i=regex.keySet().iterator(); i.hasNext();) { + String key = (String)i.next(); + String target = data.getKeyProperty(key); + + // Try to match the value of the property of the object name + if (target != null && !((Pattern)regex.get(key)).matcher(target).matches()) { + return false; + } + } + return true; + } + + /** + * Try to match the attribute list using teh regular expression map + * @param data - attribute list to match + * @param regex - regex map + * @return true if the attribute list matches the regex map + */ + protected boolean matches(AttributeList data, Map regex) { + for (Iterator i=regex.keySet().iterator(); i.hasNext();) { + String key = (String)i.next(); + + // Try to match each regex to the attributes of the mbean including its ObjectName + for (Iterator j=data.iterator(); j.hasNext();) { + Attribute attrib = (Attribute)j.next(); + + // Try to match to the properties of the ObjectName + if (attrib.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) { + String target = ((ObjectName)attrib.getValue()).getKeyProperty(key); + + if (target == null || !((Pattern)regex.get(key)).matcher(target).matches()) { + return false; + } else { + // If match skip to the next regex + break; + } + + // Try to match to the mbean attributes + } else if (attrib.getName().equals(key)) { + if (!((Pattern)regex.get(key)).matcher(attrib.getValue().toString()).matches()) { + return false; + } else { + // If match skip to the next regex + break; + } + + // If mbean does not contain the specified attribute + } else { + return false; + } + } + } + return true; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MapTransformFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MapTransformFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MapTransformFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MapTransformFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,259 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import org.apache.activemq.broker.console.formatter.GlobalWriter; +import org.apache.activemq.broker.console.AmqMessagesUtil; +import org.apache.activemq.command.ActiveMQDestination; +import org.apache.activemq.command.ActiveMQObjectMessage; +import org.apache.activemq.command.ActiveMQBytesMessage; +import org.apache.activemq.command.ActiveMQTextMessage; +import org.apache.activemq.command.ActiveMQMapMessage; +import org.apache.activemq.command.ActiveMQStreamMessage; +import org.apache.activemq.command.ActiveMQMessage; + +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import javax.management.AttributeList; +import javax.management.Attribute; +import javax.jms.JMSException; +import javax.jms.DeliveryMode; +import java.util.Map; +import java.util.Properties; +import java.util.Iterator; +import java.util.Enumeration; +import java.lang.reflect.Method; + +public class MapTransformFilter extends ResultTransformFilter { + /** + * Creates a Map transform filter that is able to transform a variety of objects to a properties map object + * @param next - the next query filter + */ + public MapTransformFilter(QueryFilter next) { + super(next); + } + + /** + * Transform the given object to a Map object + * @param object - object to transform + * @return map object + */ + protected Object transformElement(Object object) throws Exception { + // Use reflection to determine how the object should be transformed + try { + Method method = this.getClass().getDeclaredMethod("transformToMap", new Class[] {object.getClass()}); + return (Map)method.invoke(this, new Object[] {object}); + } catch (NoSuchMethodException e) { + GlobalWriter.print("Unable to transform mbean of type: " + object.getClass().getName() + ". No corresponding transformToMap method found."); + return null; + } + } + + /** + * Transform an ObjectInstance mbean to a Map + * @param obj - ObjectInstance format of an mbean + * @return map object + */ + protected Map transformToMap(ObjectInstance obj) { + return transformToMap(obj.getObjectName()); + } + + /** + * Transform an ObjectName mbean to a Map + * @param objname - ObjectName format of an mbean + * @return map object + */ + protected Map transformToMap(ObjectName objname) { + Properties props = new Properties(); + + // Parse object properties + Map objProps = objname.getKeyPropertyList(); + for (Iterator i=objProps.keySet().iterator(); i.hasNext();) { + Object key = i.next(); + Object val = objProps.get(key); + if (val != null) { + props.setProperty(key.toString(), val.toString()); + } + } + + return props; + } + + /** + * Transform an Attribute List format of an mbean to a Map + * @param list - AttributeList format of an mbean + * @return map object + */ + protected Map transformToMap(AttributeList list) { + Properties props = new Properties(); + for (Iterator i=list.iterator(); i.hasNext();) { + Attribute attrib = (Attribute)i.next(); + + // If attribute is an ObjectName + if (attrib.getName().equals(MBeansAttributeQueryFilter.KEY_OBJECT_NAME_ATTRIBUTE)) { + props.putAll(transformToMap((ObjectName)attrib.getValue())); + } else { + if (attrib.getValue() != null) { + props.setProperty(attrib.getName(), attrib.getValue().toString()); + } + } + } + + return props; + } + + /** + * Transform an ActiveMQTextMessage to a Map + * @param msg - text message to trasnform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQTextMessage msg) throws JMSException { + Properties props = new Properties(); + + props.putAll(transformToMap((ActiveMQMessage)msg)); + if (msg.getText() != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSText", msg.getText()); + } + + return props; + } + + /** + * Transform an ActiveMQBytesMessage to a Map + * @param msg - bytes message to transform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQBytesMessage msg) throws JMSException { + Properties props = new Properties(); + + props.putAll(transformToMap((ActiveMQMessage)msg)); + + long bodyLength = msg.getBodyLength(); + byte[] msgBody; + int i=0; + // Create separate bytes messages + for (i=0; i<(bodyLength/Integer.MAX_VALUE); i++) { + msgBody = new byte[Integer.MAX_VALUE]; + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSBytes:" + (i+1), new String(msgBody)); + } + msgBody = new byte[(int)(bodyLength % Integer.MAX_VALUE)]; + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSBytes:" + (i+1), new String(msgBody)); + + return props; + } + + /** + * Transform an ActiveMQMessage to a Map + * @param msg - object message to transform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQObjectMessage msg) throws JMSException { + Properties props = new Properties(); + + props.putAll(transformToMap((ActiveMQMessage)msg)); + if (msg.getObject() != null) { + // Just add the class name and toString value of the object + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSObjectClass", msg.getObject().getClass().getName()); + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSObjectString", msg.getObject().toString()); + } + return props; + } + + /** + * Transform an ActiveMQMapMessage to a Map + * @param msg - map message to transform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQMapMessage msg) throws JMSException { + Properties props = new Properties(); + + props.putAll(transformToMap((ActiveMQMessage)msg)); + + // Get map properties + Enumeration e = msg.getMapNames(); + while (e.hasMoreElements()) { + String key = (String)e.nextElement(); + Object val = msg.getObject(key); + if (val != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + key, val.toString()); + } + } + + return props; + } + + /** + * Transform an ActiveMQStreamMessage to a Map + * @param msg - stream message to transform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQStreamMessage msg) throws JMSException { + Properties props = new Properties(); + + props.putAll(transformToMap((ActiveMQMessage)msg)); + // Just set the toString of the message as the body of the stream message + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_BODY_PREFIX + "JMSStreamMessage", msg.toString()); + + return props; + } + + /** + * Transform an ActiveMQMessage to a Map + * @param msg - message to transform + * @return map object + * @throws JMSException + */ + protected Map transformToMap(ActiveMQMessage msg) throws JMSException { + Properties props = new Properties(); + + // Get JMS properties + if (msg.getJMSCorrelationID() != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSCorrelationID", msg.getJMSCorrelationID()); + } + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDeliveryMode", (msg.getJMSDeliveryMode()==DeliveryMode.PERSISTENT) ? "persistent" : "non-persistent"); + if (msg.getJMSDestination() != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSDestination", ((ActiveMQDestination)msg.getJMSDestination()).getPhysicalName()); + } + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSExpiration", Long.toString(msg.getJMSExpiration())); + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSMessageID", msg.getJMSMessageID()); + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSPriority", Integer.toString(msg.getJMSPriority())); + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSRedelivered", Boolean.toString(msg.getJMSRedelivered())); + if (msg.getJMSReplyTo() != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSReplyTo", ((ActiveMQDestination)msg.getJMSReplyTo()).getPhysicalName()); + } + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSTimestamp", Long.toString(msg.getJMSTimestamp())); + if (msg.getJMSType() != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_HEADER_PREFIX + "JMSType", msg.getJMSType()); + } + + // Get custom properties + Enumeration e = msg.getPropertyNames(); + while (e.hasMoreElements()) { + String name = (String)e.nextElement(); + if (msg.getObjectProperty(name) != null) { + props.setProperty(AmqMessagesUtil.JMS_MESSAGE_CUSTOM_PREFIX + name, msg.getObjectProperty(name).toString()); + } + } + + return props; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MessagesQueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MessagesQueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MessagesQueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/MessagesQueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,152 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import org.apache.activemq.command.ActiveMQTopic; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.ActiveMQConnectionFactory; + +import javax.jms.Destination; +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Session; +import javax.jms.QueueBrowser; +import java.net.URI; +import java.util.Collections; +import java.util.List; +import java.util.Iterator; + +public class MessagesQueryFilter extends AbstractQueryFilter { + + private URI brokerUrl; + private Destination destination; + + /** + * Create a JMS message query filter + * @param brokerUrl - broker url to connect to + * @param destination - JMS destination to query + */ + public MessagesQueryFilter(URI brokerUrl, Destination destination) { + super(null); + this.brokerUrl = brokerUrl; + this.destination = destination; + } + + /** + * Queries the specified destination using the message selector format query + * @param queries - message selector queries + * @return list messages that matches the selector + * @throws Exception + */ + public List query(List queries) throws Exception { + String selector = ""; + + // Convert to message selector + for (Iterator i=queries.iterator(); i.hasNext();) { + selector = selector + "(" + i.next().toString() + ") AND "; + } + + // Remove last AND + if (selector != "") { + selector = selector.substring(0, selector.length() - 5); + } + + if (destination instanceof ActiveMQQueue) { + return queryMessages((ActiveMQQueue)destination, selector); + } else { + return queryMessages((ActiveMQTopic)destination, selector); + } + } + + /** + * Query the messages of a queue destination using a queue browser + * @param queue - queue destination + * @param selector - message selector + * @return list of messages that matches the selector + * @throws Exception + */ + protected List queryMessages(ActiveMQQueue queue, String selector) throws Exception { + Connection conn = createConnection(getBrokerUrl()); + + Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); + QueueBrowser browser = sess.createBrowser(queue, selector); + + List messages = Collections.list(browser.getEnumeration()); + + conn.close(); + + return messages; + } + + /** + * Query the messages of a topic destination using a message consumer + * @param topic - topic destination + * @param selector - message selector + * @return list of messages that matches the selector + * @throws Exception + */ + protected List queryMessages(ActiveMQTopic topic, String selector) throws Exception { + // TODO: should we use a durable subscriber or a retroactive non-durable subscriber? + // TODO: if a durable subscriber is used, how do we manage it? subscribe/unsubscribe tasks? + return null; + } + + /** + * Create and start a JMS connection + * @param brokerUrl - broker url to connect to. + * @return JMS connection + * @throws JMSException + */ + protected Connection createConnection(URI brokerUrl) throws JMSException { + Connection conn = (new ActiveMQConnectionFactory(brokerUrl)).createConnection(); + conn.start(); + return conn; + } + + /** + * Get the broker url being used. + * @return broker url + */ + public URI getBrokerUrl() { + return brokerUrl; + } + + /** + * Set the broker url to use. + * @param brokerUrl - broker url + */ + public void setBrokerUrl(URI brokerUrl) { + this.brokerUrl = brokerUrl; + } + + /** + * Get the destination being used. + * @return - JMS destination + */ + public Destination getDestination() { + return destination; + } + + /** + * Set the destination to use. + * @param destination - JMS destination + */ + public void setDestination(Destination destination) { + this.destination = destination; + } + +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/PropertiesViewFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/PropertiesViewFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/PropertiesViewFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/PropertiesViewFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,118 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.Set; +import java.util.Iterator; +import java.util.Map; +import java.util.HashMap; +import java.util.Collection; +import java.util.ArrayList; +import java.util.List; + +public class PropertiesViewFilter implements QueryFilter { + protected QueryFilter next; + protected Set viewFilter; + + /** + * Creates a filter that will select the properties of a map object to view + * @param next - the next query filter that will return a collection of maps + */ + public PropertiesViewFilter(QueryFilter next) { + this(null, next); + } + + /** + * Creates a filter that will select the properties of a map object to view + * @param viewFilter - the properties to view + * @param next - the next query filter that will return a collection of maps + */ + public PropertiesViewFilter(Set viewFilter, QueryFilter next) { + this.next = next; + this.viewFilter = viewFilter; + } + + /** + * Filter the properties to view of the query result + * @param query - the query string + * @return list of objects that has been view filtered + */ + public List query(String query) throws Exception { + return filterViewCollection(next.query(query), viewFilter); + } + + /** + * Filter the properties to view of the query result + * @param queries - the query map + * @return list of objects that has been view filtered + * @throws Exception + */ + public List query(List queries) throws Exception { + return filterViewCollection(next.query(queries), viewFilter); + } + + /** + * Filter the view of each element in the collection + * @param result - the lists to filter the view from + * @param viewFilter - the views to select + * @return lsit of objects whose view has been filtered + */ + protected List filterViewCollection(Collection result, Set viewFilter) { + // Use a list to allow duplicate entries + List newCollection = new ArrayList(); + + for (Iterator i=result.iterator(); i.hasNext();) { + newCollection.add(filterView((Map)i.next())); + } + + return newCollection; + } + + /** + * Select only the attributes to view from the map data + * @param data - data to filter the view from + * @return - data with the view filtered + */ + protected Map filterView(Map data) { + // If no view specified, display all attributes + if (viewFilter == null || viewFilter.isEmpty()) { + return data; + } + + Map newData; + try { + // Lets try to use the same class as the original + newData = (Map)data.getClass().newInstance(); + } catch (Exception e) { + // Lets use a default HashMap + newData = new HashMap(); + } + + // Filter the keys to view + for (Iterator i=viewFilter.iterator(); i.hasNext();) { + Object key = i.next(); + Object val = data.get(key); + + if (val != null) { + newData.put(key, val); + } + } + + return newData; + } + +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/QueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/QueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/QueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/QueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,39 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.List; + +public interface QueryFilter { + public static final String QUERY_DELIMETER = ","; + + /** + * Interface for querying + * @param queryStr - the query string + * @return collection of objects that satisfies the query + * @throws Exception + */ + public List query(String queryStr) throws Exception; + + /** + * Interface for querying + * @param queries - list of individual queries + * @return collection of objects that satisfies the query + * @throws Exception + */ + public List query(List queries) throws Exception; +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/RegExQueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/RegExQueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/RegExQueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/RegExQueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,128 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.regex.Pattern; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Iterator; + +public abstract class RegExQueryFilter extends AbstractQueryFilter { + public static final String REGEX_PREFIX = "REGEX:QUERY:"; + + /** + * Creates a regular expression query that is able to match an object using key-value pattern regex filtering + * @param next + */ + protected RegExQueryFilter(QueryFilter next) { + super(next); + } + + /** + * Separates the regular expressions queries from the usual queries. A query is a regex query, if it is key-value pair + * with the format =, and value is a pattern that satisfies the isRegularExpression method. + * @param queries - list of queries + * @return filtered objects that matches the regex query + * @throws Exception + */ + public List query(List queries) throws Exception { + Map regex = new HashMap(); + List newQueries = new ArrayList(); + + // Lets parse for regular expression queries + for (Iterator i=queries.iterator(); i.hasNext();) { + // Get key-value pair + String token = (String)i.next(); + String key = ""; + String val = ""; + int pos = token.indexOf("="); + if (pos >= 0) { + val = token.substring(pos + 1); + key = token.substring(0, pos); + } + + // Add the regex query to list and make it a non-factor in the succeeding queries + if (isRegularExpression(val)) { + regex.put(key, compileQuery(val)); + + // Add the normal query to the query list + } else { + newQueries.add(token); + } + } + + // Filter the result using the regular expressions specified + return filterCollectionUsingRegEx(regex, next.query(newQueries)); + } + + /** + * Checks if a given string is a regular expression query. Currently, a pattern is a regex query, if it starts with + * the RegExQueryFilter.REGEX_PREFIX. + * @param query + * @return + */ + protected boolean isRegularExpression(String query) { + return query.startsWith(REGEX_PREFIX); + } + + /** + * Compiles the regex query to a pattern. + * @param query - query string to compile + * @return regex pattern + */ + protected Pattern compileQuery(String query) { + return Pattern.compile(query.substring(REGEX_PREFIX.length())); + } + + /** + * Filter the specified colleciton using the regex patterns extracted. + * @param regex - regex map + * @param data - list of objects to filter + * @return filtered list of objects that matches the regex map + * @throws Exception + */ + protected List filterCollectionUsingRegEx(Map regex, List data) throws Exception { + // No regular expressions filtering needed + if (regex==null || regex.isEmpty()) { + return data; + } + + List filteredElems = new ArrayList(); + + // Get each data object to filter + for (Iterator i=data.iterator(); i.hasNext();) { + Object dataElem = i.next(); + // If properties of data matches all the regex pattern, add it + if (matches(dataElem, regex)) { + filteredElems.add(dataElem); + } + } + + return filteredElems; + } + + /** + * Determines how the object is to be matched to the regex map. + * @param data - object to match + * @param regex - regex map + * @return true, if the object matches the regex map, false otherwise + * @throws Exception + */ + protected abstract boolean matches(Object data, Map regex) throws Exception; +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/ResultTransformFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/ResultTransformFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/ResultTransformFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/ResultTransformFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,76 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public abstract class ResultTransformFilter implements QueryFilter { + private QueryFilter next; + + /** + * Contructs a query filter that transform the format of the query result + * @param next - the query filter to retrieve the results from + */ + protected ResultTransformFilter(QueryFilter next) { + this.next = next; + } + + /** + * Transforms the queried results to a collection of different objects + * @param query - the query string + * @return collections of transformed objects + * @throws Exception + */ + public List query(String query) throws Exception { + return transformList(next.query(query)); + } + + /** + * Transforms the queried results to a collection of different objects + * @param queries - the query map + * @return collections of transformed objects + * @throws Exception + */ + public List query(List queries) throws Exception { + return transformList(next.query(queries)); + } + + /** + * Transforms a collection to a collection of different objects. + * @param result - the collection to transform + * @return collection of properties objects + */ + protected List transformList(List result) throws Exception { + List props = new ArrayList(); + + for (Iterator i=result.iterator(); i.hasNext();) { + props.add(transformElement(i.next())); + } + + return props; + } + + /** + * Transform a result object + * @param obj - the object instance to transform + * @return the transformed object + */ + protected abstract Object transformElement(Object obj) throws Exception; + +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/StubQueryFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/StubQueryFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/StubQueryFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/StubQueryFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,51 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.List; + +public class StubQueryFilter implements QueryFilter{ + private List data; + + /** + * Creates a stub query that returns the given collections as the query result + * @param data - the stub query result + */ + public StubQueryFilter(List data) { + this.data = data; + } + + /** + * Returns the provided stub data as a stub query result + * @param queryStr - not use + * @return the stub query result + * @throws Exception + */ + public List query(String queryStr) throws Exception { + return data; + } + + /** + * Returns the provided stub data as a stub query result + * @param queries - not use + * @return the stub query result + * @throws Exception + */ + public List query(List queries) throws Exception { + return data; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToMsgSelectorTransformFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToMsgSelectorTransformFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToMsgSelectorTransformFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToMsgSelectorTransformFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,68 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +public class WildcardToMsgSelectorTransformFilter extends WildcardTransformFilter{ + /** + * Creates a filter that is able to transform a wildcard query to a message selector format + * @param next - next query filter + */ + public WildcardToMsgSelectorTransformFilter(QueryFilter next) { + super(next); + } + + /** + * Use to determine if a query string is a wildcard query. A query string is a wildcard query if it is a key-value + * pair with the format = and the value is enclosed in '' and contains '*' and '?'. + * @param query - query string + * @return true, if the query string is a wildcard query, false otherwise + */ + protected boolean isWildcardQuery(String query) { + // If the query is a key=value pair + String key = query; + String val = ""; + int pos = key.indexOf("="); + if (pos >= 0) { + val = key.substring(pos + 1); + key = key.substring(0, pos); + } + + // If the value contains wildcards and is enclose by ' + return val.startsWith("'") && val.endsWith("'") && ((val.indexOf("*") >= 0) || (val.indexOf("?") >= 0)); + } + + /** + * Transform a wildcard query to message selector format + * @param query - query string to transform + * @return message selector format string + */ + protected String transformWildcardQuery(String query) { + // If the query is a key=value pair + String key = query; + String val = ""; + int pos = key.indexOf("="); + if (pos >= 0) { + val = key.substring(pos + 1); + key = key.substring(0, pos); + } + + val = val.replaceAll("[?]", "_"); + val = val.replaceAll("[*]", "%"); + + return key + " LIKE " + val; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToRegExTransformFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToRegExTransformFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToRegExTransformFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardToRegExTransformFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,71 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +public class WildcardToRegExTransformFilter extends WildcardTransformFilter { + /** + * Creates a filter that is able to transform a wildcard query to a regular expression query string + * @param next - next query filter + */ + public WildcardToRegExTransformFilter(RegExQueryFilter next) { + super(next); + } + + /** + * Use to determine if a query string is a wildcard query. A query string is a wildcard query if it is a key-value + * pair with the format = and the value contains '*' and '?'. + * @param query - query string + * @return true, if the query string is a wildcard query, false otherwise + */ + protected boolean isWildcardQuery(String query) { + // If the query is a key=value pair + String key = query; + String val = ""; + int pos = key.indexOf("="); + if (pos >= 0) { + val = key.substring(pos + 1); + key = key.substring(0, pos); + } + + // If the value contains wildcards + return ((val.indexOf("*") >= 0) || (val.indexOf("?") >= 0)); + } + + /** + * Transform a wildcard query to regular expression format + * @param query - query string to transform + * @return regex query string + */ + protected String transformWildcardQuery(String query) { + // Get the key=value pair + String key = query; + String val = ""; + int pos = key.indexOf("="); + if (pos >= 0) { + val = key.substring(pos + 1); + key = key.substring(0, pos); + } + + val = val.replaceAll("[.]", "\\\\."); // Escape all dot characters. From (.) to (\.) + val = val.replaceAll("[?]", "."); // Match single characters + val = val.replaceAll("[*]", ".*?"); // Match all characters, use reluctant quantifier + val = "(" + val +")"; // Let's group the query for clarity + val = RegExQueryFilter.REGEX_PREFIX + val; // Flag as a regular expression query + + return key + "=" + val; + } +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardTransformFilter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardTransformFilter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardTransformFilter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/filter/WildcardTransformFilter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,73 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.filter; + +import java.util.Iterator; +import java.util.ArrayList; +import java.util.List; + +public abstract class WildcardTransformFilter extends AbstractQueryFilter { + + /** + * Creates a wildcard transform filter that is able to convert a wildcard expression (determined by isWildcardQuery) + * to a another query type (use transformWildcardQuery). + * @param next - the next query filter + */ + protected WildcardTransformFilter(QueryFilter next) { + super(next); + } + + /** + * Converts the query list to set of different queries + * @param queries - query list to transform + * @return - result of the query + * @throws Exception + */ + public List query(List queries) throws Exception { + List newQueries = new ArrayList(); + + for (Iterator i=queries.iterator(); i.hasNext();) { + String queryToken = (String)i.next(); + + // Transform the wildcard query + if (isWildcardQuery(queryToken)) { + // Transform the value part only + newQueries.add(transformWildcardQuery(queryToken)); + + // Maintain the query as is + } else { + newQueries.add(queryToken); + } + } + + return next.query(newQueries); + } + + /** + * Use to determine is a query string is a wildcard query + * @param query - query string + * @return true, if the query string is a wildcard query, false otherwise + */ + protected abstract boolean isWildcardQuery(String query); + + /** + * Use to transform a wildcard query string to another query format + * @param query - query string to transform + * @return transformed query + */ + protected abstract String transformWildcardQuery(String query); +} Added: incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/formatter/CommandShellOutputFormatter.java URL: http://svn.apache.org/viewcvs/incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/formatter/CommandShellOutputFormatter.java?rev=371880&view=auto ============================================================================== --- incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/formatter/CommandShellOutputFormatter.java (added) +++ incubator/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/broker/console/formatter/CommandShellOutputFormatter.java Tue Jan 24 02:23:34 2006 @@ -0,0 +1,243 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.console.formatter; + +import javax.management.ObjectInstance; +import javax.management.ObjectName; +import javax.management.AttributeList; +import javax.management.Attribute; +import javax.jms.Message; +import java.util.Map; +import java.util.Collection; +import java.util.Iterator; +import java.io.PrintStream; +import java.io.OutputStream; + +public class CommandShellOutputFormatter implements OutputFormatter { + private OutputStream outputStream; + private PrintStream out; + + public CommandShellOutputFormatter(OutputStream out) { + + this.outputStream = out; + if (out instanceof PrintStream) { + this.out = (PrintStream)out; + } else { + this.out = new PrintStream(out); + } + } + + /** + * Retrieve the output stream being used by the formatter + * @return + */ + public OutputStream getOutputStream() { + return outputStream; + } + + /** + * Print an ObjectInstance format of an mbean + * @param mbean - mbean to print + */ + public void printMBean(ObjectInstance mbean) { + printMBean(mbean.getObjectName()); + } + + /** + * Print an ObjectName format of an mbean + * @param mbean - mbean to print + */ + public void printMBean(ObjectName mbean) { + printMBean(mbean.getKeyPropertyList()); + } + + /** + * Print an AttributeList format of an mbean + * @param mbean - mbean to print + */ + public void printMBean(AttributeList mbean) { + for (Iterator i=mbean.iterator(); i.hasNext();) { + Attribute attrib = (Attribute)i.next(); + if (attrib.getValue() instanceof ObjectName) { + printMBean((ObjectName)attrib.getValue()); + } else if (attrib.getValue() instanceof ObjectInstance) { + printMBean((ObjectInstance)attrib.getValue()); + } else { + out.println(attrib.getName() + " = " + attrib.getValue().toString()); + out.println(); + } + } + } + + /** + * Print a Map format of an mbean + * @param mbean - mbean to print + */ + public void printMBean(Map mbean) { + for (Iterator i=mbean.keySet().iterator(); i.hasNext();) { + String key = (String)i.next(); + String val = mbean.get(key).toString(); + out.println(key + " = " + val); + } + out.println(); + } + + /** + * Print a collection of mbean + * @param mbean - collection of mbeans + */ + public void printMBean(Collection mbean) { + for (Iterator i=mbean.iterator(); i.hasNext();) { + Object obj = i.next(); + if (obj instanceof ObjectInstance) { + printMBean((ObjectInstance)obj); + } else if (obj instanceof ObjectName) { + printMBean((ObjectName)obj); + } else if (obj instanceof Map) { + printMBean((Map)obj); + } else if (obj instanceof AttributeList) { + printMBean((AttributeList)obj); + } else if (obj instanceof Collection) { + printMessage((Collection)obj); + } else { + printException(new UnsupportedOperationException("Unknown mbean type: " + obj.getClass().getName())); + } + } + } + + /** + * Print a Map format of a JMS message + * @param msg + */ + public void printMessage(Map msg) { + for (Iterator i=msg.keySet().iterator(); i.hasNext();) { + String key = (String)i.next(); + String val = msg.get(key).toString(); + out.println(key + " = " + val); + } + out.println(); + } + + /** + * Print a Message format of a JMS message + * @param msg - JMS message to print + */ + public void printMessage(Message msg) { + // TODO + } + + /** + * Print a collection of JMS messages + * @param msg - collection of JMS messages + */ + public void printMessage(Collection msg) { + for (Iterator i=msg.iterator(); i.hasNext();) { + Object obj = i.next(); + if (obj instanceof Message) { + printMessage((Message)obj); + } else if (obj instanceof Map) { + printMessage((Map)obj); + } else if (obj instanceof Collection) { + printMessage((Collection)obj); + } else { + printException(new UnsupportedOperationException("Unknown message type: " + obj.getClass().getName())); + } + } + } + + /** + * Print help messages + * @param helpMsgs - help messages to print + */ + public void printHelp(String[] helpMsgs) { + for (int i=0; i