cmlenz 2003/06/10 02:20:32
Modified: integration/ant/src/java/org/apache/cactus/integration/ant/deployment
WebXmlTag.java WebXmlMerger.java WebXml.java
integration/ant/src/test/org/apache/cactus/integration/ant/deployment
TestWebXml.java TestWebXmlMerger.java
documentation/docs/xdocs/integration/ant
task_webxmlmerge.xml
Log:
Add better support for security roles to WebXml.
Implement duplicate checking when merge security roles.
Revision Changes Path
1.3 +7 -1
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlTag.java
Index: WebXmlTag.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlTag.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WebXmlTag.java 26 May 2003 09:52:38 -0000 1.2
+++ WebXmlTag.java 10 Jun 2003 09:20:31 -0000 1.3
@@ -245,6 +245,12 @@
new WebXmlTag("security-role");
/**
+ * Element name 'role-name',
+ */
+ public static final WebXmlTag ROLE_NAME =
+ new WebXmlTag("role-name");
+
+ /**
* Element name 'env-entry',
*/
public static final WebXmlTag ENV_ENTRY =
1.4 +11 -2
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java
Index: WebXmlMerger.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXmlMerger.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- WebXmlMerger.java 15 May 2003 21:55:51 -0000 1.3
+++ WebXmlMerger.java 10 Jun 2003 09:20:32 -0000 1.4
@@ -329,7 +329,16 @@
*/
protected final void mergeSecurityRoles(WebXml theWebXml)
{
- int count = insertElements(theWebXml, WebXmlTag.SECURITY_ROLE);
+ Iterator securityRoleNames = theWebXml.getSecurityRoleNames();
+ int count = 0;
+ while (securityRoleNames.hasNext())
+ {
+ String securityRoleName = (String) securityRoleNames.next();
+ if (!this.webXml.hasSecurityRole(securityRoleName))
+ {
+ this.webXml.addSecurityRole(securityRoleName);
+ }
+ }
if (count > 0)
{
this.log.trace("Merged " + count + " security role"
1.3 +87 -2
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java
Index: WebXml.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/WebXml.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- WebXml.java 14 May 2003 10:56:45 -0000 1.2
+++ WebXml.java 10 Jun 2003 09:20:32 -0000 1.3
@@ -445,7 +445,7 @@
{
throw new NullPointerException();
}
- if (hasFilter(theServletName))
+ if (hasServlet(theServletName))
{
throw new IllegalStateException("Servlet '" + theServletName
+ "' already defined");
@@ -703,6 +703,91 @@
public final boolean hasServlet(String theServletName)
{
return (getServlet(theServletName) != null);
+ }
+
+ /**
+ * Adds a new security role to the descriptor.
+ *
+ * @param theRoleName The name of the role to add
+ */
+ public final void addSecurityRole(String theRoleName)
+ {
+ if (theRoleName == null)
+ {
+ throw new NullPointerException();
+ }
+ if (hasSecurityRole(theRoleName))
+ {
+ throw new IllegalStateException("Security role '" + theRoleName
+ + "' already defined");
+ }
+ Element securityRoleElement =
+ this.document.createElement(WebXmlTag.SECURITY_ROLE.getTagName());
+ securityRoleElement.appendChild(
+ createNestedText(WebXmlTag.ROLE_NAME, theRoleName));
+ addElement(WebXmlTag.SECURITY_ROLE, securityRoleElement);
+ }
+
+ /**
+ * Returns the element that contains the specified security role, or
+ * <code>null</code> if the role is not defined in the descriptor.
+ *
+ * @param theRoleName The name of the role
+ * @return The DOM element representing the security role
+ */
+ public final Element getSecurityRole(String theRoleName)
+ {
+ if (theRoleName == null)
+ {
+ throw new NullPointerException();
+ }
+ Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE);
+ while (securityRoleElements.hasNext())
+ {
+ Element securityRoleElement = (Element) securityRoleElements.next();
+ if (theRoleName.equals(getNestedText(
+ securityRoleElement, WebXmlTag.ROLE_NAME)))
+ {
+ return securityRoleElement;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns a list of the security role names defined in the deployment
+ * descriptor
+ *
+ * @return An iterator over the list of security role names, or an empty
+ * iterator if no security roles are defined in the descriptor
+ */
+ public final Iterator getSecurityRoleNames()
+ {
+ List securityRoleNames = new ArrayList();
+ Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE);
+ while (securityRoleElements.hasNext())
+ {
+ Element securityRoleElement = (Element) securityRoleElements.next();
+ String securityRoleName =
+ getNestedText(securityRoleElement, WebXmlTag.ROLE_NAME);
+ if (securityRoleName != null)
+ {
+ securityRoleNames.add(securityRoleName);
+ }
+ }
+ return securityRoleNames.iterator();
+ }
+
+ /**
+ * Returns whether a specific security role has been defined
+ *
+ * @param theRoleName The name of the role
+ * @return <code>true</code> if the security role is defined,
+ * <code>false</code> otherwise
+ */
+ public final boolean hasSecurityRole(String theRoleName)
+ {
+ return (getSecurityRole(theRoleName) != null);
}
/**
1.3 +65 -26
jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java
Index: TestWebXml.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXml.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestWebXml.java 26 May 2003 13:27:19 -0000 1.2
+++ TestWebXml.java 10 Jun 2003 09:20:32 -0000 1.3
@@ -1129,19 +1129,19 @@
}
/**
- * Tests whether retrieving the security-role elements from an empty
- * descriptor results in an empty iterator.
+ * Tests whether checking an empty descriptor for some security roles
+ * results in <code>false</code>.
*
* @throws Exception If an unexpected error occurs
*/
- public void testGetSecurityRoleEmpty()
+ public void testHasSecurityRoleEmpty()
throws Exception
{
String xml = "<web-app></web-app>";
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
WebXml webXml = new WebXml(doc);
- Iterator securityRoles = webXml.getElements(WebXmlTag.SECURITY_ROLE);
- assertTrue(!securityRoles.hasNext());
+ assertTrue(!webXml.hasSecurityRole("someRole"));
+ assertTrue(!webXml.getSecurityRoleNames().hasNext());
}
/**
@@ -1153,15 +1153,24 @@
throws Exception
{
String xml = "<web-app>"
- + " <security-role>"
- + " <role-name>r1</role-name>"
- + " </security-role>"
+ + " <security-role>".trim()
+ + " <role-name>r1</role-name>".trim()
+ + " </security-role>".trim()
+ "</web-app>";
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
WebXml webXml = new WebXml(doc);
- Iterator securityRoles = webXml.getElements(WebXmlTag.SECURITY_ROLE);
- assertNotNull(securityRoles.next());
- assertTrue(!securityRoles.hasNext());
+ assertTrue(webXml.hasSecurityRole("r1"));
+ Element securityRoleElement = webXml.getSecurityRole("r1");
+ assertNotNull(securityRoleElement);
+ assertEquals("security-role", securityRoleElement.getNodeName());
+ assertEquals("role-name",
+ securityRoleElement.getFirstChild().getNodeName());
+ assertEquals("r1",
+ securityRoleElement.getFirstChild().getFirstChild().getNodeValue());
+ Iterator securityRoleNames = webXml.getSecurityRoleNames();
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("r1", securityRoleNames.next());
+ assertTrue(!securityRoleNames.hasNext());
}
/**
@@ -1174,23 +1183,53 @@
throws Exception
{
String xml = "<web-app>"
- + " <security-role>"
- + " <role-name>r1</role-name>"
- + " </security-role>"
- + " <security-role>"
- + " <role-name>r2</role-name>"
- + " </security-role>"
- + " <security-role>"
- + " <role-name>r3</role-name>"
- + " </security-role>"
+ + " <security-role>".trim()
+ + " <role-name>r1</role-name>".trim()
+ + " </security-role>".trim()
+ + " <security-role>".trim()
+ + " <role-name>r2</role-name>".trim()
+ + " </security-role>".trim()
+ + " <security-role>".trim()
+ + " <role-name>r3</role-name>".trim()
+ + " </security-role>".trim()
+ "</web-app>";
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
WebXml webXml = new WebXml(doc);
- Iterator securityRoles = webXml.getElements(WebXmlTag.SECURITY_ROLE);
- assertNotNull(securityRoles.next());
- assertNotNull(securityRoles.next());
- assertNotNull(securityRoles.next());
- assertTrue(!securityRoles.hasNext());
+ assertTrue(webXml.hasSecurityRole("r1"));
+ Element securityRoleElement1 = webXml.getSecurityRole("r1");
+ assertNotNull(securityRoleElement1);
+ assertEquals("security-role", securityRoleElement1.getNodeName());
+ assertEquals("role-name",
+ securityRoleElement1.getFirstChild().getNodeName());
+ assertEquals("r1",
+ securityRoleElement1.getFirstChild().getFirstChild().
+ getNodeValue());
+ assertTrue(webXml.hasSecurityRole("r2"));
+ Element securityRoleElement2 = webXml.getSecurityRole("r2");
+ assertNotNull(securityRoleElement2);
+ assertEquals("security-role", securityRoleElement2.getNodeName());
+ assertEquals("role-name",
+ securityRoleElement2.getFirstChild().getNodeName());
+ assertEquals("r2",
+ securityRoleElement2.getFirstChild().getFirstChild().
+ getNodeValue());
+ assertTrue(webXml.hasSecurityRole("r3"));
+ Element securityRoleElement3 = webXml.getSecurityRole("r3");
+ assertNotNull(securityRoleElement3);
+ assertEquals("security-role", securityRoleElement3.getNodeName());
+ assertEquals("role-name",
+ securityRoleElement3.getFirstChild().getNodeName());
+ assertEquals("r3",
+ securityRoleElement3.getFirstChild().getFirstChild().
+ getNodeValue());
+ Iterator securityRoleNames = webXml.getSecurityRoleNames();
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("r1", securityRoleNames.next());
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("r2", securityRoleNames.next());
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("r3", securityRoleNames.next());
+ assertTrue(!securityRoleNames.hasNext());
}
/**
1.4 +64 -1
jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java
Index: TestWebXmlMerger.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/test/org/apache/cactus/integration/ant/deployment/TestWebXmlMerger.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TestWebXmlMerger.java 26 May 2003 13:27:19 -0000 1.3
+++ TestWebXmlMerger.java 10 Jun 2003 09:20:32 -0000 1.4
@@ -686,6 +686,69 @@
}
/**
+ * Tests whether a single security role is correctly inserted into an empty
+ * descriptor.
+ *
+ * @throws Exception If an unexpected error occurs
+ */
+ public void testMergeSecurityRoleIntoEmptyDocument()
+ throws Exception
+ {
+ String srcXml = "<web-app></web-app>";
+ Document srcDoc =
+ builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
+ WebXml srcWebXml = new WebXml(srcDoc);
+ String mergeXml = "<web-app>"
+ + " <security-role>"
+ + " <role-name>role1</role-name>"
+ + " </security-role>"
+ + "</web-app>";
+ Document mergeDoc =
+ builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
+ WebXml mergeWebXml = new WebXml(mergeDoc);
+ WebXmlMerger merger = new WebXmlMerger(srcWebXml);
+ merger.mergeSecurityRoles(mergeWebXml);
+ Iterator securityRoleNames = srcWebXml.getSecurityRoleNames();
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("role1", securityRoleNames.next());
+ assertTrue(!securityRoleNames.hasNext());
+ }
+
+ /**
+ * Tests whether a single security role is ignored when the source
+ * descriptor already contains a role with the same name.
+ *
+ * @throws Exception If an unexpected error occurs
+ */
+ public void testMergeSecurityRoleIntoDocumentWithSameRole()
+ throws Exception
+ {
+ String srcXml = "<web-app>"
+ + " <security-role>"
+ + " <description>A role</description>"
+ + " <role-name>role1</role-name>"
+ + " </security-role>"
+ + "</web-app>";
+ Document srcDoc =
+ builder.parse(new ByteArrayInputStream(srcXml.getBytes()));
+ WebXml srcWebXml = new WebXml(srcDoc);
+ String mergeXml = "<web-app>"
+ + " <security-role>"
+ + " <role-name>role1</role-name>"
+ + " </security-role>"
+ + "</web-app>";
+ Document mergeDoc =
+ builder.parse(new ByteArrayInputStream(mergeXml.getBytes()));
+ WebXml mergeWebXml = new WebXml(mergeDoc);
+ WebXmlMerger merger = new WebXmlMerger(srcWebXml);
+ merger.mergeSecurityRoles(mergeWebXml);
+ Iterator securityRoleNames = srcWebXml.getSecurityRoleNames();
+ assertTrue(securityRoleNames.hasNext());
+ assertEquals("role1", securityRoleNames.next());
+ assertTrue(!securityRoleNames.hasNext());
+ }
+
+ /**
* Tests whether a single EJB reference is correctly inserted into an empty
* descriptor.
*
1.5 +2 -1
jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml
Index: task_webxmlmerge.xml
===================================================================
RCS file:
/home/cvs/jakarta-cactus/documentation/docs/xdocs/integration/ant/task_webxmlmerge.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- task_webxmlmerge.xml 16 May 2003 07:29:23 -0000 1.4
+++ task_webxmlmerge.xml 10 Jun 2003 09:20:32 -0000 1.5
@@ -48,7 +48,8 @@
</li>
<li>
<emphasis>Security Roles</emphasis>: Any <security-role>
- elements get inserted into the resulting descriptor verbatim.
+ elements that are not already defined in the source descriptor
+ are inserted into the resulting descriptor.
</li>
<li>
<emphasis>Environment Entries</emphasis>: Any <env-entry>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]