Wiki source code of Writing XWiki components

Version 29.1 by Vincent Massol on 2010/11/14

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 This tutorial guides you through the creation of an 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]].
4
5 {{info}}Components replace the older Plugin architecture which has been deprecated a while ago.{{/info}}
6
7 You should start by reading the [[Reference document on XWiki Components>>code:Modules.ComponentModule]].
8
9 = Let's get started! =
10
11 Enough talking, let's see some code!
12
13 In the followings we will guide you through writing a simple component, helping you to quickly get oriented in XWiki components world and explaining how it works.
14
15 == Creating a XWiki component using Maven ==
16
17 As you've read in the [[XWiki Component Reference>>code:Modules.ComponentModule]] writing a component is a three-streps process (component interface, component implementation, registration of component). To make it easier for you to get started, we have created a maven archetype to help create a simple component module with a single command.
18
19 After you've [installed Maven>http://dev.xwiki.org/xwiki/bin/view/Community/Building#HInstallingMaven], open a shell prompt an type:
20 {{code language="none"}}
21 mvn archetype:generate -DarchetypeCatalog=http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-tools/trunk/xwiki-archetypes/xwiki-archetype-component/archetype-catalog.xml
22 {{/code}}
23
24 Then follow the instructions. For example:
25 {{code language="none"}}
26 vmassol@target $ mvn archetype:generate -DarchetypeCatalog=http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-tools/trunk/xwiki-archetypes/xwiki-archetype-component/archetype-catalog.xml
27 [INFO] Scanning for projects...
28 [INFO]
29 [INFO] ------------------------------------------------------------------------
30 [INFO] Building Maven Stub Project (No POM) 1
31 [INFO] ------------------------------------------------------------------------
32 [INFO]
33 [INFO] >>> maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom >>>
34 [INFO]
35 [INFO] <<< maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom <<<
36 [INFO]
37 [INFO] --- maven-archetype-plugin:2.0:generate (default-cli) @ standalone-pom ---
38 [INFO] Generating project in Interactive mode
39 [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
40 Choose archetype:
41 1: http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-tools/trunk/xwiki-archetypes/xwiki-archetype-component/archetype-catalog.xml -> xwiki-archetype-component (Make it easy to create a maven project for creating a XWiki Component.)
42 Choose a number: : 1
43 Downloading: http://maven.xwiki.org/snapshots/org/xwiki/platform/tools/xwiki-archetype-component/1.0-SNAPSHOT/maven-metadata.xml
44 Downloaded: http://maven.xwiki.org/snapshots/org/xwiki/platform/tools/xwiki-archetype-component/1.0-SNAPSHOT/maven-metadata.xml (383 B at 3.4 KB/sec)
45 Downloading: http://maven.xwiki.org/snapshots/org/xwiki/platform/tools/xwiki-archetype-component/1.0-SNAPSHOT/maven-metadata.xml
46 Downloaded: http://maven.xwiki.org/snapshots/org/xwiki/platform/tools/xwiki-archetype-component/1.0-SNAPSHOT/maven-metadata.xml (383 B at 4.9 KB/sec)
47 Define value for property 'groupId': : com.acme
48 Define value for property 'artifactId': : example
49 Define value for property 'version': 1.0-SNAPSHOT: :
50 Define value for property 'package': com.acme: :
51 Confirm properties configuration:
52 groupId: com.acme
53 artifactId: example
54 version: 1.0-SNAPSHOT
55 package: com.acme
56 Y: : Y
57 [INFO] ----------------------------------------------------------------------------
58 [INFO] Using following parameters for creating project from Old (1.x) Archetype: xwiki-archetype-component:1.0-SNAPSHOT
59 [INFO] ----------------------------------------------------------------------------
60 [INFO] Parameter: groupId, Value: com.acme
61 [INFO] Parameter: packageName, Value: com.acme
62 [INFO] Parameter: package, Value: com.acme
63 [INFO] Parameter: artifactId, Value: example
64 [INFO] Parameter: basedir, Value: /Users/vmassol/dev/xwiki/trunks-clean4/platform/tools/xwiki-archetypes/target
65 [INFO] Parameter: version, Value: 1.0-SNAPSHOT
66 [INFO] ********************* End of debug info from resources from generated POM ***********************
67 [INFO] project created from Old (1.x) Archetype in dir: /Users/vmassol/dev/xwiki/trunks-clean4/platform/tools/xwiki-archetypes/target/example
68 [INFO] ------------------------------------------------------------------------
69 [INFO] BUILD SUCCESS
70 [INFO] ------------------------------------------------------------------------
71 [INFO] Total time: 12.868s
72 [INFO] Finished at: Sun Nov 14 18:27:52 CET 2010
73 [INFO] Final Memory: 9M/81M
74 [INFO] ------------------------------------------------------------------------
75 vmassol@target $
76 {{/code}}
77
78 Then go in the created directory (##example## in our example above) and run ##mvn install## to build your component.
79
80 == The Component explained ==
81
82 Assume, for the following explanations, that the package you used is ##com.acme##
83
84 Navigating in the component project folder, you will see the following standard Maven project structure:
85
86 {{code}}
87 pom.xml
88 src/main/java/com/acme/HelloWorld.java
89 src/main/java/com/acme/internal/DefaultHelloWorld.java
90 src/main/resources/META-INF/components.txt
91 src/test/java/com/acme/HelloWorldTest.java
92 {{/code}}
93
94 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.
95
96 If you have a look in the ##pom.xml##, you'll notice the following dependencies:
97
98 {{code}}
99 <dependencies>
100 <dependency>
101 <groupId>org.xwiki.platform</groupId>
102 <artifactId>xwiki-core-component-default</artifactId>
103 <version>${platform.core.version}</version>
104 </dependency>
105 <!-- Testing dependencies -->
106 <dependency>
107 <groupId>org.xwiki.platform</groupId>
108 <artifactId>xwiki-core-shared-tests</artifactId>
109 <version>${platform.core.version}</version>
110 <scope>test</scope>
111 </dependency>
112 </dependencies>
113 {{/code}}
114
115 The code above defines the dependency on the ##xwiki-core-component-default## 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.
116
117 The interface file (##HelloWorld.java##) contains the definition of a regular Java interface, and looks like this:
118
119 {{code}}
120 @ComponentRole /* annotation used for declaring the service our component provides */
121 public interface HelloWorld
122 {
123 String sayHello();
124 }
125 {{/code}}
126
127 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()##.
128
129 Then we have the implementation of the interface, the ##DefaltHelloWorld## class.
130
131 {{code}}
132 @Component /* annotation used for declaring a component implementation */
133 public class DefaultHelloWorld implements HelloWorld
134 {{/code}}
135
136 Note that optionally, the ##@Component## annotation can specify a //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 lile:
137
138 {{code}}
139 @Component("database")
140 public class DatabaseHelloWorld implements HelloWorld
141 {{/code}}
142
143 Then the ##sayHello## in ##DefaultHelloWorld## is basic in this example:
144
145 {{code}}
146 /**
147 * Says hello by returning a greeting to the caller.
148 *
149 * @return A greeting.
150 */
151 public String sayHello()
152 {
153 return "Hello world!";
154 }
155 {{/code}}
156
157 And now, the ##components.txt## file, in which component implementations present in this jar are specified for the ##ComponentManager## to register them.
158
159 {{code}}com.acme.internal.DefaultHelloWorld{{/code}}
160
161 = How to find my component and use it? =
162
163 == From other components ==
164
165 To access your component from another component we use the components engine, and specify the dependencies declarative, leaving instantiation and component injection to the be handled by the component manager. The most straightforward way is the use of the requirements mechanism of plexus, specifying that our component is required by the component that needs to access it.
166
167 Don't forget that any code that uses the component we wrote needs to have the component interface accessible in its classpath. Even if instantiation and dependency is handled by the engine at runtime, the code still needs to compile. If the two components are not in the same module (the same .jar), don't forget to add the module of the greeter component as a dependency of the module of any component that uses it.
168
169 Then, to effectively use the ##HelloWorld## component, we need a reference to it in the the component that uses it. For this, we 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:
170
171 {{code}}
172 @Component
173 public class DefaultSocializer extends AbstractLogEnabled implements Socializer, Initializable
174 {
175 [...]
176
177 /** Will be injected by the component manager */
178 @Requirement
179 private HelloWorld helloWorld;
180
181 [...]
182 }
183 {{/code}}
184
185 Note the ##@Requirement## annotation, which instructs the component manager to inject the required component where needed.
186
187 The content of ##components.txt## should be updated with:
188
189 {{code}}org.xwiki.component.internal.DefaultSocializer{{/code}}
190
191 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:
192
193 {{code}}
194 public class DefaultSocializer extends AbstractLogEnabled implements Socializer, Initializable
195 {
196 [...]
197
198 public void startConversation()
199 {
200 this.helloWorld.sayHello();
201
202 [...]
203 }
204
205 [...]
206 }
207 {{/code}}
208
209 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.
210
211 TODO: refer to the other ways of implementing dependencies but requirements mechanism. Details, explanations, links.
212
213 == From non-components java code (e.g. older plugins) ==
214
215 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 (see more at [[http://plexus.codehaus.org/guides/developer-guide/building-components/component-identity.html>>http://plexus.codehaus.org/guides/developer-guide/building-components/component-identity.html]]). The ##getComponent## function also has a version without the ##hint## parameter, that uses the default hint.
216
217 To use our greetings provider component, we simply invoke:
218
219 {{code}}
220 HelloWorld greeter = (HelloWorld) Utils.getComponent(HelloWorld.class);
221 //use the HelloWorld service
222 greeter.sayHello();
223 {{/code}}
224
225 Note that, even if, in fact, 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**. 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.
226
227 {{info}}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 described above.{{/info}}
228
229 == From wiki pages ==
230
231 In order to use a component in wiki pages, we need to expose it to the scripting environments: groovy and velocity.
232
233 === Accessing a component from groovy ===
234
235 Since, in groovy, we have access to all classes and functions in XWiki (all this protected by the requirement for programming rights), it means that we can use the same method as in [[the previous section>>WritingComponents#HFromnoncomponentsjavacode28egolderplugins29]], using the ##Utils## class. A simple page that would print the greeting from the ##HelloWorld## component would look like this (of course, with your own package for the HelloWorld interface):
236
237 {{code}}
238 <%
239 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
240 println greeter.sayHello();
241 %>
242 {{/code}}
243
244 TODO: talk about the future plans (?) to make a component accessible in the groovy context through a groovy bridge.
245
246 === Accessing a component from velocity ===
247
248 XWiki dev team is currently working on the design of a VelocityBridge interface that will handle components access from velocity. Until this specification is ready and its first implementation done, we can do it as follows:
249
250 We write another component in our package, that implements the [[##VelocityContextInitializer##>>http://svn.xwiki.org/svnroot/xwiki/platform/core/trunk/xwiki-velocity/src/main/java/org/xwiki/velocity/VelocityContextInitializer.java]], which is responsible for the initialization of the velocity context in XWiki through its method ##initialize(VelocityContext context)##, called automatically when a new velocity context is created. As you probably guessed, we will use this function to add our ##HelloWorld## component to the velocity context.
251
252 {{code}}
253 @Component("helloWorld")
254 public class HelloWorldVelocityContextInitializer implements VelocityContextInitializer
255 {
256 /** The key to add to the velocity context */
257 public static final String VELOCITY_CONTEXT_KEY = "greeter";
258
259 /** The component instance to add to the velocity context, injected by the component manager */
260 @Requirement
261 private HelloWorld helloWorld;
262
263 /**
264 * Add the component instance to the velocity context received as parameter.
265 */
266 public void initialize(VelocityContext context)
267 {
268 context.put(VELOCITY_CONTEXT_KEY, this.helloWorld);
269 }
270 }
271 {{/code}}
272
273 The result of this will be the availability of the ##HelloWorld## instance in velocity through the key //greeter// (as in ##$greeter.sayHello()##).
274
275 This instance of ##HelloWorld## will be injected by the component manager in the ##helloWorld## member, due to the ##@Requirement## annotation.
276
277 In order to have the ##VelocityContextInitializer## component accessible to the component manager, therefore initialized indeed when a velocity context is created, we specify it as a component in the ##components.txt## file (in our case, the same file where we have ##DefaultHelloWorld## component):
278
279 {{code}}
280 [...]
281 org.component.internal.vcinitializer.HelloWorldVelocityContextInitializer
282 {{/code}}
283
284 Note that this time, we specify a hint for component identification, because we need to differentiate this implementation of the ##VelocityContextInitializer## from the other implementations, as it is not the only component with this role in XWiki.
285
286 Of course, in order to for all this to compile, we need to have the ##VelocityContextInitializer## interface available on the classpath so we have this new dependency in the component module's pom:
287
288 {{code}}
289 <dependency>
290 <groupId>org.xwiki.platform</groupId>
291 <artifactId>xwiki-core-velocity</artifactId>
292 <version>${platform.core.version}</version>
293 </dependency>
294 {{/code}}
295
296 And that's it, you have made your ##HelloWorld## component velocity-accessible! Just recompile your package, copy it in the WEB-INF/lib folder of your xwiki webbapp container, and restart the server. You'll be able to get a greeting in velocity through:
297
298 {{code}}$greeter.sayHello(){{/code}}
299
300 For the automatic creation of a velocity accessible xwiki component through this method, we have also created a maven archetype for this purpose too, the [[xwiki-archetype-velocity-component-1.0-SNAPSHOT.jar>>attach:xwiki-archetype-velocity-component-1.0-SNAPSHOT.jar]]. Download it and use it as described in [[the first part of this tutorial>>WritingComponents#HCreatingaXWikicomponentusingmaven]].
301
302 = How do I find other code? =
303
304 == The XWiki data model ==
305
306 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##.
307
308 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 declared the classes in ##xwiki-core## as the default implementation for those components.
309
310 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]].
311
312 For example:
313
314 {{code}}
315 @Component
316 public class DefaultHelloWorld implements HelloWorld
317 {
318 /** Provides access to documents. Injected by the Component Manager. */
319 @Requirement
320 private DocumentAccessBridge documentAccessBridge;
321
322 [...]
323
324 private String getConfiguredGreeting()
325 {
326 return documentAccessBridge.getProperty("XWiki.XWikiPreferences", "greeting_text");
327 }
328 {{/code}}
329
330 == The XWiki context ==
331
332 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.
333
334 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.
335
336 In short, if you want to get access to the execution context (which holds context information inserted by the new components), you must declare a requirement on the ##Execution## component (located in the ##xwiki-core-context## module), and then you can write:
337
338 {{code}}
339 /** Provides access to the request context. Injected by the Component Manager. */
340 @Requirement
341 private Execution execution;
342
343 [...]
344
345 private void workWithTheContext()
346 {
347 ExecutionContext context = execution.getContext();
348 // Do something with the execution context
349 }
350 {{/code}}
351
352 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.
353
354 {{code}}
355 private void workWithTheContext()
356 {
357 ExecutionContext context = execution.getContext();
358 Map<Object, Object> xwikiContext = (Map<Object, Object>) context.getProperty("xwikicontext");
359 // Do something with the XWiki context
360 }
361 {{/code}}
362
363 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>>#HAccessingacomponentfromvelocity]].
364
365 == Code outside components ==
366
367 You can use external libraries as in any other maven module, just declare the right dependencies in your module's ##pom.xml##.
368
369 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.
370
371 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.
372
373 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.
374
375 = Deploying and using the component =
376
377 In order to have your component work with XWiki, build the maven module, and find the produced **.jar** in the target folder. Copy this .jar to the **/WEB-INF/lib** folder of your wiki instance, restart the servlet container and you're done: you should be able to access your component from velocity or groovy code, and other potential components depending on it should be able to look it up.

Get Connected