Example #1
0
  /**
   * Gets the type from a field name.
   *
   * @param fieldRequire the name of the required field
   * @param manipulation the metadata extracted from metadata.xml file
   * @return the type of the field or {@code null} if it wasn't found
   */
  private static String getTypeFromAttributeField(String fieldRequire, PojoMetadata manipulation) {

    FieldMetadata field = manipulation.getField(fieldRequire);
    if (field == null) {
      return null;
    } else {
      return FieldMetadata.getReflectionType(field.getFieldType());
    }
  }
Example #2
0
  /**
   * Gets all the methods available which get this name.
   *
   * @param methodName the name of the required methods
   * @param manipulation the metadata extract from metadata.xml file
   * @param description the description which appears in JMX console
   * @return the array of methods with the right name
   */
  private MethodField[] getMethodsFromName(
      String methodName, PojoMetadata manipulation, String description) {

    MethodMetadata[] methods = manipulation.getMethods(methodName);
    if (methods.length == 0) {
      return null;
    }

    MethodField[] ret = new MethodField[methods.length];

    if (methods.length == 1) {
      ret[0] = new MethodField(methods[0], description);
      return ret;
    } else {
      for (int i = 0; i < methods.length; i++) {
        ret[i] = new MethodField(methods[i], description);
      }
      return ret;
    }
  }
