View Javadoc

1   /*
2    * Created on Jun 7, 2005
3    *
4    * A generic ServiceLocator interface.
5    */
6   package net.sf.tlc.core;
7   
8   import java.util.Set;
9   
10  /***
11   * A generic ServiceLocator interface. Services can be registered and looked up
12   * by name or class (interface).
13   * 
14   * @author aisrael
15   */
16  public interface ServiceLocator {
17  
18      /***
19       * Register a service (object) by unique name. Any object previously bound
20       * to the given name will be overwritten.
21       * 
22       * @param name
23       *            a name to uniquely identify this 'service'
24       * @param o
25       *            Object
26       */
27      void register(final String name, final Object o);
28  
29      /***
30       * Register a service as a provider of the given class / interface.
31       * Implementations are encouraged to check that the given object implements
32       * or extends the given class. Calls register(c.getName(), o)
33       * 
34       * @param c
35       *            Class
36       * @param o
37       *            Object
38       */
39      void register(final Class c, final Object o);
40  
41      /***
42       * Register an object as a service, using the object's class name as the
43       * service name. Calls register(o.getClass().getName(), o)
44       * 
45       * @param o
46       *            Object
47       */
48      void register(final Object o);
49  
50      /***
51       * Remove a named service.
52       * 
53       * @param name
54       *            service name
55       */
56      void unregister(final String name);
57  
58      /***
59       * Remove a service object. First checks if a service named with the
60       * object's class name exists in the registry, and that the service maps to
61       * the given object. Throws an IllegalArgumentException otherwise.
62       * 
63       * @param o
64       *            Object
65       */
66      void unregister(final Object o);
67  
68      /***
69       * Attempt to find and retrieve a service by name.
70       * 
71       * @param name
72       *            String
73       * @return the named service if found, or null
74       */
75      Object findService(final String name);
76  
77      /***
78       * Attempt to find and retrive a service by class or interface. First
79       * attempts to find a named service by calling findService(c.getName()). If
80       * that fails, then implementations may attempt to return a suitable object
81       * that extends the superclass or interface.
82       * 
83       * @param c
84       *            Class
85       * @return a named service, a suitable class, or null
86       */
87      Object findService(final Class c);
88  
89      /***
90       * @return the Set of service names registered with the ServiceLocator
91       */
92      Set keySet();
93  }