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 2006/12/03 04:39:13 UTC
svn commit: r481698 - in /portals/pluto/trunk: pluto-container/
pluto-container/src/main/java/org/apache/pluto/core/
pluto-container/src/main/java/org/apache/pluto/internal/impl/
pluto-container/src/test/java/org/apache/pluto/core/ pluto-portal-driver-...
Author: ddewolf
Date: Sat Dec 2 19:39:12 2006
New Revision: 481698
URL: http://svn.apache.org/viewvc?view=rev&rev=481698
Log:
Changing algorithm to create applicationId. Now derives the contextPath
Added:
portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/
portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java (with props)
Modified:
portals/pluto/trunk/pluto-container/pom.xml
portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/core/PortletContextManager.java
portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletContextImpl.java
portals/pluto/trunk/pluto-portal-driver-impl/pluto-portal-driver-impl.iml
portals/pluto/trunk/pluto-portal/src/main/webapp/WEB-INF/fragments/admin/page/view.jsp
Modified: portals/pluto/trunk/pluto-container/pom.xml
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/pom.xml?view=diff&rev=481698&r1=481697&r2=481698
==============================================================================
--- portals/pluto/trunk/pluto-container/pom.xml (original)
+++ portals/pluto/trunk/pluto-container/pom.xml Sat Dec 2 19:39:12 2006
@@ -78,6 +78,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <version>2.2</version>
+ </dependency>
+ <dependency>
<groupId>jmock</groupId>
<artifactId>jmock</artifactId>
<version>${jmock.version}</version>
@@ -102,6 +107,7 @@
<include>**/*Test.java</include>
</includes>
<excludes>
+ <!-- Abstract -->
<exclude>**/PlutoTestCase.java</exclude>
</excludes>
</configuration>
Modified: portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/core/PortletContextManager.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/core/PortletContextManager.java?view=diff&rev=481698&r1=481697&r2=481698
==============================================================================
--- portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/core/PortletContextManager.java (original)
+++ portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/core/PortletContextManager.java Sat Dec 2 19:39:12 2006
@@ -15,6 +15,8 @@
*/
package org.apache.pluto.core;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.pluto.PortletContainerException;
import org.apache.pluto.descriptors.portlet.PortletAppDD;
import org.apache.pluto.internal.InternalPortletContext;
@@ -23,15 +25,17 @@
import org.apache.pluto.spi.optional.PortletRegistryEvent;
import org.apache.pluto.spi.optional.PortletRegistryListener;
import org.apache.pluto.spi.optional.PortletRegistryService;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import javax.servlet.ServletContext;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import java.util.HashSet;
+import java.util.Map;
/**
* Manager used to cache the portlet configurations which have
@@ -44,7 +48,9 @@
*/
public class PortletContextManager implements PortletRegistryService {
- /** Log Instance */
+ /**
+ * Log Instance
+ */
private static final Log LOG = LogFactory.getLog(PortletContextManager.class);
/**
@@ -58,7 +64,7 @@
* The PortletContext cache map: key is servlet context, and value is the
* associated portlet context.
*/
- private MultiKeyedMap portletContexts = new MultiKeyedMap();
+ private Map portletContexts = new HashMap();
/**
* The registered listeners that should be notified upon
@@ -96,19 +102,22 @@
*/
public InternalPortletContext register(ServletContext servletContext)
throws PortletContainerException {
+ String applicationId = getContextPath(servletContext);
if (!portletContexts.containsKey(servletContext)) {
+
PortletAppDD portletAppDD = PortletDescriptorRegistry.getRegistry()
.getPortletAppDD(servletContext);
+
PortletContextImpl portletContext = new PortletContextImpl(
- servletContext, portletAppDD);
+ applicationId, servletContext, portletAppDD);
if (portletContext.getApplicationId() == null) {
throw new IllegalStateException("Unable to resolve unique identifier for portletContext.");
}
- portletContexts.put(servletContext, portletContext);
+ portletContexts.put(applicationId, portletContext);
fireRegistered(portletContext);
}
- return (InternalPortletContext) portletContexts.get(servletContext);
+ return (InternalPortletContext)portletContexts.get(applicationId);
}
public void remove(InternalPortletContext context) {
@@ -122,8 +131,8 @@
}
public PortletAppDD getPortletApplicationDescriptor(String name) throws PortletContainerException {
- InternalPortletContext ipc = (InternalPortletContext)portletContexts.get(name);
- if(ipc != null) {
+ InternalPortletContext ipc = (InternalPortletContext) portletContexts.get(name);
+ if (ipc != null) {
return ipc.getPortletApplicationDefinition();
}
return null;
@@ -147,7 +156,7 @@
((PortletRegistryListener) i.next()).portletApplicationRegistered(event);
}
- LOG.info("Portlet Context '"+context.getApplicationId()+"' registered.");
+ LOG.info("Portlet Context '" + context.getApplicationId() + "' registered.");
}
private void fireRemoved(InternalPortletContext context) {
@@ -160,27 +169,62 @@
((PortletRegistryListener) i.next()).portletApplicationRemoved(event);
}
- LOG.info("Portlet Context '"+context.getApplicationId()+"' removed.");
+ LOG.info("Portlet Context '" + context.getApplicationId() + "' removed.");
}
- class MultiKeyedMap extends HashMap {
- public void put(PortletContextImpl context) {
- put(context.getApplicationId(),context);
- put(context.getServletContext(), context);
+//
+// Utility
+
+ /**
+ * Servlet 2.5 ServletContext.getContextPath() method.
+ */
+ private static Method contextPathGetter;
- String contextPath = context.getContextPath();
+ static {
+ try {
+ contextPathGetter = ServletContext.class.getMethod("getContextPath", new Class[0]);
+ }
+ catch (NoSuchMethodException e) {
+ LOG.warn("Servlet 2.4 or below detected. Unable to find getContextPath on ServletContext.");
+ }
+ }
- if (contextPath != null) {
- put(contextPath, context);
+ protected String getContextPath(ServletContext context) {
+ String contextPath = null;
+ if (contextPathGetter != null) {
+ try {
+ contextPath = (String) contextPathGetter.invoke(context, new Class[0]);
+ } catch (Exception e) {
+ LOG.warn("Unable to directly retrieve context path from ServletContext. Computing. . . ");
}
}
- public PortletAppDD remove(PortletContextImpl context) {
- PortletAppDD dd = (PortletAppDD) remove(context.getApplicationId());
- remove(context.getContextPath());
- remove(context.getServletContext());
- return dd;
+ if(contextPath == null) {
+ contextPath = computeContextPath(context);
+ }
+
+ return contextPath;
+ }
+
+ private static final String WEB_XML = "/WEB-INF/web.xml";
+
+ protected String computeContextPath(ServletContext context) {
+ try {
+ URL webXmlUrl = context.getResource(WEB_XML);
+ String path = webXmlUrl.toExternalForm();
+ path = path.substring(0, path.indexOf(WEB_XML));
+ path = path.substring(path.lastIndexOf("/"));
+
+ int id = path.indexOf(".war");
+ if(id > 0) {
+ path = path.substring(0, id);
+ }
+
+ return path;
+ } catch (MalformedURLException e) {
+ LOG.warn("Erorr retrieving web.xml from ServletContext. Unable to derive contextPath.");
+ return null;
}
}
}
Modified: portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletContextImpl.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletContextImpl.java?view=diff&rev=481698&r1=481697&r2=481698
==============================================================================
--- portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletContextImpl.java (original)
+++ portals/pluto/trunk/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletContextImpl.java Sat Dec 2 19:39:12 2006
@@ -51,30 +51,13 @@
private static final String CONTEXT_PATH =
"org.apache.pluto.PORTLET_APP_CONTEXT_PATH";
- /**
- * Servlet 2.5 ServletContext.getContextPath() method.
- */
- private static Method contextPathGetter;
- static {
- try {
- contextPathGetter = ServletContext.class.getMethod("getContextPath", new Class[0]);
- }
- catch (NoSuchMethodException e) {
- LOG.warn("Servlet 2.4 or below detected. Unable to find getContextPath on ServletContext.");
- }
- }
// Private Member Variables ------------------------------------------------
- /**
- * Portlet Application Descriptor.
- */
+ private String applicationId;
+ private String applicationName;
private PortletAppDD portletAppDD = null;
-
- /**
- * ServletContext in which we are contained.
- */
private ServletContext servletContext = null;
// Constructor -------------------------------------------------------------
@@ -85,72 +68,24 @@
* @param servletContext the servlet context in which we are contained.
* @param portletAppDD the portlet application descriptor.
*/
- public PortletContextImpl(ServletContext servletContext,
+ public PortletContextImpl(String portletApplicationId,
+ ServletContext servletContext,
PortletAppDD portletAppDD) {
this.servletContext = servletContext;
this.portletAppDD = portletAppDD;
+ this.applicationId = portletApplicationId;
+ this.applicationName = servletContext.getServletContextName();
+ if(applicationName == null) {
+ applicationName = applicationId;
+ }
}
- /**
- * Retrieves the public applicationId published for this context.
- * The algorithm for creating the applicationId is:
- * <li>Attempt to retrieve the contextPath property value from the ServletContext if it exists (Servlet 2.5)</li>
- * <li>Attempt to retrieve the org.apache.pluto.CONTEXT_PATH attribute from the ServletContext</li>
- * <li>If all else fails, use the SerlvetContext.getServletContextName() value</li>
- * <li>Generate a random string</li>
- *
- * @return the published application Id for this context.
- */
public String getApplicationId() {
- String applicationId = getContextPath();
- if(applicationId == null) {
- applicationId = servletContext.getServletContextName();
- if(applicationId != null) {
- applicationId = Integer.toHexString(applicationId.hashCode());
- }
- }
-
- if(applicationId == null) {
- applicationId = RandomStringUtils.randomAlphanumeric(8);
- }
-
return applicationId;
}
public String getApplicationName() {
- String applicationName = servletContext.getServletContextName();
-
- if(applicationName == null) {
- applicationName = getContextPath();
- }
-
- if(applicationName == null) {
- applicationName = getApplicationId();
- }
-
return applicationName;
- }
-
- /**
- * Retrieve the contextPath of the SerlvetContext
- *
- * @return
- */
- public String getContextPath() {
- String contextPath = null;
- if (contextPathGetter != null) {
- try {
- contextPath = (String) contextPathGetter.invoke(servletContext, new Class[0]);
- } catch (Exception e) {
- LOG.warn("Unable to directly retrieve context path from ServletContext.");
- }
- }
-
- if (contextPath == null) {
- contextPath = (String) servletContext.getAttribute(CONTEXT_PATH);
- }
-
- return contextPath;
}
// PortletContext Impl -----------------------------------------------------
Added: portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java?view=auto&rev=481698
==============================================================================
--- portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java (added)
+++ portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java Sat Dec 2 19:39:12 2006
@@ -0,0 +1,41 @@
+package org.apache.pluto.core;
+
+import junit.framework.TestCase;
+
+import javax.servlet.ServletContext;
+
+import org.easymock.EasyMock;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+
+public class PortletContextManagerTest extends TestCase {
+
+ private PortletContextManager manager;
+ private ServletContext context;
+
+ public void setUp() {
+ /* Java5 Required!
+ context = EasyMock.createMock(ServletContext.class);
+ */
+ manager = PortletContextManager.getManager();
+ }
+
+ public void testComputeContextPath() throws MalformedURLException {
+ /* Java5 Required!
+ URL url = new URL("file://usr/local/apache-tomcat-5.1.19/webapps/my-test-context/WEB-INF/web.xml");
+ EasyMock.expect(context.getResource("/WEB-INF/web.xml")).andReturn(url);
+ EasyMock.replay(context);
+ assertEquals("/my-test-context", manager.computeContextPath(context));
+ EasyMock.verify(context);
+
+
+ EasyMock.reset(context);
+ url = new URL("file://usr/local/apache-tomcat-5.1.19/webapps/my-test-context.war!/WEB-INF/web.xml");
+ EasyMock.expect(context.getResource("/WEB-INF/web.xml")).andReturn(url);
+ EasyMock.replay(context);
+ assertEquals("/my-test-context", manager.computeContextPath(context));
+ */
+ }
+}
Propchange: portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: portals/pluto/trunk/pluto-container/src/test/java/org/apache/pluto/core/PortletContextManagerTest.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Rev
Modified: portals/pluto/trunk/pluto-portal-driver-impl/pluto-portal-driver-impl.iml
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-portal-driver-impl/pluto-portal-driver-impl.iml?view=diff&rev=481698&r1=481697&r2=481698
==============================================================================
--- portals/pluto/trunk/pluto-portal-driver-impl/pluto-portal-driver-impl.iml (original)
+++ portals/pluto/trunk/pluto-portal-driver-impl/pluto-portal-driver-impl.iml Sat Dec 2 19:39:12 2006
@@ -214,6 +214,15 @@
<SOURCES />
</library>
</orderEntry>
+ <orderEntry type="module-library">
+ <library>
+ <CLASSES>
+ <root url="jar:///Users/ddewolf/.m2/repository/org/easymock/easymock/2.2/easymock-2.2.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
<orderEntryProperties />
</component>
</module>
Modified: portals/pluto/trunk/pluto-portal/src/main/webapp/WEB-INF/fragments/admin/page/view.jsp
URL: http://svn.apache.org/viewvc/portals/pluto/trunk/pluto-portal/src/main/webapp/WEB-INF/fragments/admin/page/view.jsp?view=diff&rev=481698&r1=481697&r2=481698
==============================================================================
--- portals/pluto/trunk/pluto-portal/src/main/webapp/WEB-INF/fragments/admin/page/view.jsp (original)
+++ portals/pluto/trunk/pluto-portal/src/main/webapp/WEB-INF/fragments/admin/page/view.jsp Sat Dec 2 19:39:12 2006
@@ -21,7 +21,7 @@
<p>
<select name="page">
<c:forEach items="${driverConfig.pages}" var="page">
- <option value="<c:out value="${page.name}"/>"><c:out value="${page.name}"/> (<c:out value="${page.uri}"/>)</option>
+ <option value="<c:out value="${page.name}"/>"><c:out value="${page.name}"/></option>
</c:forEach>
</select>
</p>