servicefinder.java

来自「resetful样式的ws样例,一种面向资源的webservices服务」· Java 代码 · 共 689 行 · 第 1/3 页

JAVA
689
字号
     * @param service  The service class for which providers are being sought;     *                 used to construct error detail strings     * @param u        The URL naming the configuration file to be parsed     * @param returned A Set containing the names of provider classes that have already     *                 been returned.  This set will be updated to contain the names     *                 that will be yielded from the returned <tt>Iterator</tt>.     * @return A (possibly empty) <tt>Iterator</tt> that will yield the     *         provider-class names in the given configuration file that are     *         not yet members of the returned set     * @throws ServiceConfigurationError If an I/O error occurs while reading from the given URL, or     *                                   if a configuration-file format error is detected     */    @SuppressWarnings({"StatementWithEmptyBody"})    private static Iterator<String> parse(Class service, URL u, Set<String> returned)    throws ServiceConfigurationError {        InputStream in = null;        BufferedReader r = null;        ArrayList<String> names = new ArrayList<String>();        try {            URLConnection uConn = u.openConnection();            uConn.setUseCaches(false);            in = uConn.getInputStream();            r = new BufferedReader(new InputStreamReader(in, "utf-8"));            int lc = 1;            while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0) ;        } catch (IOException x) {            fail(service, ": " + x);        } finally {            try {                if (r != null) r.close();                if (in != null) in.close();            } catch (IOException y) {                fail(service, ": " + y);            }        }        return names.iterator();    }        private static class AbstractLazyIterator<T> {        final Class<T> service;        final ClassLoader loader;        final boolean ignoreOnClassNotFound;        final ComponentProvider componentProvider;                Enumeration<URL> configs = null;        Iterator<String> pending = null;        Set<String> returned = new TreeSet<String>();        String nextName = null;                private AbstractLazyIterator(Class<T> service, ClassLoader loader,                 boolean ignoreOnClassNotFound, ComponentProvider componentProvider) {            this.service = service;            this.loader = loader;            this.ignoreOnClassNotFound = ignoreOnClassNotFound;            this.componentProvider = componentProvider;        }                public boolean hasNext() throws ServiceConfigurationError {            if (nextName != null) {                return true;            }            if (configs == null) {                try {                    String fullName = PREFIX + service.getName();                    if (loader == null)                        configs = ClassLoader.getSystemResources(fullName);                    else                        configs = loader.getResources(fullName);                } catch (IOException x) {                    fail(service, ": " + x);                }            }                        while (nextName == null) {                while ((pending == null) || !pending.hasNext()) {                    if (!configs.hasMoreElements()) {                        return false;                    }                    pending = parse(service, configs.nextElement(), returned);                }                nextName = pending.next();                if (ignoreOnClassNotFound) {                    try {                        Class.forName(nextName, true, loader);                    } catch (ClassNotFoundException ex) {                        // Provider implementation not found                        if(LOGGER.isLoggable(Level.CONFIG)) {                            LOGGER.log(Level.CONFIG,                                     SpiMessages.PROVIDER_NOT_FOUND(nextName, service));                        }                        nextName = null;                    } catch (NoClassDefFoundError ex) {                        // Dependent class of provider not found                        if(LOGGER.isLoggable(Level.CONFIG)) {                            // This assumes that ex.getLocalizedMessage() returns                            // the name of a dependent class that is not found                            LOGGER.log(Level.CONFIG ,                                     SpiMessages.DEPENDENT_CLASS_OF_PROVIDER_NOT_FOUND(                                    ex.getLocalizedMessage(), nextName, service));                        }                        nextName = null;                     }                }            }            return true;        }                public void remove() {            throw new UnsupportedOperationException();        }            }        private static final class LazyClassIterator<T> extends AbstractLazyIterator<T>             implements Iterator<Class<T>> {        private LazyClassIterator(Class<T> service, ClassLoader loader,                 boolean ignoreOnClassNotFound, ComponentProvider componentProvider) {            super(service, loader, ignoreOnClassNotFound, componentProvider);        }                @SuppressWarnings("unchecked")        public Class<T> next() {            if (!hasNext()) {                throw new NoSuchElementException();            }            String cn = nextName;            nextName = null;            try {                return (Class<T>)Class.forName(cn, true, loader);            } catch (ClassNotFoundException ex) {                fail(service,                         SpiMessages.PROVIDER_NOT_FOUND(cn, service));            } catch (NoClassDefFoundError ex) {                fail(service,                        SpiMessages.DEPENDENT_CLASS_OF_PROVIDER_NOT_FOUND(                        ex.getLocalizedMessage(), cn, service));            } catch (Exception x) {                fail(service,                        SpiMessages.PROVIDER_CLASS_COULD_NOT_BE_LOADED(cn, service, x.getLocalizedMessage()),                        x);            }                        return null;    /* This cannot happen */        }    }        private static final class LazyObjectIterator<T> extends AbstractLazyIterator<T>             implements Iterator<T> {        private T t;                private LazyObjectIterator(Class<T> service, ClassLoader loader,                 boolean ignoreOnClassNotFound, ComponentProvider componentProvider) {            super(service, loader, ignoreOnClassNotFound, componentProvider);        }                public boolean hasNext() throws ServiceConfigurationError {            if (nextName != null) {                return true;            }            if (configs == null) {                try {                    String fullName = PREFIX + service.getName();                    if (loader == null)                        configs = ClassLoader.getSystemResources(fullName);                    else                        configs = loader.getResources(fullName);                } catch (IOException x) {                    fail(service, ": " + x);                }            }                        while (nextName == null) {                while ((pending == null) || !pending.hasNext()) {                    if (!configs.hasMoreElements()) {                        return false;                    }                    pending = parse(service, configs.nextElement(), returned);                }                nextName = pending.next();                try {                    t = service.cast(componentProvider.getInstance(null,                             Class.forName(nextName, true, loader)));                } catch (ClassNotFoundException ex) {                    if (ignoreOnClassNotFound) {                        // Provider implementation not found                        if(LOGGER.isLoggable(Level.WARNING)) {                            LOGGER.log(Level.WARNING,                                     SpiMessages.PROVIDER_NOT_FOUND(nextName, service));                        }                                            nextName = null;                    } else                        fail(service,                                 SpiMessages.PROVIDER_NOT_FOUND(nextName, service));                } catch (NoClassDefFoundError ex) {                    // Dependent class of provider not found                    if (ignoreOnClassNotFound) {                        if(LOGGER.isLoggable(Level.CONFIG)) {                            // This assumes that ex.getLocalizedMessage() returns                            // the name of a dependent class that is not found                            LOGGER.log(Level.CONFIG ,                                     SpiMessages.DEPENDENT_CLASS_OF_PROVIDER_NOT_FOUND(                                    ex.getLocalizedMessage(), nextName, service));                        }                        nextName = null;                     } else                        fail(service,                                SpiMessages.PROVIDER_COULD_NOT_BE_CREATED(nextName, service, ex.getLocalizedMessage()),                                ex);                } catch(Exception ex) {                    fail(service,                            SpiMessages.PROVIDER_COULD_NOT_BE_CREATED(nextName, service, ex.getLocalizedMessage()),                            ex);                                    }            }            return true;        }                public T next() {            if (!hasNext()) {                throw new NoSuchElementException();            }            String cn = nextName;            nextName = null;            return t;        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?