View Javadoc
1   package org.codehaus.plexus;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.codehaus.plexus.classworlds.realm.ClassRealm;
23  import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
24  import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener;
25  import org.codehaus.plexus.component.repository.ComponentDescriptor;
26  import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException;
27  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
28  import org.codehaus.plexus.configuration.PlexusConfigurationException;
29  import org.codehaus.plexus.context.Context;
30  
31  /**
32   * PlexusContainer is the entry-point for loading and accessing other
33   * components.
34   */
35  public interface PlexusContainer {
36      String ROLE = PlexusContainer.class.getName();
37  
38      // ------------------------------------------------------------------------
39      // Lookup
40      // ------------------------------------------------------------------------
41  
42      /**
43       * Looks up and returns a component object with the given unique key or role.
44       * @param role a unique key for the desired component
45       * @return a Plexus component object
46       * @throws ComponentLookupException in case of lookup error.
47       */
48      Object lookup(String role) throws ComponentLookupException;
49  
50      /**
51       * Looks up and returns a component object with the given unique role/role-hint combination.
52       * @param role a non-unique key for the desired component
53       * @param roleHint a hint for the desired component implementation
54       * @return a Plexus component object
55       * @throws ComponentLookupException in case of lookup error.
56       */
57      Object lookup(String role, String roleHint) throws ComponentLookupException;
58  
59      /**
60       * Looks up and returns a component object with the given unique key or role.
61       * @param type the unique type of the component within the container
62       * @param <T> The type.
63       * @return a Plexus component object
64       * @throws ComponentLookupException in case of lookup error.
65       */
66      <T> T lookup(Class<T> type) throws ComponentLookupException;
67  
68      /**
69       * Looks up and returns a component object with the given unique role/role-hint combination.
70       * @param type the non-unique type of the component
71       * @param roleHint a hint for the desired component implementation
72       * @param <T> The type.
73       * @return a Plexus component object
74       * @throws ComponentLookupException in case of lookup error.
75       */
76      <T> T lookup(Class<T> type, String roleHint) throws ComponentLookupException;
77  
78      /**
79       * Looks up and returns a component object with the given unique role/role-hint combination.
80       * @param type the non-unique type of the component
81       * @param role a non-unique key for the desired component
82       * @param roleHint a hint for the desired component implementation
83       * @param <T> The type.
84       * @return a Plexus component object
85       * @throws ComponentLookupException in case of lookup error.
86       */
87      <T> T lookup(Class<T> type, String role, String roleHint) throws ComponentLookupException;
88  
89      /**
90       * Looks up and returns a component object matching the given component descriptor.
91       * @param componentDescriptor the descriptor of the component
92       * @param <T> The type.
93       * @return a Plexus component object
94       * @throws ComponentLookupException in case of lookup error.
95       */
96      <T> T lookup(ComponentDescriptor<T> componentDescriptor) throws ComponentLookupException;
97  
98      /**
99       * Looks up and returns a List of component objects with the given role.
100      * @param role a non-unique key for the desired components
101      * @return a List of component objects
102      * @throws ComponentLookupException in case of lookup error.
103      */
104     List<Object> lookupList(String role) throws ComponentLookupException;
105 
106     /**
107      * Looks up and returns a List of component objects with the given role.
108      * @param role a non-unique key for the desired components
109      * @param roleHints the list of hints.
110      * @return a List of component objects
111      * @throws ComponentLookupException in case of lookup error.
112      */
113     List<Object> lookupList(String role, List<String> roleHints) throws ComponentLookupException;
114 
115     /**
116      * Looks up and returns a List of component objects with the given role.
117      * @param type the non-unique type of the components
118      * @param <T> The type.
119      * @return a List of component objects
120      * @throws ComponentLookupException in case of lookup error.
121      */
122     <T> List<T> lookupList(Class<T> type) throws ComponentLookupException;
123 
124     /**
125      * Looks up and returns a List of component objects with the given role.
126      * @param type the non-unique type of the components
127      * @param roleHints the list of hints.
128      * @param <T> The type.
129      * @return a List of component objects
130      * @throws ComponentLookupException in case of lookup error.
131      */
132     <T> List<T> lookupList(Class<T> type, List<String> roleHints) throws ComponentLookupException;
133 
134     /**
135      * Looks up and returns a Map of component objects with the given role, keyed by all available role-hints.
136      * @param role a non-unique key for the desired components
137      * @return a Map of component objects
138      * @throws ComponentLookupException in case of lookup error.
139      */
140     Map<String, Object> lookupMap(String role) throws ComponentLookupException;
141 
142     /**
143      * Looks up and returns a Map of component objects with the given role, keyed by all available role-hints.
144      * @param role a non-unique key for the desired components
145      * @param roleHints the list of hints.
146      * @return a Map of component objects
147      * @throws ComponentLookupException in case of lookup error.
148      */
149     Map<String, Object> lookupMap(String role, List<String> roleHints) throws ComponentLookupException;
150 
151     /**
152      * Looks up and returns a Map of component objects with the given role, keyed by all available role-hints.
153      * @param type the non-unique type of the components
154      * @param <T> The type.
155      * @return a Map of component objects
156      * @throws ComponentLookupException in case of lookup error.
157      */
158     <T> Map<String, T> lookupMap(Class<T> type) throws ComponentLookupException;
159 
160     /**
161      * Looks up and returns a Map of component objects with the given role, keyed by all available role-hints.
162      * @param type the non-unique type of the components
163      * @param roleHints the list of hints.
164      * @param <T> The type.
165      * @return a Map of component objects
166      * @throws ComponentLookupException in case of lookup error.
167      */
168     <T> Map<String, T> lookupMap(Class<T> type, List<String> roleHints) throws ComponentLookupException;
169 
170     // ----------------------------------------------------------------------
171     // Component Descriptor Lookup
172     // ----------------------------------------------------------------------
173 
174     /**
175      * Returns the ComponentDescriptor with the given component role and the default role hint.
176      * Searches up the hierarchy until one is found, null if none is found.
177      * @param role a unique role for the desired component's descriptor
178      * @return the ComponentDescriptor with the given component role
179      */
180     ComponentDescriptor<?> getComponentDescriptor(String role);
181 
182     /**
183      * Returns the ComponentDescriptor with the given component role and hint.
184      * Searches up the hierarchy until one is found, null if none is found.
185      * @param role a unique role for the desired component's descriptor
186      * @param roleHint a hint showing which implementation should be used
187      * @return the ComponentDescriptor with the given component role
188      */
189     ComponentDescriptor<?> getComponentDescriptor(String role, String roleHint);
190 
191     /**
192      * Returns the ComponentDescriptor with the given component role and hint.
193      * Searches up the hierarchy until one is found, null if none is found.
194      * @param type the Java type of the desired component
195      * @param role a unique role for the desired component's descriptor
196      * @param roleHint a hint showing which implementation should be used
197      * @param <T> The type.
198      * @return the ComponentDescriptor with the given component role
199      */
200     <T> ComponentDescriptor<T> getComponentDescriptor(Class<T> type, String role, String roleHint);
201 
202     /**
203      * Returns a Map of ComponentDescriptors with the given role, keyed by role-hint. Searches up the hierarchy until
204      * all are found, an empty Map if none are found.
205      * @param role a non-unique key for the desired components
206      * @return a Map of component descriptors keyed by role-hint
207      */
208     Map<String, ComponentDescriptor<?>> getComponentDescriptorMap(String role);
209 
210     /**
211      * Returns a Map of ComponentDescriptors with the given role, keyed by role-hint. Searches up the hierarchy until
212      * all are found, an empty Map if none are found.
213      * @param type the Java type of the desired components
214      * @param role a non-unique key for the desired components
215      * @param <T> The type.
216      * @return a Map of component descriptors keyed by role-hint
217      */
218     <T> Map<String, ComponentDescriptor<T>> getComponentDescriptorMap(Class<T> type, String role);
219 
220     /**
221      * Returns a List of ComponentDescriptors with the given role. Searches up the hierarchy until all are found, an
222      * empty List if none are found.
223      * @param role a non-unique key for the desired components
224      * @return a List of component descriptors
225      */
226     List<ComponentDescriptor<?>> getComponentDescriptorList(String role);
227 
228     /**
229      * Returns a List of ComponentDescriptors with the given role. Searches up the hierarchy until all are found, an
230      * empty List if none are found.
231      * @param type the Java type of the desired components
232      * @param role a non-unique key for the desired components
233      * @param <T> The type.
234      * @return a List of component descriptors
235      */
236     <T> List<ComponentDescriptor<T>> getComponentDescriptorList(Class<T> type, String role);
237 
238     /**
239      * Adds a component descriptor to this container. componentDescriptor should have realmId set.
240      * @param componentDescriptor {@link ComponentDescriptor}
241      * @throws CycleDetectedInComponentGraphException In case of an error.
242      */
243     void addComponentDescriptor(ComponentDescriptor<?> componentDescriptor)
244             throws CycleDetectedInComponentGraphException;
245 
246     /**
247      * Releases the component from the container. This is dependent upon how the implementation manages the component,
248      * but usually enacts some standard lifecycle shutdown procedure on the component. In every case, the component is
249      * no longer accessible from the container (unless another is created).
250      * @param component the plexus component object to release
251      * @throws ComponentLifecycleException in case of an error.
252      */
253     void release(Object component) throws ComponentLifecycleException;
254 
255     /**
256      * Releases all Mapped component values from the container.
257      * @see PlexusContainer#release( Object component )
258      * @param components Map of plexus component objects to release
259      * @throws ComponentLifecycleException in case of an error.
260      */
261     void releaseAll(Map<String, ?> components) throws ComponentLifecycleException;
262 
263     /**
264      * Releases all Listed components from the container.
265      * @see PlexusContainer#release( Object component )
266      * @param components List of plexus component objects to release
267      * @throws ComponentLifecycleException in case of an error.
268      */
269     void releaseAll(List<?> components) throws ComponentLifecycleException;
270 
271     /**
272      * Returns true if this container has the keyed component.
273      * @param role a non-unique key for the desired component
274      * @return true if this container has the keyed component
275      */
276     boolean hasComponent(String role);
277 
278     /**
279      * Returns true if this container has a component with the given role/role-hint.
280      * @param role a non-unique key for the desired component
281      * @param roleHint a hint for the desired component implementation
282      * @return true if this container has a component with the given role/role-hint
283      */
284     boolean hasComponent(String role, String roleHint);
285 
286     /**
287      * Returns true if this container has a component with the given role/role-hint.
288      * @param type the non-unique type of the component
289      * @return true if this container has a component with the given role/role-hint
290      */
291     boolean hasComponent(Class<?> type);
292 
293     /**
294      * Returns true if this container has a component with the given role/role-hint.
295      * @param type the non-unique type of the component
296      * @param roleHint a hint for the desired component implementation
297      * @return true if this container has a component with the given role/role-hint
298      */
299     boolean hasComponent(Class<?> type, String roleHint);
300 
301     /**
302      * Returns true if this container has a component with the given role/role-hint.
303      * @param type the non-unique type of the component
304      * @param role a non-unique key for the desired component
305      * @param roleHint a hint for the desired component implementation
306      * @return true if this container has a component with the given role/role-hint
307      */
308     boolean hasComponent(Class<?> type, String role, String roleHint);
309 
310     /**
311      * Disposes of this container, which in turn disposes all of it's components. This container should also remove
312      * itself from the container hierarchy.
313      */
314     void dispose();
315 
316     // ----------------------------------------------------------------------
317     // Context
318     // ----------------------------------------------------------------------
319 
320     /**
321      * Add a key/value pair to this container's Context.
322      * @param key any unique object valid to the Context's implementation
323      * @param value any object valid to the Context's implementation
324      */
325     void addContextValue(Object key, Object value);
326 
327     /**
328      * Returns this container's context. A Context is a simple data store used to hold values which may alter the
329      * execution of the Container.
330      * @return this container's context.
331      */
332     Context getContext();
333 
334     /**
335      * Returns the Classworld's ClassRealm of this Container, which acts as the default parent for all contained
336      * components.
337      * @return the ClassRealm of this Container
338      */
339     ClassRealm getContainerRealm();
340 
341     // ----------------------------------------------------------------------
342     // Discovery
343     // ----------------------------------------------------------------------
344 
345     /**
346      * Adds the listener to this container. ComponentDiscoveryListeners have the ability to respond to various
347      * ComponentDiscoverer events.
348      * @param listener A listener which responds to different ComponentDiscoveryEvents
349      */
350     void registerComponentDiscoveryListener(ComponentDiscoveryListener listener);
351 
352     /**
353      * Removes the listener from this container.
354      * @param listener A listener to remove
355      */
356     void removeComponentDiscoveryListener(ComponentDiscoveryListener listener);
357 
358     /**
359      * Discovers components in the given realm.
360      * @param childRealm {@link ClassRealm}
361      * @return list {@link ComponentDescriptor}
362      * @throws PlexusConfigurationException in case of an error.
363      * @throws CycleDetectedInComponentGraphException in case of an error.
364      */
365     List<ComponentDescriptor<?>> discoverComponents(ClassRealm childRealm)
366             throws PlexusConfigurationException, CycleDetectedInComponentGraphException;
367 
368     /**
369      * Discovers components in the given realm.
370      * @param realm the {@link ClassRealm}.
371      * @param data The data.
372      * @return list {@link ComponentDescriptor}
373      * @throws PlexusConfigurationException in case of an error.
374      * @throws CycleDetectedInComponentGraphException in case of an error.
375      */
376     List<ComponentDescriptor<?>> discoverComponents(ClassRealm realm, Object data)
377             throws PlexusConfigurationException, CycleDetectedInComponentGraphException;
378 
379     // ----------------------------------------------------------------------------
380     // Component/Plugin ClassRealm creation
381     // ----------------------------------------------------------------------------
382 
383     ClassRealm createChildRealm(String id);
384 
385     ClassRealm getComponentRealm(String realmId);
386 
387     /**
388      * Dissociate the realm with the specified id from the container. This will
389      * remove all components contained in the realm from the component repository.
390      *
391      * @param componentRealm Realm to remove from the container.
392      * @throws PlexusContainerException {@link PlexusContainerException}.
393      */
394     void removeComponentRealm(ClassRealm componentRealm) throws PlexusContainerException;
395 
396     /**
397      * Returns the lookup realm for this container, which is either
398      * the container realm or the realm set by {@link MutablePlexusContainer#setLookupRealm(ClassRealm)}.
399      * @return {@link ClassRealm}
400      */
401     ClassRealm getLookupRealm();
402 
403     /**
404      * Sets the lookup realm to use for lookup calls that don't have a ClassRealm parameter.
405      * @param realm the new realm to use.
406      * @return The previous lookup realm. It is advised to set it back once the old-style lookups have completed.
407      */
408     ClassRealm setLookupRealm(ClassRealm realm);
409 
410     /**
411      * XXX ideally i'd like to place this in a plexus container specific utility class.
412      *
413      * Utility method to retrieve the lookup realm for a component instance.
414      * If the component's classloader is a ClassRealm, that realm is returned,
415      * otherwise the result of getLookupRealm is returned.
416      * @param component The component.
417      * @return {@link ClassRealm}
418      */
419     ClassRealm getLookupRealm(Object component);
420 
421     void addComponent(Object component, String role) throws CycleDetectedInComponentGraphException;
422 
423     /**
424      * Adds live component instance to this container.
425      *
426      * Component instance is not associated with any class realm and will
427      * be ignored during lookup is lookup realm is provided using thread context
428      * classloader.
429      * @param component The component.
430      * @param role The role.
431      * @param roleHint The hint.
432      * @param <T> The type.
433      */
434     <T> void addComponent(T component, Class<?> role, String roleHint);
435 }