예제 #1
0
  @Override
  public Module createModule(final ModuleInfo info) {
    final Module existing = getRegisteredModuleInstance(info);
    if (existing != null) return existing;

    try {
      final Module module = info.createModule();
      getContext().inject(module);
      Priority.inject(module, info.getPriority());
      return module;
    } catch (final ModuleException exc) {
      log.error("Cannot create module: " + info.getDelegateClassName());
    }
    return null;
  }
예제 #2
0
  /**
   * Checks the {@link ObjectService} for a single registered {@link Module} instance of the given
   * {@link ModuleInfo}. In this way, if you want to repeatedly reuse the same module instance
   * instead of creating a new one with each execution, you can register it with the {@link
   * ObjectService} and it will be automatically returned by the {@link #createModule(ModuleInfo)}
   * method (and hence automatically used whenever a {@link #run} method with {@link ModuleInfo}
   * argument is called).
   */
  private Module getRegisteredModuleInstance(final ModuleInfo info) {
    final Class<?> type = ClassUtils.loadClass(info.getDelegateClassName());
    if (type == null || !Module.class.isAssignableFrom(type)) return null;

    // the module metadata's delegate class extends Module, so there is hope
    @SuppressWarnings("unchecked")
    final Class<? extends Module> moduleType = (Class<? extends Module>) type;

    // ask the object service for an instance of the delegate type
    final List<? extends Module> objects = objectService.getObjects(moduleType);
    if (objects == null || objects.isEmpty()) {
      // the object service has no such instances
      return null;
    }
    if (objects.size() > 1) {
      // there are multiple instances; it's not clear which one to use
      log.warn("Ignoring multiple candidate module instances for class: " + type.getName());
      return null;
    }
    // found exactly one instance; return it!
    return objects.get(0);
  }