1 5 package org.exoplatform.portlets.jmx.component; 6 7 import javax.management.MBeanOperationInfo ; 8 import javax.management.MBeanServer ; 9 import javax.management.MBeanParameterInfo ; 10 import javax.management.ObjectName ; 11 import org.exoplatform.faces.core.component.UIExoCommand; 12 import org.exoplatform.faces.core.event.ExoActionEvent; 13 import org.exoplatform.faces.core.event.ExoActionListener; 14 15 19 public class UIOperation extends UIExoCommand { 20 final static public String EXECUTE_ACTION = "execute" ; 21 final static public String [] EMPTY_PARAM = new String [0] ; 22 23 private MBeanServer mserver_ ; 24 private MBeanOperationInfo operation_ ; 25 private ObjectName name_ ; 26 private Object result_ ; 27 28 public UIOperation() { 29 setId("UIOperation") ; 30 setClazz("UIOperation") ; 31 setRendererType("OperationRenderer") ; 32 addActionListener(ExecuteActionListener.class, EXECUTE_ACTION) ; 33 addActionListener(CancelActionListener.class, CANCEL_ACTION) ; 34 } 35 36 public String getFamily() { 37 return "org.exoplatform.portlets.jmx.component.UIOperation" ; 38 } 39 40 public MBeanOperationInfo getMBeanOperationInfo() { return operation_; } 41 42 public void setOperation(MBeanServer server, ObjectName name, MBeanOperationInfo operation) { 43 operation_ = operation ; 44 mserver_ = server ; 45 name_ = name ; 46 result_ = null ; 47 } 48 49 public Object getResult() { return result_ ; } 50 51 public void execute(String [] param) throws Exception { 52 if(param == null) param = EMPTY_PARAM ; 53 MBeanParameterInfo [] paramInfo = operation_.getSignature() ; 54 String [] signatures = new String [param.length] ; 55 Object [] args = new Object [param.length] ; 56 for(int i = 0 ; i < param.length ; i++) { 57 signatures[i] = paramInfo[i].getType() ; 58 args[i] = getParameter(param[i], signatures[i]) ; 59 } 60 result_ = mserver_.invoke(name_, operation_.getName(), args, signatures) ; 61 } 62 63 private Object getParameter(String value, String type) throws Exception { 64 if(type.equals("java.lang.String")) return value ; 65 if(type.equals("java.lang.Boolean") || 66 type.equals(Boolean.TYPE.getName())) return new Boolean (value) ; 67 if(type.equals("java.lang.Integer") || 68 type.equals(Integer.TYPE.getName())) return new Integer (value) ; 69 if(type.equals("java.lang.Long") || 70 type.equals(Long.TYPE.getName())) return new Long (value) ; 71 if(type.equals("java.lang.Float") || 72 type.equals(Float.TYPE.getName())) return new Float (value) ; 73 if(type.equals("java.lang.Double") || 74 type.equals(Double.TYPE.getName())) return new Double (value) ; 75 throw new Exception ("We do not support parameter type: " + type) ; 76 } 77 78 static public class ExecuteActionListener extends ExoActionListener { 79 public void execute(ExoActionEvent event) throws Exception { 80 UIOperation uiOperation = (UIOperation) event.getComponent() ; 81 String [] param = event.getParameterValues("parameter") ; 82 uiOperation.execute(param) ; 83 } 84 } 85 86 static public class CancelActionListener extends ExoActionListener { 87 public void execute(ExoActionEvent event) throws Exception { 88 UIOperation uiOperation = (UIOperation) event.getComponent() ; 89 uiOperation.setRenderedSibling(UIMBean.class) ; 90 } 91 } 92 } | Popular Tags |