1 19 package org.netbeans.modules.j2ee.sun.util; 20 21 import java.beans.PropertyEditor ; 22 23 import java.io.File ; 24 import java.util.Arrays ; 25 import java.util.Vector ; 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.util.ResourceBundle ; 29 30 import javax.management.Attribute ; 31 import javax.management.MBeanAttributeInfo ; 32 import javax.management.ObjectName ; 33 34 import org.openide.nodes.PropertySupport; 35 import org.openide.execution.NbClassPath; 36 37 import org.netbeans.modules.j2ee.sun.ide.runtime.nodes.DomainRootNode; 38 import org.netbeans.modules.j2ee.sun.ide.runtime.nodes.ResourceLeafNode; 39 import org.netbeans.modules.j2ee.sun.bridge.apis.AppserverMgmtActiveNode; 40 41 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePair; 42 import org.netbeans.modules.j2ee.sun.ide.editors.NameValuePairsPropertyEditor; 43 import org.netbeans.modules.j2ee.sun.ide.editors.LogLevelEditor; 44 45 48 public class PropertySupportFactory { 49 50 private static Logger logger; 51 private static PropertySupportFactory factory; 52 private EnhancedPropertyEditorFactory editorFactory; 53 54 private static final String ARRAY_DELIM = " , "; 55 56 ResourceBundle bundle = ResourceBundle.getBundle("org/netbeans/modules/j2ee/sun/util/Bundle"); 57 58 59 static java.util.Map typeToClassesMap = new java.util.HashMap (); 60 61 static { 62 typeToClassesMap.put("short", String .class); 63 typeToClassesMap.put("long", String .class); 64 typeToClassesMap.put("int", String .class); 65 typeToClassesMap.put("boolean", String .class); typeToClassesMap.put("float", String .class); 67 typeToClassesMap.put("double", String .class); 68 typeToClassesMap.put("byte", String .class); 69 typeToClassesMap.put("char", String .class); 70 typeToClassesMap.put(String [].class.getName(), String .class); 71 typeToClassesMap.put(Boolean .class.getName(), String .class); 72 typeToClassesMap.put(java.util.Map .class.getName(), String .class); 73 }; 74 75 static { 76 logger = Logger.getLogger("org.netbeans.modules.j2ee.sun"); 77 } 78 79 82 private PropertySupportFactory() { 83 editorFactory = EnhancedPropertyEditorFactory.getInstance(); 84 } 85 86 87 92 public static PropertySupportFactory getInstance() { 93 if(factory == null) { 94 factory = new PropertySupportFactory(); 95 } 96 return factory; 97 } 98 99 100 122 public PropertySupport getPropertySupport( 123 final AppserverMgmtActiveNode parent, final Attribute attr, 124 final MBeanAttributeInfo info) { 125 PropertySupport support = null; 126 String attrName = attr.getName(); 127 if(Arrays.asList(PropertyConstants.CUSTOM_PROPERTIES).contains(attrName)){ 128 support = getCustomPropertyEditors(parent, attr, info, attrName); 129 }else{ 130 if(attr.getValue() instanceof String []){ 131 String [] strArray = (String [])attr.getValue(); 132 if(info.isWritable()) 133 support = createStringArrayWritableProperty(parent, attr, info, strArray.getClass()); 134 else 135 support = createStringArrayReadOnlyProperty(attr, info, strArray.getClass()); 136 support.setValue ("item.separator", " "); }else{ 138 if(info.isWritable()) { 139 support = createReadWritePropertySupport(parent, attr, info); 140 } else { 141 support = createReadOnlyPropertySupport(attr, info); 142 } 143 } 144 } 145 return support; 146 } 147 148 private PropertySupport getCustomPropertyEditors(final AppserverMgmtActiveNode parent, final Attribute attr, 149 final MBeanAttributeInfo info, String attrName){ 150 PropertySupport support = null; 151 if(attrName.equals(PropertyConstants.PROPERTY_PAIRS_FIELD) && (!parent.getNodeType().equals(NodeTypes.JVM)) 152 && (parent instanceof ResourceLeafNode)){ 153 ResourceLeafNode resourceNode = (ResourceLeafNode)parent; 154 support = createExtraProperties(resourceNode, attr, info); 155 }else{ 156 if(attrName.equals(PropertyConstants.DATASOURCE_TYPE_FIELD)){ 157 if(parent.getNodeType().equals(NodeTypes.CONNECTION_POOL)) 158 support = createWritablePropertySupportWithEditor(parent, attr, info, attrName); 159 else 160 support = createWritablePropertySupportWithoutEditor(parent, attr, info); 161 }else 162 support = createWritablePropertySupportWithEditor(parent, attr, info, attrName); 163 } 164 return support; 165 } 166 167 177 private PropertySupport createReadOnlyPropertySupport( 178 final Attribute attr, final MBeanAttributeInfo info) { 179 String name = attr.getName(); 180 return new PropertySupport.ReadOnly(name, 181 getClassFromStringType(info.getType()), name, name) { 182 public Object getValue() { 183 return calculateReturnObjectType(attr); 184 } 185 }; 186 } 187 188 189 199 private PropertySupport createReadWritePropertySupport( 200 final AppserverMgmtActiveNode parent, final Attribute attr, 201 final MBeanAttributeInfo info) { 202 PropertySupport support = null; 203 if(attr.getValue() instanceof Boolean ) { 204 support = 205 createWritablePropertySupportWithEditor(parent, attr, info, "Boolean"); } else { 207 if(Arrays.asList(PropertyConstants.JVM_STR_TO_ARR).contains(attr.getName())){ 208 if(parent.isServerLocal()){ 209 support = createNetBeansClassPathProperty(parent, attr, info); 210 }else{ 211 String [] arrayNames = new String []{}; 212 support = createModifiedStringArrayReadOnlyProperty(parent, attr, info, arrayNames.getClass()); 213 } 214 }else{ 215 support = 216 createWritablePropertySupportWithoutEditor(parent, attr, info); 217 } 218 } 219 return support; 220 } 221 222 223 227 private PropertySupport createWritablePropertySupportWithEditor( 228 final AppserverMgmtActiveNode parent, final Attribute attr, 229 final MBeanAttributeInfo info, final String customType) { 230 String name = attr.getName(); 231 return new PropertySupport.ReadWrite(name, 232 getClassFromStringType(info.getType()), name, name) { 233 Attribute attribute = attr; 234 public Object getValue() { 235 Object obj = attribute.getValue(); 236 if(obj != null) { 237 obj = obj.toString(); 238 } 239 return obj; 240 } 241 242 public void setValue(Object obj) { 243 attribute = revertAttribute(parent, getName(), obj, attribute); 244 } 245 246 public PropertyEditor getPropertyEditor() { 247 return editorFactory.getEnhancedPropertyEditor( 248 attribute.getValue(), customType); 249 } 250 }; 251 } 252 253 254 258 private PropertySupport createWritablePropertySupportWithoutEditor( 259 final AppserverMgmtActiveNode parent, final Attribute attr, 260 final MBeanAttributeInfo info) { 261 String name = attr.getName(); 262 return new PropertySupport.ReadWrite(name, 263 getClassFromStringType(info.getType()), name, name) { 264 Attribute attribute = attr; 265 public Object getValue() { 266 return calculateReturnObjectType(attribute); 267 } 268 269 public void setValue(Object obj) { 270 attribute = revertAttribute(parent, getName(), obj, attribute); 271 } 272 }; 273 } 274 275 276 277 281 private Object calculateReturnObjectType(Attribute attr) { 282 Object obj = attr.getValue(); 283 if(obj instanceof ObjectName []) { 284 ObjectName [] objNames = (ObjectName [])obj; 285 String [] arrayNames = new String [objNames.length]; 286 for(int i=0; i < objNames.length; i++){ 287 arrayNames[i] = objNames[i].toString(); 288 } 289 return arrayNames; 290 } if(obj instanceof String []) { 291 String [] values = (String [])obj; 292 StringBuffer returnVals = new StringBuffer (); 293 int pos = 0; 294 for(int i=0; i < values.length; i++){ 295 returnVals.append(values[i]); 296 pos++; 297 if(pos < values.length) { 298 returnVals.append(ARRAY_DELIM); 299 } 300 } 301 return returnVals; 302 } else if(obj != null || !(obj instanceof String )) { 303 if(obj == null) { 304 return ""; 305 } 306 obj = obj.toString(); 307 return obj; 308 } else { 309 return ""; 310 } 311 } 312 313 314 318 private static Class getClassFromStringType(final String type) { 319 Class clazz = null; 320 try { 321 clazz = (Class ) typeToClassesMap.get(type); 322 if (clazz == null) { 323 clazz = Class.forName(type); 324 } 325 if (clazz == null) { 326 throw new ClassNotFoundException (type); 327 } 328 if (!String .class.isAssignableFrom(clazz) 329 && ! Number .class.isAssignableFrom(clazz)) { 330 throw new ClassNotFoundException (type); 331 } 332 } catch(ClassNotFoundException e) { 333 logger.log(Level.FINE, e.getMessage(), e); 334 } 335 return clazz; 336 } 337 338 PropertySupport createExtraProperties(final ResourceLeafNode parent, final Attribute attr, final MBeanAttributeInfo info) { 339 return new PropertySupport.ReadWrite( 340 info.getName(), 341 NameValuePairsPropertyEditor.class, 342 bundle.getString("LBL_ExtParams"), bundle.getString("DSC_ExtParams")) { Attribute attribute = attr; 345 public Object getValue() { 346 return attribute.getValue(); 347 } 348 349 public void setValue(Object obj) { 350 if(obj instanceof Object []){ 351 java.util.Map attributeMap = (java.util.Map )attribute.getValue(); 353 354 Object [] currentVal = (Object [])obj; 356 NameValuePair[] pairs = getNameValuePairs(currentVal); 357 java.util.Map propertyList = new java.util.HashMap (); 358 Object [] props = new Object [pairs.length]; 359 for(int i=0; i<pairs.length; i++){ 360 Attribute attr = new Attribute (pairs[i].getParamName(), pairs[i].getParamValue()); 361 props[i] = attr; 362 propertyList.put(pairs[i].getParamName(), pairs[i].getParamValue()); 363 } 364 parent.updateExtraProperty(props, attributeMap); 365 366 attribute = new Attribute (getName(), propertyList); 368 } 369 } 370 371 public PropertyEditor getPropertyEditor(){ 372 return new NameValuePairsPropertyEditor(attribute.getValue()); 373 } 374 375 376 }; 377 } 379 PropertySupport createStringArrayReadOnlyProperty(final Attribute a, final MBeanAttributeInfo attr, final Class type) { 380 return new PropertySupport.ReadOnly( 381 attr.getName(), 382 type, 383 attr.getName(), 384 attr.getName()) { 385 Attribute attribute = a; 386 public Object getValue() { 387 Object val[] = (Object [])attribute.getValue(); 388 if (attribute.getValue() instanceof ObjectName []){ 389 ObjectName [] objNames = (ObjectName [])val; 390 String [] arrayNames = new String [objNames.length]; 391 for(int i=0; i<objNames.length; i++){ 392 arrayNames[i] = objNames[i].toString(); 393 } 394 return (Object )arrayNames; 395 }else{ 396 return attribute.getValue(); 397 } 398 } 399 }; 400 } 402 PropertySupport createStringArrayWritableProperty(final AppserverMgmtActiveNode parent, final Attribute attr, final MBeanAttributeInfo info, final Class type) { 403 return new PropertySupport.ReadWrite( 404 info.getName(), 405 type, 406 info.getName(), 407 info.getName()) { 408 Attribute attribute = attr; 409 public Object getValue() { 410 Object val[] = (Object [])attribute.getValue(); 411 if (attribute.getValue() instanceof ObjectName []){ 412 ObjectName [] objNames = (ObjectName [])val; 413 String [] arrayNames = new String [objNames.length]; 414 for(int i=0; i<objNames.length; i++){ 415 arrayNames[i] = objNames[i].toString(); 416 } 417 return (Object )arrayNames; 418 }else{ 419 String [] values = (String [])attribute.getValue(); 420 return values; 421 } 422 } 423 public void setValue(Object obj) { 424 attribute = revertAttribute(parent, getName(), obj, attribute); 425 } 426 }; 427 } 429 PropertySupport createNetBeansClassPathProperty(final AppserverMgmtActiveNode parent, final Attribute attr, final MBeanAttributeInfo info) { 430 return new PropertySupport.ReadWrite( 431 info.getName(), 432 NbClassPath.class, 433 info.getName(), 434 info.getName()) { 435 Attribute attribute = attr; 436 public Object getValue() { 437 if(attribute.getValue() != null){ 438 String val = replacePathSeperatorToken(attribute.getValue().toString()); 439 return new NbClassPath(val); 440 }else 441 return null; 442 } 443 444 public void setValue(Object val){ 445 String value = ((NbClassPath)val).getClassPath(); 446 value = stripQuotes(value); 447 Object obj = replacePathSeperator(value); 448 attribute = revertAttribute(parent, getName(), obj, attribute); 449 } 450 }; 451 } 453 public PropertySupport createLogLevelProperty(final DomainRootNode parent, final Attribute attr, final MBeanAttributeInfo info) { 454 return new PropertySupport.ReadWrite( 455 info.getName(), 456 LogLevelEditor.class, 457 info.getName(), 458 info.getName()) { 459 Attribute attribute = attr; 460 public Object getValue() { 461 return attribute.getValue(); 462 } 463 464 public void setValue(Object val){ 465 Attribute updatedAttribute = null; 466 try { 467 updatedAttribute = parent.setSheetProperty(getName(), val); 468 } catch (RuntimeException rex) { 469 } 471 if(updatedAttribute != null){ 472 attribute = updatedAttribute; 473 } 474 } 475 476 public PropertyEditor getPropertyEditor(){ 477 return new LogLevelEditor(); 478 } 479 }; 480 } 482 PropertySupport createModifiedStringArrayReadOnlyProperty(final AppserverMgmtActiveNode parent, final Attribute attr, final MBeanAttributeInfo info, final Class classType) { 483 return new PropertySupport.ReadOnly( 484 info.getName(), 485 classType, 486 info.getName(), 487 info.getName()) { 488 char sepChar = ';'; 489 Attribute attribute = attr; 490 public Object getValue() { 491 Object val = attribute.getValue(); 492 String [] value = null; 493 if(val != null){ 494 String strVal = replacePathSeperatorToken(val.toString()); 495 if(strVal != null){ 496 sepChar = getSeperationChar(strVal); 497 } 498 value = createClasspathArray(strVal); 499 } 500 return value; 501 } 502 }; 503 } 505 public String replacePathSeperatorToken(String tokenString){ 506 String token = "path.separator"; String resolvedToken = File.pathSeparator; 508 tokenString = tokenString.replaceAll("\\$\\{"+token+"\\}", resolvedToken); return tokenString; 510 } 511 512 public String replacePathSeperator(String tokenString){ 513 String token = "path.separator"; String resolvedToken = File.pathSeparator; 515 tokenString = tokenString.replaceAll(resolvedToken, "\\$\\{"+token+"\\}"); return tokenString; 517 } 518 519 private Attribute revertAttribute(AppserverMgmtActiveNode parent, String attrName, Object attrValue, Attribute attribute){ 520 Attribute updatedAttribute = null; 521 try { 522 updatedAttribute = parent.setSheetProperty(attrName, attrValue); 523 } catch (RuntimeException rex) { 524 } 526 527 if(updatedAttribute != null){ 528 return updatedAttribute; 529 } 530 531 return attribute; 532 } 533 534 private NameValuePair[] getNameValuePairs(Object [] attrVal){ 535 NameValuePair[] pairs = new NameValuePair[attrVal.length]; 536 for (int j = 0; j < attrVal.length; j++) { 537 NameValuePair pair = (NameValuePair)attrVal[j]; 538 pairs[j] = pair; 539 } 540 return pairs; 541 } 542 543 private static String [] createClasspathArray(Object cpath){ 544 Vector path = new Vector (); 545 if(cpath != null){ 546 String classPath = cpath.toString(); 547 char sepChar = getSeperationChar(classPath); 548 while(classPath.indexOf(sepChar) != -1){ 549 int index = classPath.indexOf(sepChar); 550 String val = classPath.substring(0, index); 551 path.add(val); 552 classPath = classPath.substring(index+1, classPath.length()); 553 } 554 path.add(classPath); 555 } 556 if(path != null){ 557 Object [] finalPath = (Object [])path.toArray(); 558 String [] value = new String [finalPath.length]; 559 for(int i=0; i<finalPath.length; i++){ 560 value[i] = finalPath[i].toString(); 561 } 562 563 return value; 564 }else 565 return null; 566 } 567 568 private static char getSeperationChar(String classPath){ 569 if(classPath.indexOf(";") != -1) return ';'; 571 else 572 return ':'; 573 } 574 575 private String stripQuotes(String classPath){ 576 if(classPath.startsWith("\"")){ int index = classPath.indexOf("\""); classPath = classPath.substring(index + 1, classPath.lastIndexOf("\"")); } 580 return classPath; 581 } 582 } 583 | Popular Tags |