ManagementReprImplJSR160.java

00001 
00025 package org.objectweb.jonas.jmx;
00026 
00027 import java.util.HashSet;
00028 import java.util.Properties;
00029 import java.util.Set;
00030 
00031 import javax.management.Attribute;
00032 import javax.management.AttributeList;
00033 import javax.management.MBeanException;
00034 import javax.management.MBeanInfo;
00035 import javax.management.MBeanServerConnection;
00036 import javax.management.ObjectName;
00037 import javax.management.ReflectionException;
00038 import javax.management.RuntimeErrorException;
00039 import javax.management.RuntimeMBeanException;
00040 import javax.management.RuntimeOperationsException;
00041 import javax.naming.Context;
00042 
00043 import org.objectweb.jonas.common.Log;
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045 import org.objectweb.util.monolog.api.Logger;
00046 
00051 public class ManagementReprImplJSR160 implements ManagementRepr {
00052 
00056     private static Logger logger = null;
00061     private MBeanServerConnection mbeanServerConnection = null;
00062 
00066     protected ManagementReprImplJSR160() {
00067         logger = Log.getLogger("org.objectweb.jonas.jmx");
00068         logger.log(BasicLevel.DEBUG, "Management Representativ based on JSR160 created for jonasAdmin");
00069     }
00070 
00075     public boolean isRegistered(ObjectName on) {
00076         try {
00077             return mbeanServerConnection.isRegistered(on);
00078         } catch (Exception e) {
00079             return false;
00080         }
00081     }
00082 
00089     public Object getAttribute(ObjectName on, String attribute)
00090     throws ManagementException {
00091 
00092         try {
00093             return mbeanServerConnection.getAttribute(on, attribute);
00094         } catch (Exception e) {
00095             throw new ManagementException("Error while getting attribute " + attribute + ": "
00096                     + e.getClass().getName(), e);
00097         }
00098     }
00099 
00106     public AttributeList getAttributes(ObjectName on, String[] attributes)
00107     throws ManagementException {
00108 
00109         try {
00110             return mbeanServerConnection.getAttributes(on, attributes);
00111         } catch (Exception e) {
00112             throw new ManagementException("Error while getting attributes: "
00113                     + e.getClass().getName(), e);
00114         }
00115     }
00116 
00123     public void setAttribute(ObjectName on, String attribute, Object value)
00124     throws ManagementException {
00125         if(logger.isLoggable(BasicLevel.DEBUG)) {
00126             logger.log(BasicLevel.DEBUG
00127                     , "Set Attribute called, on " + on.toString() + " to change attribute " + attribute
00128                     + " value to " + (String) value.toString());
00129         }
00130 
00131         try {
00132             mbeanServerConnection.setAttribute(on, new Attribute(attribute, value));
00133         } catch (Exception e) {
00134             throw new ManagementException("Error while setting attribute " + attribute + ": "
00135                     + e.getClass().getName(), e);
00136         }
00137     }
00138 
00145     public AttributeList setAttributes(ObjectName on, AttributeList attributes)
00146     throws ManagementException {
00147 
00148         try {
00149             return mbeanServerConnection.setAttributes(on, attributes);
00150         } catch (Exception e) {
00151             throw new ManagementException("Error while setting attributes: "
00152                     + e.getClass().getName(), e);
00153         }
00154     }
00155 
00160     public Object invoke(ObjectName on, String operation, Object[] param, String[] signature)
00161     throws ManagementException {
00162 
00163         try {
00164             return mbeanServerConnection.invoke(on, operation, param, signature);
00165         } catch (Exception e) {
00166             String message = "";
00167             String targetExcName = null;
00168             Throwable exc = null;
00169             if (e instanceof MBeanException ||
00170                     e instanceof ReflectionException ||
00171                     e instanceof RuntimeMBeanException ||
00172                     e instanceof RuntimeOperationsException ||
00173                     e instanceof RuntimeErrorException) {
00174 
00175                 Exception targetExc = null;
00176                 if (e instanceof MBeanException)                {
00177                     targetExc = ((MBeanException) e).getTargetException();
00178                 }
00179                 else if (e instanceof ReflectionException) {
00180                     targetExc = ((ReflectionException) e).getTargetException();
00181                 }
00182                 else if (e instanceof RuntimeMBeanException) {
00183                     targetExc = ((RuntimeMBeanException) e).getTargetException();
00184                 }
00185                 else if (e instanceof RuntimeOperationsException) {
00186                     targetExc = ((RuntimeOperationsException) e).getTargetException();
00187                 }
00188                 else if (e instanceof RuntimeErrorException) {
00189                     Error atargetExc = ((RuntimeErrorException) e).getTargetError();
00190                     targetExc = new Exception(atargetExc.getMessage());
00191                 }
00192                 targetExcName = targetExc.toString();
00193                 exc = targetExc;
00194             }
00195             else {
00196                 exc = e;
00197             }
00198             if(logger.isLoggable(BasicLevel.DEBUG)) {
00199                 logger.log(BasicLevel.DEBUG
00200                         , "Exception ----[ " + e.toString() + " while invoking operation " + operation +
00201                         " on MBean " + on.toString() + " ]-------");
00202                 if (targetExcName != null) {
00203                     logger.log(BasicLevel.DEBUG, "-------[ Embedded error : ]-------");
00204                     logger.log(BasicLevel.DEBUG, "-------[ " + targetExcName + " ]-------");
00205                 }
00206             }
00207 
00208             throw new ManagementException(message, exc);
00209         }
00210     }
00211 
00216     public java.util.Set queryNames(ObjectName on)
00217     throws ManagementException {
00218         try {
00219             return (java.util.Set) mbeanServerConnection.queryNames(on, null);
00220         } catch (Exception e) {
00221             throw new ManagementException("Error while getting MBeans names: " + e.getClass().getName()
00222                     , e);
00223         }
00224     }
00225 
00231     public MBeanInfo getMBeanInfo(ObjectName name)
00232     throws ManagementException {
00233         try {
00234             return (MBeanInfo) mbeanServerConnection.getMBeanInfo(name);
00235         } catch (Exception e) {
00236             throw new ManagementException("Error while getting MBean info: " + e.getClass().getName()
00237                     , e);
00238         }
00239     }
00240 
00246     public Context getContext() throws javax.naming.NamingException {
00247         return null;
00248     }
00249 
00254     public String getCurrentRMIConnectorName() {
00255         return "jonas";
00256     }
00257 
00261     public void setCurrentRMIConnectorName(String name) throws Exception {
00262         ;
00263     }
00264 
00268     public void resetCurrentRMIConnectorName() {
00269         ;
00270     }
00271 
00277     public Set getRMIConnectorsNames() throws javax.naming.NamingException {
00278         HashSet res = new HashSet();
00279         res.add("jonas");
00280         return res;
00281     }
00282 
00287     public String getJonasNamingServiceURL() {
00288         return "url";
00289     }
00290 
00295     public void setJonasNamingServiceURL(String url) throws javax.naming.NamingException {
00296         ;
00297     }
00298 
00303     public void setNamingEnvCtx(Properties env) throws javax.naming.NamingException {
00304         ;
00305     }
00309     public MBeanServerConnection getMBeanServerConnection() {
00310         return mbeanServerConnection;
00311     }
00315     public void setMBeanServerConnection(
00316             MBeanServerConnection mbeanServerConnection) {
00317         this.mbeanServerConnection = mbeanServerConnection;
00318     }
00319 }

Generated on Tue Feb 15 15:05:17 2005 for JOnAS by  doxygen 1.3.9.1