コード例 #1
0
  public void processConfiguration(
      ConverterLookup converterLookup,
      Object object,
      ClassLoader classLoader,
      PlexusConfiguration configuration,
      ExpressionEvaluator expressionEvaluator)
      throws ComponentConfigurationException {
    int items = configuration.getChildCount();

    for (int i = 0; i < items; i++) {
      PlexusConfiguration childConfiguration = configuration.getChild(i);

      String elementName = childConfiguration.getName();

      ComponentValueSetter valueSetter =
          new ComponentValueSetter(fromXML(elementName), object, converterLookup);

      valueSetter.configure(childConfiguration, classLoader, expressionEvaluator);
      /*
      Object value = valueSetter.getConverter().fromConfiguration(
          converterLookup, childConfiguration, valueSetter.getValueType(),
          object.getClass(), classLoader, expressionEvaluator
      );

      if ( value != null )
      {
          valueSetter.setValue( value );
      }
      */
    }
  }
コード例 #2
0
  private void writeConfiguration(XMLWriter w, PlexusConfiguration configuration)
      throws ComponentDescriptorCreatorException, PlexusConfigurationException {
    if (configuration == null || configuration.getChildCount() == 0) {
      return;
    }

    if (!configuration.getName().equals("configuration")) {
      throw new ComponentDescriptorCreatorException(
          "The root node of the configuration must be " + "'configuration'.");
    }

    writePlexusConfiguration(w, configuration);
  }
コード例 #3
0
  private void writePlexusConfiguration(XMLWriter xmlWriter, PlexusConfiguration c)
      throws PlexusConfigurationException {
    if (c.getAttributeNames().length == 0 && c.getChildCount() == 0 && c.getValue() == null) {
      return;
    }

    xmlWriter.startElement(c.getName());

    // ----------------------------------------------------------------------
    // Write the attributes
    // ----------------------------------------------------------------------

    String[] attributeNames = c.getAttributeNames();

    for (int i = 0; i < attributeNames.length; i++) {
      String attributeName = attributeNames[i];

      xmlWriter.addAttribute(attributeName, c.getAttribute(attributeName));
    }

    // ----------------------------------------------------------------------
    // Write the children
    // ----------------------------------------------------------------------

    PlexusConfiguration[] children = c.getChildren();

    if (children.length > 0) {
      for (int i = 0; i < children.length; i++) {
        writePlexusConfiguration(xmlWriter, children[i]);
      }
    } else {
      String value = c.getValue();

      if (value != null) {
        xmlWriter.writeText(value);
      }
    }

    xmlWriter.endElement();
  }
コード例 #4
0
  public Object fromConfiguration(
      ConverterLookup converterLookup,
      PlexusConfiguration configuration,
      Class type,
      Class baseType,
      ClassLoader classLoader,
      ExpressionEvaluator expressionEvaluator,
      ConfigurationListener listener)
      throws ComponentConfigurationException {
    Object retValue = fromExpression(configuration, expressionEvaluator, type);
    if (retValue != null) {
      return retValue;
    }

    Class implementation = getClassForImplementationHint(null, configuration, classLoader);

    if (implementation != null) {
      retValue = instantiateObject(implementation);
    } else {
      // we can have 2 cases here:
      //  - provided collection class which is not abstract
      //     like Vector, ArrayList, HashSet - so we will just instantantiate it
      // - we have an abtract class so we have to use default collection type
      int modifiers = type.getModifiers();

      if (Modifier.isAbstract(modifiers)) {
        retValue = getDefaultCollection(type);
      } else {
        try {
          retValue = type.newInstance();
        } catch (IllegalAccessException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        } catch (InstantiationException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        }
      }
    }
    // now we have collection and we have to add some objects to it

    for (int i = 0; i < configuration.getChildCount(); i++) {
      PlexusConfiguration c = configuration.getChild(i);

      Class childType = getImplementationClass(null, baseType, c, classLoader);

      ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);

      Object object =
          converter.fromConfiguration(
              converterLookup, c, childType, baseType, classLoader, expressionEvaluator, listener);

      Collection collection = (Collection) retValue;
      collection.add(object);
    }

    return retValue;
  }
コード例 #5
0
  public Object fromConfiguration(
      ConverterLookup converterLookup,
      PlexusConfiguration configuration,
      Class type,
      Class baseType,
      ClassLoader classLoader,
      ExpressionEvaluator expressionEvaluator)
      throws ComponentConfigurationException {
    Object retValue = fromExpression(configuration, expressionEvaluator, type);
    if (retValue != null) {
      return retValue;
    }

    Class implementation = getClassForImplementationHint(null, configuration, classLoader);

    if (implementation != null) {
      retValue = instantiateObject(implementation);
    } else {
      // we can have 2 cases here:
      //  - provided collection class which is not abstract
      //     like Vector, ArrayList, HashSet - so we will just instantantiate it
      // - we have an abtract class so we have to use default collection type
      int modifiers = type.getModifiers();

      if (Modifier.isAbstract(modifiers)) {
        retValue = getDefaultCollection(type);
      } else {
        try {
          retValue = type.newInstance();
        } catch (IllegalAccessException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        } catch (InstantiationException e) {
          String msg =
              "An attempt to convert configuration entry "
                  + configuration.getName()
                  + "' into "
                  + type
                  + " object failed: "
                  + e.getMessage();

          throw new ComponentConfigurationException(msg, e);
        }
      }
    }
    // now we have collection and we have to add some objects to it

    for (int i = 0; i < configuration.getChildCount(); i++) {
      PlexusConfiguration c = configuration.getChild(i);
      // Object o = null;

      String configEntry = c.getName();

      String name = StringUtils.capitalizeFirstLetter(fromXML(configEntry));

      Class childType = getClassForImplementationHint(null, c, classLoader);

      if (childType == null) {
        // Some classloaders don't create Package objects for classes
        // so we have to resort to slicing up the class name

        String baseTypeName = baseType.getName();

        int lastDot = baseTypeName.lastIndexOf('.');

        String className;

        if (lastDot == -1) {
          className = name;
        } else {
          String basePackage = baseTypeName.substring(0, lastDot);

          className = basePackage + "." + name;
        }

        childType = loadClass(className, classLoader);
      }

      ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);

      Object object =
          converter.fromConfiguration(
              converterLookup, c, childType, baseType, classLoader, expressionEvaluator);

      Collection collection = (Collection) retValue;
      collection.add(object);
    }

    return retValue;
  }