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.io.ObjectInputStream;
022import java.util.Set;
023
024import javax.management.*;
025import javax.management.loading.ClassLoaderRepository;
026import javax.management.remote.MBeanServerForwarder;
027
028/**
029 * This class is an adapter for the {@link MBeanServerForwarder} interface. It implements all the methods of the interface
030 * and merely delegates to the underlying {@link MBeanServer} if it has been set. When the {@link MBeanServer} is not set
031 * the methods do nothing and/or return {@code null}.
032 * <p>This class is provided as a convenience for when only a few methods need to be overriden in a subclass.
033 * @author Laurent Cohen
034 */
035public class MBeanServerForwarderAdapter implements MBeanServerForwarder {
036  /**
037   * The underlyng {@link MBeanServer} this object delegates to.
038   */
039  private MBeanServer mbs;
040  /**
041   * Optional parameters that may be passed by the configuration.
042   */
043  protected String[] parameters;
044
045  @Override
046  public ObjectInstance createMBean(final String className, final ObjectName name)
047    throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException {
048    return (mbs == null) ? null : mbs.createMBean(className, name);
049  }
050
051  @Override
052  public ObjectInstance createMBean(final String className, final ObjectName name, final ObjectName loaderName)
053    throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException {
054    return (mbs == null) ? null : mbs.createMBean(className, name, loaderName);
055  }
056
057  @Override
058  public ObjectInstance createMBean(final String className, final ObjectName name, final Object[] params, final String[] signature)
059    throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException {
060    return (mbs == null) ? null : mbs.createMBean(className, name, params, signature);
061  }
062
063  @Override
064  public ObjectInstance createMBean(final String className, final ObjectName name, final ObjectName loaderName, final Object[] params, final String[] signature)
065    throws ReflectionException, InstanceAlreadyExistsException, MBeanRegistrationException, MBeanException, NotCompliantMBeanException, InstanceNotFoundException {
066    return (mbs == null) ? null : mbs.createMBean(className, name, loaderName, params, signature);
067  }
068
069  @Override
070  public ObjectInstance registerMBean(final Object object, final ObjectName name) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
071    return (mbs == null) ? null : mbs.registerMBean(object, name);
072  }
073
074  @Override
075  public void unregisterMBean(final ObjectName name) throws InstanceNotFoundException, MBeanRegistrationException {
076    if (mbs != null) mbs.unregisterMBean(name);
077  }
078
079  @Override
080  public ObjectInstance getObjectInstance(final ObjectName name) throws InstanceNotFoundException {
081    return (mbs == null) ? null : mbs.getObjectInstance(name);
082  }
083
084  @Override
085  public Set<ObjectInstance> queryMBeans(final ObjectName name, final QueryExp query) {
086    return (mbs == null) ? null : mbs.queryMBeans(name, query);
087  }
088
089  @Override
090  public Set<ObjectName> queryNames(final ObjectName name, final QueryExp query) {
091    return (mbs == null) ? null : mbs.queryNames(name, query);
092  }
093
094  @Override
095  public boolean isRegistered(final ObjectName name) {
096    return (mbs == null) ? null : mbs.isRegistered(name);
097  }
098
099  @Override
100  public Integer getMBeanCount() {
101    return (mbs == null) ? null : mbs.getMBeanCount();
102  }
103
104  @Override
105  public Object getAttribute(final ObjectName name, final String attribute) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
106    return (mbs == null) ? null : mbs.getAttribute(name, attribute);
107  }
108
109  @Override
110  public AttributeList getAttributes(final ObjectName name, final String[] attributes) throws InstanceNotFoundException, ReflectionException {
111    return (mbs == null) ? null : mbs.getAttributes(name, attributes);
112  }
113
114  @Override
115  public void setAttribute(final ObjectName name, final Attribute attribute) throws InstanceNotFoundException, AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
116    mbs.setAttribute(name, attribute);
117  }
118
119  @Override
120  public AttributeList setAttributes(final ObjectName name, final AttributeList attributes) throws InstanceNotFoundException, ReflectionException {
121    return (mbs == null) ? null : mbs.setAttributes(name, attributes);
122  }
123
124  @Override
125  public Object invoke(final ObjectName name, final String operationName, final Object[] params, final String[] signature) throws InstanceNotFoundException, MBeanException, ReflectionException {
126    return (mbs == null) ? null : mbs.invoke(name, operationName, params, signature);
127  }
128
129  @Override
130  public String getDefaultDomain() {
131    return (mbs == null) ? null : mbs.getDefaultDomain();
132  }
133
134  @Override
135  public String[] getDomains() {
136    return (mbs == null) ? null : mbs.getDomains();
137  }
138
139  @Override
140  public void addNotificationListener(final ObjectName name, final NotificationListener listener, final NotificationFilter filter, final Object handback) throws InstanceNotFoundException {
141    if (mbs != null) mbs.addNotificationListener(name, listener, filter, handback);
142  }
143
144  @Override
145  public void addNotificationListener(final ObjectName name, final ObjectName listener, final NotificationFilter filter, final Object handback) throws InstanceNotFoundException {
146    if (mbs != null) mbs.addNotificationListener(name, listener, filter, handback);
147  }
148
149  @Override
150  public void removeNotificationListener(final ObjectName name, final ObjectName listener) throws InstanceNotFoundException, ListenerNotFoundException {
151    if (mbs != null) mbs.removeNotificationListener(name, listener);
152  }
153
154  @Override
155  public void removeNotificationListener(final ObjectName name, final ObjectName listener, final NotificationFilter filter, final Object handback) throws InstanceNotFoundException, ListenerNotFoundException {
156    if (mbs != null) mbs.removeNotificationListener(name, listener, filter, handback);
157  }
158
159  @Override
160  public void removeNotificationListener(final ObjectName name, final NotificationListener listener) throws InstanceNotFoundException, ListenerNotFoundException {
161    if (mbs != null) mbs.removeNotificationListener(name, listener);
162  }
163
164  @Override
165  public void removeNotificationListener(final ObjectName name, final NotificationListener listener, final NotificationFilter filter, final Object handback) throws InstanceNotFoundException, ListenerNotFoundException {
166    mbs.removeNotificationListener(name, listener, filter, handback);
167  }
168
169  @Override
170  public MBeanInfo getMBeanInfo(final ObjectName name) throws InstanceNotFoundException, IntrospectionException, ReflectionException {
171    return (mbs == null) ? null : mbs.getMBeanInfo(name);
172  }
173
174  @Override
175  public boolean isInstanceOf(final ObjectName name, final String className) throws InstanceNotFoundException {
176    return (mbs == null) ? null : mbs.isInstanceOf(name, className);
177  }
178
179  @Override
180  public Object instantiate(final String className) throws ReflectionException, MBeanException {
181    return (mbs == null) ? null : mbs.instantiate(className);
182  }
183
184  @Override
185  public Object instantiate(final String className, final ObjectName loaderName) throws ReflectionException, MBeanException, InstanceNotFoundException {
186    return (mbs == null) ? null : mbs.instantiate(className, loaderName);
187  }
188
189  @Override
190  public Object instantiate(final String className, final Object[] params, final String[] signature) throws ReflectionException, MBeanException {
191    return (mbs == null) ? null : mbs.instantiate(className, params, signature);
192  }
193
194  @Override
195  public Object instantiate(final String className, final ObjectName loaderName, final Object[] params, final String[] signature) throws ReflectionException, MBeanException, InstanceNotFoundException {
196    return (mbs == null) ? null : mbs.instantiate(className, loaderName, params, signature);
197  }
198
199  @Override
200  @SuppressWarnings("deprecation")
201  public ObjectInputStream deserialize(final ObjectName name, final byte[] data) throws InstanceNotFoundException, OperationsException {
202    return (mbs == null) ? null : mbs.deserialize(name, data);
203  }
204
205  @Override
206  @SuppressWarnings("deprecation")
207  public ObjectInputStream deserialize(final String className, final byte[] data) throws OperationsException, ReflectionException {
208    return (mbs == null) ? null : mbs.deserialize(className, data);
209  }
210
211  @Override
212  @SuppressWarnings("deprecation")
213  public ObjectInputStream deserialize(final String className, final ObjectName loaderName, final byte[] data) throws InstanceNotFoundException, OperationsException, ReflectionException {
214    return (mbs == null) ? null : mbs.deserialize(className, loaderName, data);
215  }
216
217  @Override
218  public ClassLoader getClassLoaderFor(final ObjectName mbeanName) throws InstanceNotFoundException {
219    return (mbs == null) ? null : mbs.getClassLoaderFor(mbeanName);
220  }
221
222  @Override
223  public ClassLoader getClassLoader(final ObjectName loaderName) throws InstanceNotFoundException {
224    return (mbs == null) ? null : mbs.getClassLoader(loaderName);
225  }
226
227  @Override
228  public ClassLoaderRepository getClassLoaderRepository() {
229    return (mbs == null) ? null : mbs.getClassLoaderRepository();
230  }
231
232  @Override
233  public MBeanServer getMBeanServer() {
234    return mbs;
235  }
236
237  @Override
238  public void setMBeanServer(final MBeanServer mbs) {
239    this.mbs = mbs;
240  }
241
242  /**
243   * Set the parameters defined in the configuration, if any.
244   * @param parameters the parameters as an array of strings.
245   */
246  public void setParameters(final String[] parameters) {
247    this.parameters = parameters;
248  }
249
250  /**
251   * Get the parameters defined in the configuration, if any.
252   * @return the parameters as an array of strings.
253   */
254  public String[] getParameters() {
255    return parameters;
256  }
257}