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 java.util.Map;
022
023import org.jppf.classloader.DelegationModel;
024import org.jppf.management.diagnostics.DiagnosticsMBean;
025import org.jppf.node.provisioning.JPPFNodeProvisioningMBean;
026import org.jppf.utils.*;
027import org.slf4j.*;
028
029/**
030 * Node-specific JMX connection wrapper, implementing a user-friendly interface for the monitoring and management of a node.
031 *  Note that this class implements the interface {@link org.jppf.management.JPPFNodeAdminMBean JPPFNodeAdminMBean}.
032 * @author Laurent Cohen
033 */
034public class JMXNodeConnectionWrapper extends JMXConnectionWrapper implements JPPFNodeAdminMBean {
035  /**
036   * Explicit serialVersionUID.
037   */
038  private static final long serialVersionUID = 1L;
039  /**
040   * Logger for this class.
041   */
042  private static Logger log = LoggerFactory.getLogger(JMXNodeConnectionWrapper.class);
043  /**
044   * Determines whether debug log statements are enabled.
045   */
046  private static boolean debugEnabled = LoggingUtils.isDebugEnabled(log);
047
048  /**
049   * Initialize a local connection to the MBean server.
050   */
051  public JMXNodeConnectionWrapper() {
052    local = true;
053  }
054
055  /**
056   * Initialize the connection to the remote MBean server.
057   * @param host the host the server is running on.
058   * @param port the RMI port used by the server.
059   */
060  public JMXNodeConnectionWrapper(final String host, final int port) {
061    this(host, port, false);
062  }
063
064  /**
065   * Initialize the connection to the remote MBean server.
066   * @param host the host the server is running on.
067   * @param port the port used by the server.
068   * @param secure specifies whether the connection should be established over SSL/TLS.
069   */
070  public JMXNodeConnectionWrapper(final String host, final int port, final boolean secure) {
071    super(host, port, secure);
072    local = false;
073  }
074
075  /**
076   * Get the latest state information from the node.
077   * @return a <code>JPPFNodeState</code> information.
078   * @throws Exception if an error occurs while invoking the Node MBean.
079   */
080  @Override
081  public JPPFNodeState state() throws Exception {
082    return (JPPFNodeState) invoke(JPPFNodeAdminMBean.MBEAN_NAME,        "state");
083  }
084
085  /**
086   * Set the size of the node's thread pool.
087   * @param size the size as an int.
088   * @throws Exception if an error occurs while invoking the Node MBean.
089   */
090  @Override
091  public void updateThreadPoolSize(final Integer size) throws Exception {
092    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "updateThreadPoolSize", new Object[] { size }, new String[] { "java.lang.Integer" });
093  }
094
095  /**
096   * Get detailed information about the node's JVM properties, environment variables
097   * and runtime information such as memory usage and available processors.
098   * @return a <code>JPPFSystemInformation</code> instance.
099   * @throws Exception if an error occurs while invoking the Node MBean.
100   */
101  @Override
102  public JPPFSystemInformation systemInformation() throws Exception {
103    return (JPPFSystemInformation) invoke(JPPFNodeAdminMBean.MBEAN_NAME,        "systemInformation");
104  }
105
106  /**
107   * Shutdown the node.
108   * @throws Exception if an error is raised when invoking the node mbean.
109   */
110  @Override
111  public void shutdown() throws Exception {
112    if (debugEnabled) log.debug("node " + this + " shutdown requested");
113    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "shutdown");
114  }
115
116  /**
117   * {@inheritDoc}
118   * @since 5.0
119   */
120  @Override
121  public void shutdown(final Boolean interruptIfRunning) throws Exception {
122    if (debugEnabled) log.debug("node {} shutdown requested with interruptIfRunning = {}", this, interruptIfRunning);
123    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "shutdown", new Object[] {interruptIfRunning}, new String[] {Boolean.class.getName()});
124  }
125
126  /**
127   * Restart the node.
128   * @throws Exception if an error is raised when invoking the node mbean.
129   */
130  @Override
131  public void restart() throws Exception {
132    if (debugEnabled) log.debug("node " + this + " restart requested");
133    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "restart");
134  }
135
136  /**
137   * {@inheritDoc}
138   * @since 5.0
139   */
140  @Override
141  public void restart(final Boolean interruptIfRunning) throws Exception {
142    if (debugEnabled) log.debug("node {} restart requested with interruptIfRunning = {}", this, interruptIfRunning);
143    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "restart", new Object[] {interruptIfRunning}, new String[] {Boolean.class.getName()});
144  }
145
146  /**
147   * Reset the node's executed tasks counter to zero.
148   * @throws Exception if an error is raised when invoking the node mbean.
149   */
150  @Override
151  public void resetTaskCounter() throws Exception {
152    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "resetTaskCounter");
153  }
154
155  /**
156   * Set the node's executed tasks counter to the specified value.
157   * @param n the new value of the task counter.
158   * @throws Exception if an error is raised when invoking the node mbean.
159   */
160  @Override
161  public void setTaskCounter(final Integer n) throws Exception {
162    setAttribute(JPPFNodeAdminMBean.MBEAN_NAME, "TaskCounter", n);
163  }
164
165  /**
166   * Update the priority of all execution threads.
167   * @param newPriority the new priority to set.
168   * @throws Exception if an error is raised when invoking the node mbean.
169   */
170  @Override
171  public void updateThreadsPriority(final Integer newPriority) throws Exception {
172    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "updateThreadsPriority", new Object[] { newPriority }, new String[] { "java.lang.Integer" });
173  }
174
175  /**
176   * Update the configuration properties of the node.
177   * @param configOverrides the set of properties to update.
178   * @param restart specifies whether the node should be restarted after updating the properties.
179   * @throws Exception if an error is raised when invoking the node mbean.
180   */
181  @Override
182  public void updateConfiguration(final Map<Object, Object> configOverrides, final Boolean restart) throws Exception {
183    updateConfiguration(configOverrides, restart, true);
184  }
185
186  /**
187   * Update the configuration properties of the node.
188   * @param configOverrides the set of properties to update.
189   * @param restart specifies whether the node should be restarted after updating the properties.
190   * @param interruptIfRunning when {@code true}, then restart the node even if it is executing tasks, when {@code false}, then only shutdown the node when it is no longer executing.
191   * This parameter only applies when the {@code restart} parameter is {@code true}.
192   * @throws Exception if an error is raised when invoking the node mbean.
193   * @since 5.2
194   */
195  @Override
196  public void updateConfiguration(final Map<Object, Object> configOverrides, final Boolean restart, final Boolean interruptIfRunning) throws Exception {
197    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "updateConfiguration",
198        new Object[] { configOverrides, restart, interruptIfRunning }, new String[] { "java.util.Map", "java.lang.Boolean", "java.lang.Boolean" });
199  }
200
201  /**
202   * Cancel the job with the specified id.
203   * @param jobUuid the id of the job to cancel.
204   * @param requeue true if the job should be requeued on the server side, false otherwise.
205   * @throws Exception if any error occurs.
206   */
207  @Override
208  public void cancelJob(final String jobUuid, final Boolean requeue) throws Exception {
209    invoke(JPPFNodeAdminMBean.MBEAN_NAME, "cancelJob", new Object[] { jobUuid, requeue }, new String[] { "java.lang.String", "java.lang.Boolean" });
210  }
211
212  @Override
213  public DelegationModel getDelegationModel() throws Exception {
214    return (DelegationModel) getAttribute(JPPFNodeAdminMBean.MBEAN_NAME, "DelegationModel");
215  }
216
217  @Override
218  public void setDelegationModel(final DelegationModel model) throws Exception {
219    setAttribute(JPPFNodeAdminMBean.MBEAN_NAME, "DelegationModel", model);
220  }
221
222  @Override
223  public DiagnosticsMBean getDiagnosticsProxy() throws Exception {
224    return getProxy(DiagnosticsMBean.MBEAN_NAME_NODE, DiagnosticsMBean.class);
225  }
226
227  @Override
228  public NodePendingAction pendingAction() {
229    try {
230      return (NodePendingAction) invoke(JPPFNodeAdminMBean.MBEAN_NAME,  "pendingAction");
231    } catch (final Exception e) {
232      if (debugEnabled) log.debug("error invoking {} on MBean {}: {}", ReflectionUtils.getCurrentMethodName(), JPPFNodeAdminMBean.MBEAN_NAME, ExceptionUtils.getStackTrace(e));
233    }
234    return null;
235  }
236
237  @Override
238  public boolean cancelPendingAction() {
239    try {
240      return (Boolean) invoke(JPPFNodeAdminMBean.MBEAN_NAME,  "hasPendingAction");
241    } catch (final Exception e) {
242      if (debugEnabled) log.debug("error invoking {} on MBean {}: {}", ReflectionUtils.getCurrentMethodName(), JPPFNodeAdminMBean.MBEAN_NAME, ExceptionUtils.getStackTrace(e));
243    }
244    return false;
245  }
246
247  /**
248   * A shortcut method for {@code getProxy(JPPFNodeMaintenanceMBean.MBEAN_NAME, JPPFNodeMaintenanceMBean.class)}.
249   * @return a dynamic proxy implementing the {@link JPPFNodeMaintenanceMBean} interface.
250   * @throws Exception if any error occurs.
251   * @since 5.2
252   */
253  public JPPFNodeMaintenanceMBean getJPPFNodeMaintenanceMProxy() throws Exception {
254    return getProxy(JPPFNodeMaintenanceMBean.MBEAN_NAME, JPPFNodeMaintenanceMBean.class);
255  }
256
257  /**
258   * A shortcut method for {@code getProxy(JPPFNodeTaskMonitorMBean.MBEAN_NAME, JPPFNodeTaskMonitorMBean.class)}.
259   * @return a dynamic proxy implementing the {@link JPPFNodeTaskMonitorMBean} interface.
260   * @throws Exception if any error occurs.
261   * @since 5.2
262   */
263  public JPPFNodeTaskMonitorMBean getJPPFNodeTaskMonitorProxy() throws Exception {
264    return getProxy(JPPFNodeTaskMonitorMBean.MBEAN_NAME, JPPFNodeTaskMonitorMBean.class);
265  }
266
267  /**
268   * A shortcut method for {@code getProxy(JPPFNodeProvisioningMBean.MBEAN_NAME, JPPFNodeProvisioningMBean.class)}.
269   * @return a dynamic proxy implementing the {@link JPPFNodeProvisioningMBean} interface.
270   * @throws Exception if any error occurs.
271   * @since 5.2
272   */
273  public JPPFNodeProvisioningMBean getJPPFNodeProvisioningProxy() throws Exception {
274    return getProxy(JPPFNodeProvisioningMBean.MBEAN_NAME, JPPFNodeProvisioningMBean.class);
275  }
276}