cmlenz 2003/06/10 04:22:50
Modified: integration/ant/src/java/org/apache/cactus/integration/ant
CactifyWarTask.java
Log:
Some refactorings before putting the "add-features hat" back on... not perfect, but
better
Revision Changes Path
1.18 +167 -156
jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/CactifyWarTask.java
Index: CactifyWarTask.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/CactifyWarTask.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- CactifyWarTask.java 10 Jun 2003 09:21:38 -0000 1.17
+++ CactifyWarTask.java 10 Jun 2003 11:22:50 -0000 1.18
@@ -128,9 +128,9 @@
// Inner Classes -----------------------------------------------------------
/**
- * Class for nested <code><redirector></code> elements.
+ * Abstract base class for nested redirector elements.
*/
- public static class Redirector
+ public abstract static class Redirector
{
// Instance Variables --------------------------------------------------
@@ -138,30 +138,31 @@
/**
* The name of the redirector.
*/
- private String name;
+ protected String name;
/**
* The URL pattern that the redirector will be mapped to.
*/
- private String mapping;
+ protected String mapping;
/**
* Comma-separated list of role names that should be granted access to
* the redirector.
*/
- private String roles;
+ protected String roles;
- // Public Methods ------------------------------------------------------
+ // Abstract Methods ----------------------------------------------------
/**
- * Returns the name of the redirector.
+ * Merges the definition of the redirector into the provided deployment
+ * descriptor.
*
- * @return The name
+ * @param theWebXml The deployment descriptor into which the redirector
+ * definition should be merged
*/
- public String getName()
- {
- return this.name;
- }
+ public abstract void mergeInto(WebXml theWebXml);
+
+ // Public Methods ------------------------------------------------------
/**
* Sets the name of the redirector.
@@ -174,16 +175,6 @@
}
/**
- * Returns the URL pattern the redirector will be mapped to.
- *
- * @return The URL pattern
- */
- public String getMapping()
- {
- return this.mapping;
- }
-
- /**
* Sets the URL pattern that the redirector should be mapped to.
*
* @param theMapping The URL pattern to set
@@ -194,27 +185,130 @@
}
/**
- * Returns the comma-separated list of role names that should be granted
+ * Sets the comma-separated list of role names that should be granted
* access to the redirector.
*
- * @return The roles in a comma-separated list
+ * @param theRoles The roles to set
*/
- public String getRoles()
+ public void setRoles(String theRoles)
{
- return this.roles;
+ this.roles = theRoles;
}
+ // Protected Methods ---------------------------------------------------
+
/**
- * Sets the comma-separated list of role names that should be granted
- * access to the redirector.
+ * Adds the comma-separated list of security roles to a deployment
+ * descriptor.
*
- * @param theRoles The roles to set
+ * @param theWebXml The deployment descriptor
*/
- public void setRoles(String theRoles)
+ protected void addRoles(WebXml theWebXml)
{
- this.roles = theRoles;
+ StringTokenizer tokenizer = new StringTokenizer(this.roles, ",");
+ while (tokenizer.hasMoreTokens())
+ {
+ String role = tokenizer.nextToken().trim();
+ if (!theWebXml.hasSecurityRole(role))
+ {
+ theWebXml.addSecurityRole(role);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Implementation of <code>Redirector</code> for filter test redirectors.
+ */
+ public static final class FilterRedirector extends Redirector
+ {
+
+ /**
+ * Default constructor.
+ */
+ public FilterRedirector()
+ {
+ this.name = "FilterRedirector";
+ this.mapping = DEFAULT_FILTER_REDIRECTOR_MAPPING;
+ }
+
+ /**
+ * @see CactifyWarTask.Redirector#mergeInto
+ */
+ public void mergeInto(WebXml theWebXml)
+ {
+ if (WebXmlVersion.V2_3.compareTo(theWebXml.getVersion()) <= 0)
+ {
+ theWebXml.addFilter(this.name, FILTER_REDIRECTOR_CLASS);
+ theWebXml.addFilterMapping(this.name, this.mapping);
+ if (this.roles != null)
+ {
+ addRoles(theWebXml);
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Implementation of <code>Redirector</code> for JSP test redirectors.
+ */
+ public static final class JspRedirector extends Redirector
+ {
+
+ /**
+ * Default constructor.
+ */
+ public JspRedirector()
+ {
+ this.name = "JspRedirector";
+ this.mapping = DEFAULT_JSP_REDIRECTOR_MAPPING;
+ }
+
+ /**
+ * @see CactifyWarTask.Redirector#mergeInto
+ */
+ public void mergeInto(WebXml theWebXml)
+ {
+ theWebXml.addJspFile(this.name, "/jspRedirector.jsp");
+ theWebXml.addServletMapping(this.name, this.mapping);
+ if (this.roles != null)
+ {
+ addRoles(theWebXml);
+ }
}
+
+ }
+ /**
+ * Implementation of <code>Redirector</code> for servlet test redirectors.
+ */
+ public static final class ServletRedirector extends Redirector
+ {
+
+ /**
+ * Default constructor.
+ */
+ public ServletRedirector()
+ {
+ this.name = "ServletRedirector";
+ this.mapping = DEFAULT_SERVLET_REDIRECTOR_MAPPING;
+ }
+
+ /**
+ * @see CactifyWarTask.Redirector#mergeInto
+ */
+ public void mergeInto(WebXml theWebXml)
+ {
+ theWebXml.addServlet(this.name, SERVLET_REDIRECTOR_CLASS);
+ theWebXml.addServletMapping(this.name, this.mapping);
+ if (this.roles != null)
+ {
+ addRoles(theWebXml);
+ }
+ }
+
}
/**
@@ -247,19 +341,9 @@
private File mergeWebXml;
/**
- * The Cactus filter redirector.
+ * The Cactus test redirectors.
*/
- private List filterRedirectors = new ArrayList();
-
- /**
- * The Cactus JSP redirector.
- */
- private List jspRedirectors = new ArrayList();
-
- /**
- * The Cactus servlet redirector.
- */
- private List servletRedirectors = new ArrayList();
+ private List redirectors = new ArrayList();
/**
* For resolving entities such as DTDs.
@@ -336,33 +420,33 @@
}
/**
- * Adds the Cactus filter test redirector (only one permitted).
+ * Adds a Cactus filter test redirector.
*
* @param theFilterRedirector The redirector to add
*/
- public void addFilterRedirector(Redirector theFilterRedirector)
+ public void addFilterRedirector(FilterRedirector theFilterRedirector)
{
- this.filterRedirectors.add(theFilterRedirector);
+ this.redirectors.add(theFilterRedirector);
}
/**
- * Adds the Cactus JSP test redirector (only one permitted).
+ * Adds a Cactus JSP test redirector.
*
* @param theJspRedirector The redirector to add
*/
- public void addJspRedirector(Redirector theJspRedirector)
+ public void addJspRedirector(JspRedirector theJspRedirector)
{
- this.jspRedirectors.add(theJspRedirector);
+ this.redirectors.add(theJspRedirector);
}
/**
- * Adds the Cactus servlet test redirector (only one permitted).
+ * Adds a Cactus servlet test redirector.
*
* @param theServletRedirector The redirector to add
*/
- public void addServletRedirector(Redirector theServletRedirector)
+ public void addServletRedirector(ServletRedirector theServletRedirector)
{
- this.servletRedirectors.add(theServletRedirector);
+ this.redirectors.add(theServletRedirector);
}
/**
@@ -613,107 +697,53 @@
{
WebXml webXml = WebXmlIo.newWebXml(theWebXml.getVersion());
- if (WebXmlVersion.V2_3.compareTo(webXml.getVersion()) <= 0)
+ boolean filterRedirectorDefined = false;
+ boolean jspRedirectorDefined = false;
+ boolean servletRedirectorDefined = false;
+
+ // add the user defined redirectors
+ for (Iterator i = this.redirectors.iterator(); i.hasNext();)
{
- // Add the filter redirector
- if (!this.filterRedirectors.isEmpty())
+ Redirector redirector = (Redirector) i.next();
+ if (redirector instanceof FilterRedirector)
{
- Iterator i = this.filterRedirectors.iterator();
- while (i.hasNext())
- {
- Redirector redirector = (Redirector) i.next();
- String name = redirector.getName();
- if (name == null)
- {
- name = "FilterRedirector";
- }
- String mapping = redirector.getMapping();
- if (mapping == null)
- {
- mapping = DEFAULT_FILTER_REDIRECTOR_MAPPING;
- }
- webXml.addFilter(name, FILTER_REDIRECTOR_CLASS);
- webXml.addFilterMapping(name, mapping);
- if (redirector.getRoles() != null)
- {
- addRoles(webXml, redirector.getRoles());
- }
- }
+ filterRedirectorDefined = true;
}
- else
+ else if (redirector instanceof JspRedirector)
{
- // add the default filter redirector
- webXml.addFilter("FilterRedirector",
- FILTER_REDIRECTOR_CLASS);
- webXml.addFilterMapping("FilterRedirector",
- DEFAULT_FILTER_REDIRECTOR_MAPPING);
+ jspRedirectorDefined = true;
}
- }
-
- // Add the JSP redirector
- if (!this.jspRedirectors.isEmpty())
- {
- Iterator i = this.jspRedirectors.iterator();
- while (i.hasNext())
+ else if (redirector instanceof ServletRedirector)
{
- Redirector redirector = (Redirector) i.next();
- String name = redirector.getName();
- if (name == null)
- {
- name = "JspRedirector";
- }
- String mapping = redirector.getMapping();
- if (mapping == null)
- {
- mapping = DEFAULT_JSP_REDIRECTOR_MAPPING;
- }
- webXml.addJspFile(name, "/jspRedirector.jsp");
- webXml.addServletMapping(name, mapping);
- if (redirector.getRoles() != null)
- {
- addRoles(webXml, redirector.getRoles());
- }
+ servletRedirectorDefined = true;
}
- }
- else
- {
- webXml.addJspFile("JspRedirector", "/jspRedirector.jsp");
- webXml.addServletMapping("JspRedirector",
- DEFAULT_JSP_REDIRECTOR_MAPPING);
+ redirector.mergeInto(webXml);
}
- // Add the servlet redirector
- if (!this.servletRedirectors.isEmpty())
+ // now add the default redirectors if they haven't been provided by
+ // the user
+ if (!filterRedirectorDefined
+ && (WebXmlVersion.V2_3.compareTo(webXml.getVersion()) <= 0))
{
- Iterator i = this.servletRedirectors.iterator();
- while (i.hasNext())
- {
- Redirector redirector = (Redirector) i.next();
- String name = redirector.getName();
- if (name == null)
- {
- name = "ServletRedirector";
- }
- String mapping = redirector.getMapping();
- if (mapping == null)
- {
- mapping = DEFAULT_SERVLET_REDIRECTOR_MAPPING;
- }
- webXml.addServlet(name, SERVLET_REDIRECTOR_CLASS);
- webXml.addServletMapping(name, mapping);
- if (redirector.getRoles() != null)
- {
- addRoles(webXml, redirector.getRoles());
- }
- }
+ // add the default filter redirector
+ webXml.addFilter("FilterRedirector",
+ FILTER_REDIRECTOR_CLASS);
+ webXml.addFilterMapping("FilterRedirector",
+ DEFAULT_FILTER_REDIRECTOR_MAPPING);
}
- else
+ if (!servletRedirectorDefined)
{
webXml.addServlet("ServletRedirector",
SERVLET_REDIRECTOR_CLASS);
webXml.addServletMapping("ServletRedirector",
DEFAULT_SERVLET_REDIRECTOR_MAPPING);
}
+ if (!jspRedirectorDefined)
+ {
+ webXml.addJspFile("JspRedirector", "/jspRedirector.jsp");
+ webXml.addServletMapping("JspRedirector",
+ DEFAULT_JSP_REDIRECTOR_MAPPING);
+ }
return webXml;
}
@@ -721,25 +751,6 @@
{
throw new BuildException(
"Could not parse deployment descriptor", e);
- }
- }
-
- /**
- * Adds a comma-separated list of security roles to a deployment descriptor.
- *
- * @param theWebXml The deployment descriptor
- * @param theRoles The comma-separated list of role names
- */
- private void addRoles(WebXml theWebXml, String theRoles)
- {
- StringTokenizer tokenizer = new StringTokenizer(theRoles, ",");
- while (tokenizer.hasMoreTokens())
- {
- String role = tokenizer.nextToken().trim();
- if (!theWebXml.hasSecurityRole(role))
- {
- theWebXml.addSecurityRole(role);
- }
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]