KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > adventnet > jmx > utils > JmxUtilities


1 /**
2 * The XMOJO Project 5
3 * Copyright © 2003 XMOJO.org. All rights reserved.
4
5 * NO WARRANTY
6
7 * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
8 * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
9 * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
10 * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
11 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
13 * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
14 * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
15 * REPAIR OR CORRECTION.
16
17 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
18 * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
19 * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
20 * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
21 * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
22 * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
23 * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
24 * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGES.
26 **/

27
28 package com.adventnet.jmx.utils;
29
30 import java.util.Enumeration JavaDoc;
31 import java.util.Hashtable JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34
35 /**
36  * The internal utility class used by the jmx extension package classes.
37  * This contains the pattern matching static methods useful for ObjectName
38  * pattern matching.
39  */

40 public class JmxUtilities
41 {
42     public JmxUtilities()
43     {
44     }
45
46     public static boolean followsObjectNamePattern(String JavaDoc string, String JavaDoc pattern)
47                                                     throws Exception JavaDoc
48     {
49         ObjectName JavaDoc patON = new ObjectName JavaDoc("tempDomain:"+pattern);
50         if(patON.isPropertyPattern())
51             return true;
52
53         return false;
54     }
55
56     public static boolean objectNameSpecificChecks(String JavaDoc string, String JavaDoc pattern) throws Exception JavaDoc
57     {
58         ObjectName JavaDoc stringON = new ObjectName JavaDoc("tempDomain:"+string);
59         ObjectName JavaDoc patON = new ObjectName JavaDoc("tempDomain:"+pattern);
60
61         Hashtable JavaDoc patProps = patON.getKeyPropertyList();
62         Hashtable JavaDoc stringProps = stringON.getKeyPropertyList();
63
64         for(Enumeration JavaDoc e=patProps.keys();e.hasMoreElements();)
65         {
66             String JavaDoc key = (String JavaDoc)e.nextElement();
67             String JavaDoc value = (String JavaDoc)patProps.get(key);
68
69             if(stringProps.get(key) != null && stringProps.get(key).equals(value));
70             else
71                 return false;
72
73         }
74         return true;
75
76     }
77
78     public static boolean checkPattern( String JavaDoc info, String JavaDoc pat,boolean wc)
79     {
80         boolean returnFlag;
81
82         if(!wc)
83         {
84             return info.equals(pat);
85         }
86
87         try
88         {
89             if(followsObjectNamePattern(info, pat))
90                 return objectNameSpecificChecks( info, pat);
91         }
92         catch(Exception JavaDoc e){}
93
94         returnFlag = wildcardMatch(pat,info);
95
96         return returnFlag;
97     }
98
99     public static boolean wildCPattern(String JavaDoc p_string,String JavaDoc p_pattern)
100     {
101         char c;
102         char p;
103         boolean retval;
104
105         for (;;)
106         {
107             if(p_pattern.length() == 0) /* End of pattern. If end of string return true */
108             {
109                 if(p_string.length() == 0)
110                 {
111                     return true;
112                 }
113                 else
114                 {
115                     return false;
116                 }
117             }
118
119             if(p_string.length() == 0)
120             {
121                 /* Critical condition */
122                 return false;
123             }
124
125             p = p_pattern.charAt(0);
126             p_pattern = p_pattern.substring(1);
127
128             if(p == '$')
129             {
130                 if(p_pattern.length() == 0)
131                 {
132                     /* Critical condition */
133                     return false;
134                 }
135
136                 p = p_pattern.charAt(0);
137                 p_pattern =p_pattern.substring(1);
138                 //System.out.println("P before switch is " + p);
139

140                 switch (p)
141                 {
142                 case '*':
143                     while (p_string.length() != 0)
144                     {
145                         p_string = p_string.substring(1);
146                         //System.out.println("PString is " + p_string);
147

148                         /* Match zero or more char */
149                         retval = wildCPattern(p_string,p_pattern);
150
151                         if (retval == true)
152                         {
153                             //System.out.println("Returning as retVal true\n");
154
return true;
155                         }
156                     }
157
158                     retval = wildCPattern (p_string,p_pattern);
159
160                     return retval;
161
162                 case '?':
163                     /* match any one char */
164                     if (p_string.length() == 0)
165                     {
166                         /* not end of string */
167                         //System.out.println("Returning as CRITICAL condition III\n");
168
return false;
169                     }
170                     p_string = p_string.substring(1);
171
172                     break;
173                 case '$': /* check for $ character */
174                     p_string = p_string.substring(1);
175                     c = p_string.charAt(0);
176                     //System.out.println("C is " + c);
177
if (c != '$')
178                     {
179                         return false;
180                     }
181                     break;
182                 default: /* Wild Character Not Found */
183                     //System.out.println("Returning as NO_WILD CARD character found\n");
184
return false;
185                 } /* End of switch */
186             }
187             else
188             {
189                 c = p_string.charAt(0);
190                 p_string = p_string.substring(1);
191                 if (c != p) /* check for exact char */
192                 {
193                     //System.out.println("Returning as not match condition %c "+c + "\n");
194
return false; /* not a match */
195                 }
196             }
197
198         } /* End of For */
199
200     }
201
202     public static boolean wildcardMatch(String JavaDoc pattern, String JavaDoc string)
203     {
204         int stringLength = string.length();
205         int stringIndex = 0;
206         for (int patternIndex = 0; patternIndex < pattern.length(); ++patternIndex)
207         {
208             char c = pattern.charAt(patternIndex);
209             if (c == '*')
210             {
211                 // Recurse with the pattern without this '*' and the actual string, until
212
// match is found or we inspected the whole string
213
while (stringIndex < stringLength)
214                 {
215                     if (wildcardMatch(pattern.substring(patternIndex + 1), string.substring(stringIndex))) {return true;}
216                     // No match found, try a shorter string, since we are matching '*'
217
++stringIndex;
218                 }
219             }
220             else if (c == '?')
221             {
222                 // Increment the string index, since '?' match a single char in the string
223
++stringIndex;
224                 if (stringIndex > stringLength) {return false;}
225             }
226             else
227             {
228                 // A normal character in the pattern, must match the one in the string
229
if (stringIndex >= stringLength || c != string.charAt(stringIndex)) {return false;}
230                 ++stringIndex;
231             }
232         }
233
234         // I've inspected the whole pattern, but not the whole string
235
return stringIndex == stringLength;
236     }
237 }
Popular Tags