Java 类sun.misc.Service 实例源码

项目:VarJ    文件:InetAddress.java   
public Object run() {
    Iterator itr
            = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
            NameServiceDescriptor nsd 
        = (NameServiceDescriptor)itr.next();
            if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
        try {
                    nameService = nsd.createNameService();
                    break;
        } catch (Exception e) {
            e.printStackTrace();
                    System.err.println(
            "Cannot create name service:"
                 +providerName+": " + e);
        }
            }
    } /* while */
        return null;
}
项目:VarJ    文件:SelectorProvider.java   
private static boolean loadProviderAsService() {
Iterator i = Service.providers(SelectorProvider.class,
                   ClassLoader.getSystemClassLoader());
for (;;) {
    try {
    if (!i.hasNext())
        return false;
    provider = (SelectorProvider)i.next();
    return true;
    } catch (ServiceConfigurationError sce) {
    if (sce.getCause() instanceof SecurityException) {
        // Ignore the security exception, try the next provider
        continue;
    }
    throw sce;
    }
}
   }
项目:openwonderland    文件:Webstart.java   
public static void webstartSetup() {
    logger.warning("Running from Java Web Start. Performing setup.");

    // setup a URL stream handler for the Wonderland protocols.  This
    // works around the fact that URL will only find handlers
    // loaded in the system classloader, and in webstart the
    // handlers we need (wonderland.protocol.*) are in the jnlp classloader.
    //
    //
    // Note if we return null here, it will go on to try the normal
    // mechanisms defined in URL
    URL.setURLStreamHandlerFactory(new WonderlandURLStreamHandlerFactory());

    // set our own security manager
    logger.info("Setting security manager");
    System.setSecurityManager(new JnlpSecurityManager());

    // discover listeners using service loader mechanism
    Iterator<WebstartStartupListener> it = 
            Service.providers(WebstartStartupListener.class);
    while (it.hasNext()) {
        WebstartStartupListener wsl = it.next();
        wsl.onStartup();
    }
}
项目:jdk-1.7-annotated    文件:InetAddress.java   
public NameService run() {
    Iterator itr = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
        NameServiceDescriptor nsd
            = (NameServiceDescriptor)itr.next();
        if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
            try {
                return nsd.createNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(
                    "Cannot create name service:"
                     +providerName+": " + e);
            }
        }
    }

    return null;
}
项目:jdk7-jdk    文件:InetAddress.java   
public NameService run() {
    Iterator itr = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
        NameServiceDescriptor nsd
            = (NameServiceDescriptor)itr.next();
        if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
            try {
                return nsd.createNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(
                    "Cannot create name service:"
                     +providerName+": " + e);
            }
        }
    }

    return null;
}
项目:jdk7-jdk    文件:HttpServerProvider.java   
private static boolean loadProviderAsService() {
    Iterator i = Service.providers(HttpServerProvider.class,
                                   ClassLoader.getSystemClassLoader());
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = (HttpServerProvider)i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
项目:openjdk-source-code-learn    文件:InetAddress.java   
public NameService run() {
    Iterator itr = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
        NameServiceDescriptor nsd
            = (NameServiceDescriptor)itr.next();
        if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
            try {
                return nsd.createNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(
                    "Cannot create name service:"
                     +providerName+": " + e);
            }
        }
    }

    return null;
}
项目:openjdk-source-code-learn    文件:HttpServerProvider.java   
private static boolean loadProviderAsService() {
    Iterator i = Service.providers(HttpServerProvider.class,
                                   ClassLoader.getSystemClassLoader());
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = (HttpServerProvider)i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
项目:openjdk-jdk7u-jdk    文件:InetAddress.java   
public NameService run() {
    Iterator itr = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
        NameServiceDescriptor nsd
            = (NameServiceDescriptor)itr.next();
        if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
            try {
                return nsd.createNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(
                    "Cannot create name service:"
                     +providerName+": " + e);
            }
        }
    }

    return null;
}
项目:openjdk-jdk7u-jdk    文件:HttpServerProvider.java   
private static boolean loadProviderAsService() {
    Iterator i = Service.providers(HttpServerProvider.class,
                                   ClassLoader.getSystemClassLoader());
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = (HttpServerProvider)i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
项目:openjdk-icedtea7    文件:InetAddress.java   
public NameService run() {
    Iterator itr = Service.providers(NameServiceDescriptor.class);
    while (itr.hasNext()) {
        NameServiceDescriptor nsd
            = (NameServiceDescriptor)itr.next();
        if (providerName.
            equalsIgnoreCase(nsd.getType()+","
                +nsd.getProviderName())) {
            try {
                return nsd.createNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(
                    "Cannot create name service:"
                     +providerName+": " + e);
            }
        }
    }

    return null;
}
项目:openjdk-icedtea7    文件:HttpServerProvider.java   
private static boolean loadProviderAsService() {
    Iterator i = Service.providers(HttpServerProvider.class,
                                   ClassLoader.getSystemClassLoader());
    for (;;) {
        try {
            if (!i.hasNext())
                return false;
            provider = (HttpServerProvider)i.next();
            return true;
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
                // Ignore the security exception, try the next provider
                continue;
            }
            throw sce;
        }
    }
}
项目:openjdk-jdk10    文件:Main.java   
public static void main() {
    // in jdk.unsupported
    Class<?> caller = Reflection.getCallerClass(2);

    // removed
    JPEGCodec r = new JPEGCodec();

    // removed
    SoftCache s = new SoftCache();

    // removed
    Service.providers(S.class);
}
项目:openjdk9    文件:Main.java   
public static void main() {
    // in jdk.unsupported
    Class<?> caller = Reflection.getCallerClass(2);

    // removed
    JPEGCodec r = new JPEGCodec();

    // removed
    SoftCache s = new SoftCache();

    // removed
    Service.providers(S.class);
}
项目:VarJ    文件:DriverManager.java   
public Object run() {

    // uncomment the followin line before mustang integration   
        // Service s = Service.lookup(java.sql.Driver.class);
    // ps = s.iterator();

    ps = Service.providers(java.sql.Driver.class);

    /* Load these drivers, so that they can be instantiated. 
     * It may be the case that the driver class may not be there
         * i.e. there may be a packaged driver with the service class
         * as implementation of java.sql.Driver but the actual class
         * may be missing. In that case a sun.misc.ServiceConfigurationError
         * will be thrown at runtime by the VM trying to locate 
     * and load the service.
         * 
     * Adding a try catch block to catch those runtime errors
         * if driver not available in classpath but it's 
     * packaged as service and that service is there in classpath.
     */

    try {
           while (ps.hasNext()) {
               ps.next();
           } // end while
    } catch(Throwable t) {
        // Do nothing
    }
        return null;
    }
