001    /*
002     * Copyright 2006-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.processing;
027    
028    import java.io.BufferedReader;
029    import java.io.FileNotFoundException;
030    import java.io.IOException;
031    import java.io.InputStream;
032    import java.io.InputStreamReader;
033    import java.net.MalformedURLException;
034    import java.net.URL;
035    
036    /**
037     * Utility class to determine if a service can be found on the
038     * path that might be used to create a class loader.
039     *
040     * <p><b>This is NOT part of any API supported by Sun Microsystems.
041     * If you write code that depends on this, you do so at your own risk.
042     * This code and its internal interfaces are subject to change or
043     * deletion without notice.</b>
044     *
045     */
046    // based on sun.misc.Service
047    class ServiceProxy {
048        static class ServiceConfigurationError extends Error {
049            static final long serialVersionUID = 7732091036771098303L;
050    
051            ServiceConfigurationError(String msg) {
052                super(msg);
053            }
054        }
055    
056        private static final String prefix = "META-INF/services/";
057    
058        private static void fail(Class<?> service, String msg)
059                throws ServiceConfigurationError {
060            throw new ServiceConfigurationError(service.getName() + ": " + msg);
061        }
062    
063        private static void fail(Class<?> service, URL u, int line, String msg)
064                throws ServiceConfigurationError {
065            fail(service, u + ":" + line + ": " + msg);
066        }
067    
068        /**
069         * Parse the content of the given URL as a provider-configuration file.
070         *
071         * @param  service
072         *         The service class for which providers are being sought;
073         *         used to construct error detail strings
074         *
075         * @param  url
076         *         The URL naming the configuration file to be parsed
077         *
078         * @return true if the name of a service is found
079         *
080         * @throws ServiceConfigurationError
081         *         If an I/O error occurs while reading from the given URL, or
082         *         if a configuration-file format error is detected
083         */
084        private static boolean parse(Class<?> service, URL u) throws ServiceConfigurationError {
085            InputStream in = null;
086            BufferedReader r = null;
087            try {
088                in = u.openStream();
089                r = new BufferedReader(new InputStreamReader(in, "utf-8"));
090                int lc = 1;
091                String ln;
092                while ((ln = r.readLine()) != null) {
093                    int ci = ln.indexOf('#');
094                    if (ci >= 0) ln = ln.substring(0, ci);
095                    ln = ln.trim();
096                    int n = ln.length();
097                    if (n != 0) {
098                        if ((ln.indexOf(' ') >= 0) || (ln.indexOf('\t') >= 0))
099                            fail(service, u, lc, "Illegal configuration-file syntax");
100                        int cp = ln.codePointAt(0);
101                        if (!Character.isJavaIdentifierStart(cp))
102                            fail(service, u, lc, "Illegal provider-class name: " + ln);
103                        for (int i = Character.charCount(cp); i < n; i += Character.charCount(cp)) {
104                            cp = ln.codePointAt(i);
105                            if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
106                                fail(service, u, lc, "Illegal provider-class name: " + ln);
107                        }
108                        return true;
109                    }
110                }
111            } catch (FileNotFoundException x) {
112                return false;
113            } catch (IOException x) {
114                fail(service, ": " + x);
115            } finally {
116                try {
117                    if (r != null) r.close();
118                } catch (IOException y) {
119                    fail(service, ": " + y);
120                }
121                try {
122                    if (in != null) in.close();
123                } catch (IOException y) {
124                    fail(service, ": " + y);
125                }
126            }
127            return false;
128        }
129    
130        /**
131         * Return true if a description for at least one service is found in the
132         * service configuration files in the given URLs.
133         */
134        public static boolean hasService(Class<?> service, URL[] urls)
135                throws ServiceConfigurationError {
136            for (URL url: urls) {
137                try {
138                    String fullName = prefix + service.getName();
139                    URL u = new URL(url, fullName);
140                    boolean found = parse(service, u);
141                    if (found)
142                        return true;
143                } catch (MalformedURLException e) {
144                    // should not happen; ignore it if it does
145                }
146            }
147            return false;
148        }
149    }