Author: vmassol
Date: 2008-01-31 14:55:09 +0100 (Thu, 31 Jan 2008)
New Revision: 7240

Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/Utils.java
   
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServlet.java
   
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServletContextListener.java
Log:
XWIKI-2050: Add a bridge to allow current XWiki code to use XWiki components


Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/Utils.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/Utils.java 
    2008-01-31 13:30:41 UTC (rev 7239)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/web/Utils.java 
    2008-01-31 13:55:09 UTC (rev 7240)
@@ -31,6 +31,8 @@
 import org.apache.ecs.filter.CharacterFilter;
 import org.apache.log4j.MDC;
 import org.apache.struts.upload.MultipartRequestWrapper;
+import org.xwiki.component.manager.ComponentManager;
+import org.xwiki.component.manager.ComponentLookupException;
 
 import javax.servlet.http.HttpServletRequest;
 import java.io.IOException;
@@ -250,6 +252,19 @@
             mode = XWikiContext.MODE_PORTLET;
         }
         context.setMode(mode);
+
+        // This is a temporary bridge so that non XWiki component classes can 
lookup XWiki
+        // components. A ComponentManager instance has been set up in the 
Servlet Context and
+        // we now populate the XWiki Context with it so that code can then use 
it to look up
+        // components.
+        // This is of course not necessary for XWiki components since they 
just need to implement
+        // the Composable interface to get access to the Component Manager or 
better they simply
+        // need to define the Components they require as field members and 
configure the Plexus
+        // deployment descriptors (components.xml) so that they are 
automatically injected.
+        ComponentManager componentManager =
+            (ComponentManager) 
engine_context.getAttribute(ComponentManager.class.getName());
+        context.put(ComponentManager.class.getName(), componentManager);
+
         return context;
     }
 
@@ -470,4 +485,28 @@
         }
         return fileupload;
     }
+
+    /**
+     * Lookup a XWiki component by role and hint.
+     *
+     * @param role the component's identity (usually the component's interface 
name as a String)
+     * @param hint a value to differentiate different component 
implementations for the same role
+     * @param context the XWiki Context where the Component Manager is stored
+     * @return the component's Object
+     */
+    public static Object getComponent(String role, String hint, XWikiContext 
context)
+    {
+        ComponentManager componentManager =
+            (ComponentManager) context.get(ComponentManager.class.getName());
+        Object component = null;
+        if (componentManager != null) {
+            try {
+                component = componentManager.lookup(role, hint);
+            } catch (ComponentLookupException e) {
+                throw new RuntimeException("Failed to load component [" + role 
+ "]", e);
+            }
+        }
+        return component;
+    }
+
 }

Modified: 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServlet.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServlet.java
       2008-01-31 13:30:41 UTC (rev 7239)
+++ 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServlet.java
       2008-01-31 13:55:09 UTC (rev 7240)
@@ -41,14 +41,14 @@
 
         // Initializes XWiki's Container with the Servlet 
request/response/session so that
         // components needing them can depend on the Container Manager 
component to get them.
-        ServletContainer containerManager =
+        ServletContainer container =
             (ServletContainer) lookup(Container.ROLE, "servlet");
         try {
-            containerManager.initialize(httpServletRequest, 
httpServletResponse);
+            container.initialize(httpServletRequest, httpServletResponse);
         } catch (ServletContainerException e) {
             try {
                 // Call the error Action to handle the exception
-                manager.handleRequest(containerManager, "error", e);
+                manager.handleRequest(container, "error", e);
                 return;
             } catch (ActionException ae) {
                 throw new ServletException("Failed to call the error Action", 
ae);
@@ -57,12 +57,12 @@
 
         // Call the Action Manager to handle the request
         try {
-            manager.handleRequest(containerManager);
+            manager.handleRequest(container);
         } catch (ActionException e) {
             // We haven't been able to handle the exception in ActionManager 
so generate a
             // container exception.
             throw new ServletException("Failed to handle request ["
-                + containerManager.getRequest() + "]", e);
+                + container.getRequest() + "]", e);
         }
     }
 }

Modified: 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServletContextListener.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServletContextListener.java
        2008-01-31 13:30:41 UTC (rev 7239)
+++ 
xwiki-platform/core/trunk/xwiki-plexus/src/main/java/org/xwiki/plexus/XWikiPlexusServletContextListener.java
        2008-01-31 13:55:09 UTC (rev 7240)
@@ -22,11 +22,14 @@
 
 import org.codehaus.plexus.servlet.PlexusServletContextListener;
 import org.codehaus.plexus.servlet.PlexusServletUtils;
+import 
org.codehaus.plexus.personality.plexus.lifecycle.phase.PlexusContainerLocator;
+import org.codehaus.plexus.PlexusContainer;
 import org.xwiki.action.ActionException;
 import org.xwiki.action.ActionManager;
 import org.xwiki.container.Container;
 import org.xwiki.container.servlet.ServletContainer;
 import org.xwiki.container.servlet.ServletContainerException;
+import org.xwiki.plexus.manager.PlexusComponentManager;
 
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
@@ -40,22 +43,37 @@
         super.contextInitialized(servletContextEvent);
 
         // Initializes XWiki's Container with the Servlet Context
-        ServletContainer containerManager = null;
+        ServletContainer container = null;
         try {
-            containerManager = (ServletContainer) PlexusServletUtils.lookup(
+            container = (ServletContainer) PlexusServletUtils.lookup(
                 servletContextEvent.getServletContext(), Container.ROLE, 
"servlet");
-            
containerManager.initialize(servletContextEvent.getServletContext());
+            container.initialize(servletContextEvent.getServletContext());
         } catch (ServletException se) {
             throw new RuntimeException("Failed to lookup component role [" + 
Container.ROLE
                 + "] for hint [servlet]", se);
         } catch (ServletContainerException sce) {
             ActionManager manager = 
lookupActionManager(servletContextEvent.getServletContext());
             try {
-                manager.handleRequest(containerManager, "error", sce);
+                manager.handleRequest(container, "error", sce);
             } catch (ActionException ae) {
                 throw new RuntimeException("Failed to call the error Action", 
ae);
             }
         }
+
+        // This is a temporary bridge to allow non XWiki components to lookup 
XWiki components.
+        // We're putting the XWiki Component Manager instance in the Servlet 
Context so that it's
+        // available in the XWikiAction class which in turn puts it into the 
XWikiContext instance.
+        // Class that need to lookup then just need to get it from the 
XWikiContext instance.
+        // This is of course not necessary for XWiki components since they 
just need to implement
+        // the Composable interface to get access to the Component Manager or 
better they simply
+        // need to define the Components they require as field members and 
configure the Plexus
+        // deployment descriptors (components.xml) so that they are 
automatically injected.
+        PlexusContainer plexusContainer =
+            
PlexusServletUtils.getPlexusContainer(servletContextEvent.getServletContext());
+        org.xwiki.component.manager.ComponentManager xwikiManager = new 
PlexusComponentManager(
+            new PlexusContainerLocator(plexusContainer));
+        servletContextEvent.getServletContext().setAttribute(
+            org.xwiki.component.manager.ComponentManager.class.getName(), 
xwikiManager);
     }
 
     private ActionManager lookupActionManager(ServletContext servletContext)

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to