Package artofillusion.util

Examples of artofillusion.util.SearchlistClassLoader


    Locale.setDefault(local);

    HelpSet helpset;
    Page parent;
    URL helpurl;
    SearchlistClassLoader loader = new
        SearchlistClassLoader(this.getClass().getClassLoader());
    //    (SearchlistClassLoader) this.getClass().getClassLoader();
       
    File dir = new File(ArtOfIllusion.PLUGIN_DIRECTORY, "Help");

    /*
     *  NTJ: hack to get library files working for the EA

    String[] jars = new String[] { "helpgui-1.1a.jar",
        "jhall.jar", "jhelpaction.jar", "pircbot.jar",
              "AOIHelp.zip"
    };

    URLClassLoader myldr = null;
    Method addURL = null;
    File urlfile = null;
    Object[] urlarg = new Object[1];
    try {
        myldr = (URLClassLoader) getClass().getClassLoader();
        Class [] sig = new Class[] { URL.class };
        addURL = URLClassLoader.class.getDeclaredMethod("addURL",
                    sig);
        addURL.setAccessible(true);
    } catch (Exception e) {
        System.out.println("failed to get addURL: " + e);
    }

    for (i = 0; i < jars.length; i++) {
        try {
      urlfile = new File(dir, jars[i]);
      urlarg[0] = urlfile.toURI().toURL();
      addURL.invoke(myldr, urlarg);
        } catch (Exception e) {
      System.out.println("failed to update classloader: " +
             e);
        }
    }
    */

    try {
        if (loader.getResource("helpset/AOIHelp.hs") == null) {

      throw new RuntimeException("cannot load global helpset");
        }

        /*
         *  NTJ: JavaHelp is optional.
         *  If we fail to find it in the classpath, fall back to
         *  HelpGUI
         */
        try {
      JHelpAction.startHelpWorker("helpset/AOIHelp.hs");

      Method getbroker =
          JHelpAction.class.getDeclaredMethod("getHelpBroker",
                  null);
      getbroker.setAccessible(true);

      global = (HelpSet)
          ((HelpBroker) getbroker.invoke(null, null)).getHelpSet();
      helplib = JAVAHELP_LIB;
        } catch (Throwable e) {
      System.out.println("HelpPlugin: could not initialise JavaHelp");
        }

        /*
         *  NTJ: drive HelpSet manually
        helpurl = HelpSet.findHelpSet(loader, "helpset/AOIHelp",
              local);

        global = new HelpSet(helpurl);
        broker = global.createHelpBroker();

        final HelpBroker brok = broker;

        (new Thread() {
          public void run()
          { brok.initPresentation(); }
      }).start();
        */

        /*
         *  NTJ: Using (open-source) HelpGui derivative as fallback
         */
        java.util.List list;
        if (helplib == 0) {
      try {
          docset = new DocSet(Translate.getLocale());

          list =
        PluginRegistry.getPlugins(DocSet.Parser.class);

          DocSet.Parser parser;
          for (i = 0; i < list.size(); i++) {
        parser = (DocSet.Parser) list.get(i);
        System.out.println("HelpPlugin.INIT: " +
               "registering parser: " +
               parser.getClass().getName());
        DocSet.registerParser(parser);
          }

          docset.setLoader(loader);
          docset.add("helpset/AOIHelp.hs", null);
         
          helplib = HELPGUI_LIB;
      } catch (Throwable t) {
          System.out.println("HelpPlugin: could not initialise HelpGUI");
      }
        }

        helpset = global;
        parent = null;

        enabled = (helplib != 0);

        /*
         *  scan Plugins/Help and add any help resources found.
         */

        File files[] = dir.listFiles();
        File subdir[], helpfile;
        int cut, subcount;

        ZipFile zf = null;
        ZipEntry ze = null;
        String basename;
        ClassLoader ldr;
        URL url[] = new URL[1];

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

      // create a subsection for each subdirectory
      if (files[i].isDirectory()) {
          subdir = files[i].listFiles();
          subcount = subdir.length;

          if (subcount > 0) {
        if (helplib == JAVAHELP_LIB) {
            helpset = new HelpSet(loader);
            global.add(helpset);
        }
        else if (helplib == HELPGUI_LIB) {
            parent = docset.addSection(files[i].getName(), null);
        }
          }
      }
      else {
          subdir = null;
          subcount = 1;
          helpset = global;
          parent = null;
      }

      for (k = 0; k < subcount; k++) {
          helpfile = (subdir != null ? subdir[k] : files[i]);

          try {
        // look inside all zip files
        zf = new ZipFile(helpfile);

        // look for a helpset entry (*.hs)
        Enumeration iter = zf.entries();
        while (iter.hasMoreElements()) {
            ze = (ZipEntry) iter.nextElement();
            if (ze.isDirectory()) continue;
            if (ze.getName().endsWith(".hs")) break;
            ze = null;
        }

        // no helpset entry found, try next...
        if (ze == null) continue;

        basename = ze.getName();
        cut = basename.lastIndexOf('.');
        if (cut > 0)
            basename = basename.substring(0, cut);

        // if it's a helpset, add it to the resources
        System.out.println("HelpPlugin.ADD: " +
               basename);


        url[0] = helpfile.toURI().toURL();
        ldr = new URLClassLoader(url);
        PluginRegistry.registerResource("help",
                basename,
                ldr,
                ze.getName(),
                null);

          } catch (Exception e) {}
          finally {
        if (zf != null) {
            zf.close();
            zf = null;
        }
          }
      }
        }

        HelpSet plugset=null;
        if (helplib == JAVAHELP_LIB) plugset = new HelpSet(loader);

        // load all the registered help resources
        list = PluginRegistry.getResources("help");

        System.out.println("resources found=" + list.size());

        if (list.size() > 0) {
      if (docset != null)
          parent = docset.addSection("Plugins", null);

      PluginRegistry.PluginResource resource;
      for (i = 0; i < list.size(); i++) {
          resource =
        (PluginRegistry.PluginResource) list.get(i);

          // don't load global help a second time
          if (resource.getId().equals("helpset/AOIHelp"))
        continue;

          System.out.println("Help: linking: " +
                 resource.getURL().getPath());

          loader.add(resource.getClassLoader());

          System.out.println("loading: " + resource.getId() +
                 " from " +
                 resource.getURL().getPath());

          /*
           * NTJ: *ugh* After all that work, something in
           *   in JavaHelp is only using the loader of the
           *   plugin. I suspect it is JHelpDev...
           */

          if (plugset != null)
        //plugset.add(new HelpSet(resource.getClassLoader(), resource.getURL()));
        plugset.add(new HelpSet(loader, resource.getURL()));
          else {
        loader.add(resource.getClassLoader());
        docset.add(resource.getURL().getPath(),parent);
          }
      }
        }

