Author: chris.laprun(a)jboss.com
Date: 2011-04-06 10:29:24 -0400 (Wed, 06 Apr 2011)
New Revision: 6169
Modified:
components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
Log:
- Updated semantics of PortletContext.getApplicationName() to include prefix so that
it's not confusing. Will need to be checked upstream but usage lookup in WSRP and
GateIn show that this shouldn't be an issue.
- Added support for PortletContext formatted as invokerId./application.portlet
- Added documentation and test case.
Modified: components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java
===================================================================
--- components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-06
13:39:23 UTC (rev 6168)
+++ components/pc/trunk/api/src/main/java/org/gatein/pc/api/PortletContext.java 2011-04-06
14:29:24 UTC (rev 6169)
@@ -50,13 +50,11 @@
String trimmedId = id.trim();
if (trimmedId.startsWith(PREFIX)) // only consider components if the id starts with
'/'
{
- String compound = trimmedId.substring(1); // exclude starting '/'
-
- int separator = compound.indexOf(SEPARATOR); // find first separator, other
separator are considered part of the portlet name
+ int separator = trimmedId.indexOf(SEPARATOR); // find first separator, other
separator are considered part of the portlet name
if (separator != -1)
{
- portletName = compound.substring(separator + 1).trim();
- applicationName = compound.substring(0, separator).trim();
+ portletName = trimmedId.substring(separator + 1).trim();
+ applicationName = PREFIX + trimmedId.substring(1, separator).trim();
}
else
{
@@ -66,8 +64,35 @@
}
else
{
- portletName = null;
- applicationName = null;
+ // check if we have the case: invokerId./application.portlet
+ int prefix = trimmedId.indexOf(PREFIX);
+ int invoker = trimmedId.indexOf(SEPARATOR);
+
+ // find first separator, check if it could be an invoker id
+ if (invoker > 0 && invoker < prefix)
+ {
+ // check if we have a second separator, which would indicate a portlet
context with invoker id
+ int separator = trimmedId.indexOf(SEPARATOR, prefix);
+ if (separator != -1)
+ {
+ String invokerId = trimmedId.substring(0, invoker).trim();
+ portletName = trimmedId.substring(separator + 1).trim();
+ trimmedId = trimmedId.substring(invoker + 1).trim();
+ applicationName = PREFIX + trimmedId.substring(1,
trimmedId.indexOf(SEPARATOR)).trim();
+ this.id = invokerId + SEPARATOR + buildIdFrom(applicationName,
portletName); // recreate id with invoker
+ return;
+ }
+ else
+ {
+ portletName = null;
+ applicationName = null;
+ }
+ }
+ else
+ {
+ portletName = null;
+ applicationName = null;
+ }
}
if (portletName == null || applicationName == null)
@@ -76,11 +101,38 @@
}
else
{
- this.id = PREFIX + applicationName + SEPARATOR + portletName;
+ this.id = buildIdFrom(applicationName, portletName);
}
}
+ private PortletContext(String applicationName, String portletName, boolean
formatApplicationName)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(applicationName,
"portlet application id", "PortletContext");
+ ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(portletName,
"container id", "PortletContext");
+ if (!applicationName.startsWith(PREFIX))
+ {
+ if (formatApplicationName)
+ {
+ applicationName = PREFIX + applicationName;
+ }
+ else
+ {
+ throw new IllegalArgumentException("Application name must start with
'" + PREFIX + "'. Was: " + applicationName);
+ }
+ }
+
+ this.applicationName = applicationName;
+ this.portletName = portletName;
+ this.id = buildIdFrom(applicationName, portletName);
+ }
+
+ private String buildIdFrom(final String applicationName, final String portletName)
+ {
+ return applicationName + SEPARATOR + portletName;
+ }
+
+
public boolean equals(Object o)
{
if (this == o)
@@ -160,8 +212,35 @@
return portletName;
}
- public static PortletContext createPortletContext(String portletApplicationId, String
containerId)
+ /**
+ * Creates a new PortletContext referencing the specified portlet in the specified
application (usually a web
+ * application).
+ *
+ * @param applicationName the application name (usually a web application context
path)
+ * @param portletName the portlet name
+ * @return a newly created PortletContext referencing the specified portlet in the
specified application.
+ * @throws IllegalArgumentException if the specified arguments are null or empty and
if the application name is not
+ * properly formatted.
+ */
+ public static PortletContext createPortletContext(String applicationName, String
portletName)
{
- return PortletContext.createPortletContext(portletApplicationId + SEPARATOR +
containerId);
+ return createPortletContext(applicationName, portletName, false);
}
+
+ /**
+ * Creates a new PortletContext referencing the specified portlet in the specified
application (usually a web
+ * application).
+ *
+ * @param applicationName the application name (usually a web application
context path)
+ * @param portletName the portlet name
+ * @param formatApplicationName <code>true</code> if the application name
should be formatted before attempting to
+ * create the PortletContext,
<code>false</code> otherwise.
+ * @return a newly created PortletContext referencing the specified portlet in the
specified application.
+ * @throws IllegalArgumentException if the specified arguments are null or empty and
if the application name is not
+ * properly formatted.
+ */
+ public static PortletContext createPortletContext(String applicationName, String
portletName, boolean formatApplicationName)
+ {
+ return new PortletContext(applicationName, portletName, formatApplicationName);
+ }
}
Modified:
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java
===================================================================
---
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-06
13:39:23 UTC (rev 6168)
+++
components/pc/trunk/api/src/test/java/org/gatein/pc/api/PortletContextTestCase.java 2011-04-06
14:29:24 UTC (rev 6169)
@@ -24,7 +24,6 @@
import junit.framework.TestCase;
-
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision$
@@ -34,12 +33,12 @@
public void testGetComponents()
{
PortletContext context =
PortletContext.createPortletContext("/applicationName.portletName");
- assertEquals("applicationName", context.getApplicationName());
+ assertEquals("/applicationName", context.getApplicationName());
assertEquals("portletName", context.getPortletName());
assertEquals("/applicationName.portletName", context.getId());
context = PortletContext.createPortletContext("\t\t\n
/applicationName.portletName \t");
- assertEquals("applicationName", context.getApplicationName());
+ assertEquals("/applicationName", context.getApplicationName());
assertEquals("portletName", context.getPortletName());
assertEquals("/applicationName.portletName", context.getId());
@@ -54,21 +53,72 @@
assertEquals("applicationName.portletName", context.getId());
context =
PortletContext.createPortletContext("/applicationName.portlet.Name");
- assertEquals("applicationName", context.getApplicationName());
+ assertEquals("/applicationName", context.getApplicationName());
assertEquals("portlet.Name", context.getPortletName());
assertEquals("/applicationName.portlet.Name", context.getId());
context = PortletContext.createPortletContext("/.");
- assertEquals("", context.getApplicationName());
+ assertEquals("/", context.getApplicationName());
assertEquals("", context.getPortletName());
assertEquals("/.", context.getId());
context = PortletContext.createPortletContext("/ applicationName\t. portlet
Name");
- assertEquals("applicationName", context.getApplicationName());
+ assertEquals("/applicationName", context.getApplicationName());
assertEquals("portlet Name", context.getPortletName());
assertEquals("/applicationName.portlet Name", context.getId());
}
+ public void testPortletContextWithInvokerId()
+ {
+ PortletContext context =
PortletContext.createPortletContext("local./foo.bar");
+ assertEquals("/foo", context.getApplicationName());
+ assertEquals("bar", context.getPortletName());
+ assertEquals("local./foo.bar", context.getId());
+
+ context = PortletContext.createPortletContext(" local\t . / foo \t. \t\n
bar");
+ assertEquals("/foo", context.getApplicationName());
+ assertEquals("bar", context.getPortletName());
+ assertEquals("local./foo.bar", context.getId());
+
+ context = PortletContext.createPortletContext("local.foo.bar");
+ assertNull(context.getApplicationName());
+ assertNull(context.getPortletName());
+ assertEquals("local.foo.bar", context.getId());
+
+ context = PortletContext.createPortletContext("local./foo");
+ assertNull(context.getApplicationName());
+ assertNull(context.getPortletName());
+ assertEquals("local./foo", context.getId());
+ }
+
+ public void testCreateFromComponents()
+ {
+ PortletContext context;
+ try
+ {
+ context = PortletContext.createPortletContext("applicationName",
"portletName");
+ fail("'applicationName' is not a properly formatted application
name");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // expected
+ }
+
+ PortletContext fromId =
PortletContext.createPortletContext("/applicationName.portletName");
+
+ context = PortletContext.createPortletContext("applicationName",
"portletName", true);
+ assertEquals("/applicationName", context.getApplicationName());
+ assertEquals("portletName", context.getPortletName());
+ assertEquals("/applicationName.portletName", context.getId());
+ assertEquals(context, fromId);
+
+ context = PortletContext.createPortletContext("/applicationName",
"portletName");
+ assertEquals("/applicationName", context.getApplicationName());
+ assertEquals("portletName", context.getPortletName());
+ assertEquals("/applicationName.portletName", context.getId());
+ assertEquals(context, fromId);
+ }
+
public void testCreateFromNullOrEmpty()
{
try