项目:VarJ    文件:Preferences.java   
private static PreferencesFactory factory1() {
// 2. Try service provider interface
Iterator i = Service.providers(PreferencesFactory.class,
                   ClassLoader.getSystemClassLoader());
// choose first provider instance
while (i.hasNext()) {
    try {
    return (PreferencesFactory) i.next();
    } catch (ServiceConfigurationError sce) {
    if (sce.getCause() instanceof SecurityException) {
        // Ignore the security exception, try the next provider
        continue;
    }
    throw sce;
    }
}

// 3. Use platform-specific system-wide default
String platformFactory =
    System.getProperty("os.name").startsWith("Windows")
    ? "java.util.prefs.WindowsPreferencesFactory"
    : "java.util.prefs.FileSystemPreferencesFactory";
try {
    return (PreferencesFactory)
    Class.forName(platformFactory, false, null).newInstance();
} catch (Exception e) {
    InternalError error = new InternalError(
    "Can't instantiate platform default Preferences factory "
    + platformFactory);
    error.initCause(e);
    throw error;
}
   }
项目:openwonderland    文件:DeployManager.java   
/**
 * Find and return all the classes that implement the ModuleDeployerSPI
 * inteface
 * 
 * @return
 */
private Class[] getClasses() {
    Iterator<ModuleDeployerSPI> it = Service.providers(ModuleDeployerSPI.class);

    // use a linked hash set to preserve a static ordering
    Collection<Class> names = new LinkedHashSet<Class>();
    while (it.hasNext() == true) {
        names.add(it.next().getClass());
    }

    return names.toArray(new Class[]{} );
}
项目:openwonderland    文件:ScannedClassLoader.java   
/**
 * Get instances of all classes that implement the given interface,
 * defined either by service provider or by annoation.
 * @param annot the annotation to search for
 * @param clazz the class of the return type
 * @return an iterator of instantiated instances of the given type
 */