View Full Code Here


      int i, j, idx;
      URL urlList[];
      ArrayList aoiloaders;
      ClassLoader ldr = null;
      URLClassLoader urlldr = null;
      SearchlistClassLoader searchldr = null;
      Object obj;

      java.util.List list = PluginRegistry.getPluginClassLoaders();
      HashMap loaders = new HashMap(list.size());
      for (i = 0; i < list.size(); i++) {
    obj = list.get(i);
    if (obj instanceof URLClassLoader)
        urlList = ((URLClassLoader) obj).getURLs();
    else
        urlList = ((SearchlistClassLoader) obj).getURLs();

    if (urlList.length > 0)
        loaders.put(urlList[0], obj);
      }

      /*
      try {
    Field ldrfield =
        PluginRegistry.class.getDeclaredField("pluginLoaders");

    ldrfield.setAccessible(true);

    aoiloaders = (ArrayList) ldrfield.get(null);

      } catch (Exception e) {
    System.out.println("SPManager: cannot get pluginsLoaders: " +
           e);
    aoiloaders = new ArrayList();
      }
       */

      URL[] urlarg = new URL[1];
      Class[] sig = new Class[] { URL.class };
      Method addUrl = null;

      try {
    addUrl =
        URLClassLoader.class.getDeclaredMethod("addURL", sig);
    addUrl.setAccessible(true);
      } catch (Exception e) {
    System.out.println("Error getting addURL method: " + e);
      }

      // get details of all local plugins
      SPMObjectInfo info;
      StringBuffer errs = null;
      Class plugType;
      File files[], urlfile;
      URL url;
      Map.Entry entry;
      String key[], value;
      File plugdir = new File(PLUGIN_DIRECTORY);
      if (plugdir.exists()) {
    files = plugdir.listFiles();
    for (i = 0; i < files.length; i++) {
        info = new SPMObjectInfo(files[i].getAbsolutePath());

        if (info.invalid) {
      if (errs == null) errs = new StringBuffer(1024);
      if (errs.length() > 0) errs.append('\n');
      errs.append(SPMTranslate.text("pluginFailure",
        info.getName()));
        }

        if (info.actions != null && info.actions.size() > 0) {

      try {
          url = files[i].toURI().toURL();
      } catch (Exception e) {
          continue;
      }

      //System.out.println("SPM: url=" + url);

      // get the classloader for the current plugin
      obj = loaders.get(url);

      if (obj == null) {
          System.out.println("SPManager: could not find"
            + " classloader: "
            + files[i].getPath());
          continue;
      }

      // cast or convert it to a SearchlistClassLoader
      if (obj instanceof SearchlistClassLoader) {
          searchldr = (SearchlistClassLoader) obj;
          //System.out.println("loader is a srchloader");
      }
      else {
          urlldr = (URLClassLoader) obj;

          /*
          searchldr =
        new SearchlistClassLoader((ClassLoader) obj);

          idx = aoiloaders.indexOf(obj);
          if (idx >= 0) aoiloaders.set(idx, searchldr);
          else System.out.println("SPM: loader not in list");

          loaders.put(url, searchldr);
           */
      }

      // ok, now perform the actions
      for (Iterator iter = info.actions.entrySet().iterator();
      iter.hasNext(); ) {

          entry = (Map.Entry) iter.next();
          key = entry.getKey().toString().split(":");

          //System.out.println("SPM: action=" +
          //         entry.getValue().toString());

          try {
        if (key[0].startsWith("/"))
            urlfile = new File(APP_DIRECTORY, key[0].substring(1));
        else
            urlfile = new File(plugdir, key[0]);
       
        url = urlfile.toURI().toURL();
          } catch (Exception e) {
        System.out.println("Error making url: " + e);
        continue;
          }

          System.out.println("SPM: adding path: " + url);

          value = entry.getValue().toString();

          /*
          if ("merge".equalsIgnoreCase(value)) {
        if (searchldr != null)
            searchldr.merge(url);
        else if (addUrl != null) {
            try {
          urlarg[0] = url;
          addUrl.invoke(urlldr, urlarg);
            } catch (Exception e) {
          System.out.println("Error invoking: "
                 + e);
            }
        }
        else System.out.println("Could not merge path"
              + url);           
          }
           */
          if ("classpath".equalsIgnoreCase(value)) {
        if (searchldr != null)
            searchldr.add(url);
        else if (addUrl != null) {
            try {
          //urlarg[0] = url;
          //addUrl.invoke(urlldr, urlarg);        // non-varargs call (1.4)
          addUrl.invoke(urlldr, url);    // varargs call (1.5)
            } catch (Exception e) {
          System.out.println("Error invoking: "
            + e);
            }
        }
        else System.out.println("Could not add path" +
          url);           
          }
          else if ("import".equalsIgnoreCase(value)) {
        ldr = (ClassLoader) loaders.get(url);
       
        if (key.length == 1) {
            if (obj != null) searchldr.add(ldr);
            else System.out.println("SPM: could not find"
              + " loader for: " + url);
        }
        /*
         * NTJ - disabled. No longer needed, and requires a new method in SearchlistClassLoader
View Full Code Here

TOP

Related Classes of artofillusion.util.SearchlistClassLoader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.