private void loadDeploymentFactories()

in src/main/java/org/apache/openejb/cts/deploy/DeployTestUtil.java [153:237]


  private void loadDeploymentFactories() throws Exception {

    File deployPlatformJarFile;

    try {
      deployPlatformJarFile = new File(dmp.getJarFile());
    } catch (Exception e) {
      throw new Exception(
          "Could not contruct file object: " + dmp.getJarFile());
    }

    // Obtain DeploymentFactory names from manifest and load those entries
    TestUtil.logHarness("loadDeploymentFactories loading factories from file:"
        + deployPlatformJarFile.getName());

    Manifest mf = new java.util.jar.JarFile(deployPlatformJarFile)
        .getManifest();

    // This should be rewritten to get multiple class names from the manifest
    TestUtil.logHarness(
        "Looking for manifest entry: " + DEPLOYMENT_MANIFEST_ENTRY_NAME);
    String dfClassNames = mf.getMainAttributes()
        .getValue(DEPLOYMENT_MANIFEST_ENTRY_NAME);

    if (dfClassNames == null) {
      throw new Exception("Invalid Jar File: No manifest entry.");
    }

    // Parse class names, assuming that class names are separated by space
    java.util.StringTokenizer st = new java.util.StringTokenizer(dfClassNames,
        " ");
    Vector classNames = new Vector();
    while (st.hasMoreTokens())
      classNames.add(st.nextToken());

    // Load classes
    try {
      URL[] urls = new URL[] { deployPlatformJarFile.toURL() };
      URLClassLoader urlClassLoader = new URLClassLoader(urls,
          this.getClass().getClassLoader());

      // Do i need to set the contect class loader ?
      TestUtil.logHarness(
          "loadDeploymentFactories about to load classes :" + dfClassNames);
      Thread.currentThread().setContextClassLoader(urlClassLoader);

      // Load all classes
      for (int classNameIndex = 0; classNameIndex < classNames
          .size(); classNameIndex++) {

        Class factory = null;
        DeploymentFactory df = null;

        try {
          factory = urlClassLoader
              .loadClass((String) classNames.elementAt(classNameIndex));
          df = (DeploymentFactory) factory.newInstance();
        } catch (Exception e) {
          e.printStackTrace();
        }

        if (df instanceof DeploymentFactory) {
          DeploymentFactoryManager.getInstance()
              .registerDeploymentFactory((DeploymentFactory) df);
          TestUtil
              .logMsg("Registered DeploymentFactory: " + df.getDisplayName());
        } else {
          throw new Exception("Does not implement DeploymentFactory");
        }

        // Class.forName((String)classNames.elementAt(classNameIndex),
        // true, urlClassLoader);

        // Register DeploymentFactory with DeploymentFactoryManager
        // DeploymentFactory df = (DeploymentFactory)classNames.elementAt
        // (classNameIndex);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
      throw new Exception(
          "Could not load DeploymentFactory class: " + ex.getMessage());
    }

  }