/**
   * @todo pico components can have theoretically more then one constructor which is rather stupid
   *     idea
   */
  public Object newInstance(
      ComponentDescriptor componentDescriptor, ClassRealm classRealm, PlexusContainer container)
      throws ComponentInstantiationException {
    try {

      String implementation = componentDescriptor.getImplementation();

      Class implementationClass = classRealm.loadClass(implementation);

      Constructor constructor = implementationClass.getConstructors()[0];

      Class[] parameterTypes = constructor.getParameterTypes();

      Object[] params = new Object[parameterTypes.length];

      for (int i = 0; i < parameterTypes.length; i++) {
        Class parameterType = parameterTypes[i];

        params[i] = lookupComponent(parameterType, componentDescriptor, container);
      }

      Object retValue = constructor.newInstance(params);

      return retValue;

    } catch (Exception e) {
      String msg =
          "Component " + componentDescriptor.getHumanReadableKey() + " cannot be instantiated";

      throw new ComponentInstantiationException(msg, e);
    }
  }
 public String toString() {
   return descriptor.getImplementationClass().getName()
       + (property != null ? "." + property : "")
       + (descriptor.getSource() != null
           ? "(" + descriptor.getSource() + ")"
           : "(Unknown Source)");
 }
  public void execute(Object object, ComponentManager manager, ClassRealm lookupRealm)
      throws PhaseExecutionException {
    try {
      ComponentDescriptor<?> descriptor = manager.getComponentDescriptor();

      String configuratorId = descriptor.getComponentConfigurator();

      if (StringUtils.isEmpty(configuratorId)) {
        configuratorId = DEFAULT_CONFIGURATOR_ID;
      }

      ComponentConfigurator componentConfigurator =
          manager.getContainer().lookup(ComponentConfigurator.class, configuratorId);

      PlexusConfiguration configuration =
          manager.getContainer().getConfigurationSource().getConfiguration(descriptor);

      if (configuration != null) {
        ClassRealm realm = manager.getRealm();

        componentConfigurator.configureComponent(object, configuration, realm);
      }
    } catch (ComponentLookupException e) {
      throw new PhaseExecutionException(
          "Unable to auto-configure component as its configurator could not be found", e);
    } catch (ComponentConfigurationException e) {
      throw new PhaseExecutionException("Unable to auto-configure component", e);
    }
  }
Exemple #4
0
  private String getErrorMessage(
      ComponentDescriptor descriptor, ComponentRequirement requirement, String causeDescription) {
    StringBuffer msg = new StringBuffer("Component composition failed.");

    msg.append("  Failed to resolve requirement for component of role: '");

    msg.append(descriptor.getRole());

    msg.append("'");

    if (descriptor.getRoleHint() != null) {
      msg.append(" and role-hint: '");

      msg.append(descriptor.getRoleHint());

      msg.append("'. ");
    }

    if (requirement != null) {
      msg.append("Failing requirement: " + requirement.getHumanReadableKey());
    }
    if (causeDescription != null) {
      msg.append(causeDescription);
    }

    return msg.toString();
  }
Exemple #5
0
  /** @todo Change this to include components looked up from parents as well... */
  public List lookupList(String role) throws ComponentLookupException {
    List components = new ArrayList();

    List componentDescriptors = getComponentDescriptorList(role);

    if (componentDescriptors != null) {
      // Now we have a list of component descriptors.

      for (Iterator i = componentDescriptors.iterator(); i.hasNext(); ) {
        ComponentDescriptor descriptor = (ComponentDescriptor) i.next();

        String roleHint = descriptor.getRoleHint();

        Object component;

        if (roleHint != null) {
          component = lookup(role, roleHint);
        } else {
          component = lookup(role);
        }

        components.add(component);
      }
    }

    return components;
  }
  private ComponentRequirement getMatchingRequirement(
      ComponentDescriptor componentDescriptor, List requirements) {

    ComponentRequirement retValue = null;

    String role = componentDescriptor.getRole();

    String roleHint = componentDescriptor.getRoleHint();

    if (componentDescriptor.getRoleHint() != null) {
      for (Iterator iterator = requirements.iterator(); iterator.hasNext(); ) {
        ComponentRequirement requirement = (ComponentRequirement) iterator.next();

        if (role.equals(requirement.getRole()) && roleHint.equals(requirement.getRoleHint())) {
          retValue = requirement;

          break;
        }
      }
    } else {
      for (Iterator iterator = requirements.iterator(); iterator.hasNext(); ) {
        ComponentRequirement requirement = (ComponentRequirement) iterator.next();

        if (role.equals(requirement.getRole())) {
          retValue = requirement;

          break;
        }
      }
    }

    return retValue;
  }
