KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > ObjectNameMatch


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import java.util.Hashtable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.regex.Matcher JavaDoc;
27 import java.util.regex.Pattern JavaDoc;
28 import java.util.regex.PatternSyntaxException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 /** JMX ObjectName comparision utility methods
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 42934 $
35  */

36 public class ObjectNameMatch
37 {
38    /** Compare two ObjectNames to see if the match via equality or as
39     * a pattern.
40     * @param n0 An ObjectName or pattern
41     * @param n1 An ObjectName or pattern
42     * @return true if n0 and n1 match, false otherwise
43     */

44    public static boolean match(ObjectName JavaDoc n0, ObjectName JavaDoc n1)
45    {
46       boolean match = n0.equals(n1);
47       if( match == true )
48          return true;
49
50       // First compare the domains
51
String JavaDoc d0 = n0.getDomain();
52       String JavaDoc d1 = n1.getDomain();
53       int star0 = d0.indexOf('*');
54       int star1 = d1.indexOf('*');
55
56       if( star0 >= 0 )
57       {
58          if( star1 >= 0 )
59          {
60             match = d0.equals(d1);
61          }
62          else
63          {
64             try
65             {
66                Pattern JavaDoc domainRE = Pattern.compile(d0);
67                Matcher JavaDoc m = domainRE.matcher(d1);
68                match = m.matches();
69             }
70             catch(PatternSyntaxException JavaDoc e)
71             {
72             }
73          }
74       }
75       else if( star1 >= 0 )
76       {
77          if( star0 >= 0 )
78          {
79             match = d0.equals(d1);
80          }
81          else
82          {
83             try
84             {
85                Pattern JavaDoc domainRE = Pattern.compile(d1);
86                Matcher JavaDoc m = domainRE.matcher(d0);
87                match = m.matches();
88             }
89             catch(PatternSyntaxException JavaDoc e)
90             {
91             }
92          }
93       }
94       else
95       {
96          match = d0.equals(d1);
97       }
98
99       if( match == false )
100          return false;
101
102       // Next compare properties
103
if( n0.isPropertyPattern() )
104       {
105          Hashtable JavaDoc props0 = n0.getKeyPropertyList();
106          Hashtable JavaDoc props1 = n1.getKeyPropertyList();
107          Iterator JavaDoc iter = props0.keySet().iterator();
108          while( match == true && iter.hasNext() )
109          {
110             String JavaDoc key = (String JavaDoc) iter.next();
111             String JavaDoc value = (String JavaDoc) props0.get(key);
112             match &= value.equals(props1.get(key));
113          }
114       }
115       else if( n1.isPropertyPattern() )
116       {
117          Hashtable JavaDoc props0 = n0.getKeyPropertyList();
118          Hashtable JavaDoc props1 = n1.getKeyPropertyList();
119          Iterator JavaDoc iter = props1.keySet().iterator();
120          while( iter.hasNext() )
121          {
122             String JavaDoc key = (String JavaDoc) iter.next();
123             String JavaDoc value = (String JavaDoc) props1.get(key);
124             match &= value.equals(props0.get(key));
125          }
126       }
127
128       return match;
129    }
130
131 }
132
Popular Tags