< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/ws/util/ServiceFinder.java

Print this page




  59  * in the service itself.  Service providers may be installed in an
  60  * implementation of the Java platform in the form of extensions, that is, jar
  61  * files placed into any of the usual extension directories.  Providers may
  62  * also be made available by adding them to the applet or application class
  63  * path or by some other platform-specific means.
  64  * <p/>
  65  * <p> In this lookup mechanism a service is represented by an interface or an
  66  * abstract class.  (A concrete class may be used, but this is not
  67  * recommended.)  A provider of a given service contains one or more concrete
  68  * classes that extend this <i>service class</i> with data and code specific to
  69  * the provider.  This <i>provider class</i> will typically not be the entire
  70  * provider itself but rather a proxy that contains enough information to
  71  * decide whether the provider is able to satisfy a particular request together
  72  * with code that can create the actual provider on demand.  The details of
  73  * provider classes tend to be highly service-specific; no single class or
  74  * interface could possibly unify them, so no such class has been defined.  The
  75  * only requirement enforced here is that provider classes must have a
  76  * zero-argument constructor so that they may be instantiated during lookup.
  77  * <p/>
  78  * <p> A service provider identifies itself by placing a provider-configuration
  79  * file in the resource directory <tt>META-INF/services</tt>.  The file's name
  80  * should consist of the fully-qualified name of the abstract service class.
  81  * The file should contain a list of fully-qualified concrete provider-class
  82  * names, one per line.  Space and tab characters surrounding each name, as
  83  * well as blank lines, are ignored.  The comment character is <tt>'#'</tt>
  84  * (<tt>0x23</tt>); on each line all characters following the first comment
  85  * character are ignored.  The file must be encoded in UTF-8.
  86  * <p/>
  87  * <p> If a particular concrete provider class is named in more than one
  88  * configuration file, or is named in the same configuration file more than
  89  * once, then the duplicates will be ignored.  The configuration file naming a
  90  * particular provider need not be in the same jar file or other distribution
  91  * unit as the provider itself.  The provider must be accessible from the same
  92  * class loader that was initially queried to locate the configuration file;
  93  * note that this is not necessarily the class loader that found the file.
  94  * <p/>
  95  * <p> <b>Example:</b> Suppose we have a service class named
  96  * <tt>java.io.spi.CharCodec</tt>.  It has two abstract methods:
  97  * <p/>
  98  * <pre>
  99  *   public abstract CharEncoder getEncoder(String encodingName);
 100  *   public abstract CharDecoder getDecoder(String encodingName);
 101  * </pre>
 102  * <p/>
 103  * Each method returns an appropriate object or <tt>null</tt> if it cannot
 104  * translate the given encoding.  Typical <tt>CharCodec</tt> providers will
 105  * support more than one encoding.
 106  * <p/>
 107  * <p> If <tt>sun.io.StandardCodec</tt> is a provider of the <tt>CharCodec</tt>
 108  * service then its jar file would contain the file
 109  * <tt>META-INF/services/java.io.spi.CharCodec</tt>.  This file would contain
 110  * the single line:
 111  * <p/>
 112  * <pre>
 113  *   sun.io.StandardCodec    # Standard codecs for the platform
 114  * </pre>
 115  * <p/>
 116  * To locate an codec for a given encoding name, the internal I/O code would
 117  * do something like this:
 118  * <p/>
 119  * <pre>
 120  *   CharEncoder getEncoder(String encodingName) {
 121  *       for( CharCodec cc : ServiceFinder.find(CharCodec.class) ) {
 122  *           CharEncoder ce = cc.getEncoder(encodingName);
 123  *           if (ce != null)
 124  *               return ce;
 125  *       }
 126  *       return null;
 127  *   }
 128  * </pre>
 129  * <p/>


 152         public ServiceName(String className, URL config) {
 153             this.className = className;
 154             this.config = config;
 155         }
 156     }
 157 
 158     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader, Component component) {
 159         return new ServiceFinder<T>(service, loader, component);
 160     }
 161 
 162     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, Component component) {
 163         return find(service,Thread.currentThread().getContextClassLoader(),component);
 164     }
 165 
 166     /**
 167      * Locates and incrementally instantiates the available providers of a
 168      * given service using the given class loader.
 169      * <p/>
 170      * <p> This method transforms the name of the given service class into a
 171      * provider-configuration filename as described above and then uses the
 172      * <tt>getResources</tt> method of the given class loader to find all
 173      * available files with that name.  These files are then read and parsed to
 174      * produce a list of provider-class names.  The iterator that is returned
 175      * uses the given class loader to lookup and then instantiate each element
 176      * of the list.
 177      * <p/>
 178      * <p> Because it is possible for extensions to be installed into a running
 179      * Java virtual machine, this method may return different results each time
 180      * it is invoked. <p>
 181      *
 182      * @param service The service's abstract service class
 183      * @param loader  The class loader to be used to load provider-configuration files
 184      *                and instantiate provider classes, or <tt>null</tt> if the system
 185      *                class loader (or, failing that the bootstrap class loader) is to
 186      *                be used
 187      * @throws ServiceConfigurationError If a provider-configuration file violates the specified format
 188      *                                   or names a provider class that cannot be found and instantiated
 189      * @see #find(Class)
 190      */
 191     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader) {
 192         return find(service, loader, ContainerResolver.getInstance().getContainer());
 193     }
 194 
 195     /**
 196      * Locates and incrementally instantiates the available providers of a
 197      * given service using the context class loader.  This convenience method
 198      * is equivalent to
 199      * <p/>
 200      * <pre>
 201      *   ClassLoader cl = Thread.currentThread().getContextClassLoader();
 202      *   return Service.providers(service, cl);
 203      * </pre>
 204      *


 210      */
 211     public static <T> ServiceFinder<T> find(Class<T> service) {
 212         return find(service,Thread.currentThread().getContextClassLoader());
 213     }
 214 
 215     private ServiceFinder(Class<T> service, ClassLoader loader, Component component) {
 216         this.serviceClass = service;
 217         this.classLoader = loader;
 218         this.component = getComponentEx(component);
 219     }
 220 
 221     private static ServiceName[] serviceClassNames(Class serviceClass, ClassLoader classLoader) {
 222         ArrayList<ServiceName> l = new ArrayList<ServiceName>();
 223         for (Iterator<ServiceName> it = new ServiceNameIterator(serviceClass,classLoader);it.hasNext();) l.add(it.next());
 224         return l.toArray(new ServiceName[l.size()]);
 225     }
 226 
 227     /**
 228      * Returns discovered objects incrementally.
 229      *
 230      * @return An <tt>Iterator</tt> that yields provider objects for the given
 231      *         service, in some arbitrary order.  The iterator will throw a
 232      *         <tt>ServiceConfigurationError</tt> if a provider-configuration
 233      *         file violates the specified format or if a provider class cannot
 234      *         be found and instantiated.
 235      */
 236     @SuppressWarnings("unchecked")
 237         public Iterator<T> iterator() {
 238         Iterator<T> it = new LazyIterator<T>(serviceClass,classLoader);
 239         return component != null ?
 240                         new CompositeIterator<T>(
 241                                         component.getIterableSPI(serviceClass).iterator(),it) :
 242                         it;
 243     }
 244 
 245     /**
 246      * Returns discovered objects all at once.
 247      *
 248      * @return
 249      *      can be empty but never null.
 250      *
 251      * @throws ServiceConfigurationError
 252      */


 302                 cp = ln.codePointAt(i);
 303                 if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
 304                     fail(service, u, lc, "Illegal provider-class name: " + ln);
 305             }
 306             if (!returned.contains(ln)) {
 307                 names.add(ln);
 308                 returned.add(ln);
 309             }
 310         }
 311         return lc + 1;
 312     }
 313 
 314     /**
 315      * Parse the content of the given URL as a provider-configuration file.
 316      *
 317      * @param service  The service class for which providers are being sought;
 318      *                 used to construct error detail strings
 319      * @param u        The URL naming the configuration file to be parsed
 320      * @param returned A Set containing the names of provider classes that have already
 321      *                 been returned.  This set will be updated to contain the names
 322      *                 that will be yielded from the returned <tt>Iterator</tt>.
 323      * @return A (possibly empty) <tt>Iterator</tt> that will yield the
 324      *         provider-class names in the given configuration file that are
 325      *         not yet members of the returned set
 326      * @throws ServiceConfigurationError If an I/O error occurs while reading from the given URL, or
 327      *                                   if a configuration-file format error is detected
 328      */
 329     @SuppressWarnings({"StatementWithEmptyBody"})
 330     private static Iterator<String> parse(Class service, URL u, Set<String> returned)
 331         throws ServiceConfigurationError {
 332         InputStream in = null;
 333         BufferedReader r = null;
 334         ArrayList<String> names = new ArrayList<String>();
 335         try {
 336             in = u.openStream();
 337             r = new BufferedReader(new InputStreamReader(in, "utf-8"));
 338             int lc = 1;
 339             while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0) ;
 340         } catch (IOException x) {
 341             fail(service, ": " + x);
 342         } finally {
 343             try {




  59  * in the service itself.  Service providers may be installed in an
  60  * implementation of the Java platform in the form of extensions, that is, jar
  61  * files placed into any of the usual extension directories.  Providers may
  62  * also be made available by adding them to the applet or application class
  63  * path or by some other platform-specific means.
  64  * <p/>
  65  * <p> In this lookup mechanism a service is represented by an interface or an
  66  * abstract class.  (A concrete class may be used, but this is not
  67  * recommended.)  A provider of a given service contains one or more concrete
  68  * classes that extend this <i>service class</i> with data and code specific to
  69  * the provider.  This <i>provider class</i> will typically not be the entire
  70  * provider itself but rather a proxy that contains enough information to
  71  * decide whether the provider is able to satisfy a particular request together
  72  * with code that can create the actual provider on demand.  The details of
  73  * provider classes tend to be highly service-specific; no single class or
  74  * interface could possibly unify them, so no such class has been defined.  The
  75  * only requirement enforced here is that provider classes must have a
  76  * zero-argument constructor so that they may be instantiated during lookup.
  77  * <p/>
  78  * <p> A service provider identifies itself by placing a provider-configuration
  79  * file in the resource directory {@code META-INF/services}.  The file's name
  80  * should consist of the fully-qualified name of the abstract service class.
  81  * The file should contain a list of fully-qualified concrete provider-class
  82  * names, one per line.  Space and tab characters surrounding each name, as
  83  * well as blank lines, are ignored.  The comment character is {@code '#'}
  84  * ({@code 0x23}); on each line all characters following the first comment
  85  * character are ignored.  The file must be encoded in UTF-8.
  86  * <p/>
  87  * <p> If a particular concrete provider class is named in more than one
  88  * configuration file, or is named in the same configuration file more than
  89  * once, then the duplicates will be ignored.  The configuration file naming a
  90  * particular provider need not be in the same jar file or other distribution
  91  * unit as the provider itself.  The provider must be accessible from the same
  92  * class loader that was initially queried to locate the configuration file;
  93  * note that this is not necessarily the class loader that found the file.
  94  * <p/>
  95  * <p> <b>Example:</b> Suppose we have a service class named
  96  * {@code java.io.spi.CharCodec}.  It has two abstract methods:
  97  * <p/>
  98  * <pre>
  99  *   public abstract CharEncoder getEncoder(String encodingName);
 100  *   public abstract CharDecoder getDecoder(String encodingName);
 101  * </pre>
 102  * <p/>
 103  * Each method returns an appropriate object or {@code null} if it cannot
 104  * translate the given encoding.  Typical {@code CharCodec} providers will
 105  * support more than one encoding.
 106  * <p/>
 107  * <p> If {@code sun.io.StandardCodec} is a provider of the {@code CharCodec}
 108  * service then its jar file would contain the file
 109  * {@code META-INF/services/java.io.spi.CharCodec}.  This file would contain
 110  * the single line:
 111  * <p/>
 112  * <pre>
 113  *   sun.io.StandardCodec    # Standard codecs for the platform
 114  * </pre>
 115  * <p/>
 116  * To locate an codec for a given encoding name, the internal I/O code would
 117  * do something like this:
 118  * <p/>
 119  * <pre>
 120  *   CharEncoder getEncoder(String encodingName) {
 121  *       for( CharCodec cc : ServiceFinder.find(CharCodec.class) ) {
 122  *           CharEncoder ce = cc.getEncoder(encodingName);
 123  *           if (ce != null)
 124  *               return ce;
 125  *       }
 126  *       return null;
 127  *   }
 128  * </pre>
 129  * <p/>


 152         public ServiceName(String className, URL config) {
 153             this.className = className;
 154             this.config = config;
 155         }
 156     }
 157 
 158     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader, Component component) {
 159         return new ServiceFinder<T>(service, loader, component);
 160     }
 161 
 162     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, Component component) {
 163         return find(service,Thread.currentThread().getContextClassLoader(),component);
 164     }
 165 
 166     /**
 167      * Locates and incrementally instantiates the available providers of a
 168      * given service using the given class loader.
 169      * <p/>
 170      * <p> This method transforms the name of the given service class into a
 171      * provider-configuration filename as described above and then uses the
 172      * {@code getResources} method of the given class loader to find all
 173      * available files with that name.  These files are then read and parsed to
 174      * produce a list of provider-class names.  The iterator that is returned
 175      * uses the given class loader to lookup and then instantiate each element
 176      * of the list.
 177      * <p/>
 178      * <p> Because it is possible for extensions to be installed into a running
 179      * Java virtual machine, this method may return different results each time
 180      * it is invoked. <p>
 181      *
 182      * @param service The service's abstract service class
 183      * @param loader  The class loader to be used to load provider-configuration files
 184      *                and instantiate provider classes, or {@code null} if the system
 185      *                class loader (or, failing that the bootstrap class loader) is to
 186      *                be used
 187      * @throws ServiceConfigurationError If a provider-configuration file violates the specified format
 188      *                                   or names a provider class that cannot be found and instantiated
 189      * @see #find(Class)
 190      */
 191     public static <T> ServiceFinder<T> find(@NotNull Class<T> service, @Nullable ClassLoader loader) {
 192         return find(service, loader, ContainerResolver.getInstance().getContainer());
 193     }
 194 
 195     /**
 196      * Locates and incrementally instantiates the available providers of a
 197      * given service using the context class loader.  This convenience method
 198      * is equivalent to
 199      * <p/>
 200      * <pre>
 201      *   ClassLoader cl = Thread.currentThread().getContextClassLoader();
 202      *   return Service.providers(service, cl);
 203      * </pre>
 204      *


 210      */
 211     public static <T> ServiceFinder<T> find(Class<T> service) {
 212         return find(service,Thread.currentThread().getContextClassLoader());
 213     }
 214 
 215     private ServiceFinder(Class<T> service, ClassLoader loader, Component component) {
 216         this.serviceClass = service;
 217         this.classLoader = loader;
 218         this.component = getComponentEx(component);
 219     }
 220 
 221     private static ServiceName[] serviceClassNames(Class serviceClass, ClassLoader classLoader) {
 222         ArrayList<ServiceName> l = new ArrayList<ServiceName>();
 223         for (Iterator<ServiceName> it = new ServiceNameIterator(serviceClass,classLoader);it.hasNext();) l.add(it.next());
 224         return l.toArray(new ServiceName[l.size()]);
 225     }
 226 
 227     /**
 228      * Returns discovered objects incrementally.
 229      *
 230      * @return An {@code Iterator} that yields provider objects for the given
 231      *         service, in some arbitrary order.  The iterator will throw a
 232      *         {@code ServiceConfigurationError} if a provider-configuration
 233      *         file violates the specified format or if a provider class cannot
 234      *         be found and instantiated.
 235      */
 236     @SuppressWarnings("unchecked")
 237         public Iterator<T> iterator() {
 238         Iterator<T> it = new LazyIterator<T>(serviceClass,classLoader);
 239         return component != null ?
 240                         new CompositeIterator<T>(
 241                                         component.getIterableSPI(serviceClass).iterator(),it) :
 242                         it;
 243     }
 244 
 245     /**
 246      * Returns discovered objects all at once.
 247      *
 248      * @return
 249      *      can be empty but never null.
 250      *
 251      * @throws ServiceConfigurationError
 252      */


 302                 cp = ln.codePointAt(i);
 303                 if (!Character.isJavaIdentifierPart(cp) && (cp != '.'))
 304                     fail(service, u, lc, "Illegal provider-class name: " + ln);
 305             }
 306             if (!returned.contains(ln)) {
 307                 names.add(ln);
 308                 returned.add(ln);
 309             }
 310         }
 311         return lc + 1;
 312     }
 313 
 314     /**
 315      * Parse the content of the given URL as a provider-configuration file.
 316      *
 317      * @param service  The service class for which providers are being sought;
 318      *                 used to construct error detail strings
 319      * @param u        The URL naming the configuration file to be parsed
 320      * @param returned A Set containing the names of provider classes that have already
 321      *                 been returned.  This set will be updated to contain the names
 322      *                 that will be yielded from the returned {@code Iterator}.
 323      * @return A (possibly empty) {@code Iterator} that will yield the
 324      *         provider-class names in the given configuration file that are
 325      *         not yet members of the returned set
 326      * @throws ServiceConfigurationError If an I/O error occurs while reading from the given URL, or
 327      *                                   if a configuration-file format error is detected
 328      */
 329     @SuppressWarnings({"StatementWithEmptyBody"})
 330     private static Iterator<String> parse(Class service, URL u, Set<String> returned)
 331         throws ServiceConfigurationError {
 332         InputStream in = null;
 333         BufferedReader r = null;
 334         ArrayList<String> names = new ArrayList<String>();
 335         try {
 336             in = u.openStream();
 337             r = new BufferedReader(new InputStreamReader(in, "utf-8"));
 338             int lc = 1;
 339             while ((lc = parseLine(service, u, r, lc, names, returned)) >= 0) ;
 340         } catch (IOException x) {
 341             fail(service, ": " + x);
 342         } finally {
 343             try {


< prev index next >