Exemple #7
0
  /**
   * TODO: Enhance the ComponentRepository so that it can take entire ComponentSetDescriptors
   * instead of just ComponentDescriptors.
   */
  public List discoverComponents(ClassRealm classRealm)
      throws PlexusConfigurationException, ComponentRepositoryException {
    List discoveredComponentDescriptors = new ArrayList();

    for (Iterator i = componentDiscovererManager.getComponentDiscoverers().iterator();
        i.hasNext(); ) {
      ComponentDiscoverer componentDiscoverer = (ComponentDiscoverer) i.next();

      List componentSetDescriptors = componentDiscoverer.findComponents(getContext(), classRealm);

      for (Iterator j = componentSetDescriptors.iterator(); j.hasNext(); ) {
        ComponentSetDescriptor componentSet = (ComponentSetDescriptor) j.next();

        List componentDescriptors = componentSet.getComponents();

        if (componentDescriptors != null) {
          for (Iterator k = componentDescriptors.iterator(); k.hasNext(); ) {
            ComponentDescriptor componentDescriptor = (ComponentDescriptor) k.next();

            componentDescriptor.setComponentSetDescriptor(componentSet);

            // If the user has already defined a component descriptor for this particular
            // component then do not let the discovered component descriptor override
            // the user defined one.
            if (getComponentDescriptor(componentDescriptor.getComponentKey()) == null) {
              addComponentDescriptor(componentDescriptor);

              // We only want to add components that have not yet been
              // discovered in a parent realm. We don't quite have fine
              // grained control over this right now but this is for
              // dynamic additions which are only happening from maven
              // at the moment. And plugins have a parent realm and
              // a grand parent realm so if the component has been
              // discovered it's most likely in those realms.

              // I actually need to keep track of what realm a component
              // was discovered in so that i can accurately search the
              // parents.

              discoveredComponentDescriptors.add(componentDescriptor);
            }
          }

          // discoveredComponentDescriptors.addAll( componentDescriptors );
        }
      }
    }

    return discoveredComponentDescriptors;
  }
Exemple #8
0
  protected Object createComponentInstance(ComponentDescriptor componentDescriptor)
      throws ComponentInstantiationException, ComponentLifecycleException {
    String componentFactoryId = componentDescriptor.getComponentFactory();

    ComponentFactory componentFactory = null;

    Object component = null;

    try {
      if (componentFactoryId != null) {
        componentFactory =
            container.getComponentFactoryManager().findComponentFactory(componentFactoryId);
      } else {
        componentFactory = container.getComponentFactoryManager().getDefaultComponentFactory();
      }

      component =
          componentFactory.newInstance(
              componentDescriptor, container.getContainerRealm(), container);
    } catch (UndefinedComponentFactoryException e) {
      throw new ComponentInstantiationException(
          "Unable to create component as factory '" + componentFactoryId + "' could not be found",
          e);
    } finally {
      // the java factory is a special case, without a component manager.
      // Don't bother releasing the java factory.
      if (StringUtils.isNotEmpty(componentFactoryId) && !"java".equals(componentFactoryId)) {
        release(componentFactory);
      }
    }

    return component;
  }
  public ComponentManager createComponentManager(
      ComponentDescriptor descriptor, MutablePlexusContainer container)
      throws UndefinedComponentManagerException, UndefinedLifecycleHandlerException {
    String componentManagerId = descriptor.getInstantiationStrategy();

    ComponentManager componentManager;

    if (componentManagerId == null) {
      componentManagerId = defaultComponentManagerId;
    }

    componentManager = copyComponentManager(componentManagerId);

    componentManager.setup(container, findLifecycleHandler(descriptor), descriptor);

    componentManager.initialize();

    activeComponentManagers.put(descriptor.getComponentKey(), componentManager);

    return componentManager;
  }
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof ComponentStackElement)) {
      return false;
    }

    ComponentStackElement that = (ComponentStackElement) o;

    return descriptor.equals(that.descriptor)
        && (property != null ? property.equals(that.property) : that.property == null);
  }
  private LifecycleHandler findLifecycleHandler(ComponentDescriptor descriptor)
      throws UndefinedLifecycleHandlerException {
    String lifecycleHandlerId = descriptor.getLifecycleHandler();

    LifecycleHandler lifecycleHandler;

    if (lifecycleHandlerId == null) {
      lifecycleHandler = lifecycleHandlerManager.getDefaultLifecycleHandler();
    } else {
      lifecycleHandler = lifecycleHandlerManager.getLifecycleHandler(lifecycleHandlerId);
    }

    return lifecycleHandler;
  }
