empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r683198 [8/8] - in /incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext: ./ .settings/ src/ src/META-INF/ src/org/ src/org/apache/ src/org/apache/empire/ src/org/apache/empire/struts2/ src/org/apache/empire/struts2/action/ src/org/apac...
Date Wed, 06 Aug 2008 09:34:45 GMT
Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/EmpireStrutsDispatcher.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/EmpireStrutsDispatcher.java?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/EmpireStrutsDispatcher.java
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/EmpireStrutsDispatcher.java
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,240 @@
+package org.apache.empire.struts2.web;
+
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.empire.struts2.action.Disposable;
+import org.apache.struts2.dispatcher.Dispatcher;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+
+
+public class EmpireStrutsDispatcher extends Dispatcher
+{
+    // Logger
+    protected static Log log = LogFactory.getLog(EmpireStrutsDispatcher.class);
+
+    private static final ThreadLocal<Object> currentRequest = new ThreadLocal<Object>();
+
+    public final String APPLICATION_CLASS = "ApplicationClass";
+    public final String SESSION_CLASS     = "SessionClass";
+    public final String REQUEST_CLASS     = "RequestClass";
+
+    private String sessionClassName = null;
+    private String requestClassName = null;
+    
+    private boolean logRequestWarning = true;
+    
+    // Accessor for Current Request
+    public static Object getCurrentRequest()
+    {
+        return currentRequest.get();
+    }
+    
+    public EmpireStrutsDispatcher(ServletContext servletContext, Map<String, String>
initParams)
+    {
+        super(servletContext, initParams);
+        // Create Applicaton Object (if specified)
+        String appClassName = initParams.get( APPLICATION_CLASS );
+        if (appClassName!=null)
+        {
+            createApplicationObject( servletContext, appClassName);
+        }   
+        
+        // remember Session Class Name
+        sessionClassName = initParams.get( SESSION_CLASS );
+        requestClassName = initParams.get( REQUEST_CLASS );
+
+        // if no request class is specified use default
+        if (requestClassName==null || requestClassName.length()==0)
+            requestClassName = DefaultWebRequest.class.getName();
+    }
+    
+    @Override
+    public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext
context,
+            ActionMapping mapping) throws ServletException
+    {
+        try {
+            // Create Applicaton Class
+            if (sessionClassName!=null)
+            {
+                Object applObj = context.getAttribute( WebApplication.APPLICATION_NAME );
+                createSessionObject(request, applObj);
+            }
+            // Create Applicaton Class
+            boolean continueProcessing = true; 
+            if (requestClassName!=null)
+            {
+                Object sessObj = request.getSession().getAttribute( WebSession.SESSION_NAME
);
+                continueProcessing = createRequestObject(request, response, sessObj);
+            }
+            // Call Default
+            if (continueProcessing)
+            {
+                log.debug("calling Struts for request processing");
+                super.serviceAction(request, response, context, mapping);
+            }
+          
+        } catch (Exception e) {
+            
+            log.error("Failed to process request due to Exception.", e);
+
+        } finally {
+            // cleanup
+            log.debug("cleanung up request");
+            // Dispose action first
+            int exitCode = disposeAction(request);
+            // Call Exit on Request
+            Object reqObj = currentRequest.get();
+            if (reqObj!=null)
+            {
+                if (reqObj instanceof WebRequest)
+                {
+                    ((WebRequest)reqObj).exit(exitCode);
+                }
+                // Clear Thread local
+                currentRequest.set( null );
+            }
+        }
+    }
+
+    /*
+    @Override
+    public HttpServletRequest wrapRequest(HttpServletRequest request, ServletContext servletContext)
+        throws IOException
+    {
+        HttpServletRequest wrappedRequest = super.wrapRequest(request, servletContext);
+        if (wrappedRequest instanceof MultiPartRequestWrapper)
+        {
+            boolean hasError = ((MultiPartRequestWrapper)wrappedRequest).hasErrors();
+            if (hasError)
+            {   // Multipart-Error!   
+                log.error("Multipart-Request has errors. Use normal Wrapper");
+                return new StrutsRequestWrapper(request);
+            }
+        }
+        return wrappedRequest;
+    }
+    */
+
+    private int disposeAction(HttpServletRequest request)
+    {
+        int exitCode = 0;
+        // Is the action still the?
+        Object action = request.getAttribute("action");
+        if (action!=null)
+        {
+            if (action instanceof Disposable)
+            {   // Call dispose and return exitCode
+                exitCode = ((Disposable)action).dispose();
+            }
+            else
+            {   // Disposible interface not implemented
+                log.warn("Cannot dispose action. Action does not implement the Disposible
interface)");
+            }
+            // Cleanup the "action" Attribute on the HttpServletRequest
+            request.setAttribute("action", null);
+        }
+        else
+        {
+            log.warn("Cannot dispose action. Action is not supplied on the request. (Missing
ActionBasicsInterceptor?)");
+        }
+        // Exit Code
+        return exitCode;
+    }
+    
+    private Object createObject(String className)
+        throws ClassNotFoundException, IllegalAccessException, InstantiationException
+    {
+        // Create Instance for Object  
+        Class c = Class.forName(className);
+        return c.newInstance();
+    }
+
+    protected void createApplicationObject(ServletContext servletContext, String appClassName)
+    {
+        try {
+            // Create Applicaton Class  
+            if (servletContext.getAttribute( WebApplication.APPLICATION_NAME )==null)
+            {
+                Object app = createObject( appClassName );
+                if (app instanceof WebApplication)
+                {
+                    ((WebApplication)app).init( servletContext );
+                }
+                else
+                    log.warn("Application object does not implement IWebApplication!");
+                // Store it
+                servletContext.setAttribute( WebApplication.APPLICATION_NAME, app);
+            }    
+        } catch(Exception e) {
+            log.error("Failed to create web application object of type " + appClassName +
" --> " + e.getMessage());
+        }
+    }
+    
+    protected void createSessionObject(HttpServletRequest request, Object applObj)
+    {
+        try {
+            // createSessionObject
+            HttpSession httpSession = request.getSession(false);
+            if (httpSession==null ||  httpSession.getAttribute( WebSession.SESSION_NAME)==null)
+            {
+                Object session = createObject(sessionClassName);
+                httpSession = request.getSession(true);
+                // Call init() if Session Object implements IWebSession 
+                if (session instanceof WebSession)
+                {
+                    ((WebSession)session).init( httpSession, applObj );
+                }
+                else
+                    log.warn("Session object does not implement IWebSession!");
+                // Save Session
+                httpSession.setAttribute( WebSession.SESSION_NAME , session);
+            }
+        } catch(Exception e) {
+            // Error  
+            log.error("Failed to create session [" + sessionClassName + "] msg= " + e.getMessage());
+        }    
+    }
+
+    protected boolean createRequestObject(HttpServletRequest request, HttpServletResponse
response, Object sessObj)
+    {
+        try {
+            // createSessionObject
+            Object reqObj = request.getAttribute( WebRequest.REQUEST_NAME );
+            if (reqObj==null)
+            {
+                // Create Request
+                reqObj = createObject( requestClassName );
+                // Store Request in Thread Local variable
+                currentRequest.set( reqObj );
+                // Call init() if Request Object implements IWebRequest 
+                if (reqObj instanceof WebRequest)
+                {
+                    return ((WebRequest)reqObj).init( request, response, sessObj );
+                }
+                else if (logRequestWarning)
+                {
+                    log.warn("Request object does not implement IWebRequest!");
+                    logRequestWarning = false;
+                }
+            }
+            // continue processing
+            return true;
+
+        } catch(Exception e) {
+            // Error  
+            log.error("Failed to create request [" + requestClassName + "] msg= " + e.getMessage());
+            requestClassName = null; // don't try again
+            // continue processing
+            return true;
+        }    
+    }
+    
+}

Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebApplication.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebApplication.java?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebApplication.java
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebApplication.java
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,10 @@
+package org.apache.empire.struts2.web;
+
+import javax.servlet.ServletContext;
+
+public interface WebApplication
+{
+    public final String APPLICATION_NAME  = "webApp";
+
+    void init(ServletContext servletContext);
+}

Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebErrors.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebErrors.java?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebErrors.java
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebErrors.java
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,26 @@
+/*
+ * ESTEAM Software GmbH, 22.07.2007
+ */
+package org.apache.empire.struts2.web;
+
+import org.apache.empire.commons.ErrorType;
+
+public class WebErrors
+{
+    // general 
+    public static final ErrorType InvalidFormData = new ErrorType("error.web.recordsDontMatch",
"The form submitted is invalid for the current context.");
+    public static final ErrorType ColumnNotFound  = new ErrorType("error.web.columnNotFound",
  "The column {0} has not been found!");
+    public static final ErrorType FieldError      = new ErrorType("error.web.fieldError",
"Field {0}: {1}");
+
+    // Parsing errors
+    public static final ErrorType InputInvalidValue    = new ErrorType("error.web.input.invalidValue",
"Invalid Field value");
+    public static final ErrorType InputNoIntegerFormat = new ErrorType("error.web.input.integerFormat",
"Invalid Integer format");
+    public static final ErrorType InputNoNumberFormat  = new ErrorType("error.web.input.numberFormat",
"Invalid number format");
+    public static final ErrorType InputNoDateFormat    = new ErrorType("error.web.input.dateFormat",
"Invalid date format");
+    public static final ErrorType InputTimeMissing     = new ErrorType("error.web.input.date.notime",
"No time specified.");
+
+    // Validation errors
+    public static final ErrorType InputValueRequired   = new ErrorType("error.web.input.required",
  "Field is required"); 
+    public static final ErrorType InputValueOutOfRange = new ErrorType("error.web.input.outOfRange",
"The value must be in the range of {0} and {1}.");
+    public static final ErrorType InputTextTooShort    = new ErrorType("error.web.input.textTooShort",
 "The value must contain at least {0} characters."); 
+}

Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebRequest.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebRequest.java?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebRequest.java
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebRequest.java
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,47 @@
+package org.apache.empire.struts2.web;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public interface WebRequest
+{    
+    /**
+     * This String is the attribute name of this request object on the applications request
scope
+     */
+    public final String REQUEST_NAME  = "webRequest";
+    
+    /**
+     * Initializes the request object
+     * This method is called from the EmpiretrutsDispatcher
+     *  
+     * @param request the HttpServletRequest
+     * @param response the HttpServletResponse
+     * @param session the sessionObject
+     * 
+     * @return true if the request should continue processing or false otherwise
+     */
+    boolean init(HttpServletRequest request, HttpServletResponse response, Object session);
+
+    /**
+     * returns the current HttpRequestObject
+     * @return the httpServletRequest
+     */
+    public HttpServletRequest getHttpRequest();
+
+    /**
+     * returns the current HttpResponseObject
+     * @return the httpServletResponse
+     */
+    public HttpServletResponse getHttpResponse();
+    
+    /**
+     * This function is called from the EmpireStrutsDispatcher when a request ends
+     * if an action was accociated with the request and the action implements the Disposible
interface
+     * then the exit code returned by Disposible.dispose() is passed with the exitCode parameter
+     * 
+     * You might use the exitCode e.g. to commit or rollback a transaction on the JDBC-Connection
+     * 
+     * @param exitCode
+     */
+    void exit(int exitCode);
+}

Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebSession.java
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebSession.java?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebSession.java
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/WebSession.java
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,10 @@
+package org.apache.empire.struts2.web;
+
+import javax.servlet.http.HttpSession;
+
+public interface WebSession
+{
+    public final String SESSION_NAME  = "webSession";
+
+    void init(HttpSession httpSession, Object application);
+}

Added: incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/package.html
URL: http://svn.apache.org/viewvc/incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/package.html?rev=683198&view=auto
==============================================================================
--- incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/package.html
(added)
+++ incubator/empire-db/trunk/struts2-ext/Empire-struts2-ext/src/org/apache/empire/struts2/web/package.html
Wed Aug  6 02:34:41 2008
@@ -0,0 +1,14 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+<!--
+/*
+ * ESTEAM Software GmbH, 12.12.2007
+ */
+-->
+</head>
+<body>
+
+
+
+</body></html>
\ No newline at end of file



Mime
View raw message