You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by dd...@apache.org on 2004/11/08 04:26:40 UTC

svn commit: rev 56898 - portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core

Author: ddewolf
Date: Sun Nov  7 19:26:39 2004
New Revision: 56898

Added:
   portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletDescriptorRegistry.java
Modified:
   portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletContextManager.java
   portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletEntity.java
Log:
Implementing simple Portlet Descriptor Registry

Modified: portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletContextManager.java
==============================================================================
--- portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletContextManager.java	(original)
+++ portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletContextManager.java	Sun Nov  7 19:26:39 2004
@@ -25,7 +25,6 @@
 import org.apache.pluto.PortletContainerException;
 import org.apache.pluto.util.StringManager;
 import org.apache.pluto.binding.PortletAppDD;
-import org.apache.pluto.binding.XMLBindingFactory;
 import org.apache.pluto.core.impl.PortletContextImpl;
 
 /**
@@ -38,14 +37,7 @@
  */
 public class PortletContextManager {
 
-    /** Portlet deployment descriptor location. */
-    private static final String PORTLET_XML = "/WEB-INF/portlet.xml";
 
-    /** Exception Messages. */
-    private static final StringManager EXCEPTIONS =
-        StringManager.getManager(
-            PortletContextManager.class.getPackage().getName()
-        );
 
     /** Singleton instance. */
     private static PortletContextManager manager;
@@ -80,35 +72,14 @@
     public InternalPortletContext getContext(ServletContext context)
         throws PortletContainerException {
         if (!contexts.containsKey(context)) {
-            PortletAppDD def = createDefinition(context);
+            PortletAppDD def = PortletDescriptorRegistry.getRegistry()
+                .getPortletAppDD(context);
             PortletContext pc = new PortletContextImpl(context, def);
             contexts.put(context, pc);
         }
         return (InternalPortletContext) contexts.get(context);
     }
 
-    /**
-     * Create the portlet.xml deployment descriptor representation.
-     *
-     * @param ctx
-     * @return
-     * @throws PortletContainerException
-     */
-    private PortletAppDD createDefinition(ServletContext ctx)
-    throws PortletContainerException {
-        PortletAppDD app = null;
-        try {
-            InputStream in = ctx.getResourceAsStream(PORTLET_XML);
-            app = XMLBindingFactory.createXMLBinding().getPortletAppDD(in);
-        } catch (IOException io) {
-            throw new PortletContainerException(
-                EXCEPTIONS.getString(
-                    "error.context.descriptor.load",
-                    new String[] {ctx.getServletContextName()}
-                ),
-                io);
-        }
-        return app;
-    }
+
 }
 