Exemple #12
0
  private void processCoreComponentConfiguration(
      String role, BasicComponentConfigurator configurator, PlexusConfiguration c)
      throws ComponentConfigurationException {
    String implementation = c.getAttribute("implementation", null);

    if (implementation == null) {

      String msg =
          "Core component: '"
              + role
              + "' + which is needed by plexus to function properly cannot "
              + "be instantiated. Implementation attribute was not specified in plexus.conf."
              + "This is highly irregular, your plexus JAR is most likely corrupt.";

      throw new ComponentConfigurationException(msg);
    }

    ComponentDescriptor componentDescriptor = new ComponentDescriptor();

    componentDescriptor.setRole(role);

    componentDescriptor.setImplementation(implementation);

    PlexusConfiguration configuration = new XmlPlexusConfiguration("configuration");

    configuration.addChild(c);

    try {
      configurator.configureComponent(this, configuration, plexusRealm);
    } catch (ComponentConfigurationException e) {
      // TODO: don't like rewrapping the same exception, but better than polluting this all through
      // the config code
      String message = "Error configuring component: " + componentDescriptor.getHumanReadableKey();
      throw new ComponentConfigurationException(message, e);
    }
  }
Exemple #13
0
  private ComponentManager createComponentManager(ComponentDescriptor descriptor)
      throws ComponentLookupException {
    ComponentManager componentManager;

    try {
      componentManager = componentManagerManager.createComponentManager(descriptor, this);
    } catch (UndefinedComponentManagerException e) {
      String message =
          "Cannot create component manager for "
              + descriptor.getComponentKey()
              + ", so we cannot provide a component instance.";

      throw new ComponentLookupException(message, e);
    } catch (UndefinedLifecycleHandlerException e) {
      String message =
          "Cannot create component manager for "
              + descriptor.getComponentKey()
              + ", so we cannot provide a component instance.";

      throw new ComponentLookupException(message, e);
    }

    return componentManager;
  }
  private ComponentInstantiationException makeException(
      ClassRealm componentClassRealm,
      ComponentDescriptor componentDescriptor,
      Class implementationClass,
      Throwable e) {
    // ----------------------------------------------------------------------
    // Display the realm when there is an error, We should probably return a string here so we
    // can incorporate this into the error message for easy debugging.
    // ----------------------------------------------------------------------

    componentClassRealm.display();

    String msg = "Could not instanciate component: " + componentDescriptor.getHumanReadableKey();

    return new ComponentInstantiationException(msg, e);
  }
  public Object newInstance(
      ComponentDescriptor componentDescriptor, ClassRealm classRealm, PlexusContainer container)
      throws ComponentInstantiationException {
    Class implementationClass = null;

    try {
      String implementation = componentDescriptor.getImplementation();

      implementationClass = classRealm.loadClass(implementation);

      int modifiers = implementationClass.getModifiers();

      if (Modifier.isInterface(modifiers)) {
        throw new ComponentInstantiationException(
            "Cannot instanciate implementation '"
                + implementation
                + "' because the class is a interface.");
      }

      if (Modifier.isAbstract(modifiers)) {
        throw new ComponentInstantiationException(
            "Cannot instanciate implementation '"
                + implementation
                + "' because the class is abstract.");
      }

      Object instance = implementationClass.newInstance();

      return instance;
    } catch (InstantiationException e) {
      throw makeException(classRealm, componentDescriptor, implementationClass, e);
    } catch (ClassNotFoundException e) {
      throw makeException(classRealm, componentDescriptor, implementationClass, e);
    } catch (IllegalAccessException e) {
      throw makeException(classRealm, componentDescriptor, implementationClass, e);
    } catch (LinkageError e) {
      throw makeException(classRealm, componentDescriptor, implementationClass, e);
    }
  }
  private Object lookupComponent(
      Class parameterType, ComponentDescriptor componentDescriptor, PlexusContainer container)
      throws ComponentInstantiationException {
    try {
      String role;

      if (parameterType.isArray()) {
        role = parameterType.getComponentType().getName();

      } else {
        role = parameterType.getName();
      }

      Object retValue = null;

      List requirements = componentDescriptor.getRequirements();

      ComponentRequirement requirement = getMatchingRequirement(componentDescriptor, requirements);

      if (requirement == null) {
        if (parameterType.isArray()) {

          List dependencies = container.lookupList(role);

          retValue = Array.newInstance(parameterType.getComponentType(), dependencies.size());

          int len = dependencies.size();

          Object[] array = (Object[]) Array.newInstance(parameterType.getComponentType(), len);

          for (int i = 0; i < len; i++) {
            array[i] = dependencies.get(i);
          }

          retValue = array;
        } else {

          retValue = container.lookup(role);
        }
      } else {
        if (parameterType.isArray()) {
          List dependencies = container.lookupList(role);

          int len = dependencies.size();

          Object[] array = (Object[]) Array.newInstance(parameterType.getComponentType(), len);

          for (int i = 0; i < len; i++) {
            array[i] = dependencies.get(i);
          }

          retValue = array;

        } else {
          retValue = container.lookup(role);
        }
      }

      return retValue;
    } catch (ComponentLookupException e) {
      throw new ComponentInstantiationException(e);
    }
  }
 public int hashCode() {
   int result;
   result = descriptor.hashCode();
   result = 31 * result + (property != null ? property.hashCode() : 0);
   return result;
 }
