1 23 24 package com.sun.enterprise.tools.admingui.tree; 25 26 import com.sun.enterprise.tools.admingui.util.MBeanUtil; 27 28 import com.sun.enterprise.tools.jsfext.component.ComponentUtil; 29 import com.sun.enterprise.tools.jsfext.component.factory.basic.TreeAdaptor; 30 import com.sun.enterprise.tools.jsfext.component.factory.basic.TreeAdaptorBase; 31 import com.sun.enterprise.tools.jsfext.util.Util; 32 33 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent; 34 35 import java.util.Arrays ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 import java.util.Properties ; 40 41 import javax.faces.component.UIComponent; 42 import javax.faces.context.FacesContext; 43 44 import javax.management.ObjectName ; 45 46 47 57 public class MBeanTreeAdaptor extends TreeAdaptorBase { 58 59 62 private MBeanTreeAdaptor() { 63 } 64 65 71 protected MBeanTreeAdaptor(LayoutComponent desc, UIComponent parent) { 72 super(desc, parent); 73 } 74 75 79 public static TreeAdaptor getInstance(FacesContext ctx, LayoutComponent desc, UIComponent parent) { 80 return new MBeanTreeAdaptor(desc, parent); 81 } 82 83 89 public void init() { 90 FacesContext ctx = FacesContext.getCurrentInstance(); 92 93 LayoutComponent desc = getLayoutComponent(); 96 97 UIComponent parent = getParentUIComponent(); 99 100 Object val = desc.getEvaluatedOption(ctx, "objectName", parent); 102 if (val == null) { 103 throw new IllegalArgumentException ( 104 "'objectName' must be specified!"); 105 } 106 _objectName = (String ) val; 107 108 val = desc.getEvaluatedOption(ctx, "methodName", parent); 110 if (val == null) { 111 throw new IllegalArgumentException ( 112 "'methodName' must be specified!"); 113 } 114 _methodName = (String ) val; 115 116 _paramsArray = null; 118 val = desc.getEvaluatedOption(ctx, "parameters", parent); 119 if (val != null) { 120 if (val instanceof List ) { 121 _paramsArray = ((List ) val).toArray(); 122 } else { 123 _paramsArray = new Object [] {val}; 124 } 125 } 126 127 _paramTypesArray = null; 129 val = desc.getEvaluatedOption(ctx, "paramTypes", parent); 130 if (val != null) { 131 if (val instanceof String ) { 132 _paramTypesArray = new String [] {(String ) val}; 133 } else if (val instanceof List ) { 134 _paramTypesArray = (String []) 135 ((List <String >) val).toArray(new String [0]); 136 } else { 137 throw new IllegalArgumentException ( 138 "'paramTypes' must be a String or a List of types!"); 139 } 140 } 141 142 _nameAtt = (String ) desc.getEvaluatedOption( 144 ctx, "attributeName", parent); 145 if (_nameAtt != null) { 146 _nameAtt = _nameAtt.trim(); 147 if (_nameAtt.length() == 0) { 148 _nameAtt = null; 149 } 150 } 151 152 _nameMethod = (String ) desc.getEvaluatedOption( 154 ctx, "attrNameMethod", parent); 155 if (_nameMethod != null) { 156 _nameMethod = _nameMethod.trim(); 157 if (_nameMethod.length() == 0) { 158 _nameMethod = null; 159 } 160 } 161 162 168 169 setTreeNodeObject(_objectName); 175 } 176 177 181 public List getChildTreeNodeObjects(Object nodeObject) { 182 if (nodeObject == null) { 183 return null; 184 } 185 if (nodeObject.toString().equals(_objectName)) { 186 if (_children != null) { 189 return Arrays.asList((Object [])_children); 190 } 191 _children = (Object []) MBeanUtil.invoke( 192 _objectName, _methodName, 193 _paramsArray, _paramTypesArray); 194 195 FacesContext ctx = FacesContext.getCurrentInstance(); 198 Object retVal = getLayoutComponent().dispatchHandlers( 199 ctx, FilterTreeEvent.EVENT_TYPE, 200 new FilterTreeEvent(getParentUIComponent(), _children)); 201 if ((retVal != null) && (retVal instanceof Object [])) { 202 _children = (Object []) retVal; 204 } 205 } else { 206 return null; 208 } 209 210 return _children != null ? Arrays.asList((Object [])_children):null; 211 } 212 213 231 public Map getFactoryOptions(Object nodeObject) { 232 if (nodeObject == null) { 233 return null; 234 } 235 236 LayoutComponent desc = getLayoutComponent(); 237 Properties props = new Properties (); 238 if (nodeObject.toString().equals(_objectName)) { 239 241 setProperty(props, "text", desc.getOption("text")); 245 setProperty(props, "url", desc.getOption("url")); 246 setProperty(props, "imageURL", desc.getOption("imageURL")); 247 setProperty(props, "target", desc.getOption("target")); 248 setProperty(props, "action", desc.getOption("action")); 249 250 setProperty(props, "actionListener", desc.getOption("actionListener")); 256 setProperty(props, "expanded", desc.getOption("expanded")); 257 } else { 258 260 263 if (nodeObject instanceof ObjectName ) { 267 if (_nameAtt != null) { 268 setProperty(props, "text", 269 (String ) MBeanUtil.getAttribute( 270 (ObjectName ) nodeObject, _nameAtt)); 271 } else if (_nameMethod != null) { 272 setProperty(props, "text", (String ) MBeanUtil.invoke( 275 (ObjectName ) nodeObject, _nameMethod, null, null)); 276 } 277 if (!props.containsKey("text")) { 278 setProperty(props, "text", nodeObject.toString()); 280 } 281 } else if (nodeObject instanceof String ) { 282 setProperty(props, "text", (String ) nodeObject); 283 288 } else { 289 throw new RuntimeException ("'" + nodeObject 290 + "' Illegal type (" 291 + nodeObject.getClass().getName() 292 + ") for tree processing"); 293 } 294 295 setProperty(props, "url", desc.getOption("childURL")); 297 setProperty(props, "imageURL", desc.getOption("childImageURL")); 298 setProperty(props, "target", desc.getOption("childTarget")); 299 setProperty(props, "action", desc.getOption("childAction")); 300 setProperty(props, "expanded", desc.getOption("childExpanded")); 303 } 304 305 return props; 307 } 308 309 312 private void setProperty(Properties props, String key, Object value) { 313 if (value != null) { 314 props.put(key, value); 315 } 316 } 317 318 322 public String getId(Object nodeObject) { 323 if (nodeObject == null) { 324 return "nullNodeObject"; 325 } 326 if (nodeObject.toString().equals(_objectName)) { 327 return getLayoutComponent().getId( 329 FacesContext.getCurrentInstance(), getParentUIComponent()); 330 } 331 return genId(nodeObject.toString()); 332 } 333 334 342 private String genId(String uid) { 343 char [] chArr = uid.toCharArray(); 344 int len = chArr.length; 345 int newIdx = 0; 346 for (int idx=0; idx<len; idx++) { 347 if (Character.isLetter(chArr[idx])) { 348 chArr[newIdx++] = chArr[idx]; 349 } 350 } 351 return new String (chArr, 0, newIdx); 352 } 353 354 360 public Map getFacets(UIComponent comp, Object nodeObject) { 361 if (nodeObject.toString().equals(_objectName)) { 362 return null; 363 } 364 LayoutComponent desc = this.getLayoutComponent(); 365 Object val = desc.getOption("childActionListener"); 366 Map map = null; 367 if (val != null) { 368 Properties props = new Properties (); 370 props.put("actionListener", val.toString()); 371 setProperty(props, "target", desc.getOption("childTarget")); 372 setProperty(props, "text", comp.getAttributes().get("text")); 373 374 UIComponent link = ComponentUtil.getChild( 376 (UIComponent) null, "link", 377 "com.sun.enterprise.tools.jsfext.component.factory.basic.HyperlinkFactory", 378 props); 379 380 List handlers = desc.getHandlers("childCommand"); 382 if (handlers != null) { 383 link.getAttributes().put("command", handlers); 384 } 385 386 map = new HashMap (); 388 map.put("content", link); 389 } 390 return map; 391 } 392 393 404 public Map getHandlersByType(UIComponent comp, Object nodeObject) { 405 406 415 return null; 416 } 417 418 431 432 435 private String _methodName = null; 436 437 440 private String _objectName = null; 441 442 445 private Object [] _paramsArray = null; 446 447 450 private String [] _paramTypesArray= null; 451 452 455 private String _nameAtt = null; 456 457 460 private String _nameMethod = null; 461 462 465 private Object [] _children = null; 466 } 467 | Popular Tags |