Wiki source code of Writing XWiki components

Version 27.2 by MirkoMittmann on 2010/03/09

Hide last authors
Silvia Macovei 27.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
Anca Luca 1.1 2
Vincent Massol 21.1 3 This tutorial guides you through the creation of an XWiki component, which replaces the Plugin architecture and which is now the recommended way of writing XWiki modules. They should be able to execute any Java code and communicate with XWiki by using the existing XWiki (core) components, as well as being exposed to the XWiki documents scripting environment (velocity and groovy).
Anca Luca 1.1 4
Silvia Macovei 26.1 5 You should start by reading the [[Reference document on XWiki Components>>code:Modules.ComponentModule]].
Anca Luca 1.1 6
Silvia Macovei 26.1 7 {{warning}}The tutorial below is slightly outdated since some changes have been brought to the Component Module since it was written. See the [[Reference document on XWiki Components>>code:Modules.ComponentModule]] for fresh information. This tutorial needs to be rewritten and duplicate with the Reference document removed.{{/warning}}
Anca Luca 1.1 8
Silvia Macovei 27.1 9 = Let's get started! =
Anca Luca 1.1 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
Silvia Macovei 27.1 15 == Creating a XWiki component using maven ==
Anca Luca 1.1 16
Silvia Macovei 26.1 17 To simplify the three steps process of component creation in XWiki, and since the XWiki code lifecycle is based on [[maven>>http://maven.apache.org/]], we have created a maven archetype to help create a simple component module with a single command, with respect to the XWiki architecture and components specific requirements.
18
Silvia Macovei 27.1 19 * download the archetype from here: [[xwiki-archetype-component-1.0-SNAPSHOT.jar>>attach:xwiki-archetype-component-1.0-SNAPSHOT.jar]] (it will soon be uploaded on our maven repository).
Silvia Macovei 26.1 20 * use maven to install this file on your local repository by executing (make sure you replace ##path-to-jar-file## with your own path):
Silvia Macovei 27.1 21 (((
Silvia Macovei 26.1 22 {{code}}mvn install:install-file -Dfile=<path-to-jar-file> -DartifactId=xwiki-archetype-component -DgroupId=com.xpn.xwiki.platform.tools -Dversion=1.0-SNAPSHOT -Dpackaging=jar{{/code}}
Silvia Macovei 27.1 23 )))
Anca Luca 1.1 24 * now you're ready to use maven to generate the xwiki component based on this archetype. Navigate to the directory where you want your component to be located and type:
Silvia Macovei 27.1 25 (((
Silvia Macovei 26.1 26 {{code}}mvn archetype:generate -DarchetypeGroupId=com.xpn.xwiki.platform.tools -DarchetypeArtifactId=xwiki-archetype-component -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=<component-group-id> -DartifactId=<component-artifact-id> -Dpackage=<component-package> -Dversion=<component-version> -Dpackaging=jar{{/code}}
Silvia Macovei 27.1 27 )))
Anca Luca 1.1 28
Silvia Macovei 26.1 29 where you replace ##component-group-id##, ##component-artifact-id##, ##component-package##, ##component-version## with the corresponding values for your component. To create a server XWiki Watch component, for example, we used ##-DgroupId=com.xpn.xwiki.products -DartifactId=xwiki-watch-component -Dpackage=org.xwiki.watch.component -Dversion=1.1-SNAPSHOT##. Don't forget to follow the [[xwiki package names guidelines>>http://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle#HPackagenames-1]].
Anca Luca 1.1 30
Silvia Macovei 27.1 31 Now this will create a new maven module in a folder named ##component-artifact-id## in your folder, with a default xwiki component inside.
Anca Luca 1.1 32
Silvia Macovei 27.1 33 {{info}}Note that if your parent (current, from where you are executing maven) folder is the folder of a maven module (contains a ##pom.xml## file), then the command above will fail unless the module is packaged as ##pom##. If the project is packaged as ##pom##, then the newly created module will be added in its modules list, and the parent of the newly created component module will be set to this project's ##pom##.{{/info}}
Silvia Macovei 26.1 34
Silvia Macovei 27.1 35 == The component explained ==
36
Silvia Macovei 26.1 37 Assume, for the following explanations, that the package you used is ##org.xwiki.component##
38
Anca Luca 1.1 39 Navigating in the component project folder, you will see standard maven project structure like this:
Silvia Macovei 26.1 40
41 {{code}}
Anca Luca 1.1 42 pom.xml
43 src/main/java/org/xwiki/component/HelloWorld.java
44 src/main/java/org/xwiki/component/internal/DefaultHelloWorld.java
dan 19.1 45 src/main/resources/META-INF/components.txt
Anca Luca 1.1 46 src/test/java/org/xwiki/component/HelloWorldTest.java
Silvia Macovei 26.1 47 {{/code}}
Anca Luca 1.1 48
Silvia Macovei 26.1 49 which corresponds to the default files created: the ##HelloWorld## interface (service), its implementation ##DefaultHelloWorld##, a test class for this component ##HelloWorldTest##, the component declaration file ##components.txt## and the maven project ##pom## file.
50
51 If we have a look in the ##pom##, we see something like this:
52
53 {{code}}
54 <groupId>your-group-id</groupId>
Anca Luca 1.1 55 <artifactId>your-artifact-id</artifactId>
56 <version>your-version</version>
Silvia Macovei 26.1 57 {{/code}}
58
Anca Luca 1.1 59 which are the group, artifact and version you used when you created your component
Silvia Macovei 26.1 60
61 {{code}}
62 <properties>
Anca Luca 1.1 63 <!-- TODO: remove this if you inherit a project that has the core version set -->
Anca Luca 17.2 64 <platform.core.version>1.8-SNAPSHOT</platform.core.version>
Anca Luca 1.1 65 </properties>
Silvia Macovei 26.1 66 {{/code}}
Silvia Macovei 27.1 67
Silvia Macovei 26.1 68 It defines the core version for the ##xwiki-component-api## dependency. If your component is created as part of a project already depending on the core (and which most probably already has a property for the core version), use the inherited property value instead of redefining it here. Or, if the component should have the same version as the rest of the XWiki modules, you can use ##${pom.version}## as the version for other XWiki dependencies.
69
70 {{code}}
71 <dependencies>
Anca Luca 1.1 72 <dependency>
73 <groupId>org.xwiki.platform</groupId>
Oana Florea 23.1 74 <artifactId>xwiki-core-component-api</artifactId>
Anca Luca 1.1 75 <version>${platform.core.version}</version>
76 </dependency>
77 <dependency>
78 <groupId>junit</groupId>
79 <artifactId>junit</artifactId>
80 <version>3.8.1</version>
81 <scope>test</scope>
82 </dependency>
83 <!-- Add here all your other dependencies -->
84 </dependencies>
Silvia Macovei 26.1 85 {{/code}}
Silvia Macovei 27.1 86
Silvia Macovei 26.1 87 The code above defines the dependency on the ##xwiki-component module-api## in the core and the junit for the testing phase and, of course, marks the spot for you to add all your other components, modules, and library dependencies for maven to know and put them on your classpath when compiling, etc.
Anca Luca 1.1 88
Silvia Macovei 27.1 89 To inherit all the properties of the platform projects (among others, source and target compatibility with **Java 1.5**), we inherit our project from the platform pom:
90
Silvia Macovei 26.1 91 {{code}}
92 <parent>
Oana Florea 23.1 93 <groupId>org.xwiki.platform</groupId>
94 <artifactId>xwiki</artifactId>
95 <version>28</version>
96 </parent>
Silvia Macovei 26.1 97 {{/code}}
Oana Florea 23.1 98
Silvia Macovei 26.1 99 The interface file (##HelloWorld.java##) contains the definition of a regular Java interface, and looks like this:
100
101 {{code}}
dan 19.1 102 @ComponentRole /* annotation used for declaring the service our component will provide */
103 public interface HelloWorld
104 {
105 String sayHello();
106 }
Silvia Macovei 26.1 107 {{/code}}
Anca Luca 1.1 108
Silvia Macovei 26.1 109 Keep in mind that only this interface specifies the functions the other components will use to communicate with our component, no other functions besides the ones defined in this interface will be accessible to the "outside world". In our case, we'll build a polite component that can only ##sayHello()##.
Silvia Macovei 27.1 110
Silvia Macovei 26.1 111 Then we have the implementation of the interface, the ##DefaltHelloWorld## class.
Oana Florea 23.1 112
Silvia Macovei 26.1 113 {{code}}
dan 19.2 114 @Component
Anca Luca 1.1 115 public class DefaultHelloWorld extends AbstractLogEnabled implements HelloWorld, Initializable
Silvia Macovei 26.1 116 {{/code}}
dan 19.1 117
Silvia Macovei 26.1 118 Notice the ##@Component## annotation used for declaring the implementation for the component. Optionally, a component implementation can have a //hint// assigned. This is useful especially when we want to distinguish between several implementations for the same type of component. In this case, the ##DefaltHelloWorld## class would look like this:
119
120 {{code}}
dan 19.2 121 @Component("myCustomHelloWorld")
122 public class DefaultHelloWorld extends AbstractLogEnabled implements HelloWorld, Initializable
Silvia Macovei 26.1 123 {{/code}}
Silvia Macovei 27.1 124
dan 20.1 125 We can go even deeper into more advanced issues and specify multiple component hints for the same implementation, if we want. This can be done as follows:
Silvia Macovei 26.1 126
127 {{code}}
dan 20.1 128 @Component(hints = {"info", "warning", "error" })
129 public class DefaultHelloWorld extends AbstractLogEnabled implements HelloWorld, Initializable
Silvia Macovei 26.1 130 {{/code}}
dan 19.1 131
Silvia Macovei 26.1 132 This class extends ##AbstractLogEnabled## to be able to use the logging system, implements the component interface, and also the ##Initializable## interface which allows it to hook initialization code upon its instantiation by the component manager, in the ##initialize()## function:
dan 19.2 133
Silvia Macovei 26.1 134 {{code}}
dan 19.1 135 public void initialize() throws InitializationException
136 {
137 // TODO: initialize component
138 // getLogger is inherited from AbstractLogEnabled
139 getLogger().debug("DefaultHelloWorld initialized");
140 }
Silvia Macovei 26.1 141 {{/code}}
Anca Luca 1.1 142
Silvia Macovei 26.1 143 {{code}}
dan 19.1 144 /**
145 * Says hello by returning a greeting to the caller.
146 *
147 * @return A greeting.
148 */
149 public String sayHello()
150 {
151 return "Hello world!";
152 }
Silvia Macovei 26.1 153 {{/code}}
Anca Luca 1.1 154
Silvia Macovei 26.1 155 And now, the ##components.txt## file, in which component implementations present in this jar are specified for the ##ComponentManager## to register them
Anca Luca 1.1 156
Silvia Macovei 26.1 157 {{code}}org.xwiki.component.internal.DefaultHelloWorld{{/code}}
Anca Luca 1.1 158
Silvia Macovei 27.1 159 = How to find my component and use it? =
Anca Luca 24.1 160
Silvia Macovei 27.1 161 == From other components ==
Anca Luca 1.1 162
163 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.
Silvia Macovei 27.1 164
165 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.
166
Silvia Macovei 26.1 167 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:
Anca Luca 1.1 168
Silvia Macovei 26.1 169 {{code}}
dan 19.1 170 @Component
Anca Luca 1.1 171 public class DefaultSocializer extends AbstractLogEnabled implements Socializer, Initializable
172 {
173 [...]
174
175 /** Will be injected by the component manager */
dan 19.1 176 @Requirement
Anca Luca 1.1 177 private HelloWorld helloWorld;
178
179 [...]
180 }
Silvia Macovei 26.1 181 {{/code}}
Anca Luca 1.1 182
Silvia Macovei 26.1 183 Note the ##@Requirement## annotation, which instructs the component manager to inject the required component where needed.
Silvia Macovei 27.1 184
Silvia Macovei 26.1 185 The content of ##components.txt## should be updated with:
dan 19.1 186
Silvia Macovei 26.1 187 {{code}}org.xwiki.component.internal.DefaultSocializer{{/code}}
Anca Luca 1.1 188
Silvia Macovei 26.1 189 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:
Anca Luca 1.1 190
Silvia Macovei 26.1 191 {{code}}
Anca Luca 1.1 192 public class DefaultSocializer extends AbstractLogEnabled implements Socializer, Initializable
193 {
194 [...]
195
196 public void startConversation()
197 {
198 this.helloWorld.sayHello();
199
200 [...]
201 }
202
203 [...]
204 }
Silvia Macovei 26.1 205 {{/code}}
Anca Luca 1.1 206
Silvia Macovei 26.1 207 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.
Silvia Macovei 27.1 208
Anca Luca 1.1 209 TODO: refer to the other ways of implementing dependencies but requirements mechanism. Details, explanations, links.
210
Silvia Macovei 27.1 211 == From non-components java code (e.g. older plugins) ==
Anca Luca 1.1 212
Silvia Macovei 26.1 213 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.
Silvia Macovei 27.1 214
Silvia Macovei 26.1 215 To use our greetings provider component, we simply invoke:
Anca Luca 1.1 216
Silvia Macovei 26.1 217 {{code}}
dan 19.1 218 HelloWorld greeter = (HelloWorld) Utils.getComponent(HelloWorld.class);
Anca Luca 1.1 219 //use the HelloWorld service
220 greeter.sayHello();
Silvia Macovei 26.1 221 {{/code}}
Anca Luca 1.1 222
Silvia Macovei 26.1 223 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.
Silvia Macovei 27.1 224
Silvia Macovei 26.1 225 {{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}}
Anca Luca 1.1 226
Silvia Macovei 27.1 227 == From wiki pages ==
Anca Luca 15.1 228
Anca Luca 1.1 229 In order to use a component in wiki pages, we need to expose it to the scripting environments: groovy and velocity.
230
Silvia Macovei 27.1 231 === Accessing a component from groovy ===
Anca Luca 1.1 232
Silvia Macovei 26.1 233 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):
Anca Luca 1.1 234
Silvia Macovei 26.1 235 {{code}}
Anca Luca 1.1 236 <%
dan 19.1 237 def greeter = com.xpn.xwiki.web.Utils.getComponent(org.xwiki.component.HelloWorld.class);
Anca Luca 1.1 238 println greeter.sayHello();
239 %>
Silvia Macovei 26.1 240 {{/code}}
Anca Luca 1.1 241
242 TODO: talk about the future plans (?) to make a component accessible in the groovy context through a groovy bridge.
243
Silvia Macovei 27.1 244 === Accessing a component from velocity ===
Anca Luca 1.1 245
246 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:
247
Silvia Macovei 27.1 248 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.
249
Silvia Macovei 26.1 250 {{code}}
Vincent Massol 21.4 251 @Component("helloWorld")
Anca Luca 1.1 252 public class HelloWorldVelocityContextInitializer implements VelocityContextInitializer
253 {
254 /** The key to add to the velocity context */
255 public static final String VELOCITY_CONTEXT_KEY = "greeter";
256
257 /** The component instance to add to the velocity context, injected by the component manager */
dan 19.2 258 @Requirement
Anca Luca 1.1 259 private HelloWorld helloWorld;
260
261 /**
262 * Add the component instance to the velocity context received as parameter.
263 */
264 public void initialize(VelocityContext context)
265 {
266 context.put(VELOCITY_CONTEXT_KEY, this.helloWorld);
267 }
268 }
Silvia Macovei 26.1 269 {{/code}}
Anca Luca 1.1 270
Silvia Macovei 26.1 271 The result of this will be the availability of the ##HelloWorld## instance in velocity through the key //greeter// (as in ##$greeter.sayHello()##).
Silvia Macovei 27.1 272
Silvia Macovei 26.1 273 This instance of ##HelloWorld## will be injected by the component manager in the ##helloWorld## member, due to the ##@Requirement## annotation.
Silvia Macovei 27.1 274
Silvia Macovei 26.1 275 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):
Anca Luca 1.1 276
Silvia Macovei 26.1 277 {{code}}
Anca Luca 25.1 278 [...]
279 org.component.internal.vcinitializer.HelloWorldVelocityContextInitializer
Silvia Macovei 26.1 280 {{/code}}
Anca Luca 1.1 281
Silvia Macovei 26.1 282 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.
Silvia Macovei 27.1 283
Silvia Macovei 26.1 284 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:
Anca Luca 1.1 285
Silvia Macovei 26.1 286 {{code}}
287 <dependency>
Anca Luca 1.1 288 <groupId>org.xwiki.platform</groupId>
289 <artifactId>xwiki-core-velocity</artifactId>
290 <version>${platform.core.version}</version>
291 </dependency>
Silvia Macovei 26.1 292 {{/code}}
Anca Luca 1.1 293
Silvia Macovei 26.1 294 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:
Anca Luca 1.1 295
Silvia Macovei 26.1 296 {{code}}$greeter.sayHello(){{/code}}
Anca Luca 1.1 297
Silvia Macovei 27.1 298 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]].
Anca Luca 1.1 299
Silvia Macovei 27.1 300 = How do I find other code? =
Anca Luca 1.1 301
Silvia Macovei 27.1 302 == The XWiki data model ==
Anca Luca 1.1 303
Silvia Macovei 26.1 304 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##.
Silvia Macovei 27.1 305
Silvia Macovei 26.1 306 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.
Silvia Macovei 27.1 307
Silvia Macovei 26.1 308 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]].
Silvia Macovei 27.1 309
Silvia Macovei 26.1 310 For example:
Anca Luca 1.1 311
Silvia Macovei 26.1 312 {{code}}
dan 19.1 313 @Component
Anca Luca 1.1 314 public class DefaultHelloWorld implements HelloWorld
315 {
316 /** Provides access to documents. Injected by the Component Manager. */
dan 19.1 317 @Requirement
Anca Luca 1.1 318 private DocumentAccessBridge documentAccessBridge;
319
320 [...]
321
322 private String getConfiguredGreeting()
323 {
324 return documentAccessBridge.getProperty("XWiki.XWikiPreferences", "greeting_text");
325 }
Silvia Macovei 26.1 326 {{/code}}
Anca Luca 1.1 327
Silvia Macovei 27.1 328 == The XWiki context ==
Anca Luca 1.1 329
Silvia Macovei 26.1 330 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.
Silvia Macovei 27.1 331
Silvia Macovei 26.1 332 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.
Silvia Macovei 27.1 333
Silvia Macovei 26.1 334 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:
Anca Luca 1.1 335
Silvia Macovei 26.1 336 {{code}}
337 /** Provides access to the request context. Injected by the Component Manager. */
dan 19.1 338 @Requirement
Anca Luca 1.1 339 private Execution execution;
340
341 [...]
342
343 private void workWithTheContext()
344 {
345 ExecutionContext context = execution.getContext();
346 // Do something with the execution context
347 }
Silvia Macovei 26.1 348 {{/code}}
Anca Luca 1.1 349
Silvia Macovei 26.1 350 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.
351
352 {{code}}
353 private void workWithTheContext()
Anca Luca 1.1 354 {
355 ExecutionContext context = execution.getContext();
356 Map<Object, Object> xwikiContext = (Map<Object, Object>) context.getProperty("xwikicontext");
357 // Do something with the XWiki context
358 }
Silvia Macovei 26.1 359 {{/code}}
Anca Luca 1.1 360
Silvia Macovei 26.1 361 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]].
Anca Luca 1.1 362
Silvia Macovei 27.1 363 == Code outside components ==
Anca Luca 1.1 364
Silvia Macovei 26.1 365 You can use external libraries as in any other maven module, just declare the right dependencies in your module's ##pom.xml##.
Silvia Macovei 27.1 366
Silvia Macovei 26.1 367 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.
Silvia Macovei 27.1 368
Silvia Macovei 26.1 369 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.
Silvia Macovei 27.1 370
Anca Luca 1.1 371 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.
372
Silvia Macovei 27.1 373 = Deploying and using the component =
Anca Luca 1.1 374
Silvia Macovei 27.1 375 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