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