Ejemplo n.º 1
1
  public static void main(String[] args) throws Exception {
    System.setSecurityManager(new SecurityManager());

    // Create an uninitialized class loader
    try {
      new ClassLoader(null) {
        @Override
        protected void finalize() {
          loader = this;
        }
      };
    } catch (SecurityException exc) {
      // Expected
    }
    System.gc();
    System.runFinalization();

    // if 'loader' isn't null, need to ensure that it can't be used as
    // parent
    if (loader != null) {
      try {
        // Create a class loader with 'loader' being the parent
        URLClassLoader child = URLClassLoader.newInstance(new URL[0], loader);
        throw new RuntimeException("Test Failed!");
      } catch (SecurityException se) {
        System.out.println("Test Passed: Exception thrown");
      }
    } else {
      System.out.println("Test Passed: Loader is null");
    }
  }
Ejemplo n.º 2
0
  public NationClassLoader(URL[] urls, Vector nations) {
    super(urls);

    for (int i = 0; i < urls.length; i++) {
      file = urls[i].getFile();
      fileTmp = new File(file);
      System.out.println(
          "NationClassLoader - Does " + fileTmp.toString() + " exists : " + fileTmp.exists());
      name = file.substring(file.lastIndexOf("/") + 1, file.lastIndexOf("."));

      runtimeObject = null;
      try {
        jarLoader = URLClassLoader.newInstance(new URL[] {urls[i]});
        System.out.println(
            "NationClassLoader - JARloader trying : " + jarLoader.getURLs()[0].toString());
        runtimeObject = jarLoader.loadClass(name + "/" + name).newInstance();
      } catch (Exception e) {
        System.err.println("NationClassLoader - Stinking error : " + e);
      }

      if (runtimeObject != null) {
        natFile = (nationFile2) runtimeObject;
        nations.addElement(natFile);
      }
    }
  }
Ejemplo n.º 3
0
  public static void main(String[] args) throws Exception {
    Class thisClass = MethodResultTest.class;
    Class exoticClass = Exotic.class;
    String exoticClassName = Exotic.class.getName();
    ClassLoader testClassLoader = thisClass.getClassLoader();
    if (!(testClassLoader instanceof URLClassLoader)) {
      System.out.println("TEST INVALID: Not loaded by a " + "URLClassLoader: " + testClassLoader);
      System.exit(1);
    }

    URLClassLoader tcl = (URLClassLoader) testClassLoader;
    URL[] urls = tcl.getURLs();
    ClassLoader shadowLoader =
        new ShadowLoader(
            urls,
            testClassLoader,
            new String[] {
              exoticClassName, ExoticMBeanInfo.class.getName(), ExoticException.class.getName()
            });
    Class cl = shadowLoader.loadClass(exoticClassName);
    if (cl == exoticClass) {
      System.out.println(
          "TEST INVALID: Shadow class loader loaded " + "same class as test class loader");
      System.exit(1);
    }
    Thread.currentThread().setContextClassLoader(shadowLoader);

    ObjectName on = new ObjectName("a:b=c");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    mbs.createMBean(Thing.class.getName(), on);

    final String[] protos = {"rmi", "iiop", "jmxmp"};

    boolean ok = true;
    for (int i = 0; i < protos.length; i++) {
      try {
        ok &= test(protos[i], mbs, on);
        System.out.println();
      } catch (Exception e) {
        System.out.println("TEST FAILED WITH EXCEPTION:");
        e.printStackTrace(System.out);
        ok = false;
      }
    }

    if (ok) System.out.println("Test passed");
    else {
      System.out.println("TEST FAILED");
      System.exit(1);
    }
  }