Example #3
0
  /**
   * Constructs the structure JmxConfigFieldMap and the Dynamic Mbean.
   *
   * @param metadata the component metadata
   * @param dict the instance configuration
   */
  public void configure(Element metadata, Dictionary dict) {

    PojoMetadata manipulation = getPojoMetadata();

    m_instanceManager = getInstanceManager();

    m_jmxConfigFieldMap = new JmxConfigFieldMap();

    // Build the hashmap
    Element[] mbeans = metadata.getElements(JMX_CONFIG_ELT, m_namespace);
    if (mbeans == null || mbeans.length == 0) {
      mbeans = metadata.getElements(JMX_CONFIG_ALT_ELT, m_namespace);
    }

    if (mbeans.length != 1) {
      error(
          "A component must have exactly one "
              + JMX_CONFIG_ELT
              + " or "
              + JMX_CONFIG_ALT_ELT
              + " element.");
      error("The JMX handler configuration is ignored.");
      return;
    }

    Element mbean = mbeans[0];

    // retrieve kind of MBeanServer to use
    m_usesMOSGi = Boolean.parseBoolean(mbean.getAttribute(JMX_USES_MOSGI_ELT));

    // retrieve object name
    m_completeObjNameElt = mbean.getAttribute(JMX_OBJ_NAME_ELT);
    m_domainElt = mbean.getAttribute(JMX_OBJ_NAME_DOMAIN_ELT);
    m_objNameWODomainElt = mbean.getAttribute(JMX_OBJ_NAME_WO_DOMAIN_ELT);

    // test if Pojo is interested in registration callbacks
    m_registerCallbacks = manipulation.isInterfaceImplemented(MBeanRegistration.class.getName());
    if (m_registerCallbacks) {
      // don't need to check that methods exist, the pojo implements
      // MBeanRegistration interface
      String[] preRegisterParams = {MBeanServer.class.getName(), ObjectName.class.getName()};
      m_preRegisterMeth = manipulation.getMethod(PRE_REGISTER_METH_NAME, preRegisterParams);

      String[] postRegisterParams = {Boolean.class.getName()};
      m_postRegisterMeth = manipulation.getMethod(POST_REGISTER_METH_NAME, postRegisterParams);

      m_preDeregisterMeth = manipulation.getMethod(PRE_DEREGISTER_METH_NAME, new String[0]);

      m_postDeregisterMeth = manipulation.getMethod(POST_DEREGISTER_METH_NAME, new String[0]);
    }

    // set property
    Element[] attributes = mbean.getElements(JMX_PROPERTY_ELT, m_namespace);
    Element[] attributesAlt = mbean.getElements(JMX_PROPERTY_ELT_ALT, m_namespace);
    List<Element> listOfAttributes = new ArrayList<Element>();
    if (attributes != null) {
      listOfAttributes.addAll(Arrays.asList(attributes));
    }
    if (attributesAlt != null) {
      listOfAttributes.addAll(Arrays.asList(attributesAlt));
    }

    Element[] attributesOld = mbeans[0].getElements(JMX_PROPERTY_ELT);
    if (attributesOld != null) {
      warn("The JMX property element should use the '" + m_namespace + "' namespace.");
      listOfAttributes.addAll(Arrays.asList(attributesOld));
    }

    for (Element attribute : listOfAttributes) {
      boolean notif = false;
      String rights;
      String name;
      String field = attribute.getAttribute(JMX_FIELD_ELT);

      if (attribute.containsAttribute(JMX_NAME_ELT)) {
        name = attribute.getAttribute(JMX_NAME_ELT);
      } else {
        name = field;
      }
      if (attribute.containsAttribute(JMX_RIGHTS_ELT)) {
        rights = attribute.getAttribute(JMX_RIGHTS_ELT);
      } else {
        rights = "r";
      }

      PropertyField property =
          new PropertyField(name, field, rights, getTypeFromAttributeField(field, manipulation));

      if (attribute.containsAttribute(JMX_NOTIFICATION_ELT)) {
        notif = Boolean.parseBoolean(attribute.getAttribute(JMX_NOTIFICATION_ELT));
      }

      property.setNotifiable(notif);

      if (notif) {
        // add the new notifiable property in structure
        NotificationField notification =
            new NotificationField(name, this.getClass().getName() + "." + field, null);
        m_jmxConfigFieldMap.addNotificationFromName(name, notification);
      }
      m_jmxConfigFieldMap.addPropertyFromName(name, property);
      getInstanceManager().register(manipulation.getField(field), this);
      info(
          "property exposed:"
              + name
              + " "
              + field
              + ":"
              + getTypeFromAttributeField(field, manipulation)
              + " "
              + rights
              + ", Notif="
              + notif);
    }

    // set methods
    Element[] methods = mbean.getElements(JMX_METHOD_ELT, m_namespace);
    Element[] methodsAlt = mbean.getElements(JMX_METHOD_ELT_ALT, m_namespace);
    List<Element> listOfMethods = new ArrayList<Element>();
    if (methods != null) {
      listOfMethods.addAll(Arrays.asList(methods));
    }
    if (methodsAlt != null) {
      listOfMethods.addAll(Arrays.asList(methodsAlt));
    }

    Element[] methodsOld = mbeans[0].getElements(JMX_PROPERTY_ELT);
    if (methodsOld != null) {
      warn("The JMX method element should use the '" + m_namespace + "' namespace.");
      listOfMethods.addAll(Arrays.asList(methodsOld));
    }

    for (Element method : listOfMethods) {
      String name = method.getAttribute(JMX_NAME_ELT);
      if (name == null) {
        name = method.getAttribute("method");
      }
      String description = null;
      if (method.containsAttribute(JMX_DESCRIPTION_ELT)) {
        description = method.getAttribute(JMX_DESCRIPTION_ELT);
      }

      MethodField[] meth = getMethodsFromName(name, manipulation, description);

      for (int j = 0; j < meth.length; j++) {
        m_jmxConfigFieldMap.addMethodFromName(name, meth[j]);

        info("method exposed:" + meth[j].getReturnType() + " " + name);
      }
    }
  }
  /**
   * Initialize the component type.
   *
   * @param desc : component type description to populate.
   * @param metadata : component type metadata.
   * @throws ConfigurationException : metadata are incorrect.
   * @see
   *     org.apache.felix.ipojo.Handler#initializeComponentFactory(org.apache.felix.ipojo.architecture.ComponentTypeDescription,
   *     org.apache.felix.ipojo.metadata.Element)
   */
  public void initializeComponentFactory(ComponentTypeDescription desc, Element metadata)
      throws ConfigurationException {
    Element[] confs = metadata.getElements("Properties", "");
    if (confs == null) {
      return;
    }
    Element[] configurables = confs[0].getElements("Property");
    for (int i = 0; configurables != null && i < configurables.length; i++) {
      String fieldName = configurables[i].getAttribute("field");
      String methodName = configurables[i].getAttribute("method");

      if (fieldName == null && methodName == null) {
        throw new ConfigurationException(
            "Malformed property : The property needs to contain at least a field or a method");
      }

      String name = configurables[i].getAttribute("name");
      if (name == null) {
        if (fieldName == null) {
          name = methodName;
        } else {
          name = fieldName;
        }
        configurables[i].addAttribute(
            new Attribute("name", name)); // Add the type to avoid configure checking
      }

      String value = configurables[i].getAttribute("value");

      // Detect the type of the property
      PojoMetadata manipulation = getFactory().getPojoMetadata();
      String type = null;
      if (fieldName == null) {
        MethodMetadata[] method = manipulation.getMethods(methodName);
        if (method.length == 0) {
          type = configurables[i].getAttribute("type");
          if (type == null) {
            throw new ConfigurationException(
                "Malformed property : The type of the property cannot be discovered, add a 'type' attribute");
          }
        } else {
          if (method[0].getMethodArguments().length != 1) {
            throw new ConfigurationException(
                "Malformed property :  The method " + methodName + " does not have one argument");
          }
          type = method[0].getMethodArguments()[0];
          configurables[i].addAttribute(
              new Attribute("type", type)); // Add the type to avoid configure checking
        }
      } else {
        FieldMetadata field = manipulation.getField(fieldName);
        if (field == null) {
          throw new ConfigurationException(
              "Malformed property : The field "
                  + fieldName
                  + " does not exist in the implementation class");
        }
        type = field.getFieldType();
        configurables[i].addAttribute(
            new Attribute("type", type)); // Add the type to avoid configure checking
      }

      // Is the property set to immutable
      boolean immutable = false;
      String imm = configurables[i].getAttribute("immutable");
      immutable = imm != null && imm.equalsIgnoreCase("true");

      boolean mandatory = false;
      String man = configurables[i].getAttribute("mandatory");
      mandatory = man != null && man.equalsIgnoreCase("true");

      PropertyDescription pd = null;
      if (value == null) {
        pd =
            new PropertyDescription(
                name, type, null, false); // Cannot be immutable if we have no value.
      } else {
        pd = new PropertyDescription(name, type, value, immutable);
      }

      if (mandatory) {
        pd.setMandatory();
      }

      desc.addProperty(pd);
    }
  }