Ejemplo n.º 1
0
  public void testCompareTo() throws Exception {
    ClassWorld cw = new ClassWorld();
    DefaultClassRealm r = (DefaultClassRealm) cw.newRealm("test1");

    Entry entry1 = new Entry(r, "org.test");
    Entry entry2 = new Entry(r, "org.test.impl");

    assertTrue("org.test > org.test.impl", entry1.compareTo(entry2) > 0);
  }
Ejemplo n.º 2
0
  /**
   * Tests the equality is realm independant
   *
   * @throws Exception
   */
  public void testEquals() throws Exception {
    ClassWorld cw = new ClassWorld();
    DefaultClassRealm r1 = (DefaultClassRealm) cw.newRealm("test1");
    DefaultClassRealm r2 = (DefaultClassRealm) cw.newRealm("test2");

    Entry entry1 = new Entry(r1, "org.test");
    Entry entry2 = new Entry(r2, "org.test");

    assertTrue("entry1 == entry2", entry1.equals(entry2));
    assertTrue("entry1.hashCode() == entry2.hashCode()", entry1.hashCode() == entry2.hashCode());
  }
Ejemplo 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());
    }
  }
Ejemplo n.º 4
0
  /** Create a name for our application if one doesn't exist. */
  protected void initializeName() {
    if (name == null) {
      int i = 0;

      while (true) {
        try {
          classWorld.getRealm("plexus.app" + i);
          i++;
        } catch (NoSuchRealmException e) {
          setName("app" + i);
          return;
        }
      }
    }
  }
Ejemplo n.º 5
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;
  }