KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > jmx > component > UIOperation


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlets.jmx.component;
6
7 import javax.management.MBeanOperationInfo JavaDoc;
8 import javax.management.MBeanServer JavaDoc;
9 import javax.management.MBeanParameterInfo JavaDoc ;
10 import javax.management.ObjectName JavaDoc;
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 /**
16  * @email: tuan08@users.sourceforge.net
17  * @version: $Id: UIOperation.java,v 1.2 2004/08/01 18:56:11 tuan08 Exp $
18  */

19 public class UIOperation extends UIExoCommand {
20   final static public String JavaDoc EXECUTE_ACTION = "execute" ;
21   final static public String JavaDoc[] EMPTY_PARAM = new String JavaDoc[0] ;
22   
23   private MBeanServer JavaDoc mserver_ ;
24   private MBeanOperationInfo JavaDoc operation_ ;
25   private ObjectName JavaDoc name_ ;
26   private Object JavaDoc 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 JavaDoc getFamily() {
37     return "org.exoplatform.portlets.jmx.component.UIOperation" ;
38   }
39   
40   public MBeanOperationInfo JavaDoc getMBeanOperationInfo() { return operation_; }
41   
42   public void setOperation(MBeanServer JavaDoc server, ObjectName JavaDoc name, MBeanOperationInfo JavaDoc operation) {
43     operation_ = operation ;
44     mserver_ = server ;
45     name_ = name ;
46     result_ = null ;
47   }
48  
49   public Object JavaDoc getResult() { return result_ ; }
50   
51   public void execute(String JavaDoc[] param) throws Exception JavaDoc {
52     if(param == null) param = EMPTY_PARAM ;
53     MBeanParameterInfo JavaDoc[] paramInfo = operation_.getSignature() ;
54     String JavaDoc[] signatures = new String JavaDoc[param.length] ;
55     Object JavaDoc[] args = new Object JavaDoc[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 JavaDoc getParameter(String JavaDoc value, String JavaDoc type) throws Exception JavaDoc {
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 JavaDoc(value) ;
67     if(type.equals("java.lang.Integer") ||
68        type.equals(Integer.TYPE.getName())) return new Integer JavaDoc(value) ;
69     if(type.equals("java.lang.Long") ||
70         type.equals(Long.TYPE.getName())) return new Long JavaDoc(value) ;
71     if(type.equals("java.lang.Float") ||
72         type.equals(Float.TYPE.getName())) return new Float JavaDoc(value) ;
73     if(type.equals("java.lang.Double") ||
74        type.equals(Double.TYPE.getName())) return new Double JavaDoc(value) ;
75     throw new Exception JavaDoc("We do not support parameter type: " + type) ;
76   }
77   
78   static public class ExecuteActionListener extends ExoActionListener {
79     public void execute(ExoActionEvent event) throws Exception JavaDoc {
80         UIOperation uiOperation = (UIOperation) event.getComponent() ;
81         String JavaDoc[] 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 JavaDoc {
88       UIOperation uiOperation = (UIOperation) event.getComponent() ;
89       uiOperation.setRenderedSibling(UIMBean.class) ;
90     }
91   }
92 }
Popular Tags