示例#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());
    }
  }
  /**
   * 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);
    }
  }