Wiki source code of Component Module
Version 6.7 by Silvia Macovei on 2009/10/05 10:36
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc start="2" depth="4"/}}{{/box}} | ||
2 | |||
3 | = Component Module = | ||
4 | |||
5 | [[XWiki's Architecture>>platform:DevGuide.Architecture]] is based on [[Component-oriented Development>>http://en.wikipedia.org/wiki/Component-based_software_engineering]] (see also this [[Plexus Component Tutorial>>http://plexus.codehaus.org/ref/why-use-components.html]] to understand the benefits of using Components). | ||
6 | |||
7 | There are several Component Manager solutions out there for Java. To name a few: | ||
8 | * [[Guice>>http://code.google.com/p/google-guice/]] | ||
9 | * [[OSGi>>http://www.osgi.org/]] | ||
10 | * [[JSR291>>http://jcp.org/en/jsr/detail?id=291]], [[JSR277>>http://jcp.org/en/jsr/detail?id=277]], [[JSR294>>http://jcp.org/en/jsr/detail?id=294]], [[Project Jigsaw>>http://openjdk.java.net/projects/jigsaw/]] | ||
11 | |||
12 | XWiki has chosen to be independent of all existing Components Managers and instead to define some simple Component interfaces that can then be bound on any existing Component Manager. XWiki is currently implementing its own [[lightweight Component Manager>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-default/src/main/java/org/xwiki/component/embed/EmbeddableComponentManager.java]] with the idea to implement a [[Guice>>http://code.google.com/p/google-guice/]] bridge as soon as Guice starts implementing [[JSR299>>http://jcp.org/en/jsr/detail?id=299]]/[[JSR330>>http://jcp.org/en/jsr/detail?id=330]] annotations. | ||
13 | |||
14 | == Features == | ||
15 | |||
16 | The Component Module defines the following features: | ||
17 | * Annotations to declare Component interfaces, Component implementations and Component dependencies (a.k.a as Component Requirements). Note that as soon as [[JSR299>>http://jcp.org/en/jsr/detail?id=299]]/[[JSR330>>http://jcp.org/en/jsr/detail?id=330]] become official we'll drop our annotations and use these instead. | ||
18 | * Ability to have Singleton Components and Per-lookup Components (a new instance is created when the component is retrieved) | ||
19 | * Ability to define a Hint to separate different Components implementations implementing the same Component interface. | ||
20 | * Automatic Field-based injection of Component Dependencies. Support for List and Map injections. | ||
21 | * Ability for Components to perform some initialization when they are instantiated. | ||
22 | * Ability for Components to log things. | ||
23 | * Component Events to be notified when a new Component is registered/unregistered in the system. | ||
24 | * [Future] Ability to define Component Realms, i.e. the ability to isolate groups of components. | ||
25 | |||
26 | == Component Registration == | ||
27 | |||
28 | There are two ways to register a Component: | ||
29 | * By setting Java Annotations on the Component Interface and the Component implementation and declaring the Component implementation in a ##META-INF/components.txt## file. | ||
30 | * By programatically registering the Component against the Component Manager instance. | ||
31 | |||
32 | === Using Annotations === | ||
33 | |||
34 | The following Annotations are available: | ||
35 | * ##[[ComponentRole>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-api/src/main/java/org/xwiki/component/annotation/ComponentRole.java]]##: Used to declare an Interface as a Component Interface (a.k.a Role) | ||
36 | * ##[[Component>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-api/src/main/java/org/xwiki/component/annotation/Component.java]]##: Used to declare a class implementing a Component Interface as a Component implementation | ||
37 | * ##[[InstantiationStrategy>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-api/src/main/java/org/xwiki/component/annotation/InstantiationStrategy.java]]##: Used to declare a Component implementation as being a singleton or not | ||
38 | * ##[[Requirement>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-api/src/main/java/org/xwiki/component/annotation/Requirement.java]]##: Used to declare a field as requiring a Component implementation to be injected at runtime | ||
39 | |||
40 | Here's a quick example: | ||
41 | |||
42 | {{code language="java"}} | ||
43 | @ComponentRole | ||
44 | public interface Macro | ||
45 | { | ||
46 | List<Block> execute(); | ||
47 | } | ||
48 | {{/code}} | ||
49 | |||
50 | {{code language="java"}} | ||
51 | @Component("message") | ||
52 | public class MessageMacro implements Macro | ||
53 | { | ||
54 | @Requirement | ||
55 | private Execution execution; | ||
56 | |||
57 | @Requirement("box") | ||
58 | private Macro boxMacro; | ||
59 | |||
60 | public List<Block> execute() | ||
61 | { | ||
62 | ... | ||
63 | } | ||
64 | } | ||
65 | {{/code}} | ||
66 | |||
67 | In this example: | ||
68 | * The ##Macro## interface is the Component Interface | ||
69 | * The ##MessageMacro## class is declared as a Macro with a ##message## Hint (to differentiate it from other implementations). Note that you can leave the hint empty, in which case it'll be the default hint. | ||
70 | * The ##MessageMacro## needs 2 other components injected: ##Execution## and ##Macro##. The implementation injected will be found at runtime by the Component Manager. An ##Execution## implementation with a default Hint will be injected and a ##Macro## implementation with the ##box## Hint will be injected. | ||
71 | |||
72 | {{info}} | ||
73 | A Component Interface + a Hint must be unique across the system. If you have 2 Components registered with the same Component Interface and the same Hint then the Component Manager will only register one of them and a warning will be printed in the logs. That's unless you really want this and have [[defined an override>>#override]]. | ||
74 | {{/info}} | ||
75 | |||
76 | In addition, for our ##MessageMacro## component to be available at runtime, you need to list it with its fully-qualified name in a ##META-INF/components.txt## file: | ||
77 | |||
78 | {{code language="none"}} | ||
79 | org.xwiki.rendering.internal.macro.message.MessageMacro | ||
80 | {{/code}} | ||
81 | |||
82 | ==== Registering a Component with several Hints ==== | ||
83 | |||
84 | You can register a component several times, for different hints. For example, to register our ##MessageMacro## for the 3 hints ##info##, ##error##, ##warning## we could use: | ||
85 | |||
86 | {{code language="java"}} | ||
87 | @Component(hints = {"info", "warning", "error" }) | ||
88 | public class MessageMacro implements Macro | ||
89 | ... | ||
90 | {{/code}} | ||
91 | |||
92 | ==== List and Map injections ==== | ||
93 | |||
94 | You may want to need to have all the Component implementations of a given Component Interface injected. To do this you simply need to have a field of type List of Map defined. | ||
95 | |||
96 | For example to be injected all ##Macro## implementations you'd use: | ||
97 | |||
98 | {{code language="java"}} | ||
99 | @Requirement | ||
100 | private List<Macro> macros; | ||
101 | {{/code}} | ||
102 | |||
103 | or | ||
104 | |||
105 | {{code language="java"}} | ||
106 | @Requirement | ||
107 | private Map<String, Macro> macros; | ||
108 | {{/code}} | ||
109 | |||
110 | In the second example the Map keys are the Hint values. | ||
111 | |||
112 | ==== Getting access to the Component Manager ==== | ||
113 | |||
114 | Automatic dependency injection is great and easy but there are times when you don't know at compile time what you want injected. For these situation you can inject the ##ComponentManager##. For example: | ||
115 | |||
116 | {{code language="java"}} | ||
117 | @Requirement | ||
118 | private ComponentManager componentManager; | ||
119 | {{/code}} | ||
120 | |||
121 | See below for the API available on ##ComponentManager##. | ||
122 | |||
123 | {{id name="override"/}} | ||
124 | ==== Overrides ==== | ||
125 | |||
126 | Sometimes you'll have several JARs with Component implementations for the Component Interface and same Hint. In this case you need to tell the Component Manager which implementation to use. This is done by creating a ##META-INF/component-overrides.txt## file and listing the implementation to use (using the same format as for the ##components.txt## file). | ||
127 | |||
128 | == Component Manager == | ||
129 | |||
130 | The Component Manager is a key class when using Components. It allows to lookup components and register new components programatically. Here's the API it offers: | ||
131 | |||
132 | {{code language="java"}} | ||
133 | <T> boolean hasComponent(Class<T> role); | ||
134 | <T> boolean hasComponent(Class<T> role, String roleHint); | ||
135 | <T> T lookup(Class<T> role) throws ComponentLookupException; | ||
136 | <T> T lookup(Class<T> role, String roleHint) throws ComponentLookupException; | ||
137 | <T> void release(T component) throws ComponentLifecycleException; | ||
138 | <T> Map<String, T> lookupMap(Class<T> role) throws ComponentLookupException; | ||
139 | <T> List<T> lookupList(Class<T> role) throws ComponentLookupException; | ||
140 | <T> void registerComponent(ComponentDescriptor<T> componentDescriptor) throws ComponentRepositoryException; | ||
141 | <T> void registerComponent(ComponentDescriptor<T> componentDescriptor, T componentInstance) throws ComponentRepositoryException; | ||
142 | void unregisterComponent(Class< ? > role, String roleHint); | ||
143 | <T> ComponentDescriptor<T> getComponentDescriptor(Class<T> role, String roleHint); | ||
144 | <T> List<ComponentDescriptor<T>> getComponentDescriptorList(Class<T> role); | ||
145 | {{/code}} | ||
146 | |||
147 | There's only one instance of the Component Manager in the system. | ||
148 | |||
149 | Note that when you're writing a Component you normally don't even need to have access to it since all you need to do is declare dependencies using Annotations as explained above. | ||
150 | |||
151 | == Component Initialization == | ||
152 | |||
153 | If your Component implementation needs to perform some initialization, you'll need to make it implement the ##org.xwiki.component.phase.Initializable## interface. For example: | ||
154 | |||
155 | {{code language="java"}} | ||
156 | @Component | ||
157 | public class DefaultObservationManager implements ObservationManager, Initializable | ||
158 | { | ||
159 | ... | ||
160 | |||
161 | public void initialize() throws InitializationException | ||
162 | { | ||
163 | // Perform some init here. | ||
164 | } | ||
165 | } | ||
166 | {{/code}} | ||
167 | |||
168 | You are then guaranteed that when your component is instantiated its ##initialize()## method will be called. | ||
169 | |||
170 | {{info}} | ||
171 | Using a constructor doesn't always work since you might need dependency injections to be done prior to the initialization happening. | ||
172 | {{/info}} | ||
173 | |||
174 | == Component Logging == | ||
175 | |||
176 | If your Component implementation needs to log something it'll need to implement the ##org.xwiki.component.phase.LogEnabled## interface. However in order to make it even easier we're providing a ##org.xwiki.component.logging.AbstractLogEnabled## class that your component can simply extend. | ||
177 | |||
178 | For example: | ||
179 | |||
180 | {{code language="java"}} | ||
181 | @Component | ||
182 | public class DefaultObservationManager extends AbstractLogEnabled implements ObservationManager | ||
183 | { | ||
184 | public void doSomething() | ||
185 | { | ||
186 | getLogger().info("Some info level logging"); | ||
187 | ... | ||
188 | } | ||
189 | } | ||
190 | {{/code}} | ||
191 | |||
192 | See the [[Logger interface>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-component/xwiki-component-api/src/main/java/org/xwiki/component/logging/Logger.java]] for more details on the logging API. | ||
193 | |||
194 | == Tutorial == | ||
195 | |||
196 | See the [["Writing a XWiki Component" tutorial>>platform:DevGuide.WritingComponents]]. |