Search in sources :

Example 6 with ServiceLoader

use of java.util.ServiceLoader in project jdk8u_jdk by JetBrains.

the class Charset method providers.

// Creates an iterator that walks over the available providers, ignoring
// those whose lookup or instantiation causes a security exception to be
// thrown.  Should be invoked with full privileges.
//
private static Iterator<CharsetProvider> providers() {
    return new Iterator<CharsetProvider>() {

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        ServiceLoader<CharsetProvider> sl = ServiceLoader.load(CharsetProvider.class, cl);

        Iterator<CharsetProvider> i = sl.iterator();

        CharsetProvider 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 CharsetProvider next() {
            if (!getNext())
                throw new NoSuchElementException();
            CharsetProvider n = next;
            next = null;
            return n;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ServiceLoader(java.util.ServiceLoader) CharsetProvider(java.nio.charset.spi.CharsetProvider) Iterator(java.util.Iterator) ServiceConfigurationError(java.util.ServiceConfigurationError) NoSuchElementException(java.util.NoSuchElementException)

Example 7 with ServiceLoader

use of java.util.ServiceLoader in project JMRI by JMRI.

the class StartupActionModelUtil method prepareActionsHashMap.

private void prepareActionsHashMap() {
    if (this.actions == null) {
        this.actions = new HashMap<>();
        this.overrides = new HashMap<>();
        ResourceBundle rb = ResourceBundle.getBundle("apps.ActionListBundle");
        rb.keySet().stream().filter((key) -> (!key.isEmpty())).forEach((key) -> {
            try {
                Class<?> clazz = Class.forName(key);
                ActionAttributes attrs = new ActionAttributes(rb.getString(key), clazz);
                this.actions.put(clazz, attrs);
            } catch (ClassNotFoundException ex) {
                log.error("Did not find class \"{}\"", key);
            }
        });
        ServiceLoader<StartupActionFactory> loader = ServiceLoader.load(StartupActionFactory.class);
        loader.forEach(factory -> {
            for (Class<?> clazz : factory.getActionClasses()) {
                ActionAttributes attrs = new ActionAttributes(factory.getTitle(clazz), clazz);
                this.actions.put(clazz, attrs);
                for (String overridden : factory.getOverriddenClasses(clazz)) {
                    this.overrides.put(overridden, clazz);
                }
            }
        });
        // allow factories to be garbage collected
        loader.reload();
    }
}
Also used : InstanceManager(jmri.InstanceManager) SystemConnectionAction(jmri.jmrix.swing.SystemConnectionAction) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ServiceLoader(java.util.ServiceLoader) ArrayList(java.util.ArrayList) ResourceBundle(java.util.ResourceBundle) Bean(jmri.beans.Bean) Entry(java.util.Map.Entry) CheckForNull(javax.annotation.CheckForNull) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ResourceBundle(java.util.ResourceBundle)

Aggregations

ServiceLoader (java.util.ServiceLoader)7 Test (org.junit.Test)2 CMMException (java.awt.color.CMMException)1 CharsetProvider (java.nio.charset.spi.CharsetProvider)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Entry (java.util.Map.Entry)1 NoSuchElementException (java.util.NoSuchElementException)1 ResourceBundle (java.util.ResourceBundle)1 ServiceConfigurationError (java.util.ServiceConfigurationError)1 CheckForNull (javax.annotation.CheckForNull)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 InstanceManager (jmri.InstanceManager)1 Bean (jmri.beans.Bean)1 SystemConnectionAction (jmri.jmrix.swing.SystemConnectionAction)1 AbstractDeploymentChainStep (org.jboss.as.server.AbstractDeploymentChainStep)1 DeploymentProcessorTarget (org.jboss.as.server.DeploymentProcessorTarget)1