KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29
30 /**
31  * ObjectName pattern matching Helper.<p>
32  *
33  * Contains various routines for matching domains and properties.<p>
34  *
35  * Routines based on work done Trevor in the registry.
36  *
37  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
38  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
39  * @version $Revision: 37459 $
40  */

41 public class ObjectNamePatternHelper
42 {
43    // Static --------------------------------------------------------
44

45    /**
46     * Compare an object name against a pattern.
47     *
48     * @param test the string to test
49     * @param pattern the pattern to match
50     */

51    public static boolean patternMatch(ObjectName JavaDoc test, ObjectName JavaDoc pattern)
52    {
53       if (pattern.equals("*:*"))
54          return true;
55
56       if (patternMatch(test.getDomain(), pattern.getDomain()))
57       {
58          PropertyPattern propertyPattern = new PropertyPattern(pattern);
59          return propertyPattern.patternMatch(test);
60       }
61       return false;
62    }
63
64    /**
65     * Compare strings where ? and * chars are significant.
66     *
67     * @param test the string to test
68     * @param pattern the pattern to match
69     */

70    public static boolean patternMatch(String JavaDoc test, String JavaDoc pattern)
71    {
72       if (pattern.equals("*"))
73          return true;
74       return patternMatch(test.toCharArray(), 0, pattern.toCharArray(), 0);
75    }
76
77    /**
78     * Compare where ? and * chars are significant.<p>
79     *
80     * I arrived at this solution after quite a bit of trial and error - it's
81     * all a bit interwoven. Obviously I'm no good at parsers and there must
82     * be a clearer or more elegant way to do this. I'm suitably in awe of
83     * the perl regex hackers now.
84     *
85     * @param test the string to test
86     * @param tpos the start of the test string
87     * @param pattern the pattern to match
88     * @param ppos the start of the pattern string
89     */

90    public static boolean patternMatch(char[] test, int tpos, char[] pattern, int ppos)
91    {
92       int tlen = test.length;
93       int plen = pattern.length;
94
95       while (ppos < plen)
96       {
97          char c = pattern[ppos++];
98          if ('?' == c)
99          {
100             // eat a test character and make sure we're not
101
// already at the end
102
if (tpos++ == tlen)
103                return false;
104          }
105          else if ('*' == c)
106          {
107             if (ppos == plen) // shortcut - * at the end of the pattern
108
return true;
109
110             // Fell off the end
111
if (tpos == tlen)
112                return false;
113
114             // hammer the test chars recursively until we
115
// get a match or we drop off the end of test
116
do
117             {
118                if (patternMatch(test, tpos, pattern, ppos))
119                   return true;
120             }
121             while (++tpos < tlen);
122          }
123          else if (tpos == tlen || c != test[tpos++])
124             return false;
125       }
126       // fell through with no falses so make sure all of test was examined
127
return (tpos == tlen);
128    }
129
130    /**
131     * Encapsulation of property information
132     */

133    public static class PropertyPattern
134    {
135       /**
136        * Are these properties a pattern?
137        */

138       boolean isPropertyPattern;
139
140       /**
141        * The keys for the properties
142        */

143       Object JavaDoc[] propertyKeys;
144
145       /**
146        * The keys for the properties
147        */

148       Object JavaDoc[] propertyValues;
149
150       /**
151        * The canonical key property string
152        */

153       String JavaDoc canonicalKeyPropertyString;
154
155       /**
156        * Construct a new property pattern
157        *
158        * @param pattern the object name that might be a pattern
159        */

160       public PropertyPattern(ObjectName JavaDoc pattern)
161       {
162          isPropertyPattern = pattern.isPropertyPattern();
163          if (isPropertyPattern)
164          {
165             Hashtable JavaDoc patternKPList = pattern.getKeyPropertyList();
166             int length = patternKPList.size();
167             propertyKeys = new Object JavaDoc[length];
168             propertyValues = new Object JavaDoc[length];
169
170             int i = 0;
171             for (Iterator JavaDoc iterator = patternKPList.entrySet().iterator(); iterator.hasNext(); i++)
172             {
173                Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
174                propertyKeys[i] = entry.getKey();
175                propertyValues[i] = entry.getValue();
176             }
177          }
178          else
179             canonicalKeyPropertyString = pattern.getCanonicalKeyPropertyListString();
180       }
181
182       /**
183        * Test whether the object name matches the pattern
184        *
185        * @param name the name to test
186        */

187       public boolean patternMatch(ObjectName JavaDoc name)
188       {
189          if (isPropertyPattern)
190          {
191             // "*" matches everything
192
if (propertyKeys.length == 0)
193                return true;
194
195             Hashtable JavaDoc kplist = name.getKeyPropertyList();
196
197             for (int i = 0; i < propertyKeys.length; i++)
198             {
199                if (propertyValues[i].equals(kplist.get(propertyKeys[i])) == false)
200                   return false;
201             }
202             return true;
203          }
204          else
205             return canonicalKeyPropertyString.equals(name.getCanonicalKeyPropertyListString());
206       }
207    }
208 }
209
Popular Tags