001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.management;
020
021import javax.management.*;
022
023import org.jppf.utils.LoggingUtils;
024import org.slf4j.*;
025
026/**
027 * Abstract super class for all generated MBean static proxies.
028 * @author Laurent Cohen
029 */
030public class AbstractMBeanStaticProxy {
031  /**
032   * Logger for this class.
033   */
034  protected static Logger log = LoggerFactory.getLogger(JMXConnectionWrapper.class);
035  /**
036   * Determines whether debug log statements are enabled.
037   */
038  protected static boolean debugEnabled = LoggingUtils.isDebugEnabled(log);
039  /**
040   * The JMX connection used to invoke remote MBean methods.
041   */
042  protected final JMXConnectionWrapper connection;
043  /**
044   * The object name of the mbean for which this object is a proxy.
045   */
046  protected final String mbeanName;
047
048  /**
049   * Initialize this mbean static proxy.
050   * @param connection the JMX connection used to invoke remote MBean methods.
051   * @param mbeanName the object name of the mbean for which this object is a proxy.
052   */
053  public AbstractMBeanStaticProxy(final JMXConnectionWrapper connection, final String mbeanName) {
054    this.connection = connection;
055    this.mbeanName = mbeanName;
056  }
057
058  /**
059   * Invoke a method on the MBean.
060   * @param methodName the name of the method to invoke.
061   * @param params the method parameter values.
062   * @param signature the types of the method parameters.
063   * @return an object or null.
064   */
065  public Object invoke(final String methodName, final Object[] params, final String[] signature) {
066    Object result = null;
067    try {
068      result = connection.invoke(mbeanName, methodName, params, signature);
069    } catch(final Exception e) {
070      if (debugEnabled) log.debug(connection.getId() + " : error while invoking a method with the JMX connection", e);
071    }
072    return result;
073  }
074
075  /**
076   * Get the value of an attribute of the specified MBean.
077   * @param attribute the name of the attribute to read.
078   * @return an object or null.
079   */
080  public Object getAttribute(final String attribute) {
081    Object result = null;
082    try {
083      result = connection.getAttribute(mbeanName, attribute);
084    } catch(final Exception e) {
085      if (debugEnabled) log.debug(connection.getId() + " : error while getting an attribute with the JMX connection", e);
086    }
087    return result;
088  }
089
090  /**
091   * Set the value of an attribute of the specified MBean.
092   * @param attribute the name of the attribute to write.
093   * @param value the value to set on the attribute.
094   */
095  public void setAttribute(final String attribute, final Object value) {
096    try {
097      connection.setAttribute(mbeanName, attribute, value);
098    } catch(final Exception e) {
099      if (debugEnabled) log.debug(connection.getId() + " : error while setting an attribute with the JMX connection", e);
100    }
101  }
102
103  /**
104   * Register a notification listener with the MBean.
105   * @param listener the listener to register.
106   * @param filter an optional filter, may be {@code null}.
107   * @param handback a handback object.
108   */
109  public void addNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback) {
110    try {
111      connection.addNotificationListener(mbeanName, listener, filter, handback);
112    } catch (final Exception e) {
113      if (debugEnabled) log.debug(connection.getId() + " : error while adding notification filter", e);
114    }
115  }
116
117  /**
118   * Unregister a notification listener from the MBean.
119   * @param listener the listener to unregister.
120   */
121  public void removeNotificationListener(final NotificationListener listener) {
122    try {
123      connection.removeNotificationListener(mbeanName, listener, null, null);
124    } catch (final Exception e) {
125      if (debugEnabled) log.debug(connection.getId() + " : error while removing notification filter", e);
126    }
127  }
128
129  /**
130   * Unregister a notification listener from the MBean.
131   * @param listener the listener to unregister.
132   * @param filter an optional filter, may be {@code null}.
133   * @param handback a handback object.
134   */
135  public void removeNotificationListener(final NotificationListener listener, final NotificationFilter filter, final Object handback) {
136    try {
137      connection.removeNotificationListener(mbeanName, listener, filter, handback);
138    } catch (final Exception e) {
139      if (debugEnabled) log.debug(connection.getId() + " : error while removing notification filter", e);
140    }
141  }
142
143  /**
144   * Get the MBean notification information.
145   * @return an array of {@link MBeanNotificationInfo} instances.
146   */
147  public MBeanNotificationInfo[] getNotificationInfo() {
148    try {
149      return connection.getNotificationInfo(mbeanName);
150    } catch (final Exception e) {
151      if (debugEnabled) log.debug(connection.getId() + " : error getting MBeanNotificationInfo[]", e);
152    }
153    return new MBeanNotificationInfo[0];
154  }
155}