public static void main(final String[] args) throws Exception {
        // use the platform default proxy if available.
        // see sun.net.spi.DefaultProxySelector for details.
        try {
            System.setProperty("java.net.useSystemProxies","true");
        } catch (SecurityException e) {
            // failing to set this property isn't fatal
        }

        if( Util.getSystemProperty(Driver.class,"noThreadSwap")!=null )
            _main(args);    // for the ease of debugging

        // run all the work in another thread so that the -Xss option
        // will take effect when compiling a large schema. See
        // http://developer.java.sun.com/developer/bugParade/bugs/4362291.html
        final Throwable[] ex = new Throwable[1];

        Thread th = new Thread() {
            public void run() {
                try {
                    _main(args);
                } catch( Throwable e ) {
                    ex[0]=e;
                }
            }
        };
        th.start();
        th.join();

        if(ex[0]!=null) {
            // re-throw
            if( ex[0] instanceof Exception )
                throw (Exception)ex[0];
            else
                throw (Error)ex[0];
        }
    }
示例#2
0
文件: Options.java 项目: n-k/jaxb
  /**
   * Looks for all "META-INF/services/[className]" files and create one instance for each class name
   * found inside this file.
   */
  private static <T> T[] findServices(Class<T> clazz, ClassLoader classLoader) {
    // if true, print debug output
    final boolean debug =
        com.sun.tools.xjc.util.Util.getSystemProperty(Options.class, "findServices") != null;

    // if we are running on Mustang or Dolphin, use ServiceLoader
    // so that we can take advantage of JSR-277 module system.
    try {
      Class<?> serviceLoader = Class.forName("java.util.ServiceLoader");
      if (debug) System.out.println("Using java.util.ServiceLoader");
      Iterable<T> itr =
          (Iterable<T>)
              serviceLoader
                  .getMethod("load", Class.class, ClassLoader.class)
                  .invoke(null, clazz, classLoader);
      List<T> r = new ArrayList<T>();
      for (T t : itr) r.add(t);
      return r.toArray((T[]) Array.newInstance(clazz, r.size()));
    } catch (ClassNotFoundException e) {
      // fall through
    } catch (IllegalAccessException e) {
      Error x = new IllegalAccessError();
      x.initCause(e);
      throw x;
    } catch (InvocationTargetException e) {
      Throwable x = e.getTargetException();
      if (x instanceof RuntimeException) throw (RuntimeException) x;
      if (x instanceof Error) throw (Error) x;
      throw new Error(x);
    } catch (NoSuchMethodException e) {
      Error x = new NoSuchMethodError();
      x.initCause(e);
      throw x;
    }

    String serviceId = "META-INF/services/" + clazz.getName();

    // used to avoid creating the same instance twice
    Set<String> classNames = new HashSet<String>();

    if (debug) {
      System.out.println("Looking for " + serviceId + " for add-ons");
    }

    // try to find services in CLASSPATH
    try {
      Enumeration<URL> e = classLoader.getResources(serviceId);
      if (e == null) return (T[]) Array.newInstance(clazz, 0);

      ArrayList<T> a = new ArrayList<T>();
      while (e.hasMoreElements()) {
        URL url = e.nextElement();
        BufferedReader reader = null;

        if (debug) {
          System.out.println("Checking " + url + " for an add-on");
        }

        try {
          reader = new BufferedReader(new InputStreamReader(url.openStream()));
          String impl;
          while ((impl = reader.readLine()) != null) {
            // try to instanciate the object
            impl = impl.trim();
            if (classNames.add(impl)) {
              Class implClass = classLoader.loadClass(impl);
              if (!clazz.isAssignableFrom(implClass)) {
                pluginLoadFailure = impl + " is not a subclass of " + clazz + ". Skipping";
                if (debug) System.out.println(pluginLoadFailure);
                continue;
              }
              if (debug) {
                System.out.println("Attempting to instanciate " + impl);
              }
              a.add(clazz.cast(implClass.newInstance()));
            }
          }
          reader.close();
        } catch (Exception ex) {
          // let it go.
          StringWriter w = new StringWriter();
          ex.printStackTrace(new PrintWriter(w));
          pluginLoadFailure = w.toString();
          if (debug) {
            System.out.println(pluginLoadFailure);
          }
          if (reader != null) {
            try {
              reader.close();
            } catch (IOException ex2) {
              // ignore
            }
          }
        }
      }

      return a.toArray((T[]) Array.newInstance(clazz, a.size()));
    } catch (Throwable e) {
      // ignore any error
      StringWriter w = new StringWriter();
      e.printStackTrace(new PrintWriter(w));
      pluginLoadFailure = w.toString();
      if (debug) {
        System.out.println(pluginLoadFailure);
      }
      return (T[]) Array.newInstance(clazz, 0);
    }
  }