1 23 24 31 32 package com.sun.enterprise.admin.server.core.mbean.config.naming; 33 34 import com.sun.enterprise.admin.common.Name; 36 import com.sun.enterprise.admin.common.MalformedNameException; 37 38 import com.sun.enterprise.admin.common.exception.MBeanConfigException; 39 40 import javax.management.ObjectName ; 42 import javax.management.MalformedObjectNameException ; 43 44 import java.lang.reflect.Constructor ; 45 import java.util.ArrayList ; 46 import java.util.Hashtable ; 47 import java.util.Enumeration ; 48 import java.text.MessageFormat ; 49 50 import com.sun.enterprise.util.i18n.StringManager; 52 53 54 57 public class MBeanNamingDescriptor 58 { 59 String m_type; 60 String m_className; 61 String m_objectPattern; 62 String [] m_dottedPatterns; 63 String m_xpathPattern; 64 int m_mode; 65 66 int m_parmListSize = 0; 68 Object [][] m_dottedTokens = null; 69 Object [] m_objectTokens = null; 70 71 private static StringManager localStrings = 73 StringManager.getManager( MBeanNamingDescriptor.class ); 74 75 public MBeanNamingDescriptor(Object [] description) throws MBeanConfigException 76 { 77 this((String )description[0], (Integer )description[1], (String )description[2], (String )description[3], (String )description[4], (String )description[5]); 78 } 79 80 public MBeanNamingDescriptor(String type, Integer mode, String dottedPatterns, String objectPattern, String xpathPattern, String className) throws MBeanConfigException 81 { 82 m_type = type; 83 m_className = className; 84 m_dottedPatterns = splitDottedPatternsString(dottedPatterns); 85 m_xpathPattern = xpathPattern; 86 m_objectPattern = objectPattern; 87 m_mode = mode.intValue(); 88 89 try 90 { 91 m_dottedTokens = new Object [m_dottedPatterns.length][]; 92 for(int i=0; i<m_dottedPatterns.length; i++) 93 m_dottedTokens[i] = getDottedNamePatternTokens(m_dottedPatterns[i]); 94 m_objectTokens = getObjectNamePatternTokens(m_objectPattern); 95 m_parmListSize = getMaxTokenIndex(m_objectTokens) + 1; 96 97 checkConsistency(); 98 } 99 catch(Exception e) 100 { 101 String msg = localStrings.getString( "admin.server.core.mbean.config.naming.mbeandescriptor_creation_failure_for_object_pattern", objectPattern, e.getMessage() ); 102 throw new MBeanConfigException( msg ); 103 } 104 } 105 106 private void checkConsistency() throws MBeanConfigException 107 { 108 } 109 110 boolean isDottedPatternMatch(Name name) 112 { 113 if(findDottedPatternTokens(name)!=null) 114 return true; 115 return false; 116 } 117 118 private Object [] findDottedPatternTokens(Name name) 119 { 120 for(int i=0; i<m_dottedTokens.length; i++) 121 { 122 if(isDottedPatternMatch(name, m_dottedTokens[i], true)) 123 return m_dottedTokens[i]; 124 } 125 return null; 126 } 127 128 boolean isObjectNamePatternMatch(Hashtable sample) 129 { 130 if(m_objectTokens.length!=(sample.size()*2)) 131 return false; 132 for(int i=0; i<m_objectTokens.length; i = i+2) 133 { 134 String sampleVal = (String )sample.get(m_objectTokens[i]); 135 if(sampleVal==null || 136 ((m_objectTokens[i+1] instanceof String ) && !sampleVal.equals((String )m_objectTokens[i+1])) ) 137 return false; 138 } 139 return (true); 140 } 141 142 143 String [] extractParmList(String dottedName) throws MalformedNameException 144 { 145 if(m_dottedTokens==null) 146 return null; 147 Name name = new Name(dottedName); 148 Object [] tokens = findDottedPatternTokens(name); 149 if(tokens == null) 150 return null; 151 int nTokens = name.getNumParts(); 152 if(name.getNumParts()!=tokens.length) 153 return null; 154 155 String [] parmList = new String [m_parmListSize]; 156 157 for(int i=0; i<nTokens; i++) 158 { 159 if( tokens[i] instanceof Integer ) 160 { 161 parmList[((Integer )tokens[i]).intValue()] = name.getNamePart(i).toString(); 162 } 163 } 164 return parmList; 165 } 166 167 168 169 private Object [] getDottedNamePatternTokens(String dottedPattern) throws MalformedNameException 170 { 171 ArrayList list = new ArrayList (); 172 int idx = 0, idx2 = 0; 173 174 if(dottedPattern!=null) 175 { 176 while(idx<dottedPattern.length() && (idx2=dottedPattern.indexOf('.', idx))>=0) 177 { 178 if(idx == idx2) 179 list.add(""); 180 else 181 list.add(dottedPattern.substring(idx,idx2).trim()); 182 idx = idx2+1; 183 } 184 if(idx<dottedPattern.length()) 185 list.add(dottedPattern.substring(idx).trim()); 186 Object [] tokens = list.toArray(); 187 replacePlaceholdersToIntegers(tokens); 188 return tokens; 189 } 190 return null; 191 } 192 193 194 private Object [] getObjectNamePatternTokens(String objectPattern) throws MalformedObjectNameException 196 { 197 if(objectPattern!=null) 198 { 199 ObjectName objName = new ObjectName (objectPattern); 200 Hashtable ht = objName.getKeyPropertyList(); 201 Enumeration e = ht.keys(); 202 Object [] tokens = new Object [ht.size()*2]; 203 int i = 0; 204 while(e.hasMoreElements()) 205 { 206 String key = (String )e.nextElement(); 207 tokens[i++] = key; 208 tokens[i++] = ht.get(key); 209 } 210 replacePlaceholdersToIntegers(tokens); 211 return tokens; 212 } 213 return null; 214 } 215 216 public int getParmListSize() 217 { 218 return m_parmListSize; 219 } 220 public String [] extractParmList(ObjectName objectName) 221 { 222 if(m_objectTokens==null) 223 return null; 224 Hashtable ht = objectName.getKeyPropertyList(); 225 String [] parmList = new String [m_parmListSize]; 226 for(int i=0; i<m_objectTokens.length; i=i+2) 227 { 228 if( m_objectTokens[i+1] instanceof Integer ) 229 { 230 parmList[((Integer )m_objectTokens[i+1]).intValue()] = (String )ht.get(m_objectTokens[i]); 231 } 232 } 233 return parmList; 234 } 235 236 private void replacePlaceholdersToIntegers(Object [] tokens) 237 { 238 for(int i=0; i<tokens.length; i++) 239 { 240 Object idx = getIndexForPlaceholder((String )tokens[i]); 241 if(idx!=null) 242 tokens[i] = idx; 243 } 244 } 245 246 private Integer getIndexForPlaceholder(String str) 247 { 248 int len = str.length(); 249 if(len<3 || str.charAt(0)!='{' || str.charAt(len-1)!='}') 250 return null; 251 try 252 { 253 return Integer.valueOf(str.substring(1,len-1)); 254 } 255 catch(Throwable e) 256 { 257 } 258 return null; 259 } 260 261 private int getMaxTokenIndex(Object [] tokens) 262 { 263 int res = -1; 264 int current; 265 for(int i=0; i<tokens.length; i++) 266 { 267 if(tokens[i] instanceof Integer && 268 res < (current=((Integer )tokens[i]).intValue())) 269 res = current; 270 } 271 return res; 272 } 273 274 public String getMBeanClassName() 275 { 276 return m_className; 277 } 278 279 public String getType() 280 { 281 return m_type; 282 } 283 284 public int getMode() 285 { 286 return m_mode; 287 } 288 289 public String [] getDottedPatterns() 290 { 291 return m_dottedPatterns; 292 } 293 294 public Object [][] getDottedTokens() 295 { 296 return m_dottedTokens; 297 } 298 299 public String getXPathPattern() 300 { 301 return m_xpathPattern; 302 } 303 304 public ObjectName createObjectName(Object [] params) throws MalformedObjectNameException 305 { 306 return new ObjectName (formatPattern(m_objectPattern, params)); 307 } 308 public String createDottedNameName(Object [] params) 309 { 310 if(m_dottedPatterns==null || m_dottedPatterns.length<1) 311 return null; 312 return formatPattern(m_dottedPatterns[0], params); 313 } 314 public String createXPath(Object [] params) 315 { 316 return formatPattern(m_xpathPattern, params); 317 } 318 private String formatPattern(String pattern, Object [] params) 319 { 320 return MessageFormat.format(pattern, params); 321 } 322 323 private String [] splitDottedPatternsString(String names) 324 { 325 ArrayList list = new ArrayList (); 326 int idx = 0, idx2 = 0; 327 while(idx<names.length() && (idx2=names.indexOf(MBeansDescriptions.PATTERNS_SEPARATOR, idx))>=0) 328 { 329 if(idx2!=idx) 330 list.add(names.substring(idx, idx2)); 331 idx = idx2+1; 332 } 333 if(idx2<0) 334 list.add(names.substring(idx)); 335 return (String [])list.toArray(new String [list.size()]); 336 } 337 338 public static boolean isDottedPatternMatch(Name name, Object [] tokens, boolean bExactMatch) 339 { 340 if(tokens==null) 341 return false; 342 int nTokens = name.getNumParts(); 343 if(bExactMatch) 344 { 345 if(nTokens!=tokens.length) 346 return false; 347 } 348 else 349 { 350 if(nTokens>tokens.length) 351 return false; 352 } 353 for(int i=0; i<nTokens; i++) 354 { 355 if( (tokens[i] instanceof String ) && 356 !name.getNamePart(i).toString().equals((String )tokens[i])) 357 return false; 358 } 359 return true; 360 } 361 362 } 363 | Popular Tags |