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;
  }
  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);
    }
  }
Esempio n. 3
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());
    }
  }