Added: portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletDescriptorRegistry.java
==============================================================================
--- (empty file)
+++ portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletDescriptorRegistry.java	Sun Nov  7 19:26:39 2004
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pluto.core;
+
+import java.util.Map;
+import java.io.InputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletContext;
+
+import org.apache.pluto.binding.PortletAppDD;
+import org.apache.pluto.binding.XMLBindingFactory;
+import org.apache.pluto.PortletContainerException;
+import org.apache.pluto.util.StringManager;
+
+/**
+ * Simple caching mechanism used to manage
+ * portlet descriptors.  This mechanism takes
+ * special considerations to make sure that the
+ * cache is invalidated for any ServletContext that
+ * is destroyed, thus allowing for a the context
+ * to be redeployed.
+ *
+ * @author <a href="ddewolf@apache.org">David H. DeWolf</a>
+ * @version 1.0
+ * @since Nov 3, 2004
+ */
+class PortletDescriptorRegistry {
+    /** Portlet deployment descriptor location. */
+    private static final String PORTLET_XML = "/WEB-INF/portlet.xml";
+
+    /** Exception Messages. */
+    private static final StringManager EXCEPTIONS =
+        StringManager.getManager(
+            PortletDescriptorRegistry.class.getPackage().getName()
+        );
+
+    /** Singleton Instance */
+    private static PortletDescriptorRegistry registry;
+
+    /** Singleton Accessor. */
+    public static PortletDescriptorRegistry getRegistry() {
+        if(registry == null) {
+            registry = new PortletDescriptorRegistry();
+        }
+        return registry;
+    }
+
+    /**
+     * Cache of descriptors.  WeakHashMap is used so that
+     * once the context is destroyed (kinda), the cache is eliminated.
+     * Ideally we'd use a ServletContextListener, but at this
+     * point I'm wondering if we really want to add another
+     * config requirement in the servlet xml? Hmm. . .
+     */
+    private Map cache = new java.util.WeakHashMap();
+
+    private PortletDescriptorRegistry() {
+
+    }
+
+    /**
+     * Retrieve the Portlet Application Deployment Descriptor
+     * for the given servlet context.  Create it if it does
+     * not allready exist.
+     *
+     * @param context
+     * @return
+     * @throws PortletContainerException
+     */
+    public PortletAppDD getPortletAppDD(ServletContext context)
+    throws PortletContainerException {
+        PortletAppDD dd = (PortletAppDD)cache.get(context);
+        if(dd == null) {
+            dd = createDefinition(context);
+            cache.put(context, dd);
+        }
+        return dd;
+    }
+    /**
+     * Create the portlet.xml deployment descriptor representation.
+     *
+     * @param ctx the servlet context for which the DD is requested.
+     * @return the Portlet Application Deployment Descriptor info.
+     * @throws org.apache.pluto.PortletContainerException
+     */
+    private PortletAppDD createDefinition(ServletContext ctx)
+    throws PortletContainerException {
+        PortletAppDD app = null;
+        try {
+            InputStream in = ctx.getResourceAsStream(PORTLET_XML);
+            app = XMLBindingFactory.createXMLBinding().getPortletAppDD(in);
+        } catch (IOException io) {
+            throw new PortletContainerException(
+                EXCEPTIONS.getString(
+                    "error.context.descriptor.load",
+                    new String[] {ctx.getServletContextName()}
+                ),
+                io);
+        }
+        return app;
+    }
+}
+

Modified: portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletEntity.java
==============================================================================
--- portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletEntity.java	(original)
+++ portals/pluto/branches/pluto-1.1/container/src/java/org/apache/pluto/core/PortletEntity.java	Sun Nov  7 19:26:39 2004
@@ -19,17 +19,16 @@
 
 package org.apache.pluto.core;
 
-import java.io.IOException;
 import java.io.InputStream;
 
 import javax.servlet.ServletContext;
 
 import org.apache.pluto.PortletWindow;
+import org.apache.pluto.PortletContainerException;
 import org.apache.pluto.core.impl.PortletPreferenceImpl;
 import org.apache.pluto.binding.PortletAppDD;
 import org.apache.pluto.binding.PortletDD;
 import org.apache.pluto.binding.ServletDD;
-import org.apache.pluto.binding.XMLBindingFactory;
 import org.apache.pluto.binding.PortletPreferenceDD;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.logging.Log;
@@ -99,17 +98,17 @@
         if(LOG.isDebugEnabled()) {
             LOG.debug("Retrieved context: "+ctx);
         }
-        InputStream in = ctx.getResourceAsStream(PortletAppDD.PORTLET_XML);
         try {
-            PortletAppDD appDD = XMLBindingFactory.createXMLBinding().getPortletAppDD(in);
+            PortletAppDD appDD = PortletDescriptorRegistry.getRegistry()
+                .getPortletAppDD(ctx);
             PortletDD[] dds = appDD.getPortlets();
             for (int i = 0; i < dds.length && dd == null; i++) {
                 if (dds[i].getPortletName().equals(window.getPortletName())) {
                     dd = dds[i];
                 }
             }
-        } catch (IOException io) {
-            io.printStackTrace();
+        } catch (PortletContainerException io) {
+            throw new NullPointerException("Unable to load the Portlet Application Deployment Descriptor: "+io.getMessage());
         }
     }