Author: cziegeler
Date: Tue Aug 24 13:16:06 2010
New Revision: 988525

URL: http://svn.apache.org/viewvc?rev=988525&view=rev
Log:
Delay container startup until first usage

Modified:
    
sling/whiteboard/portal/container/src/main/java/org/apache/sling/portal/container/internal/DefaultPortletContainer.java

Modified: 
sling/whiteboard/portal/container/src/main/java/org/apache/sling/portal/container/internal/DefaultPortletContainer.java
URL: 
http://svn.apache.org/viewvc/sling/whiteboard/portal/container/src/main/java/org/apache/sling/portal/container/internal/DefaultPortletContainer.java?rev=988525&r1=988524&r2=988525&view=diff
==============================================================================
--- 
sling/whiteboard/portal/container/src/main/java/org/apache/sling/portal/container/internal/DefaultPortletContainer.java
 (original)
+++ 
sling/whiteboard/portal/container/src/main/java/org/apache/sling/portal/container/internal/DefaultPortletContainer.java
 Tue Aug 24 13:16:06 2010
@@ -60,6 +60,8 @@ public class DefaultPortletContainer imp
     /** The real portlet container */
     protected SlingPortletContainer portletContainer;
 
+    private boolean initialized = false;
+
     @Reference
     private SlingRepository repository;
 
@@ -77,13 +79,10 @@ public class DefaultPortletContainer imp
      */
     protected void deactivate(final ComponentContext context) {
         this.bundleContext = null;
-        if (this.portletContainer != null ) {
-            try {
-                this.portletContainer.destroy();
-            } catch (PortletContainerException e) {
-                this.logger.error("Error during shutdown of portlet 
container.", e);
-            }
-            this.portletContainer = null;
+        try {
+            this.destroy();
+        } catch (PortletContainerException e) {
+            this.logger.error("Error during shutdown of portlet container.", 
e);
         }
     }
 
@@ -93,13 +92,6 @@ public class DefaultPortletContainer imp
     protected void activate(final ComponentContext context)
     throws PortletContainerException {
         this.bundleContext = context.getBundleContext();
-        try {
-            this.initContainer();
-        } catch (PortletContainerException pce) {
-            // we log and throw!
-            logger.error("Unable to start portlet container.", pce);
-            throw pce;
-        }
     }
 
     /**
@@ -141,9 +133,23 @@ public class DefaultPortletContainer imp
      * @see org.apache.pluto.container.PortletContainer#destroy()
      */
     public void destroy() throws PortletContainerException {
-        this.portletContainer.destroy();
+        if (this.portletContainer != null ) {
+            this.portletContainer.destroy();
+            this.portletContainer = null;
+        }
+        initialized = false;
     }
 
+    private void checkInit() throws PortletContainerException {
+        if ( !initialized ) {
+            synchronized ( this ) {
+                if ( !initialized ) {
+                    this.init();
+                    this.initialized = true;
+                }
+            }
+        }
+    }
     /**
      * Lazy loading of portlets.
      * @param portletWindow
@@ -169,6 +175,7 @@ public class DefaultPortletContainer imp
                          final HttpServletRequest request,
                          final HttpServletResponse response)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.ensureLoaded(portletWindow, request, response);
         this.portletContainer.doAction(portletWindow, request, response);
     }
@@ -180,6 +187,7 @@ public class DefaultPortletContainer imp
                         final HttpServletRequest servletRequest,
                         final HttpServletResponse servletResponse)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.ensureLoaded(portletWindow, servletRequest, servletResponse);
         this.portletContainer.doAdmin(portletWindow, servletRequest, 
servletResponse);
 
@@ -192,6 +200,7 @@ public class DefaultPortletContainer imp
                        final HttpServletRequest servletRequest,
                        final HttpServletResponse servletResponse)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.portletContainer.doLoad(portletWindow, servletRequest, 
servletResponse);
     }
 
@@ -202,6 +211,7 @@ public class DefaultPortletContainer imp
                          final HttpServletRequest request,
                          final HttpServletResponse response)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.ensureLoaded(portletWindow, request, response);
         this.portletContainer.doRender(portletWindow, request, response);
     }
@@ -213,6 +223,7 @@ public class DefaultPortletContainer imp
                                 final HttpServletRequest request,
                                 final HttpServletResponse response)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.ensureLoaded(portletWindow, request, response);
         this.portletContainer.doServeResource(portletWindow, request, 
response);
     }
@@ -221,13 +232,19 @@ public class DefaultPortletContainer imp
      * @see org.apache.pluto.container.PortletContainer#getName()
      */
     public String getName() {
-        return this.portletContainer.getName();
+        return this.containerName;
     }
 
     /**
      * @see org.apache.pluto.container.PortletContainer#getContainerServices()
      */
     public ContainerServices getContainerServices() {
+        try {
+            checkInit();
+        } catch (PortletContainerException e) {
+            logger.error("Unable to start portlet container", e);
+            return null;
+        }
         return this.portletContainer.getContainerServices();
     }
 
@@ -235,6 +252,7 @@ public class DefaultPortletContainer imp
      * @see org.apache.pluto.container.PortletContainer#init()
      */
     public void init() throws PortletContainerException {
+        this.initContainer();
         this.portletContainer.init();
     }
 
@@ -242,7 +260,7 @@ public class DefaultPortletContainer imp
      * @see org.apache.pluto.container.PortletContainer#isInitialized()
      */
     public boolean isInitialized() {
-        return this.portletContainer.isInitialized();
+        return this.initialized;
     }
 
     /**
@@ -253,6 +271,7 @@ public class DefaultPortletContainer imp
                         final HttpServletResponse response,
                         final Event event)
     throws PortletException, IOException, PortletContainerException {
+        checkInit();
         this.ensureLoaded(portletWindow, request, response);
         this.portletContainer.doEvent(portletWindow, request, response, event);
     }
@@ -261,6 +280,12 @@ public class DefaultPortletContainer imp
      * @see 
org.apache.sling.portal.container.SlingPortletContainer#getPortletWindowManager()
      */
     public PortletWindowManager getPortletWindowManager() {
+        try {
+            checkInit();
+        } catch (PortletContainerException e) {
+            logger.error("Unable to start portlet container", e);
+            return null;
+        }
         return this.portletContainer.getPortletWindowManager();
     }
 
@@ -268,6 +293,12 @@ public class DefaultPortletContainer imp
      * @see 
org.apache.sling.portal.container.SlingPortletContainer#getPortletRegistryService()
      */
     public PortletRegistryService getPortletRegistryService() {
+        try {
+            checkInit();
+        } catch (PortletContainerException e) {
+            logger.error("Unable to start portlet container", e);
+            return null;
+        }
         return this.portletContainer.getPortletRegistryService();
     }
 }


Reply via email to