001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.discovery.tools;
018:
019: import java.util.Enumeration;
020:
021: import org.apache.commons.discovery.ResourceClass;
022: import org.apache.commons.discovery.ResourceClassIterator;
023: import org.apache.commons.discovery.ResourceNameIterator;
024: import org.apache.commons.discovery.resource.ClassLoaders;
025: import org.apache.commons.discovery.resource.classes.DiscoverClasses;
026: import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
027:
028: /**
029: * [this was ServiceDiscovery12... the 1.1 versus 1.2 issue
030: * has been abstracted to org.apache.commons.discover.jdk.JDKHooks]
031: *
032: * <p>Implement the JDK1.3 'Service Provider' specification.
033: * ( http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html )
034: * </p>
035: *
036: * This class supports any VM, including JDK1.1, via
037: * org.apache.commons.discover.jdk.JDKHooks.
038: *
039: * The caller will first configure the discoverer by adding ( in the desired
040: * order ) all the places to look for the META-INF/services. Currently
041: * we support loaders.
042: *
043: * The findResources() method will check every loader.
044: *
045: * @author Richard A. Sitze
046: * @author Craig R. McClanahan
047: * @author Costin Manolache
048: * @author James Strachan
049: */
050: public class Service {
051: /** Construct a new service discoverer
052: */
053: protected Service() {
054: }
055:
056: /**
057: * as described in
058: * sun/jdk1.3.1/docs/guide/jar/jar.html#Service Provider,
059: * Except this uses <code>Enumeration</code>
060: * instead of <code>Interator</code>.
061: *
062: * @return Enumeration of class instances (<code>Object</code>)
063: */
064: public static Enumeration providers(Class spiClass) {
065: return providers(new SPInterface(spiClass), null);
066: }
067:
068: /**
069: * This version lets you specify constructor arguments..
070: *
071: * @param spi SPI to look for and load.
072: * @param loaders loaders to use in search.
073: * If <code>null</code> then use ClassLoaders.getAppLoaders().
074: */
075: public static Enumeration providers(final SPInterface spi,
076: ClassLoaders loaders) {
077: if (loaders == null) {
078: loaders = ClassLoaders.getAppLoaders(spi.getSPClass(),
079: Service.class, true);
080: }
081:
082: ResourceNameIterator servicesIter = (new DiscoverServiceNames(
083: loaders)).findResourceNames(spi.getSPName());
084:
085: final ResourceClassIterator services = (new DiscoverClasses(
086: loaders)).findResourceClasses(servicesIter);
087:
088: return new Enumeration() {
089: private Object object = null;
090:
091: public boolean hasMoreElements() {
092: if (object == null) {
093: object = getNextClassInstance();
094: }
095: return object != null;
096: }
097:
098: public Object nextElement() {
099: Object obj = object;
100: object = null;
101: return obj;
102: }
103:
104: private Object getNextClassInstance() {
105: while (services.hasNext()) {
106: ResourceClass info = services.nextResourceClass();
107: try {
108: return spi.newInstance(info.loadClass());
109: } catch (Exception e) {
110: // ignore
111: }
112: }
113: return null;
114: }
115: };
116: }
117: }
|