servicefinder.java

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

JAVA
689
字号
     * @param loader The class loader to be used to load provider-configuration files     *                and instantiate provider classes, or <tt>null</tt> if the system     *                class loader (or, failing that the bootstrap class loader) is to     *                be used     * @param ignoreOnClassNotFound If a provider cannot be loaded by the class loader     *                              then move on to the next available provider.     * @param componentProvider the component provider responsible for instantiating     *                          the provider implementation     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found and instantiated     * @see #find(Class)     * @return the service finder     */    public static <T> ServiceFinder<T> find(Class<T> service,             ClassLoader loader,             boolean ignoreOnClassNotFound,            ComponentProvider componentProvider) throws ServiceConfigurationError {        return new ServiceFinder<T>(service,                loader,                ignoreOnClassNotFound,                componentProvider);    }        /**     * Locates and incrementally instantiates the available providers of a     * given service using the context class loader.  This convenience method     * is equivalent to     * <p/>     * <pre>     *   ClassLoader cl = Thread.currentThread().getContextClassLoader();     *   return Service.providers(service, cl, false);     * </pre>     * @param service The service's abstract service class     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found and instantiated     * @see #find(Class, ClassLoader)     * @return the service finder     */    public static <T> ServiceFinder<T> find(Class<T> service)             throws ServiceConfigurationError {        return find(service,                Thread.currentThread().getContextClassLoader(),                 false,                DEFAULT_COMPONENT_PROVIDER);    }        /**     * Locates and incrementally instantiates the available providers of a     * given service using the context class loader.  This convenience method     * is equivalent to     * <p/>     * <pre>     *   ClassLoader cl = Thread.currentThread().getContextClassLoader();     *   boolean ingore = ...     *   return Service.providers(service, cl, ignore);     * </pre>     * @param service The service's abstract service class     * @param ignoreOnClassNotFound If a provider cannot be loaded by the class loader     *                              then move on to the next available provider.     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found and instantiated     * @see #find(Class, ClassLoader)     * @return the service finder     */    public static <T> ServiceFinder<T> find(Class<T> service,             boolean ignoreOnClassNotFound) throws ServiceConfigurationError {        return find(service,                Thread.currentThread().getContextClassLoader(),                 ignoreOnClassNotFound,                DEFAULT_COMPONENT_PROVIDER);    }        /**     * Locates and incrementally instantiates the available providers of a     * given service using the context class loader.  This convenience method     * is equivalent to     * <p/>     * <pre>     *   ClassLoader cl = Thread.currentThread().getContextClassLoader();     *   boolean ingore = ...     *   return Service.providers(service, cl, ignore);     * </pre>     * @param service The service's abstract service class     * @param ignoreOnClassNotFound If a provider cannot be loaded by the class loader     *                              then move on to the next available provider.     * @param componentProvider the component provider responsible for instantiating     *                          the provider implementation     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found and instantiated     * @see #find(Class, ClassLoader)     * @return the service finder     */    public static <T> ServiceFinder<T> find(Class<T> service,             boolean ignoreOnClassNotFound, ComponentProvider componentProvider)             throws ServiceConfigurationError {        return find(service,                Thread.currentThread().getContextClassLoader(),                 ignoreOnClassNotFound,                componentProvider);    }        private ServiceFinder(Class<T> service, ClassLoader loader,             boolean ignoreOnClassNotFound, ComponentProvider componentProvider) {        this.serviceClass = service;        this.classLoader = loader;        this.ignoreOnClassNotFound = ignoreOnClassNotFound;        this.componentProvider = componentProvider;    }        /**     * Returns discovered objects incrementally.     *     * @return An <tt>Iterator</tt> that yields provider objects for the given     *         service, in some arbitrary order.  The iterator will throw a     *         <tt>ServiceConfigurationError</tt> if a provider-configuration     *         file violates the specified format or if a provider class cannot     *         be found and instantiated.     */    public Iterator<T> iterator() {        return new LazyObjectIterator<T>(serviceClass,classLoader,                ignoreOnClassNotFound, componentProvider);    }        /**     * Returns discovered classes incrementally.     *     * @return An <tt>Iterator</tt> that yields provider classes for the given     *         service, in some arbitrary order.  The iterator will throw a     *         <tt>ServiceConfigurationError</tt> if a provider-configuration     *         file violates the specified format or if a provider class cannot     *         be found.     */    public Iterator<Class<T>> classIterator() {        return new LazyClassIterator<T>(serviceClass,classLoader,                ignoreOnClassNotFound, componentProvider);    }        /**     * Returns discovered objects all at once.     *     * @return     *      can be empty but never null.     *     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found and instantiated     */    @SuppressWarnings("unchecked")    public T[] toArray() throws ServiceConfigurationError {        List<T> result = new ArrayList<T>();        for (T t : this) {            result.add(t);        }        return result.toArray((T[])Array.newInstance(serviceClass,result.size()));    }        /**     * Returns discovered classes all at once.     *     * @return     *      can be empty but never null.     *     * @throws ServiceConfigurationError If a provider-configuration file violates the specified format     *                                   or names a provider class that cannot be found     */    @SuppressWarnings("unchecked")    public Class<T>[] toClassArray() throws ServiceConfigurationError {        List<Class<T>> result = new ArrayList<Class<T>>();                Iterator<Class<T>> i = classIterator();        while (i.hasNext())            result.add(i.next());        return result.toArray((Class<T>[])Array.newInstance(Class.class,result.size()));    }        private static void fail(Class service, String msg, Throwable cause)    throws ServiceConfigurationError {        ServiceConfigurationError sce                = new ServiceConfigurationError(service.getName() + ": " + msg);        sce.initCause(cause);        throw sce;    }        private static void fail(Class service, String msg)    throws ServiceConfigurationError {        throw new ServiceConfigurationError(service.getName() + ": " + msg);    }        private static void fail(Class service, URL u, int line, String msg)    throws ServiceConfigurationError {        fail(service, u + ":" + line + ": " + msg);    }        /**     * Parse a single line from the given configuration file, adding the name     * on the line to both the names list and the returned set iff the name is     * not already a member of the returned set.     */    private static int parseLine(Class service, URL u, BufferedReader r, int lc,            List<String> names, Set<String> returned)            throws IOException, ServiceConfigurationError {        String ln = r.readLine();        if (ln == null) {            return -1;        }        int ci = ln.indexOf('#');        if (ci >= 0) ln = ln.substring(0, ci);        ln = ln.trim();        int n = ln.length();        if (n != 0) {            if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))                fail(service, u, lc, SpiMessages.ILLEGAL_CONFIG_SYNTAX());            int cp = ln.codePointAt(0);            if (!Character.isJavaIdentifierStart(cp))                fail(service, u, lc, SpiMessages.ILLEGAL_PROVIDER_CLASS_NAME(ln));            for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {                cp = ln.codePointAt(i);                if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))                    fail(service, u, lc, SpiMessages.ILLEGAL_PROVIDER_CLASS_NAME(ln));            }            if (!returned.contains(ln)) {                names.add(ln);                returned.add(ln);            }        }        return lc + 1;    }        /**     * Parse the content of the given URL as a provider-configuration file.     *

⌨️ 快捷键说明

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