Wiki source code of Writing XWiki Components

Version 52.4 by Denis Gervalle on 2013/06/23

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}
2 {{toc/}}
3 {{/box}}
4
5 This tutorial guides you through the creation of a XWiki component, which is a way to extend or customize the XWiki platform. Indeed the XWiki platform is composed of components and it's possible to replace the default implementations with your own implementations. It's also possible to add new component implementations to extend the platform such as by implementing new [[Rendering Macros>>DevGuide.RenderingMacroTutorial]].
6
7 {{info}}
8 Components replace the older plugin architecture which has been deprecated a while ago.
9 {{/info}}
10
11 You should start by reading the [[Reference document on XWiki Components>>extensions:Extension.Component Module]].
12
13 = Let's get started! =
14
15 Enough talking, let's see some code!
16
17 In the following tutorial we will guide you through writing a simple component, helping you to quickly get oriented in the XWiki components world and explaining how it works.
18
19 == Creating a XWiki component using Maven ==
20
21 As you've read in the [[XWiki Component Reference>>extensions:Extension.Component Module]] writing a component is a three-steps process (component interface, component implementation and registration of component).
22
23 To make it easier for you to get started, we have created a [[Maven Archetype>>http://maven.apache.org/archetype/maven-archetype-plugin/]] to help create a simple component module with a single command.
24
25 After you've [[installed Maven>>http://maven.apache.org/]], open a shell prompt an type: {{code language="none"}}mvn archetype:generate{{/code}}.
26
27 This will list all archetypes available on Maven Central. If instead you wish to directly use the XWiki Component Archetype, you can directly type (update the version to use the version you wish to use):
28
29 {{code language="none"}}
30 mvn archetype:generate \
31 -DarchetypeArtifactId=xwiki-commons-component-archetype \
32 -DarchetypeGroupId=org.xwiki.commons \
33 -DarchetypeVersion=3.3
34 {{/code}}
35
36 Then follow the instructions. For example:
37
38 {{code language="none"}}
39 vmassol@tmp $ mvn archetype:generate
40 [INFO] Scanning for projects...
41 [INFO]
42 [INFO] ------------------------------------------------------------------------
43 [INFO] Building Maven Stub Project (No POM) 1
44 [INFO] ------------------------------------------------------------------------
45 [INFO]
46 [INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
47 [INFO]
48 [INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
49 [INFO]
50 [INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
51 [INFO] Generating project in Interactive mode
52 [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
53 Choose archetype:
54 ...
55 493: remote -> org.xwiki.commons:xwiki-commons-component-archetype (Make it easy to create a maven project for creating XWiki Components.)
56 ...
57 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 152: 493
58 Choose org.xwiki.commons:xwiki-commons-component-archetype version:
59 1: 3.3-milestone-1
60 2: 3.3-milestone-2
61 3: 3.3-SNAPSHOT
62 Choose a number: 3:
63 Define value for property 'groupId': : com.acme
64 Define value for property 'artifactId': : example
65 Define value for property 'version': 1.0-SNAPSHOT: :
66 Define value for property 'package': com.acme: :
67 Confirm properties configuration:
68 groupId: com.acme
69 artifactId: example
70 version: 1.0-SNAPSHOT
71 package: com.acme
72 Y: : Y
73 [INFO] ----------------------------------------------------------------------------
74 [INFO] Using following parameters for creating project from Archetype: xwiki-commons-component-archetype:3.3-SNAPSHOT
75 [INFO] ----------------------------------------------------------------------------
76 [INFO] Parameter: groupId, Value: com.acme
77 [INFO] Parameter: artifactId, Value: example
78 [INFO] Parameter: version, Value: 1.0-SNAPSHOT
79 [INFO] Parameter: package, Value: com.acme
80 [INFO] Parameter: packageInPathFormat, Value: com/acme
81 [INFO] Parameter: package, Value: com.acme
82 [INFO] Parameter: version, Value: 1.0-SNAPSHOT
83 [INFO] Parameter: groupId, Value: com.acme
84 [INFO] Parameter: artifactId, Value: example
85 [INFO] project created from Archetype in dir: /private/tmp/example
86 [INFO] ------------------------------------------------------------------------
87 [INFO] BUILD SUCCESS
88 [INFO] ------------------------------------------------------------------------
89 [INFO] Total time: 7:58.355s
90 [INFO] Finished at: Tue Dec 06 17:18:00 CET 2011
91 [INFO] Final Memory: 7M/81M
92 [INFO] ------------------------------------------------------------------------
93 {{/code}}
94
95 Then go in the created directory (##example## in our example above) and run ##mvn install## to build your component.
96
97 == The Component explained ==
98
99 Assume, for the following explanations, that the package you used is ##com.acme##
100
101 Navigating in the component project folder, you will see the following standard Maven project structure:
102
103 {{code language="none"}}
104 pom.xml
105 src/main/java/com/acme/HelloWorld.java
106 src/main/java/com/acme/internal/DefaultHelloWorld.java
107 src/main/java/com/acme/internal/HelloWorldScriptService.java
108 src/main/resources/META-INF/components.txt
109 src/test/java/com/acme/HelloWorldTest.java
110 {{/code}}
111
112 which corresponds to the default files created: the ##HelloWorld## interface (a.k.a component role), its implementation ##DefaultHelloWorld## (component implementation), a test class for this component ##HelloWorldTest##, the component declaration file ##components.txt## and the Maven project ##pom.xml## file. The ##HelloWorldScriptService## file is described below when we explain how to make the component's API available to wiki pages.
113
114 If you have a look in ##pom.xml## you'll notice the following dependencies:
115
116 {{code language="xml"}}
117 <dependencies>
118 <dependency>
119 <groupId>org.xwiki.commons</groupId>
120 <artifactId>xwiki-commons-component-api</artifactId>
121 <version>${commons.version}</version>
122 </dependency>
123 <!-- Testing dependencies -->
124 <dependency>
125 <groupId>org.xwiki.commons</groupId>
126 <artifactId>xwiki-commons-test</artifactId>
127 <version>${commons.version}</version>
128 <scope>test</scope>
129 </dependency>
130 </dependencies>
131 {{/code}}
132
133 The code above defines the dependency on the ##xwiki-core-component-api## in the core which is where XWiki Component notions are defined. There's also a dependency on ##xwiki-core-shared-tests## which provides helper classes to easily test components.
134
135 The interface file (##HelloWorld.java##) contains the definition of a regular Java interface and looks like this:
136
137 {{code language="java"}}
138 @Role /* annotation used for declaring the service our component provides */
139 public interface HelloWorld
140 {
141 String sayHello();
142 }
143 {{/code}}
144
145 Keep in mind that this interface specifies the API that other components can use on your component. In our case, we'll build a polite component that can ##sayHello()##.
146
147 Then we have the implementation of the interface, the ##DefaultHelloWorld## class.
148
149 {{code language="java"}}
150 @Component /* annotation used for declaring a component implementation */
151 @Singleton /* annotation used for defining the component as a singleton */
152 public class DefaultHelloWorld implements HelloWorld
153 {{/code}}
154
155 Note that optionally, there is a ##@Named## annotation to specify a component //hint//. This is useful especially when we want to distinguish between several implementations for the same type of component. Image we had a special HelloWorld implementation taking the greeting message from a database; it could look like:
156
157 {{code language="java"}}
158 @Component
159 @Named("database")
160 public class DatabaseHelloWorld implements HelloWorld
161 {{/code}}
162
163 Then the ##sayHello## in ##DefaultHelloWorld## is basic in this example:
164
165 {{code language="java"}}
166 /**
167 * Says hello by returning a greeting to the caller.
168 *
169 * @return A greeting.
170 */
171 public String sayHello()
172 {
173 return "Hello world!";
174 }
175 {{/code}}
176
177 And now, the ##components.txt## file, in which component implementations present in this jar are specified for the ##ComponentManager## to register them.
178
179 {{code language="none"}}
180 com.acme.internal.DefaultHelloWorld
181 {{/code}}
182
183 = How to find my component and use it? =
184
185 == From other components ==
186
187 To access your component from another component we use the components engine, and specify the dependencies, leaving instantiation and component injection to the be handled by the component manager.
188
189 In order to use the ##HelloWorld## component, you need a reference to it in the the component that uses it. For this, you should use a member variable in the implementation of the using component, for example, a ##Socializer## component will need to be able to say hello to the world:
190
191 {{code}}
192 @Component
193 @Singleton
194 public class DefaultSocializer implements Socializer
195 {
196 [...]
197
198 /** Will be injected by the component manager */
199 @Inject
200 private HelloWorld helloWorld;
201
202 /** Will be injected by the component manager */
203 @Inject
204 @Named("database")
205 private HelloWorld databaseWorld;
206
207 [...]
208 }
209 {{/code}}
210
211 Note the ##@Inject## annotation, which instructs the component manager to inject the required component where needed.
212
213 And that's it, you can now use the ##helloWorld## member anywhere in the ##DefaultSocializer## class freely, without further concerns, it will be assigned by the component manager provided that the ##HelloWorld## component is on the classpath at runtime when the ##Socializer## is used. Such as:
214
215 {{code}}
216 public class DefaultSocializer implements Socializer
217 {
218 [...]
219
220 public void startConversation()
221 {
222 this.helloWorld.sayHello();
223
224 [...]
225 }
226
227 [...]
228 }
229 {{/code}}
230
231 More, note that all through the process of defining a communication path between two components, we never referred components implementations, all specifications being done through //roles// and //interfaces//: the implementation of a service is completely hidden from any code external to the component.
232
233 == From non-components java code (e.g. older plugins) ==
234
235 For this kind of usages, since we cannot use the component-based architecture advantages and the "magic" of the component manager, the XWiki team has created a helper method that acts like a bridge between component code and non-component code, the ##com.xpn.xwiki.web.Utils.getComponent(String role, String hint)## that gets the specified component instance from the component manager and returns it. As seen in the previous sections, the hint is an optional identifier, additional to ##role##, used to differentiate between implementations of the same interface: the //roles// identify services while the hints help differentiate between implementations. The ##getComponent## function also has a signature without the ##hint## parameter, that uses the default hint.
236
237 To use our greetings provider component, we would simply invoke:
238
239 {{code}}
240 HelloWorld greeter = Utils.getComponent(HelloWorld.class);
241 greeter.sayHello();
242
243 HelloWorld databaseGreeter = Utils.getComponent(HelloWorld.class, "database");
244 greeter.sayHello();
245 {{/code}}
246
247 {{warning}}
248 Even if the object returned by this function is an instance of the ##DefaultHelloWorld##, you should never declare your object of the implementation type nor cast to implementation instead of interface.
249 {{/warning}}
250
251 A component is represented by its interface, the implementation for such a service can be provided by any code, any class so relying on the implementation type is neither good practice (since the interface contract should be enough for a component), nor safe. In the future, a maven enforcer plugin will be setup in the build lifecycle, so that any reference to component implementations (located in an "internal" subpackage) will cause build errors.
252
253 {{info}}
254 The usage of ##Utils.getComponent()## functions is highly discouraged, reserved for this type of situations, when you need to access a component from non-componentized code. For the componentized code, you should use either dependency declaration at 'compile-time' (as shown before with annotations) or, if you need to resolve components dependencies at runtime, use the ##ComponentManager##, which you can access by implementing the Composable interface as described in the [[Component Module Reference>>extensions:Extension.Component Module]].
255 {{/info}}
256
257 == From wiki pages ==
258
259 Components can be made accessible to wiki pages by writing a ##ScriptService## implementation. They can then be accessed using any provided scripting language (velocity, groovy, python, ruby, php, etc).
260
261 Let's make our ##sayHello## method accessible:
262
263 {{code language="java"}}
264 @Component
265 @Named("hello")
266 @Singleton
267 public class HelloWorldScriptService implements ScriptService
268 {
269 @Inject
270 private HelloWorld helloWorld;
271
272 public String greet()
273 {
274 return this.helloWorld.sayHello();
275 }
276 }
277 {{/code}}
278
279 {{info}}
280 The component hint used (the ##hello## part in the ##@Component##) is the name under which the script service will be accessible from scripting languages.
281 {{/info}}
282
283 For example to access it in Velocity you'd write: {{code language="velocity"}}$services.hello.greet(){{/code}}.
284
285 From Groovy: {{code language="groovy"}}services.hello.greet(){{/code}}.
286
287 Now for our script service to work we need to register it as a component and thus add it to the ##META-INF/components.txt## file:
288
289 {{code language="none"}}
290 ...
291 com.acme.internal.HelloWorldScriptService
292 {{/code}}
293
294 We also need to make the Script Service infrastructure available in our classpath. This is done by adding the following in your ##pom.xml## file:
295
296 {{code language="xml"}}
297 <dependency>
298 <groupId>org.xwiki.commons</groupId>
299 <artifactId>xwiki-commons-script</artifactId>
300 <version>${commons.version}</version>
301 </dependency>
302 {{/code}}
303
304 = Accessing Legacy code =
305
306 By legacy we mean old XWiki code that hasn't been moved to components yet.
307
308 == The XWiki data model ==
309
310 Since the XWiki data model (documents, objects, attachments, etc.) reside in the big, old ##xwiki-core## module, and since we don't want to add the whole core and all its dependencies as a dependency of a simple lightweight component (this would eventually lead to a circular dependency, which is not allowed by maven), the current strategy, until the data model is completely turned into a component, is to use a //bridge// between the new component architecture and the old ##xwiki-core##.
311
312 In short, the way this works is based on the fact that implementations for a component don't have to be in the same ##.jar## as the interface, and there is no dependency //from// the component interface //to// the actual implementation, only the other way around. So, we made a few simple components that offer basic access to XWiki documents, and declare the classes in ##xwiki-core## as the default implementation for those components.
313
314 If your component needs to access the XWiki data model, it will use the components from the ##xwiki-core-bridge## module for that. Note that these interfaces are rather small, so you can't do everything that you could with the old model. If you need to add some methods to the bridge, feel free to propose it on the [[mailing list>>dev:Community.MailingLists]].
315
316 For example:
317
318 {{code}}
319 @Component
320 @Singleton
321 public class DefaultHelloWorld implements HelloWorld
322 {
323 /** Provides access to documents. Injected by the Component Manager. */
324 @Inject
325 private DocumentAccessBridge documentAccessBridge;
326
327 [...]
328
329 private String getConfiguredGreeting()
330 {
331 return documentAccessBridge.getProperty("XWiki.XWikiPreferences", "greeting_text");
332 }
333 {{/code}}
334
335 === Querying the data model ===
336
337 Queries can be performed by using an instance of a QueryManager, which can be obtained and used as follows :
338
339 {{code}}
340 QueryManager queryManager = (QueryManager) componentManager.getInstance(QueryManager.class);
341 Query query = queryManager.createQuery(xwqlstatement,Query.HQL);
342 List<Object> results = query.execute();
343 {{/code}}
344
345 {{info}}
346 A reference to a ComponentManager can be obtained through injection, as explained on the [[Component module extension page>>extensions:Extension.Component Module#HGettingaccesstotheComponentManager]].
347 {{/info}}
348
349 == The XWiki context ==
350
351 Note that the XWiki context is deprecated. It was an older way of keeping track of the current request, which had to be passed around from method to method, looking like a [[ball and chain>>http://en.wikipedia.org/wiki/Ball_and_chain]] present everywhere in the code.
352
353 In the component world, the current request information is held in an **[[execution context>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContext.html]]**. This is actually more powerful than the old XWiki context, as it is a generic execution context, and you can create one anytime you want and use it anyway you want. And you don't have to manually pass it around with all method calls, as execution contexts are managed by the **[[Execution component>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/Execution.html]]**, which you can use just like any other XWiki component.
354
355 In short, if you want to get access to the execution context (which holds context information inserted by the new components), you must declare an injection point on the ##Execution## component (located in the ##xwiki-commons-context## module), and then you can write:
356
357 {{code}}
358 /** Provides access to the request context. Injected by the Component Manager. */
359 @Inject
360 private Execution execution;
361
362 [...]
363
364 private void workWithTheContext()
365 {
366 ExecutionContext context = execution.getContext();
367 // Do something with the execution context
368 }
369 {{/code}}
370
371 If you still need to access the old XWiki context, then you can get a reference to it from the execution context, but you should not cast it to an ##XWikiContext##, which would pull the whole xwiki-core as a dependency, but to a ##Map##. You won't be able to access all the properties, like the current user name or the URL factory, but you can access anything placed in the internal map of the XWikiContext.
372
373 {{code}}
374 private void workWithTheContext()
375 {
376 ExecutionContext context = execution.getContext();
377 Map<Object, Object> xwikiContext = (Map<Object, Object>) context.getProperty("xwikicontext");
378 // Do something with the XWiki context
379 }
380 {{/code}}
381
382 If you want not just to use the execution context, but to make something available in every execution context, you can create an implementation of the [[ExecutionContextInitializer>>http://maven.xwiki.org/site/xwiki-core-parent/xwiki-core-context/apidocs/org/xwiki/context/ExecutionContextInitializer.html]] component, and populate newly created execution contexts, just like with [[velocity contexts>>#HFromwikipages]].
383
384 == Code outside components ==
385
386 You can use external libraries as in any other maven module, just declare the right dependencies in your module's ##pom.xml##.
387
388 As a general rule, you should **not** work with any non-componentized XWiki code, as the way the old code was designed leads to an eventual dependency on the whole ##xwiki-core## module, which we are trying to avoid. If the component you are writing is needed by other modules (which is the case with most components, since a component which isn't providing any usable/used services is kind of useless), then this will likely lead to an eventual cyclic dependency, which will break the whole build.
389
390 If you need some functionality from the old core, consider rewriting that part as a new component first, and then use that new component from your code. You should ask first on the [[devs mailing list>>dev:Community.MailingLists]], so that we can design and implement it collaboratively.
391
392 If the effort needed for this is too large, you can try creating a bridge component, by writing just the interfaces in a new module, and make the classes from the core the default implementation of those interfaces. Then, since in the end the xwiki-core, the bridge component and your component will reside in the same classpath, plexus will take care of coupling the right classes. Be careful when writing such bridges, as they are short lived (since in the end all the old code will be replaced by proper components), and if the future real component will have a different interface, then you will have to rewrite your code to adapt to the new method names, or worse, the new component logic.
393
394 = Deploying the Component =
395
396 Now that we have a functioning Component let's build it and deploy it to a XWiki Enterprise instance:
397
398 * To build the component, issue ##mvn install##. This generates a JAR in the ##target## directory of your project.
399 * To install it into a XWiki Enterprise instance, just copy that JAR file in ##XE_WAR_HOME/WEB-INF/lib## where ##XE_WAR_HOME## is where the XWiki Enterprise WAR is deployed.
400
401 Your component is now ready for service.
402
403 Enjoy!
404
405 = See also =
406
407 * [[extensions:Extension.Component Module]]

Get Connected