Пример #1
0
  // TODO: Do not swallow exception
  public void initializeResources() throws PlexusConfigurationException {
    PlexusConfiguration[] resourceConfigs = configuration.getChild("resources").getChildren();

    for (int i = 0; i < resourceConfigs.length; ++i) {
      try {
        String name = resourceConfigs[i].getName();

        if (name.equals("jar-repository")) {
          addJarRepository(new File(resourceConfigs[i].getValue()));
        } else if (name.equals("directory")) {
          File directory = new File(resourceConfigs[i].getValue());

          if (directory.exists() && directory.isDirectory()) {
            plexusRealm.addConstituent(directory.toURL());
          }
        } else {
          getLogger().warn("Unknown resource type: " + name);
        }
      } catch (MalformedURLException e) {
        getLogger()
            .error(
                "Error configuring resource: "
                    + resourceConfigs[i].getName()
                    + "="
                    + resourceConfigs[i].getValue(),
                e);
      }
    }
  }
Пример #2
0
  private void initializeClassWorlds() throws DuplicateRealmException {
    if (classWorld == null) {
      classWorld = new ClassWorld();
    }

    // Create a name for our application if one doesn't exist.
    initializeName();

    if (coreRealm == null) {
      try {
        coreRealm = classWorld.getRealm("plexus.core");
      } catch (NoSuchRealmException e) {
        /* We are being loaded with someone who hasn't
         * given us any classworlds realms.  In this case,
         * we want to use the classes already in the
         * ClassLoader for our realm.
         */
        coreRealm =
            classWorld.newRealm("plexus.core", Thread.currentThread().getContextClassLoader());
      }
    }

    // We are in a non-embedded situation
    if (plexusRealm == null) {
      try {
        plexusRealm = coreRealm.getWorld().getRealm("plexus.core.maven");
      } catch (NoSuchRealmException e) {
        // plexusRealm = coreRealm.getWorld().newRealm( "plexus.core.maven" );

        // If no app realm can be found then we will make the plexusRealm
        // the same as the app realm.

        plexusRealm = coreRealm;
      }

      // plexusRealm.importFrom( coreRealm.getId(), "" );

      addContextValue("common.classloader", plexusRealm.getClassLoader());

      Thread.currentThread().setContextClassLoader(plexusRealm.getClassLoader());
    }
  }
Пример #3
0
  public ScriptFactory(
      final Logger logger,
      final ClassRealm classRealm,
      final File jrubyJar,
      final List<String> classpathElements,
      final boolean fork)
      throws ScriptException, IOException {
    this.logger = logger;
    this.jrubyJar = jrubyJar;
    this.classpathElements =
        classpathElements == null ? NO_CLASSPATH : Collections.unmodifiableList(classpathElements);
    this.fork = fork;
    if (classRealm != null) {
      ClassRealm jruby;
      try {
        jruby = classRealm.getWorld().getRealm("jruby");
      } catch (final NoSuchRealmException e) {
        try {
          jruby = classRealm.getWorld().newRealm("jruby");
          jruby.addConstituent(jrubyJar.toURI().toURL());
        } catch (final DuplicateRealmException ee) {
          throw new ScriptException("could not setup classrealm for jruby", ee);
        }
      }
      this.classRealm = jruby;
    } else {
      this.classRealm = null;
    }

    if (fork) {
      this.launcher = new AntLauncher(logger, this);
    } else {

      this.launcher = new EmbeddedLauncher(logger, this);
    }
  }
Пример #4
0
  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);
  }
Пример #5
0
  public void addJarResource(File jar) throws PlexusContainerException {
    try {
      plexusRealm.addConstituent(jar.toURL());

      if (isStarted()) {
        discoverComponents(plexusRealm);
      }
    } catch (MalformedURLException e) {
      throw new PlexusContainerException("Cannot add jar resource: " + jar + " (bad URL)", e);
    } catch (PlexusConfigurationException e) {
      throw new PlexusContainerException(
          "Cannot add jar resource: " + jar + " (error discovering new components)", e);
    } catch (ComponentRepositoryException e) {
      throw new PlexusContainerException(
          "Cannot add jar resource: " + jar + " (error discovering new components)", e);
    }
  }
  private ClassRealm cloneClassRealm(
      final File jrubyJar, final List<String> classpathElements, final ClassRealm classRealm)
      throws ScriptException {
    // TODO how to reuse the plugin realm ?
    //        for (final String classpath : classpathElements) {
    //            if (classpath.equals(jrubyJar.getAbsolutePath())) {
    //                return null;
    //            }
    //        }
    ClassRealm newClassRealm;
    try {
      ClassRealm jruby;
      try {
        jruby = classRealm.getWorld().getRealm("jruby");
      } catch (final NoSuchRealmException e) {
        jruby = classRealm.getWorld().newRealm("jruby");
        jruby.addConstituent(jrubyJar.toURI().toURL());
      }
      try {
        jruby.getWorld().disposeRealm("pom");
      } catch (final NoSuchRealmException e) {
        // ignored
      }
      newClassRealm = jruby.createChildRealm("pom");
      for (final String classpath : classpathElements) {
        if (!classpath.contains("jruby-complete")) {
          newClassRealm.addConstituent(new File(classpath).toURI().toURL());
        }
      }
    } catch (final DuplicateRealmException e) {
      throw new ScriptException("error in naming realms", e);
    } catch (final MalformedURLException e) {
      throw new ScriptException("hmm. found some malformed URL", e);
    }

    return newClassRealm;
  }
