/*
   * (non-Javadoc)
   *
   * @see
   * javax.management.DynamicMBean#setAttributes(javax.management.AttributeList
   * )
   */
  @Override
  public AttributeList setAttributes(final AttributeList attributes) {
    assert (attributes != null);
    RifidiService service = target.get();
    if (service != null) {
      service.setAttributes(attributes);
    }

    // keep track of changed attributes since there might be an error
    AttributeList changedAttributes = new AttributeList();

    for (Attribute attribute : attributes.asList()) {

      String attrName = attribute.getName();
      Integer pos = nameToPos.get(attrName);
      if (pos == null) {
        logger.error("Error when trying to set " + attribute.getName());
      } else {
        this.attributes.set(pos, attribute);
        changedAttributes.add(this.attributes.get(pos));
      }
    }

    notifierService.attributesChanged(getServiceID(), (AttributeList) changedAttributes);
    return (AttributeList) changedAttributes.clone();
  }
Ejemplo n.º 2
0
  /**
   * Sets the values of several attributes of the Dynamic MBean, and returns the list of attributes
   * that have been set.
   */
  public AttributeList setAttributes(AttributeList attributes) {

    // Check attributes is not null to avoid NullPointerException later on
    if (attributes == null) {
      throw new RuntimeOperationsException(
          new IllegalArgumentException("AttributeList attributes cannot be null"),
          "Cannot invoke a setter of " + dClassName);
    }
    AttributeList resultList = new AttributeList();

    // if attributeNames is empty, nothing more to do
    if (attributes.isEmpty()) return resultList;

    // for each attribute, try to set it and add to the result list if successfull
    for (Iterator i = attributes.iterator(); i.hasNext(); ) {
      Attribute attr = (Attribute) i.next();
      try {
        setAttribute(attr);
        String name = attr.getName();
        Object value = getAttribute(name);
        resultList.add(new Attribute(name, value));
      } catch (JMException e) {
        e.printStackTrace();
      } catch (RuntimeException e) {
        e.printStackTrace();
      }
    }
    return (resultList);
  }
