cactus-dev@jakarta.apache.org
[Top] [All Lists]

Migrating cactus to Maven2: how about decoupling the 'share-12-13-14' an

Subject: Migrating cactus to Maven2: how about decoupling the 'share-12-13-14' and 'j2ee-1x' components?
From: Zhong ZHENG
Date: Fri, 23 Sep 2005 19:24:40 +0200
Hi cactus team,

I checked out the cactus source from the branch '1.7_CARGO', and tried to
migrate the framework subproject to maven 2 (without the EJB, jetty and
servlet samples, documentations, integration, and the aspectj code for
logging). I saw in your mailing archive that you are also planning to use
maven 2 for cactus, so I think this may interest you.

Here is my question in details:

During the migration, the most difficult thing is that there are
cross-dependencies among different components: share-12-13-14 depends on
share-13-14 as well as j2ee-1x , while j2ee-1x depends on share-12-13-14 and
share-13-14: the share-xxx component needs an implementation of, for
example, AbstractHttpServletRequestWrapper, while such implementation, which
varies depending on the servlet spec version, is provided in the
j2ee-1xcomponent.

So I wondered, why didn't you use a factory to create those wrapper
instances, and thus get rid of those cross-dependencies?

I wrote a factory class for share-12-13-14, which creates the concrete
wrapper instances for HttpServletRequestWrapper, ServletContextWrapper,
ServletConfigWrapper and PageContextWrapper via java reflection. I also
replaced the following entries in share-12-13-14:

Those entries are..........replaced by those ones.
HttpServletRequestWrapper AbstractHttpServletWrapper
ServletContextWrapper AbstractServletContextWrapper
ServletConfigWrapper AbstractServletConfigWrapper
PageContextWrapper AbstractPageContextWrapper

In this way, the shared-12-13-14 component is completely decoupled from the
other components, and it does no longer need to know the name of the
implementation class for abstract wrappers: the implementations are
configurable.

After decoupled the components, I successfully migrated cactus framework to
maven 2. I tested the migration and everything goes on well.

But I am still not sure if I did things right. Maybe you have some strong
reasons not doing what I have done... So I would like to list the source
code of my factory class below, and listen to your opinions.

Thanks in advance.

/** ServerSideWrapperFactory.java */
package org.apache.cactus.server.factory;

import java.lang.reflect.Constructor;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;

import org.apache.cactus.ServletURL;
import org.apache.cactus.server.AbstractHttpServletRequestWrapper;
import org.apache.cactus.server.AbstractPageContextWrapper;
import org.apache.cactus.server.AbstractServletConfigWrapper;
import org.apache.cactus.server.AbstractServletContextWrapper;
import org.apache.cactus.util.ChainedRuntimeException;

public class ServerSideWrapperFactory {

 private static final String PAGE_CONTEXT_WRAPPER_IMPL =
 "cactus.wrapper.impl.PageContextWrapper";

 private static final String DEFAULT_PAGE_CONTEXT_WRAPPER_IMPL =
 "org.apache.cactus.server.PageContextWrapper";

 private static final String SERVLET_CONFIG_WRAPPER_IMPL =
 "cactus.wrapper.impl.ServletConfigWrapper";

 private static final String DEFAULT_SERVLET_CONFIG_WRAPPER_IMPL =
 "org.apache.cactus.server.ServletConfigWrapper";

 private static final String SERVLET_CONTEXT_WRAPPER_IMPL =
 "cactus.wrapper.impl.ServletContextWrapper";

 private static final String DEFAULT_SERVLET_CONTEXT_WRAPPER_IMPL =
 "org.apache.cactus.server.ServletContextWrapper";

 private static final String HTTP_SERVLET_REQUEST_WRAPPER_IMPL =
 "cactus.wrapper.impl.HttpServletRequestWrapper";

 private static final String DEFAULT_HTTP_SERVLET_REQUEST_WRAPPER_IMPL =
 "org.apache.cactus.server.HttpServletRequestWrapper";


 /** Singleton factory instance. */
 private static final ServerSideWrapperFactory FACTORY =
 new ServerSideWrapperFactory();

 private ServerSideWrapperFactory() {
 // Do nothing.
 }

 public static ServerSideWrapperFactory getFactory() {
 return FACTORY;
 }

 // Public Factory Methods
--------------------------------------------------