Пример #7
0
  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);
    }
  }
Пример #8
0
  protected void initializeConfiguration()
      throws ConfigurationProcessingException, ConfigurationResourceNotFoundException,
          PlexusConfigurationException {
    // System userConfiguration

    InputStream is = coreRealm.getResourceAsStream(BOOTSTRAP_CONFIGURATION);

    if (is == null) {
      throw new IllegalStateException(
          "The internal default plexus-bootstrap.xml is missing. "
              + "This is highly irregular, your plexus JAR is "
              + "most likely corrupt.");
    }

    PlexusConfiguration systemConfiguration =
        PlexusTools.buildConfiguration(BOOTSTRAP_CONFIGURATION, new InputStreamReader(is));

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    // ----------------------------------------------------------------------
    //
    // ----------------------------------------------------------------------

    // Some of this could probably be collapsed as having a plexus.xml in your
    // META-INF/plexus directory is probably a better solution then specifying
    // a configuration with an URL but I'm leaving the configuration by URL
    // as folks might be using it ... I made this change to accomodate Maven
    // but I think it's better to discover a configuration in a standard
    // place.

    configuration = systemConfiguration;

    PlexusXmlComponentDiscoverer discoverer = new PlexusXmlComponentDiscoverer();
    PlexusConfiguration plexusConfiguration =
        discoverer.discoverConfiguration(getContext(), plexusRealm);

    if (plexusConfiguration != null) {
      configuration = PlexusConfigurationMerger.merge(plexusConfiguration, configuration);

      processConfigurationsDirectory();
    }

    if (configurationReader != null) {
      // User userConfiguration

      PlexusConfiguration userConfiguration =
          PlexusTools.buildConfiguration(
              "<User Specified Configuration Reader>",
              getInterpolationConfigurationReader(configurationReader));

      // Merger of systemConfiguration and user userConfiguration

      configuration = PlexusConfigurationMerger.merge(userConfiguration, configuration);

      processConfigurationsDirectory();
    }

    // ---------------------------------------------------------------------------
    // Now that we have the configuration we will use the ConfigurationProcessor
    // to inline any external configuration instructions.
    //
    // At his point the variables in the configuration have already been
    // interpolated so we can send in an empty Map because the context
    // values are already there.
    // ---------------------------------------------------------------------------

    ConfigurationProcessor p = new ConfigurationProcessor();

    p.addConfigurationResourceHandler(new FileConfigurationResourceHandler());

    p.addConfigurationResourceHandler(new DirectoryConfigurationResourceHandler());

    configuration = p.process(configuration, Collections.EMPTY_MAP);
  }
Пример #9
0
  public PlexusContainer createChildContainer(
      String name, List classpathJars, Map context, List discoveryListeners)
      throws PlexusContainerException {
    if (hasChildContainer(name)) {
      throw new DuplicateChildContainerException(getName(), name);
    }

    DefaultPlexusContainer child = new DefaultPlexusContainer();

    child.classWorld = classWorld;

    ClassRealm childRealm = null;

    String childRealmId = getName() + ".child-container[" + name + "]";
    try {
      childRealm = classWorld.getRealm(childRealmId);
    } catch (NoSuchRealmException e) {
      try {
        childRealm = classWorld.newRealm(childRealmId);
      } catch (DuplicateRealmException impossibleError) {
        getLogger()
            .error(
                "An impossible error has occurred. After getRealm() failed, newRealm() "
                    + "produced duplication error on same id!",
                impossibleError);
      }
    }

    childRealm.setParent(plexusRealm);

    child.coreRealm = childRealm;

    child.plexusRealm = childRealm;

    child.setName(name);

    child.setParentPlexusContainer(this);

    // ----------------------------------------------------------------------
    // Set all the child elements from the parent that were set
    // programmatically.
    // ----------------------------------------------------------------------

    child.setLoggerManager(loggerManager);

    for (Iterator it = context.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry) it.next();

      child.addContextValue(entry.getKey(), entry.getValue());
    }

    child.initialize();

    for (Iterator it = classpathJars.iterator(); it.hasNext(); ) {
      Object next = it.next();

      File jar = (File) next;

      child.addJarResource(jar);
    }

    for (Iterator it = discoveryListeners.iterator(); it.hasNext(); ) {
      ComponentDiscoveryListener listener = (ComponentDiscoveryListener) it.next();

      child.registerComponentDiscoveryListener(listener);
    }

    child.start();

    childContainers.put(name, child);

    return child;
  }