Exemple #18
0
  public static ComponentDescriptor buildComponentDescriptor(PlexusConfiguration configuration)
      throws Exception {
    ComponentDescriptor cd = new ComponentDescriptor();

    cd.setRole(configuration.getChild("role").getValue());

    cd.setRoleHint(configuration.getChild("role-hint").getValue());

    cd.setImplementation(configuration.getChild("implementation").getValue());

    cd.setVersion(configuration.getChild("version").getValue());

    cd.setComponentType(configuration.getChild("component-type").getValue());

    cd.setInstantiationStrategy(configuration.getChild("instantiation-strategy").getValue());

    cd.setLifecycleHandler(configuration.getChild("lifecycle-handler").getValue());

    cd.setComponentProfile(configuration.getChild("component-profile").getValue());

    cd.setComponentComposer(configuration.getChild("component-composer").getValue());

    cd.setComponentFactory(configuration.getChild("component-factory").getValue());

    cd.setDescription(configuration.getChild("description").getValue());

    cd.setAlias(configuration.getChild("alias").getValue());

    String s = configuration.getChild("isolated-realm").getValue();

    if (s != null) {
      cd.setIsolatedRealm(s.equals("true") ? true : false);
    }

    // ----------------------------------------------------------------------
    // Here we want to look for directives for inlining external
    // configurations. we probably want to take them from files or URLs.
    // ----------------------------------------------------------------------

    cd.setConfiguration(configuration.getChild("configuration"));

    // ----------------------------------------------------------------------
    // Requirements
    // ----------------------------------------------------------------------

    PlexusConfiguration[] requirements =
        configuration.getChild("requirements").getChildren("requirement");

    for (int i = 0; i < requirements.length; i++) {
      PlexusConfiguration requirement = requirements[i];

      ComponentRequirement cr = new ComponentRequirement();

      cr.setRole(requirement.getChild("role").getValue());

      cr.setRoleHint(requirement.getChild("role-hint").getValue());

      cr.setFieldName(requirement.getChild("field-name").getValue());

      cd.addRequirement(cr);
    }

    return cd;
  }