Ejemplo n.º 4
0
  public static void main(String[] args) throws Exception {
    System.out.println(
        "Test that target MBean class loader is used " + "before JMX Remote API class loader");

    ClassLoader jmxRemoteClassLoader = JMXServiceURL.class.getClassLoader();
    if (jmxRemoteClassLoader == null) {
      System.out.println(
          "JMX Remote API loaded by bootstrap " + "class loader, this test is irrelevant");
      return;
    }
    if (!(jmxRemoteClassLoader instanceof URLClassLoader)) {
      System.out.println("TEST INVALID: JMX Remote API not loaded by " + "URLClassLoader");
      System.exit(1);
    }

    URLClassLoader jrcl = (URLClassLoader) jmxRemoteClassLoader;
    URL[] urls = jrcl.getURLs();
    PrivateMLet mlet = new PrivateMLet(urls, null, false);
    Class shadowClass = mlet.loadClass(JMXServiceURL.class.getName());
    if (shadowClass == JMXServiceURL.class) {
      System.out.println("TEST INVALID: MLet got original " + "JMXServiceURL not shadow");
      System.exit(1);
    }

    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    mbs.registerMBean(mlet, mletName);

    final String[] protos = {"rmi", "iiop", "jmxmp"};
    boolean ok = true;
    for (int i = 0; i < protos.length; i++) {
      try {
        ok &= test(protos[i], mbs);
      } catch (Exception e) {
        System.out.println("TEST FAILED WITH EXCEPTION:");
        e.printStackTrace(System.out);
        ok = false;
      }
    }

    if (ok) System.out.println("Test passed");
    else {
      System.out.println("TEST FAILED");
      System.exit(1);
    }
  }
 /**
  * Method to create concrete instances of this abstract class. Instances are dynamically loaded.
  * Instances's names are obtained by concatenating a prefix, either "Light", "Regular", or
  * "Gourmet", with the suffix "PizzaFactory".
  *
  * @param style An integer representing the style of pizzas to create, e.g., 0 stands for
  *     "Light". @ @return a concrete pizza factory
  */
 static AbstractPizzaFactory getFactory(int style)
     throws ClassNotFoundException, InstantiationException, IllegalAccessException,
         IllegalArgumentException, MalformedURLException {
   if (0 <= style && style <= prefix.length - 1) {
     // Alternative #1 - load from current directory
     // AbstractPizzaFactory apf =
     //    (AbstractPizzaFactory) Class.forName
     //        (prefix [style] + suffix).newInstance ();
     // Alternative 2 - load from URL (including JAR file)
     // It looks like only the protocol "file:" is
     // required to load files from the execution directory
     URL[] classPath = {new URL("file:")};
     URLClassLoader classLoader = new URLClassLoader(classPath);
     String name = prefix[style] + suffix;
     Class someClass = classLoader.loadClass(name);
     AbstractPizzaFactory apf = (AbstractPizzaFactory) someClass.newInstance();
     // end of alternatives
     return apf;
   } else throw new IllegalArgumentException();
 }
Ejemplo n.º 6
0
 /**
  * adds a URL to the classloader.
  *
  * @param url the new classpath element
  */
 public void addURL(URL url) {
   super.addURL(url);
 }
Ejemplo n.º 7
0
  private static LaunchablePlugin[] findLaunchablePlugins(LoggerChannelListener listener) {
    // CAREFUL - this is called BEFORE any AZ initialisation has been performed and must
    // therefore NOT use anything that relies on this (such as logging, debug....)

    List res = new ArrayList();

    File app_dir = getApplicationFile("plugins");

    if (!(app_dir.exists()) && app_dir.isDirectory()) {

      listener.messageLogged(LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' not found");

      return (new LaunchablePlugin[0]);
    }

    File[] plugins = app_dir.listFiles();

    if (plugins == null || plugins.length == 0) {

      listener.messageLogged(LoggerChannel.LT_ERROR, "Application dir '" + app_dir + "' empty");

      return (new LaunchablePlugin[0]);
    }

    for (int i = 0; i < plugins.length; i++) {

      File plugin_dir = plugins[i];

      if (!plugin_dir.isDirectory()) {

        continue;
      }

      try {

        ClassLoader classLoader = PluginLauncherImpl.class.getClassLoader();

        ClassLoader root_cl = classLoader;

        File[] contents = plugin_dir.listFiles();

        if (contents == null || contents.length == 0) {

          continue;
        }

        // take only the highest version numbers of jars that look versioned

        String[] plugin_version = {null};
        String[] plugin_id = {null};

        contents = getHighestJarVersions(contents, plugin_version, plugin_id, true);

        for (int j = 0; j < contents.length; j++) {

          classLoader = addFileToClassPath(root_cl, classLoader, contents[j]);
        }

        Properties props = new Properties();

        File properties_file = new File(plugin_dir, "plugin.properties");

        // if properties file exists on its own then override any properties file
        // potentially held within a jar

        if (properties_file.exists()) {

          FileInputStream fis = null;

          try {
            fis = new FileInputStream(properties_file);

            props.load(fis);

          } finally {

            if (fis != null) {

              fis.close();
            }
          }
        } else {

          if (classLoader instanceof URLClassLoader) {

            URLClassLoader current = (URLClassLoader) classLoader;

            URL url = current.findResource("plugin.properties");

            if (url != null) {

              props.load(url.openStream());
            }
          }
        }

        String plugin_class = (String) props.get("plugin.class");

        // don't support multiple launchable plugins

        if (plugin_class == null || plugin_class.indexOf(';') != -1) {

          continue;
        }

        Class c = classLoader.loadClass(plugin_class);

        Plugin plugin = (Plugin) c.newInstance();

        if (plugin instanceof LaunchablePlugin) {

          preloaded_plugins.put(plugin_class, plugin);

          res.add(plugin);
        }
      } catch (Throwable e) {

        listener.messageLogged("Load of plugin in '" + plugin_dir + "' fails", e);
      }
    }

    LaunchablePlugin[] x = new LaunchablePlugin[res.size()];

    res.toArray(x);

    return (x);
  }