Exemple #1
0
  /* ------------------------------------------------------------ */
  public Object getAttribute(String name)
      throws AttributeNotFoundException, MBeanException, ReflectionException {
    Method getter = (Method) _getters.get(name);
    if (getter == null) throw new AttributeNotFoundException(name);
    try {
      Object o = _managed;
      if (getter.getDeclaringClass().isInstance(this)) o = this; // mbean method

      // get the attribute
      Object r = getter.invoke(o, (java.lang.Object[]) null);

      // convert to ObjectName if need be.
      if (r != null && _convert.contains(name)) {
        if (r.getClass().isArray()) {
          ObjectName[] on = new ObjectName[Array.getLength(r)];
          for (int i = 0; i < on.length; i++) on[i] = _mbeanContainer.findMBean(Array.get(r, i));
          r = on;
        } else if (r instanceof Collection<?>) {
          Collection<Object> c = (Collection<Object>) r;
          ObjectName[] on = new ObjectName[c.size()];
          int i = 0;
          for (Object obj : c) on[i++] = _mbeanContainer.findMBean(obj);
          r = on;
        } else {
          ObjectName mbean = _mbeanContainer.findMBean(r);
          if (mbean == null) return null;
          r = mbean;
        }
      }
      return r;
    } catch (IllegalAccessException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new AttributeNotFoundException(e.toString());
    } catch (InvocationTargetException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new ReflectionException(new Exception(e.getCause()));
    }
  }
Exemple #2
0
  /* ------------------------------------------------------------ */
  public void setAttribute(Attribute attr)
      throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException,
          ReflectionException {
    if (attr == null) return;

    if (LOG.isDebugEnabled())
      LOG.debug("setAttribute " + _managed + ":" + attr.getName() + "=" + attr.getValue());
    Method setter = (Method) _setters.get(attr.getName());
    if (setter == null) throw new AttributeNotFoundException(attr.getName());
    try {
      Object o = _managed;
      if (setter.getDeclaringClass().isInstance(this)) o = this;

      // get the value
      Object value = attr.getValue();

      // convert from ObjectName if need be
      if (value != null && _convert.contains(attr.getName())) {
        if (value.getClass().isArray()) {
          Class t = setter.getParameterTypes()[0].getComponentType();
          Object na = Array.newInstance(t, Array.getLength(value));
          for (int i = Array.getLength(value); i-- > 0; )
            Array.set(na, i, _mbeanContainer.findBean((ObjectName) Array.get(value, i)));
          value = na;
        } else value = _mbeanContainer.findBean((ObjectName) value);
      }

      // do the setting
      setter.invoke(o, new Object[] {value});
    } catch (IllegalAccessException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new AttributeNotFoundException(e.toString());
    } catch (InvocationTargetException e) {
      LOG.warn(Log.EXCEPTION, e);
      throw new ReflectionException(new Exception(e.getCause()));
    }
  }