Re: [xwiki-devs] [xwiki-notifications] r15061 - in sandbox/xwiki-core-rest: . src/main/java/org/xwiki/rest
On Jan 5, 2009, at 1:05 PM, fmancinelli (SVN) wrote:
Author: fmancinelli Date: 2009-01-05 13:05:32 +0100 (Mon, 05 Jan 2009) New Revision: 15061
Modified: sandbox/xwiki-core-rest/.classpath sandbox/xwiki-core-rest/src/main/java/org/xwiki/rest/ PageResource.java sandbox/xwiki-core-rest/src/main/java/org/xwiki/rest/ RestApplication.java Log: Componentized page representation with plexus components as suggested on the mailing list.
Modified: sandbox/xwiki-core-rest/.classpath =================================================================== --- sandbox/xwiki-core-rest/.classpath 2009-01-05 12:01:09 UTC (rev 15060) +++ sandbox/xwiki-core-rest/.classpath 2009-01-05 12:05:32 UTC (rev 15061) @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/ java"/> + <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" path="src/test/java"/> <classpathentry kind="src" path="src/test/resources"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/ org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
Modified: sandbox/xwiki-core-rest/src/main/java/org/xwiki/rest/ PageResource.java =================================================================== --- sandbox/xwiki-core-rest/src/main/java/org/xwiki/rest/ PageResource.java 2009-01-05 12:01:09 UTC (rev 15060) +++ sandbox/xwiki-core-rest/src/main/java/org/xwiki/rest/ PageResource.java 2009-01-05 12:05:32 UTC (rev 15061) @@ -1,14 +1,15 @@ package org.xwiki.rest;
+import java.util.Map; + import org.restlet.Context; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Status; import org.restlet.resource.Representation; -import org.restlet.resource.StringRepresentation; import org.restlet.resource.Variant; -import org.xwiki.rest.model.Page; +import org.xwiki.component.manager.ComponentLookupException;
import com.xpn.xwiki.api.Document;
@@ -29,9 +30,19 @@ public PageResource(Context context, Request request, Response response) { super(context, request, response); - getVariants().clear(); - getVariants().add(new Variant(MediaType.TEXT_XML)); - getVariants().add(new Variant(MediaType.TEXT_PLAIN)); + + /* Configure Media types from registered PageRepresenter components */ + try { + getVariants().clear(); + Map<String, Object> map = com.xpn.xwiki.web.Utils.getComponentManager ().lookupMap(PageRepresenter.ROLE); + for (String mediaTypeName : map.keySet()) { + MediaType mediaType = MediaType.valueOf(mediaTypeName); + getVariants().add(new Variant(mediaType)); + } + } catch (ComponentLookupException e) { + throw new RuntimeException(e); + }
The ideal way would be for PageResource to be a component. You could then have a component map directly injected in PageResource using Plexus and you wouldn't need to use the xwiki-core dep on web.Utils which we need to get rif of. -Vincent
Vincent Massol wrote:
The ideal way would be for PageResource to be a component. You could then have a component map directly injected in PageResource using Plexus and you wouldn't need to use the xwiki-core dep on web.Utils which we need to get rif of.
This is a lack of knowledge of mine about plexus... PageResource (and in general every resource) must extend org.restlet.resource.Resource. Is it compatible with the plexus model? I mean there are no interfaces/implementations involved here, we just have classes. Or maybe you are talking about a factory that is injected with all the XWiki parameters and instantiates WikiResources by initializing them with those parameters? Sorry but I am not very much into plexus yet. -Fabio
On Jan 5, 2009, at 3:02 PM, Fabio Mancinelli wrote:
Vincent Massol wrote:
The ideal way would be for PageResource to be a component. You could then have a component map directly injected in PageResource using Plexus and you wouldn't need to use the xwiki-core dep on web.Utils which we need to get rif of.
This is a lack of knowledge of mine about plexus... PageResource (and in general every resource) must extend org.restlet.resource.Resource. Is it compatible with the plexus model? I mean there are no interfaces/implementations involved here, we just have classes.
... and I don't know how RESTlet works... :) Who's instantiating PageResource? Is it us or the RESTlet framework? If it's us then it's possible to delegate the instantiation to plexus. What's the lifecycle of PageResource (should there be only 1 instance during the lifetime of the app or several)? BTW it's strange that Resource/Handler are not interfaces. Sounds like a design issue for me. We can overcome this by creating our own interface for them.
Or maybe you are talking about a factory that is injected with all the XWiki parameters and instantiates WikiResources by initializing them with those parameters?
Sorry but I am not very much into plexus yet.
-Fabio
Thanks -Vincent http://xwiki.com http://massol.net http://xwiki.org
Vincent Massol wrote:
... and I don't know how RESTlet works... :)
Who's instantiating PageResource? Is it us or the RESTlet framework?
AFAIK It's RESTlet. I just configure a route by passing the class: router.attach(UriConstants.PAGE_VERSION_URI_TEMPLATE, PageResource.class); Then RESTlet does all the magic when an actual request is received (i.e., instantiate the class and pass the actual context, request, response to the resource's constructor). -Fabio
On Jan 5, 2009, at 3:21 PM, Fabio Mancinelli wrote:
Vincent Massol wrote:
... and I don't know how RESTlet works... :)
Who's instantiating PageResource? Is it us or the RESTlet framework?
AFAIK It's RESTlet. I just configure a route by passing the class:
router.attach(UriConstants.PAGE_VERSION_URI_TEMPLATE, PageResource.class);
Then RESTlet does all the magic when an actual request is received (i.e., instantiate the class and pass the actual context, request, response to the resource's constructor).
There must be a way to pass information to the Resource. Maybe there's a notion of Context somewhere? If so then we should put the Component Manager reference in it so that PageResource have access to it. Thanks -Vincent http://xwiki.com http://massol.net http://xwiki.org
Vincent Massol wrote:
Then RESTlet does all the magic when an actual request is received (i.e., instantiate the class and pass the actual context, request, response to the resource's constructor).
There must be a way to pass information to the Resource. Maybe there's a notion of Context somewhere?
Looking at RESTlet source there is a default constructor in the Resource class that is commented with the following lines: /** * Special constructor used by IoC frameworks. Note that the init() * method MUST be invoked right after the creation of the handler in * order to keep a behavior consistent with the normal three arguments * constructor. */ So in principle it is possible to integrate plexus and restlet. I am also looking at the sonatype example you posted some time ago in order to understand how. Hopefully I will come back with a solution soon. -Fabio
Vincent Massol wrote:
There must be a way to pass information to the Resource. Maybe there's a notion of Context somewhere?
If so then we should put the Component Manager reference in it so that PageResource have access to it.
I've found an interesting piece of software that pobably does what you were saying: http://svn.sonatype.org/spice/trunk/plexus-restlet-bridge/ I will have a look at it. -Fabio
On Jan 5, 2009, at 5:01 PM, Fabio Mancinelli wrote:
Vincent Massol wrote:
There must be a way to pass information to the Resource. Maybe there's a notion of Context somewhere?
If so then we should put the Component Manager reference in it so that PageResource have access to it.
I've found an interesting piece of software that pobably does what you were saying: http://svn.sonatype.org/spice/trunk/plexus-restlet- bridge/
I will have a look at it.
Note: we cannot have a dependency on plexus since we want it to work also with OSGi for example. The init() way you've shown in the previous email sounds just perfect to me. You let plexus instantiate the class and call init(). Thanks -Vincent http://xwiki.com http://massol.net http://xwiki.org
Vincent Massol wrote:
On Jan 5, 2009, at 5:01 PM, Fabio Mancinelli wrote:
I will have a look at it.
Note: we cannot have a dependency on plexus since we want it to work also with OSGi for example.
The init() way you've shown in the previous email sounds just perfect to me. You let plexus instantiate the class and call init().
Argh :) The plexus bridge was simply perfect, it handled all the initialization and discovery of resources declared in a components.xml. Ok, I will work something out withouth dependencies... Re the plexus dependency: AFAIU we will have it anyway because we need plexus to instantiate the class. Maybe you were talking about resource classes that don't have to be subclass of some plexus-bridge superclass. Isn't it? -Fabio
On Jan 5, 2009, at 5:46 PM, Fabio Mancinelli wrote:
Vincent Massol wrote:
On Jan 5, 2009, at 5:01 PM, Fabio Mancinelli wrote:
I will have a look at it.
Note: we cannot have a dependency on plexus since we want it to work also with OSGi for example.
The init() way you've shown in the previous email sounds just perfect to me. You let plexus instantiate the class and call init().
Argh :) The plexus bridge was simply perfect, it handled all the initialization and discovery of resources declared in a components.xml. Ok, I will work something out withouth dependencies...
Re the plexus dependency: AFAIU we will have it anyway because we need plexus to instantiate the class. Maybe you were talking about resource classes that don't have to be subclass of some plexus-bridge superclass. Isn't it?
There's no import on any plexus class anywhere in our components (except in the plexus module of course). The idea is that we should be independent of the component engine and tomorrow move to OSGi without the need to change any existing module. When plexus instantiate a class we don't see it. All we do is ask xwiki's component manager to get us an instantiated class (ComponentManager.lookup()). The plexus module then uses plexus to implement the lookup but we can have an OSGi module that does the same with OSGi. Thanks -Vincent http://xwiki.com http://massol.net http://xwiki.org
Vincent Massol wrote:
On Jan 5, 2009, at 5:46 PM, Fabio Mancinelli wrote:
Vincent Massol wrote:
On Jan 5, 2009, at 5:01 PM, Fabio Mancinelli wrote:
There's no import on any plexus class anywhere in our components (except in the plexus module of course). The idea is that we should be independent of the component engine and tomorrow move to OSGi without the need to change any existing module.
When plexus instantiate a class we don't see it. All we do is ask xwiki's component manager to get us an instantiated class (ComponentManager.lookup()). The plexus module then uses plexus to implement the lookup but we can have an OSGi module that does the same with OSGi.
I committed a fully componentized version of the REST infrastructure. There are no real resources at the moment but only a dummy SpacesResource, just to show you the thing. You declare resources and their routes as components. You declare representers for different media types as components. You can find the implementation in the sandbox http://svn.xwiki.org/svnroot/xwiki/sandbox/xwiki-core-rest/src/main/java/com... and the components.xml is located at http://svn.xwiki.org/svnroot/xwiki/sandbox/xwiki-core-rest/src/main/resource... I would like to hear some comments before completely porting the remainder of what I wrote to this new architecture. Thanks. -Fabio P.S.: Plexus is a really nice tool :)
participants (2)
-
Fabio Mancinelli -
Vincent Massol