/**
  * Add the configuration for a component, or update any existing one that represents the {@link
  * ConfigType#equals(Object) same configuration}
  *
  * @param config the new configuration
  * @return true if the component was added, or false if there already was an existing and {@link
  *     ComponentConfig#hasChanged(ComponentConfig) unchanged} component configuration
  * @throws IllegalArgumentException if <code>config</code> is null
  * @see #update(ComponentConfig)
  * @see #remove(ComponentConfig)
  */
 public boolean add(ConfigType config) {
   CheckArg.isNotNull(config, "component configuration");
   try {
     this.lock.lock();
     // Find an existing configuration that matches ...
     int index = findIndexOfMatchingConfiguration(config);
     if (index >= 0) {
       // See if the matching configuration has changed ...
       ConfigType existingConfig = this.configs.get(index);
       if (existingConfig.hasChanged(config)) {
         // It has changed, so we need to replace it ...
         this.configs.set(index, config);
         this.instances.set(index, newInstance(config));
       }
       return false;
     }
     // Didn't find one, so add it ...
     if (addBeforeExistingConfigs) {
       this.configs.add(0, config);
       this.instances.add(0, newInstance(config));
     } else {
       this.configs.add(config);
       this.instances.add(newInstance(config));
     }
     return true;
   } finally {
     this.lock.unlock();
   }
 }
  /**
   * Instantiate, configure and return a new component described by the supplied configuration. This
   * method does not manage the returned instance.
   *
   * @param config the configuration describing the component
   * @return the new component, or null if the component could not be successfully configured
   * @throws IllegalArgumentException if the component could not be configured properly
   */
  @SuppressWarnings("unchecked")
  protected ComponentType newInstance(ConfigType config) {
    String[] classpath = config.getComponentClasspathArray();
    final ClassLoader classLoader = this.getClassLoaderFactory().getClassLoader(classpath);
    assert classLoader != null;
    ComponentType newInstance = null;
    try {
      // Don't use ClassLoader.loadClass(String), as it doesn't properly initialize the class
      // (specifically static initializers may not be called)
      Class<?> componentClass = Class.forName(config.getComponentClassname(), true, classLoader);
      newInstance = doCreateInstance(componentClass);
      if (newInstance instanceof Component) {
        ((Component<ConfigType>) newInstance).setConfiguration(config);
      }

      if (config.getProperties() != null) {
        for (Map.Entry<String, Object> entry : config.getProperties().entrySet()) {
          // Set the JavaBean-style property on the RepositorySource instance ...
          Reflection reflection = new Reflection(newInstance.getClass());
          reflection.invokeSetterMethodOnTarget(entry.getKey(), newInstance, entry.getValue());
        }
      }
      configure(newInstance, config);
    } catch (Throwable e) {
      throw new SystemFailureException(e);
    }
    if (newInstance instanceof Component
        && ((Component<ConfigType>) newInstance).getConfiguration() == null) {
      throw new SystemFailureException(CommonI18n.componentNotConfigured.text(config.getName()));
    }
    return newInstance;
  }
 /**
  * Find the index for the matching {@link #configs configuration} and {@link #instances
  * component}.
  *
  * @param name the configuration name; may not be null
  * @return the index, or -1 if not found
  */
 @GuardedBy(value = "lock")
 protected int findIndexOfMatchingConfiguration(String name) {
   // Iterate through the configurations and look for an existing one that matches
   for (int i = 0, length = this.configs.size(); i != length; i++) {
     ConfigType existingConfig = this.configs.get(i);
     assert existingConfig != null;
     if (existingConfig.getName().equals(name)) {
       return i;
     }
   }
   return -1;
 }
예제 #4
0
 @Override
 public Object visiteWatchList(final NodeWatchlist node, final Object data) {
   final ObjConfiguration configuration = ((ObjManifestation) data).getConfiguration();
   final ObjConfig config = new ObjConfig(ConfigType.valueOf(node._name), node._title);
   config.setId(node._id);
   config.setTitre(node._title);
   for (int i = 0; i < node.getNumChild(); i++) {
     final ColTable colTable = (ColTable) node.getChild(i).accept(this, null);
     config.addColTable(colTable);
   }
   configuration.addConfig(config);
   return null;
 }
  /** Sets the value of the attribute */
  @Override
  public void setText(Object bean, QName name, String value) throws ConfigException {
    if ("#text".equals(name.getLocalName())) {
      if (value == null || value.trim().length() == 0) return;

      throw new ConfigException(
          L.l(
              "text is not allowed for bean {0}\n  '{1}'",
              bean.getClass().getName(), value.trim()));
    }

    try {
      _putMethod.invoke(bean, name.getLocalName(), _type.valueOf(value));
    } catch (Exception e) {
      throw ConfigException.create(_putMethod, e);
    }
  }