001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import java.util.Hashtable;
025: import java.util.Iterator;
026: import java.util.regex.Matcher;
027: import java.util.regex.Pattern;
028: import java.util.regex.PatternSyntaxException;
029: import javax.management.ObjectName;
030:
031: /** JMX ObjectName comparision utility methods
032: *
033: * @author Scott.Stark@jboss.org
034: * @version $Revision: 57200 $
035: */
036: public class ObjectNameMatch {
037: /** Compare two ObjectNames to see if the match via equality or as
038: * a pattern.
039: * @param n0 An ObjectName or pattern
040: * @param n1 An ObjectName or pattern
041: * @return true if n0 and n1 match, false otherwise
042: */
043: public static boolean match(ObjectName n0, ObjectName n1) {
044: boolean match = n0.equals(n1);
045: if (match == true)
046: return true;
047:
048: // First compare the domains
049: String d0 = n0.getDomain();
050: String d1 = n1.getDomain();
051: int star0 = d0.indexOf('*');
052: int star1 = d1.indexOf('*');
053:
054: if (star0 >= 0) {
055: if (star1 >= 0) {
056: match = d0.equals(d1);
057: } else {
058: try {
059: Pattern domainRE = Pattern.compile(d0);
060: Matcher m = domainRE.matcher(d1);
061: match = m.matches();
062: } catch (PatternSyntaxException e) {
063: }
064: }
065: } else if (star1 >= 0) {
066: if (star0 >= 0) {
067: match = d0.equals(d1);
068: } else {
069: try {
070: Pattern domainRE = Pattern.compile(d1);
071: Matcher m = domainRE.matcher(d0);
072: match = m.matches();
073: } catch (PatternSyntaxException e) {
074: }
075: }
076: } else {
077: match = d0.equals(d1);
078: }
079:
080: if (match == false)
081: return false;
082:
083: // Next compare properties
084: if (n0.isPropertyPattern()) {
085: Hashtable props0 = n0.getKeyPropertyList();
086: Hashtable props1 = n1.getKeyPropertyList();
087: Iterator iter = props0.keySet().iterator();
088: while (match == true && iter.hasNext()) {
089: String key = (String) iter.next();
090: String value = (String) props0.get(key);
091: match &= value.equals(props1.get(key));
092: }
093: } else if (n1.isPropertyPattern()) {
094: Hashtable props0 = n0.getKeyPropertyList();
095: Hashtable props1 = n1.getKeyPropertyList();
096: Iterator iter = props1.keySet().iterator();
097: while (iter.hasNext()) {
098: String key = (String) iter.next();
099: String value = (String) props1.get(key);
100: match &= value.equals(props0.get(key));
101: }
102: }
103:
104: return match;
105: }
106:
107: }
|