Ejemplo n.º 3
0
  /** @see DynamicMBean#setAttributes */
  public AttributeList setAttributes(AttributeList attributes) {

    /* Sanity checking. */
    if (attributes == null) {
      throw new IllegalArgumentException("attribute list can't be null");
    }

    /* Set each attribute specified. */
    AttributeList results = new AttributeList();
    for (int i = 0; i < attributes.size(); i++) {
      Attribute attr = (Attribute) attributes.get(i);
      try {
        /* Set new value. */
        jeHelper.setAttribute(targetEnv, attr);

        /*
         * Add the name and new value to the result list. Be sure
         * to ask the MBean for the new value, rather than simply
         * using attr.getValue(), because the new value may not
         * be same if it is modified according to the JE
         * implementation.
         */
        String name = attr.getName();
        Object newValue = jeHelper.getAttribute(targetEnv, name);
        results.add(new Attribute(name, newValue));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return results;
  }
Ejemplo n.º 4
0
  /** Enables the to get the values of several attributes of the Dynamic MBean. */
  public AttributeList getAttributes(String[] attributeNames) {

    // Check attributeNames is not null to avoid NullPointerException later on
    if (attributeNames == null) {
      throw new RuntimeOperationsException(
          new IllegalArgumentException("attributeNames[] cannot be null"),
          "Cannot invoke a getter of " + dClassName);
    }

    AttributeList resultList = new AttributeList();

    // if attributeNames is empty, return an empty result list
    if (attributeNames.length == 0) return resultList;

    // build the result attribute list
    for (int i = 0; i < attributeNames.length; i++) {
      try {
        Object value = getAttribute((String) attributeNames[i]);
        resultList.add(new Attribute(attributeNames[i], value));
      } catch (JMException e) {
        e.printStackTrace();
      } catch (RuntimeException e) {
        e.printStackTrace();
      }
    }
    return (resultList);
  }
  /**
   * Makes it possible to get the values of several attributes of the MBeanServerDelegate.
   *
   * @param attributes A list of the attributes to be retrieved.
   * @return The list of attributes retrieved.
   */
  public AttributeList getAttributes(String[] attributes) {
    // If attributes is null, the get all attributes.
    //
    final String[] attn = (attributes == null ? attributeNames : attributes);

    // Prepare the result list.
    //
    final int len = attn.length;
    final AttributeList list = new AttributeList(len);
    final boolean dbg = logger.finerOn();

    // Get each requested attribute.
    //
    for (int i = 0; i < len; i++) {
      try {
        final Attribute a = new Attribute(attn[i], getAttribute(attn[i]));
        list.add(a);
      } catch (Exception x) {
        // Skip the attribute that couldn't be obtained.
        //
        if (dbg) {
          logger.finer("getAttributes", "Attribute " + attn[i] + " not found: " + x);
          logger.finest("getAttributes", x);
        }
      }
    }

    // Finally return the result.
    //
    return list;
  }
Ejemplo n.º 6
0
 @Override
 public int getLocalSize() {
   if (nc != null) {
     synchronized (this) {
       if (mBeanServer == null || jmxCacheName == null) {
         int nodeId = CacheFactory.getCluster().getLocalMember().getId();
         jmxCacheName = String.format(CACHE_JMX_NAME_TEMPLATE, serviceName, cacheName, nodeId);
         mBeanServer = MBeanHelper.findMBeanServer();
       }
     }
     try {
       AttributeList list =
           mBeanServer.getAttributes(
               new ObjectName(jmxCacheName), new String[] {"Units", "UnitFactor"});
       return ((Integer) ((Attribute) list.get(0)).getValue())
           * ((Integer) ((Attribute) list.get(1)).getValue());
     } catch (Exception e) {
       log.warn("Failed to retrieve JMX info from object " + jmxCacheName + "\n" + e);
       return -1;
     }
   } else {
     log.info("Cache is not available.");
     return -1;
   }
 }
Ejemplo n.º 7
0
 /* ------------------------------------------------------------ */
 public AttributeList getAttributes(String[] names) {
   AttributeList results = new AttributeList(names.length);
   for (int i = 0; i < names.length; i++) {
     try {
       results.add(new Attribute(names[i], getAttribute(names[i])));
     } catch (Exception e) {
       LOG.warn(Log.EXCEPTION, e);
     }
   }
   return results;
 }
Ejemplo n.º 8
0
 public synchronized AttributeList getAttributes(String[] names) {
   AttributeList al = new AttributeList();
   for (String name : names) {
     Attribute attr = getNamedAttribute(name);
     if (attr != null) {
       al.add(attr);
     } else {
       log.warn("Did not find attribute " + name);
     }
   }
   return al;
 }
Ejemplo n.º 9
0
 @Override
 public AttributeList getAttributes(final String[] attributes) {
   final AttributeList list = new AttributeList();
   for (final String n : attributes) {
     try {
       list.add(new Attribute(n, getAttribute(n)));
     } catch (final Exception ignore) {
       // no-op
     }
   }
   return list;
 }
Ejemplo n.º 10
0
  public AttributeList getAttributes(String[] names) {
    AttributeList list = new AttributeList();
    for (int i = 0; i < names.length; i++) {
      try {
        list.add(new Attribute(names[i], this.getAttribute(names[i])));

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return list;
  }
Ejemplo n.º 11
0
 private AttributeList getAttributes(ObjectName objName, String[] attrNames)
     throws InstanceNotFoundException, ReflectionException, IOException {
   final NameValueMap values =
       getCachedAttributes(objName, new TreeSet<String>(Arrays.asList(attrNames)));
   final AttributeList list = new AttributeList();
   for (String attrName : attrNames) {
     final Object value = values.get(attrName);
     if (value != null || values.containsKey(attrName)) {
       list.add(new Attribute(attrName, value));
     }
   }
   return list;
 }
Ejemplo n.º 12
0
 public final AttributeList getAttributes(String[] attributes) {
   final AttributeList result = new AttributeList(attributes.length);
   for (String attrName : attributes) {
     try {
       final Object attrValue = getAttribute(attrName);
       result.add(new Attribute(attrName, attrValue));
     } catch (Exception e) {
       // OK: attribute is not included in returned list, per spec
       // XXX: log the exception
     }
   }
   return result;
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * javax.management.DynamicMBean#setAttribute(javax.management.Attribute)
   */
  @Override
  public void setAttribute(final Attribute attribute)
      throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException,
          ReflectionException {
    assert (attribute != null);
    RifidiService service = target.get();
    if (service != null) {
      service.setAttribute(attribute);
    }

    attributes.set(nameToPos.get(attribute.getName()), attribute);
    notifierService.attributesChanged(getServiceID(), (AttributeList) attributes.clone());
  }
Ejemplo n.º 14
0
  private static boolean checkAttrs(String what, AttributeList attrs) {
    if (attrs.size() != 1) {
      System.out.println(
          "TEST FAILS: list returned by " + what + " does not have size 1: " + attrs);
      return false;
    }
    Attribute attr = (Attribute) attrs.get(0);
    if (!"Exotic".equals(attr.getName())) {
      System.out.println("TEST FAILS: " + what + " returned wrong " + "attribute: " + attr);
      return false;
    }

    return true;
  }
Ejemplo n.º 15
0
 @Override
 public AttributeList setAttributes(final AttributeList attributes) {
   final AttributeList list = new AttributeList();
   for (final Object o : attributes) {
     final Attribute attr = (Attribute) o;
     try {
       setAttribute(attr);
       list.add(attr);
     } catch (final Exception ignore) {
       // no-op
     }
   }
   return list;
 }
Ejemplo n.º 16
0
 public final AttributeList setAttributes(AttributeList attributes) {
   final AttributeList result = new AttributeList(attributes.size());
   for (Object attrObj : attributes) {
     // We can't use AttributeList.asList because it has side-effects
     Attribute attr = (Attribute) attrObj;
     try {
       setAttribute(attr);
       result.add(new Attribute(attr.getName(), attr.getValue()));
     } catch (Exception e) {
       // OK: attribute is not included in returned list, per spec
       // XXX: log the exception
     }
   }
   return result;
 }
 /*
  * (non-Javadoc)
  *
  * @see javax.management.DynamicMBean#getAttributes(java.lang.String[])
  */
 @Override
 public AttributeList getAttributes(String[] attributes) {
   assert (attributes != null);
   AttributeList ret = new AttributeList();
   Set<String> attribNames = new HashSet<String>();
   for (int count = 0; count < attributes.length; count++) {
     attribNames.add(attributes[count]);
   }
   for (Attribute attr : this.attributes.asList()) {
     if (attribNames.contains(attr.getName())) {
       ret.add(attr);
     }
   }
   return ret;
 }
 @Override
 public AttributeList getAttributes(String[] attributes) {
   updateJmxCache();
   synchronized (this) {
     AttributeList ret = new AttributeList();
     for (String key : attributes) {
       Attribute attr = attrCache.get(key);
       if (LOG.isDebugEnabled()) {
         LOG.debug(key + ": " + attr.getName() + "=" + attr.getValue());
       }
       ret.add(attr);
     }
     return ret;
   }
 }
 public AttributeList getAttributes(String[] names) {
   final AttributeList list = new AttributeList();
   for (int i = 0; i < names.length; i++) {
     try {
       final Attribute a = new Attribute(names[i], this.getAttribute(names[i]));
       list.add(a);
     } catch (Exception e) {
       if (logger.isLoggable(Level.FINEST)) {
         logger.finest("Error while accessing an attribute named: " + names[i]);
       }
       // The exception SHOULD BE squelched per the contract of this method
     }
   }
   return (list);
 }
Ejemplo n.º 20
0
 @Override
 public AttributeList getAttributes(String[] arg0) {
   AttributeList resultList = new AttributeList();
   if (arg0.length == 0) {
     return resultList;
   }
   for (int i = 0; i < arg0.length; i++) {
     try {
       Object Value = getAttribute(arg0[i]);
       resultList.add(new Attribute(arg0[i], Value));
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return resultList;
 }
  /* ------------------------------------------------------------ */
  public AttributeList setAttributes(AttributeList attrs) {
    log.debug("setAttributes");

    AttributeList results = new AttributeList(attrs.size());
    Iterator iter = attrs.iterator();
    while (iter.hasNext()) {
      try {
        Attribute attr = (Attribute) iter.next();
        setAttribute(attr);
        results.add(new Attribute(attr.getName(), getAttribute(attr.getName())));
      } catch (Exception e) {
        log.warn(LogSupport.EXCEPTION, e);
      }
    }
    return results;
  }
 /*
  * (non-Javadoc)
  *
  * @see org.rifidi.edge.configuration.Configuration#getAttributeNames()
  */
 @Override
 public String[] getAttributeNames() {
   Set<String> names = new HashSet<String>();
   for (Attribute attr : attributes.asList()) {
     names.add(attr.getName());
   }
   return names.toArray(new String[0]);
 }
Ejemplo n.º 23
0
  public synchronized AttributeList setAttributes(AttributeList list) {
    AttributeList results = new AttributeList();
    for (int i = 0; i < list.size(); i++) {
      Attribute attr = (Attribute) list.get(i);

      if (setNamedAttribute(attr)) {
        results.add(attr);
      } else {
        if (log.isWarnEnabled()) {
          log.warn(
              "Failed to update attribute name "
                  + attr.getName()
                  + " with value "
                  + attr.getValue());
        }
      }
    }
    return results;
  }
Ejemplo n.º 24
0
  /** @see DynamicMBean#getAttributes */
  public AttributeList getAttributes(String[] attributes) {

    /* Sanity checking. */
    if (attributes == null) {
      throw new IllegalArgumentException("Attributes cannot be null");
    }

    /* Get each requested attribute. */
    AttributeList results = new AttributeList();
    for (int i = 0; i < attributes.length; i++) {
      try {
        String name = attributes[i];
        Object value = jeHelper.getAttribute(targetEnv, name);
        results.add(new Attribute(name, value));
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return results;
  }
 /*
  * (non-Javadoc)
  *
  * @see javax.management.DynamicMBean#getAttribute(java.lang.String)
  */
 @Override
 public Object getAttribute(final String attribute)
     throws AttributeNotFoundException, MBeanException, ReflectionException {
   assert (attribute != null);
   for (Attribute attr : attributes.asList()) {
     if (attribute.equals(attr.getName())) {
       return attribute;
     }
   }
   throw new AttributeNotFoundException();
 }
 @Override
 public long[] getFileDecriptorInfo() {
   MBeanServer mbsc = MBeans.getMBeanServer();
   AttributeList attributes;
   try {
     attributes =
         mbsc.getAttributes(
             new ObjectName("java.lang:type=OperatingSystem"),
             new String[] {"OpenFileDescriptorCount", "MaxFileDescriptorCount"});
     List<Attribute> attrList = attributes.asList();
     long openFdCount = (Long) attrList.get(0).getValue();
     long maxFdCount = (Long) attrList.get(1).getValue();
     long[] fdCounts = {openFdCount, maxFdCount};
     return fdCounts;
   } catch (Exception e) {
     LogFactory.getLog(SdkMBeanRegistrySupport.class)
         .debug("Failed to retrieve file descriptor info", e);
   }
   return null;
 }
 AttributeList getAttributes(ObjectName name, String[] attributes)
     throws InstanceNotFoundException, ReflectionException {
   final ResourceAndRegistration reg = getRootResourceAndRegistration();
   final PathAddress address = resolvePathAddress(name, reg);
   if (address == null) {
     throw MESSAGES.mbeanNotFound(name);
   }
   final ResourceAccessControl accessControl =
       accessControlUtil.getResourceAccessWithInstanceNotFoundExceptionIfNotAccessible(
           name, address, false);
   AttributeList list = new AttributeList();
   for (String attribute : attributes) {
     try {
       list.add(
           new Attribute(attribute, getAttribute(reg, address, name, attribute, accessControl)));
     } catch (AttributeNotFoundException e) {
       throw new ReflectionException(e);
     }
   }
   return list;
 }
Ejemplo n.º 28
0
 private synchronized NameValueMap getCachedAttributes(ObjectName objName, Set<String> attrNames)
     throws InstanceNotFoundException, ReflectionException, IOException {
   NameValueMap values = cachedValues.get(objName);
   if (values != null && values.keySet().containsAll(attrNames)) {
     return values;
   }
   attrNames = new TreeSet<String>(attrNames);
   Set<String> oldNames = cachedNames.get(objName);
   if (oldNames != null) {
     attrNames.addAll(oldNames);
   }
   values = new NameValueMap();
   final AttributeList attrs =
       conn.getAttributes(objName, attrNames.toArray(new String[attrNames.size()]));
   for (Attribute attr : attrs.asList()) {
     values.put(attr.getName(), attr.getValue());
   }
   cachedValues.put(objName, values);
   cachedNames.put(objName, attrNames);
   return values;
 }
 /**
  * Set the service that this configuration wraps
  *
  * @param target the target to set
  */
 public void setTarget(final RifidiService target) {
   if (this.target.compareAndSet(null, target)) {
     if (target != null) {
       // get the whole list of attributes from the target as we might
       // only
       // have a partial list from the config file
       target.setAttributes(attributes);
       // keep a local clone
       attributes = (AttributeList) target.getAttributes().clone();
       nameToPos = new HashMap<String, Integer>();
       List<Attribute> attrs = attributes.asList();
       if (target instanceof AbstractSensor<?>) {
         for (SessionDTO sessionDTO : sessionDTOs.keySet()) {
           try {
             ((AbstractSensor<?>) target).createSensorSession(sessionDTO);
             // test to see if the session should be
             // restarted
             if (shouldStartSession(sessionDTO)) {
               // if it should, restart it
               restartSession((AbstractSensor<?>) target, sessionDTO);
             }
           } catch (CannotCreateSessionException e) {
             logger.error("Cannot Create Session ", e);
           } catch (Exception e) {
             logger.error("Cannot restart session ", e);
           }
         }
       }
       for (int count = 0; count < attributes.size(); count++) {
         nameToPos.put(attrs.get(count).getName(), count);
       }
       jmxService.publish(this);
       return;
     }
     jmxService.unpublish(this);
     return;
   }
   // if the value was already set we got a problem
   logger.warn("Tried to set the target but was already set.");
 }
Ejemplo n.º 30
0
 @SuppressWarnings("unused")
 public AttributeList setAttributes(AttributeList attributes) {
   Attribute[] atts = (Attribute[]) attributes.toArray();
   AttributeList list = new AttributeList();
   for (int i = 0; i < atts.length; i++) {
     Attribute a = atts[i];
     try {
       this.setAttribute(a);
     } catch (Exception e) {
       e.printStackTrace();
     }
   } // for
   return attributes;
 }