Source code: javax/ide/Service.java
1 package javax.ide;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import javax.ide.spi.LookupProvider;
6 import javax.ide.spi.ProviderNotFoundException;
7
8 /**
9 * Services provide access to distinct areas of functionality in the IDE for
10 * an extension developer.
11 */
12 public abstract class Service
13 {
14 private boolean _initialized = false;
15 private static final Map _loadedServices = new HashMap();
16
17 /**
18 * Initialize this manager. This is called the first time a manager is
19 * retrieved programmatically. The manager should process necessary
20 * information from the extension manifest to initialize itself.
21 * <p>
22 * This implementation does nothing.
23 */
24 protected void initialize()
25 {
26
27 }
28
29 private static Service findService( Class serviceClass )
30 throws ProviderNotFoundException
31 {
32 Service service = (Service) _loadedServices.get( serviceClass );
33 if ( service == null )
34 {
35 service = (Service) LookupProvider.lookup(
36 Thread.currentThread().getContextClassLoader(), serviceClass );
37 _loadedServices.put( serviceClass, service );
38 }
39 return service;
40 }
41
42 /**
43 * Get a service.
44 *
45 * This implementation looks for a META-INF/services/className resource on
46 * the classpath of the context classloader. If such a resource exists, it
47 * must contain the fully qualified class name of an implementation class
48 * of the specified class.
49 *
50 * @param serviceClass the class of the service.
51 * @return the service.
52 * @throws ProviderNotFoundException if an implementation of the service could not
53 * be located.
54 */
55 protected static Service getService( Class serviceClass )
56 throws ProviderNotFoundException
57 {
58 Service service = findService( serviceClass );
59
60 if ( !service._initialized )
61 {
62 service._initialized = true;
63 service.initialize();
64 }
65
66 return service;
67 }
68 }