public <T> Iterator<T> getAll(Class<? extends Annotation> annot,
                              Class<T> clazz)
{
    // get the iterator for service providers
    final Iterator<T> providers = Service.providers(clazz, this);

    // get the iterator for annotations
    final Iterator<T> annots = getInstances(annot, clazz);

    // return a combined iterator
    return new Iterator<T>() {
        private boolean p = true;

        public boolean hasNext() {
            if (p && providers.hasNext()) {
                return true;
            } else {
                p = false;
                return annots.hasNext();
            }
        }

        public T next() {
            if (p) {
                return providers.next();
            } else {
                return annots.next();
            }
        }

        public void remove() {
            throw new UnsupportedOperationException("Not supported.");
        }
    };
}
项目:jdk7-jdk    文件:JSSecurityManager.java   
static List getProviders(final Class providerClass) {
    List p = new ArrayList();
    // Service.providers(Class) just creates "lazy" iterator instance,
    // so it doesn't require do be called from privileged section
    final Iterator ps = Service.providers(providerClass);

    // the iterator's hasNext() method looks through classpath for
    // the provider class names, so it requires read permissions
    PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
        public Boolean run() {
            return ps.hasNext();
        }
    };

    while (AccessController.doPrivileged(hasNextAction)) {
        try {
            // the iterator's next() method creates instances of the
            // providers and it should be called in the current security
            // context
            Object provider = ps.next();
            if (providerClass.isInstance(provider)) {
                // $$mp 2003-08-22
                // Always adding at the beginning reverses the
                // order of the providers. So we no longer have
                // to do this in AudioSystem and MidiSystem.
                p.add(0, provider);
            }
        } catch (Throwable t) {
            //$$fb 2002-11-07: do not fail on SPI not found
            if (Printer.err) t.printStackTrace();
        }
    }
    return p;
}
项目:openjdk-source-code-learn    文件:JSSecurityManager.java   
static List getProviders(final Class providerClass) {
    List p = new ArrayList();
    // Service.providers(Class) just creates "lazy" iterator instance,
    // so it doesn't require do be called from privileged section
    final Iterator ps = Service.providers(providerClass);

    // the iterator's hasNext() method looks through classpath for
    // the provider class names, so it requires read permissions
    PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
        public Boolean run() {
            return ps.hasNext();
        }
    };

    while (AccessController.doPrivileged(hasNextAction)) {
        try {
            // the iterator's next() method creates instances of the
            // providers and it should be called in the current security
            // context
            Object provider = ps.next();
            if (providerClass.isInstance(provider)) {
                // $$mp 2003-08-22
                // Always adding at the beginning reverses the
                // order of the providers. So we no longer have
                // to do this in AudioSystem and MidiSystem.
                p.add(0, provider);
            }
        } catch (Throwable t) {
            //$$fb 2002-11-07: do not fail on SPI not found
            if (Printer.err) t.printStackTrace();
        }
    }
    return p;
}
项目:openjdk-jdk7u-jdk    文件:JSSecurityManager.java   
static synchronized List getProviders(final Class providerClass) {
    List p = new ArrayList();
    // Service.providers(Class) just creates "lazy" iterator instance,
    // so it doesn't require do be called from privileged section
    final Iterator ps = Service.providers(providerClass);

    // the iterator's hasNext() method looks through classpath for
    // the provider class names, so it requires read permissions
    PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
        public Boolean run() {
            return ps.hasNext();
        }
    };

    while (AccessController.doPrivileged(hasNextAction)) {
        try {
            // the iterator's next() method creates instances of the
            // providers and it should be called in the current security
            // context
            Object provider = ps.next();
            if (providerClass.isInstance(provider)) {
                // $$mp 2003-08-22
                // Always adding at the beginning reverses the
                // order of the providers. So we no longer have
                // to do this in AudioSystem and MidiSystem.
                p.add(0, provider);
            }
        } catch (Throwable t) {
            //$$fb 2002-11-07: do not fail on SPI not found
            if (Printer.err) t.printStackTrace();
        }
    }
    return p;
}
项目:openjdk-icedtea7    文件:JSSecurityManager.java   
static List getProviders(final Class providerClass) {
    List p = new ArrayList();
    // Service.providers(Class) just creates "lazy" iterator instance,
    // so it doesn't require do be called from privileged section
    final Iterator ps = Service.providers(providerClass);

    // the iterator's hasNext() method looks through classpath for
    // the provider class names, so it requires read permissions
    PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
        public Boolean run() {
            return ps.hasNext();
        }
    };

    while (AccessController.doPrivileged(hasNextAction)) {
        try {
            // the iterator's next() method creates instances of the
            // providers and it should be called in the current security
            // context
            Object provider = ps.next();
            if (providerClass.isInstance(provider)) {
                // $$mp 2003-08-22
                // Always adding at the beginning reverses the
                // order of the providers. So we no longer have
                // to do this in AudioSystem and MidiSystem.
                p.add(0, provider);
            }
        } catch (Throwable t) {
            //$$fb 2002-11-07: do not fail on SPI not found
            if (Printer.err) t.printStackTrace();
        }
    }
    return p;
}
项目:dekaf    文件:JdbcJarProvidersTest.java   
@Test
public void check_federated_provider_service_is_published() {
  final Iterator providers = Service.providers(IntegralIntermediateFederatedProvider.class);

  boolean ok = providers.hasNext();
  assertThat(ok).isTrue();

  Object svc = providers.next();

  assertThat(svc).isInstanceOf(IntegralIntermediateFederatedProvider.class);
  assertThat(svc).isInstanceOf(JdbcIntermediateFederatedProvider.class);
}
项目:dekaf    文件:JdbcJarProvidersTest.java   
@Test
public void check_unknown_database_provider_service_is_published() {
  final Iterator providers = Service.providers(IntegralIntermediateRdbmsProvider.class);

  boolean ok = providers.hasNext();
  assertThat(ok).isTrue();

  Object svc = providers.next();

  assertThat(svc).isInstanceOf(IntegralIntermediateRdbmsProvider.class);
  assertThat(svc).isInstanceOf(JdbcIntermediateRdbmsProvider.class);
}
项目:VarJ    文件:Charset.java   
private static Iterator providers() {
return new Iterator() {

    Class c = java.nio.charset.spi.CharsetProvider.class;
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    Iterator i = Service.providers(c, cl);
    Object next = null;

    private boolean getNext() {
        while (next == null) {
        try {
            if (!i.hasNext())
            return false;
            next = i.next();
        } catch (ServiceConfigurationError sce) {
            if (sce.getCause() instanceof SecurityException) {
            // Ignore security exceptions
            continue;
            }
            throw sce;
        }
        }
        return true;
    }

    public boolean hasNext() {
        return getNext();
    }

    public Object next() {
        if (!getNext())
        throw new NoSuchElementException();
        Object n = next;
        next = null;
        return n;
    }

    public void remove() {
        throw new UnsupportedOperationException();
    }

    };
   }
