Ejemplo n.º 1
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);
      }
    }
  }
 /**
  * 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();
 }