 public AbstractPageContextWrapper createPageContextWrapper(
 PageContext originalContext,
 ServletURL servletUrl)
 throws ChainedRuntimeException {
 String implClassName = System.getProperty(PAGE_CONTEXT_WRAPPER_IMPL);
 if (implClassName == null || implClassName.trim().equals("")) {
 implClassName = DEFAULT_PAGE_CONTEXT_WRAPPER_IMPL;
 }
 AbstractPageContextWrapper wrapper = null;
 try {
 Class clazz = Class.forName(implClassName);
 Object[] args = new Object[] { originalContext, servletUrl };
 Constructor constructor = clazz.getConstructor(new Class[] {
 PageContext.class, ServletURL.class } );
 wrapper = (AbstractPageContextWrapper)
 constructor.newInstance(args);
 } catch (Throwable th) {
 throw new ChainedRuntimeException("Failed to create "
 + "JSP page context wrapper for Cactus.", th);
 }
 return wrapper;
 }


 public AbstractServletConfigWrapper createServletConfigWrapper(
 ServletConfig originalConfig)
 throws ChainedRuntimeException {
 String implClassName = System.getProperty(SERVLET_CONFIG_WRAPPER_IMPL);
 if (implClassName == null || implClassName.trim().equals("")) {
 implClassName = DEFAULT_SERVLET_CONFIG_WRAPPER_IMPL;
 }
 AbstractServletConfigWrapper wrapper = null;
 try {
 Class clazz = Class.forName(implClassName);
 Object[] args = new Object[] { originalConfig };
 Constructor constructor = clazz.getConstructor(
 new Class[] { ServletConfig.class} );
 wrapper = (AbstractServletConfigWrapper)
 constructor.newInstance(args);
 } catch (Throwable th) {
 throw new ChainedRuntimeException("Failed to create "
 + "servlet config wrapper for Cactus.", th);
 }
 return wrapper;
 }


 public AbstractServletContextWrapper createServletContextWrapper(
 ServletContext originalContext)
 throws ChainedRuntimeException {
 String implClassName = System.getProperty(SERVLET_CONTEXT_WRAPPER_IMPL);
 if (implClassName == null || implClassName.trim().equals("")) {
 implClassName = DEFAULT_SERVLET_CONTEXT_WRAPPER_IMPL;
 }
 AbstractServletContextWrapper wrapper = null;
 try {
 Class clazz = Class.forName(implClassName);
 Object[] args = new Object[] { originalContext };
 Constructor constructor = clazz.getConstructor(
 new Class[] { ServletContext.class} );
 wrapper = (AbstractServletContextWrapper)
 constructor.newInstance(args);
 } catch (Throwable th) {
 throw new ChainedRuntimeException("Failed to create "
 + "servlet context wrapper for Cactus.", th);
 }
 return wrapper;
 }


 public AbstractHttpServletRequestWrapper createHttpServletRequestWrapper(
 HttpServletRequest originalRequest,
 ServletURL servletUrl)
 throws ChainedRuntimeException {
 String implClassName = System.getProperty(
 HTTP_SERVLET_REQUEST_WRAPPER_IMPL);
 if (implClassName == null || implClassName.trim().equals("")) {
 implClassName = DEFAULT_HTTP_SERVLET_REQUEST_WRAPPER_IMPL;
 }
 AbstractHttpServletRequestWrapper wrapper = null;
 try {
 Class clazz = Class.forName(implClassName);
 Object[] args = new Object[] { originalRequest, servletUrl };
 Constructor constructor = clazz.getConstructor(new Class[] {
 HttpServletRequest.class,
 ServletURL.class } );
 wrapper = (AbstractHttpServletRequestWrapper)
 constructor.newInstance(args);
 } catch (Throwable th) {
 throw new ChainedRuntimeException("Failed to create "
 + "HTTP servlet request wrapper for Cactus.", th);
 }
 return wrapper;
 }


}


Best Regards.

--

ZHENG Zhong

1 Avenue Alphand
75116 Paris, France
+33 6 76 80 45 90

http://heavyz.sourceforge.net
http://heavyz.blogspot.com
http://spaces.msn.com/members/zhengzhong
<Prev in Thread] Current Thread [Next in Thread>
  • Migrating cactus to Maven2: how about decoupling the 'share-12-13-14' and 'j2ee-1x' components?, Zhong ZHENG <=