项目:openwonderland    文件:CellBoundsViewer.java   
/** Creates new form CellBoundsViewer */
public CellBoundsViewer(String[] args) {
    // load properties from file
    if (args.length == 1) {
        props = loadProperties(args[0]);
    } else {
        props = loadProperties(null);
    }

    String serverName = props.getProperty(SERVER_NAME_PROP,
                                          SERVER_NAME_DEFAULT);
    String serverPort = props.getProperty(SERVER_PORT_PROP,
                                          SERVER_PORT_DEFAULT);
    String userName   = props.getProperty(USER_NAME_PROP,
                                          USER_NAME_DEFAULT);
    WonderlandConfigUtil.setUsername(userName);

    initComponents();

    final BoundsPanel boundsPanel = new BoundsPanel();
    centerPanel.add(boundsPanel, BorderLayout.CENTER);
    this.setSize(640,480);

    long userNum = System.currentTimeMillis();

    WonderlandServerInfo server = new WonderlandServerInfo(serverName,
                                              Integer.parseInt(serverPort));

    LoginParameters loginParams = new LoginParameters(userName, 
                                                      "test".toCharArray());

    // setup a classloader with the module jars
    loader = setupClassLoader();

    // create a session
    session = new CellClientSession(server, loader) {
        @Override
        protected CellCache createCellCache() {
            getCellCacheConnection().addListener(boundsPanel);
            return boundsPanel;
        }
    };

    // load any client plugins from that class loader
    Iterator<ClientPlugin> it = Service.providers(ClientPlugin.class,
                                                  loader);
    while (it.hasNext()) {
        ClientPlugin plugin = it.next();
        plugin.initialize(session);
    }

    boundsPanel.setSession(session);

    localAvatar = session.getLocalAvatar();

    try {
        session.login(loginParams);
    } catch (LoginFailureException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
}