KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > Service


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.fortress.util;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.net.URL;
25 import java.util.*;
26
27 /**
28  * This class handles looking up service providers on the class path.
29  * It implements the system described in:
30  *
31  * <a HREF="http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html#Service Provider">
32  * File Specification Under Service Provider</a>. Note that this interface is
33  * very similar to the one they describe whiehc seems to be missing in the JDK.
34  *
35  * This class adapted from <code>org.apache.batik.util.Service</code>
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version 1.0
39  */

40 public final class Service
41 {
42     private static final String SERVICES = "META-INF/services/";
43     private static final HashMap providers = new HashMap();
44
45     /**
46      * Private constructor to keep from instantiating this class
47      */

48     private Service()
49     {
50     }
51
52     /**
53      * Get all the providers for the specified services.
54      *
55      * @param klass the interface <code>Class</code>
56      * @param loader the <code>ClassLoader to be used.</code>
57      *
58      * @return an <code>Iterator</code> for the providers.
59      */

60     public static synchronized Iterator providers( final Class klass, ClassLoader loader )
61     {
62         final String serviceFile = SERVICES + klass.getName();
63         
64         if ( null == loader )
65         {
66             loader = klass.getClassLoader();
67         }
68
69         Set providerSet = (Set) providers.get( serviceFile );
70
71         if ( null == providerSet )
72         {
73             providerSet = new HashSet();
74             Enumeration enum = null;
75             boolean errorOccurred = false;
76
77             providers.put( serviceFile, providerSet );
78
79             try
80             {
81                 enum = loader.getResources( serviceFile );
82             }
83             catch ( IOException ioe )
84             {
85                 errorOccurred = true;
86             }
87
88             if ( !errorOccurred )
89             {
90                 while ( enum.hasMoreElements() )
91                 {
92                     try
93                     {
94                         final URL url = (URL) enum.nextElement();
95                         final InputStream is = url.openStream();
96                         final BufferedReader reader = new BufferedReader(
97                             new InputStreamReader( is,
98                                 "UTF-8" ) );
99
100                         String line = reader.readLine();
101                         while ( null != line )
102                         {
103                             try
104                             {
105                                 final int comment = line.indexOf( '#' );
106
107                                 if ( comment > -1 )
108                                 {
109                                     line = line.substring( 0, comment );
110                                 }
111
112                                 line.trim();
113
114                                 if ( line.length() > 0 )
115                                 {
116                                     // We just want the types, not the instances
117
providerSet.add( loader.loadClass( line ) );
118                                 }
119                             }
120                             catch ( Exception e )
121                             {
122                                 // try the next line
123
}
124
125                             line = reader.readLine();
126                         }
127                     }
128                     catch ( Exception e )
129                     {
130                         // try the next file
131
}
132                 }
133             }
134         }
135
136         return providerSet.iterator();
137     }
138
139     /**
140      * Get all the providers for the specified services.
141      *
142      * @param klass the interface <code>Class</code>
143      *
144      * @return an <code>Iterator</code> for the providers.
145      */

146     public static synchronized Iterator providers( final Class klass )
147     {
148         ClassLoader loader = Thread.currentThread().getContextClassLoader();
149
150         return providers( klass, loader );
151     }
152 }
153
Popular Tags