You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2014/09/02 09:26:04 UTC

svn commit: r1621942 - in /ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp: event/EventFactory.java view/ViewFactory.java

Author: jacopoc
Date: Tue Sep  2 07:26:03 2014
New Revision: 1621942

URL: http://svn.apache.org/r1621942
Log:
Greatly cleaned-up and simplified the EventFactory and ViewFactory classes used by the RequestHandler: they are now (mostly) immutable and thread safe.

Modified:
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java?rev=1621942&r1=1621941&r2=1621942&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java Tue Sep  2 07:26:03 2014
@@ -19,18 +19,16 @@
 package org.ofbiz.webapp.event;
 
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
 import javax.servlet.ServletContext;
 
-import javolution.util.FastMap;
-
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralRuntimeException;
 import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.webapp.control.ConfigXMLReader;
-import org.ofbiz.webapp.control.WebAppConfigurationException;
 
 /**
  * EventFactory - Event Handler Factory
@@ -39,88 +37,29 @@ public class EventFactory {
 
     public static final String module = EventFactory.class.getName();
 
-    private final URL controllerConfigURL;
-    private final ServletContext context;
-    protected Map<String, EventHandler> handlers = null;
+    private final Map<String, EventHandler> handlers = new HashMap<String, EventHandler>();
 
     public EventFactory(ServletContext context, URL controllerConfigURL) {
-        handlers = FastMap.newInstance();
-        this.controllerConfigURL = controllerConfigURL;
-        this.context = context;
-
-        // pre-load all event handlers
+        // load all the event handlers
         try {
-            this.preLoadAll();
-        } catch (EventHandlerException e) {
+            Set<Map.Entry<String,String>> handlerEntries = ConfigXMLReader.getControllerConfig(controllerConfigURL).getEventHandlerMap().entrySet();
+            if (handlerEntries != null) {
+                for (Map.Entry<String,String> handlerEntry: handlerEntries) {
+                    EventHandler handler = (EventHandler) ObjectType.getInstance(handlerEntry.getValue());
+                    handler.init(context);
+                    this.handlers.put(handlerEntry.getKey(), handler);
+                }
+            }
+        } catch (Exception e) {
             Debug.logError(e, module);
             throw new GeneralRuntimeException(e);
         }
     }
 
-    private void preLoadAll() throws EventHandlerException {
-        Set<String> handlers = null;
-        try {
-            handlers = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getEventHandlerMap().keySet();
-        } catch (WebAppConfigurationException e) {
-            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
-        }
-        if (handlers != null) {
-            for (String type: handlers) {
-                this.handlers.put(type, this.loadEventHandler(type));
-            }
-        }
-    }
-
     public EventHandler getEventHandler(String type) throws EventHandlerException {
-        // check if we are new / empty and add the default handler in
-        if (handlers.size() == 0) {
-            this.preLoadAll();
-        }
-
-        // attempt to get a pre-loaded handler
         EventHandler handler = handlers.get(type);
-
         if (handler == null) {
-            synchronized (EventHandler.class) {
-                handler = handlers.get(type);
-                if (handler == null) {
-                    handler = this.loadEventHandler(type);
-                    handlers.put(type, handler);
-                }
-            }
-            if (handler == null)
-                throw new EventHandlerException("No handler found for type: " + type);
-        }
-        return handler;
-    }
-
-    public void clear() {
-        handlers.clear();
-    }
-
-    private EventHandler loadEventHandler(String type) throws EventHandlerException {
-        EventHandler handler = null;
-        String handlerClass = null;
-        try {
-            handlerClass = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getEventHandlerMap().get(type);
-        } catch (WebAppConfigurationException e) {
-            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
-        }
-        if (handlerClass == null) {
-            throw new EventHandlerException("Unknown handler type: " + type);
-        }
-
-        try {
-            handler = (EventHandler) ObjectType.getInstance(handlerClass);
-            handler.init(context);
-        } catch (NoClassDefFoundError e) {
-            throw new EventHandlerException("No class def found for handler [" + handlerClass + "]", e);
-        } catch (ClassNotFoundException cnf) {
-            throw new EventHandlerException("Cannot load handler class [" + handlerClass + "]", cnf);
-        } catch (InstantiationException ie) {
-            throw new EventHandlerException("Cannot get instance of the handler [" + handlerClass + "]", ie);
-        } catch (IllegalAccessException iae) {
-            throw new EventHandlerException(iae.getMessage(), iae);
+            throw new EventHandlerException("No handler found for type: " + type);
         }
         return handler;
     }

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java?rev=1621942&r1=1621941&r2=1621942&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java Tue Sep  2 07:26:03 2014
@@ -19,19 +19,17 @@
 package org.ofbiz.webapp.view;
 
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
 import javax.servlet.ServletContext;
 
-import javolution.util.FastMap;
-
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralRuntimeException;
 import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.webapp.control.ConfigXMLReader;
-import org.ofbiz.webapp.control.WebAppConfigurationException;
 
 /**
  * ViewFactory - View Handler Factory
@@ -40,46 +38,29 @@ public class ViewFactory {
 
     public static final String module = ViewFactory.class.getName();
 
-    private final URL controllerConfigURL;
-    private final ServletContext context;
-    protected Map<String, ViewHandler> handlers = null;
+    private final Map<String, ViewHandler> handlers = new HashMap<String, ViewHandler>();
 
     public ViewFactory(ServletContext context, URL controllerConfigURL) {
-        this.handlers = FastMap.newInstance();
-        this.controllerConfigURL = controllerConfigURL;
-        this.context = context;
-
-        // pre-load all the view handlers
+        // load all the view handlers
         try {
-            this.preLoadAll();
-        } catch (ViewHandlerException e) {
-            Debug.logError(e, module);
-            throw new GeneralRuntimeException(e);
-        }
-    }
-
-    private void preLoadAll() throws ViewHandlerException {
-        Set<String> handlers = null;
-        try {
-            handlers = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getViewHandlerMap().keySet();
-        } catch (WebAppConfigurationException e) {
-            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
-        }
-        if (handlers != null) {
-            for (String type: handlers) {
-                this.handlers.put(type, this.loadViewHandler(type));
+            Set<Map.Entry<String,String>> handlerEntries = ConfigXMLReader.getControllerConfig(controllerConfigURL).getViewHandlerMap().entrySet();
+            if (handlerEntries != null) {
+                for (Map.Entry<String,String> handlerEntry: handlerEntries) {
+                    ViewHandler handler = (ViewHandler) ObjectType.getInstance(handlerEntry.getValue());
+                    handler.setName(handlerEntry.getKey());
+                    handler.init(context);
+                    this.handlers.put(handlerEntry.getKey(), handler);
+                }
             }
-        }
-
-        // load the "default" type
-        if (!this.handlers.containsKey("default")) {
-            try {
-                ViewHandler h = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
-                h.init(context);
-                this. handlers.put("default", h);
-            } catch (Exception e) {
-                throw new ViewHandlerException(e);
+            // load the "default" type
+            if (!this.handlers.containsKey("default")) {
+                ViewHandler defaultHandler = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
+                defaultHandler.init(context);
+                this. handlers.put("default", defaultHandler);
             }
+        } catch (Exception e) {
+            Debug.logError(e, module);
+            throw new GeneralRuntimeException(e);
         }
     }
 
@@ -87,60 +68,11 @@ public class ViewFactory {
         if (UtilValidate.isEmpty(type)) {
             type = "default";
         }
-
-        // check if we are new / empty and add the default handler in
-        if (handlers.size() == 0) {
-            this.preLoadAll();
-        }
-
         // get the view handler by type from the contextHandlers
         ViewHandler handler = handlers.get(type);
-
-        // if none found lets create it and add it in
         if (handler == null) {
-            synchronized (ViewFactory.class) {
-                handler = handlers.get(type);
-                if (handler == null) {
-                    handler = this.loadViewHandler(type);
-                    handlers.put(type, handler);
-                }
-            }
-            if (handler == null) {
-                throw new ViewHandlerException("No handler found for type: " + type);
-            }
-        }
-        return handler;
-    }
-
-    public void clear() {
-        handlers.clear();
-    }
-
-    private ViewHandler loadViewHandler(String type) throws ViewHandlerException {
-        ViewHandler handler = null;
-        String handlerClass = null;
-        try {
-            handlerClass = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getViewHandlerMap().get(type);
-        } catch (WebAppConfigurationException e) {
-            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
-        }
-        if (handlerClass == null) {
-            throw new ViewHandlerException("Unknown handler type: " + type);
+            throw new ViewHandlerException("No handler found for type: " + type);
         }
-
-        try {
-            handler = (ViewHandler) ObjectType.getInstance(handlerClass);
-            handler.setName(type);
-            handler.init(context);
-        } catch (ClassNotFoundException cnf) {
-            //throw new ViewHandlerException("Cannot load handler class", cnf);
-            Debug.logWarning("Warning: could not load view handler class because it was not found; note that some views may not work: " + cnf.toString(), module);
-        } catch (InstantiationException ie) {
-            throw new ViewHandlerException("Cannot get instance of the handler", ie);
-        } catch (IllegalAccessException iae) {
-            throw new ViewHandlerException(iae.getMessage(), iae);
-        }
-
         return handler;
     }
 }



Re: svn commit: r1621942 - in /ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp: event/EventFactory.java view/ViewFactory.java

Posted by Adrian Crum <ad...@sandglass-software.com>.
Thank you for removing Javolution. :)

Adrian Crum
Sandglass Software
www.sandglass-software.com

On 9/2/2014 8:26 AM, jacopoc@apache.org wrote:
> Author: jacopoc
> Date: Tue Sep  2 07:26:03 2014
> New Revision: 1621942
>
> URL: http://svn.apache.org/r1621942
> Log:
> Greatly cleaned-up and simplified the EventFactory and ViewFactory classes used by the RequestHandler: they are now (mostly) immutable and thread safe.
>
> Modified:
>      ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java
>      ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java
>
> Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java?rev=1621942&r1=1621941&r2=1621942&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java (original)
> +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/EventFactory.java Tue Sep  2 07:26:03 2014
> @@ -19,18 +19,16 @@
>   package org.ofbiz.webapp.event;
>
>   import java.net.URL;
> +import java.util.HashMap;
>   import java.util.Map;
>   import java.util.Set;
>
>   import javax.servlet.ServletContext;
>
> -import javolution.util.FastMap;
> -
>   import org.ofbiz.base.util.Debug;
>   import org.ofbiz.base.util.GeneralRuntimeException;
>   import org.ofbiz.base.util.ObjectType;
>   import org.ofbiz.webapp.control.ConfigXMLReader;
> -import org.ofbiz.webapp.control.WebAppConfigurationException;
>
>   /**
>    * EventFactory - Event Handler Factory
> @@ -39,88 +37,29 @@ public class EventFactory {
>
>       public static final String module = EventFactory.class.getName();
>
> -    private final URL controllerConfigURL;
> -    private final ServletContext context;
> -    protected Map<String, EventHandler> handlers = null;
> +    private final Map<String, EventHandler> handlers = new HashMap<String, EventHandler>();
>
>       public EventFactory(ServletContext context, URL controllerConfigURL) {
> -        handlers = FastMap.newInstance();
> -        this.controllerConfigURL = controllerConfigURL;
> -        this.context = context;
> -
> -        // pre-load all event handlers
> +        // load all the event handlers
>           try {
> -            this.preLoadAll();
> -        } catch (EventHandlerException e) {
> +            Set<Map.Entry<String,String>> handlerEntries = ConfigXMLReader.getControllerConfig(controllerConfigURL).getEventHandlerMap().entrySet();
> +            if (handlerEntries != null) {
> +                for (Map.Entry<String,String> handlerEntry: handlerEntries) {
> +                    EventHandler handler = (EventHandler) ObjectType.getInstance(handlerEntry.getValue());
> +                    handler.init(context);
> +                    this.handlers.put(handlerEntry.getKey(), handler);
> +                }
> +            }
> +        } catch (Exception e) {
>               Debug.logError(e, module);
>               throw new GeneralRuntimeException(e);
>           }
>       }
>
> -    private void preLoadAll() throws EventHandlerException {
> -        Set<String> handlers = null;
> -        try {
> -            handlers = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getEventHandlerMap().keySet();
> -        } catch (WebAppConfigurationException e) {
> -            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
> -        }
> -        if (handlers != null) {
> -            for (String type: handlers) {
> -                this.handlers.put(type, this.loadEventHandler(type));
> -            }
> -        }
> -    }
> -
>       public EventHandler getEventHandler(String type) throws EventHandlerException {
> -        // check if we are new / empty and add the default handler in
> -        if (handlers.size() == 0) {
> -            this.preLoadAll();
> -        }
> -
> -        // attempt to get a pre-loaded handler
>           EventHandler handler = handlers.get(type);
> -
>           if (handler == null) {
> -            synchronized (EventHandler.class) {
> -                handler = handlers.get(type);
> -                if (handler == null) {
> -                    handler = this.loadEventHandler(type);
> -                    handlers.put(type, handler);
> -                }
> -            }
> -            if (handler == null)
> -                throw new EventHandlerException("No handler found for type: " + type);
> -        }
> -        return handler;
> -    }
> -
> -    public void clear() {
> -        handlers.clear();
> -    }
> -
> -    private EventHandler loadEventHandler(String type) throws EventHandlerException {
> -        EventHandler handler = null;
> -        String handlerClass = null;
> -        try {
> -            handlerClass = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getEventHandlerMap().get(type);
> -        } catch (WebAppConfigurationException e) {
> -            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
> -        }
> -        if (handlerClass == null) {
> -            throw new EventHandlerException("Unknown handler type: " + type);
> -        }
> -
> -        try {
> -            handler = (EventHandler) ObjectType.getInstance(handlerClass);
> -            handler.init(context);
> -        } catch (NoClassDefFoundError e) {
> -            throw new EventHandlerException("No class def found for handler [" + handlerClass + "]", e);
> -        } catch (ClassNotFoundException cnf) {
> -            throw new EventHandlerException("Cannot load handler class [" + handlerClass + "]", cnf);
> -        } catch (InstantiationException ie) {
> -            throw new EventHandlerException("Cannot get instance of the handler [" + handlerClass + "]", ie);
> -        } catch (IllegalAccessException iae) {
> -            throw new EventHandlerException(iae.getMessage(), iae);
> +            throw new EventHandlerException("No handler found for type: " + type);
>           }
>           return handler;
>       }
>
> Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java?rev=1621942&r1=1621941&r2=1621942&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java (original)
> +++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/view/ViewFactory.java Tue Sep  2 07:26:03 2014
> @@ -19,19 +19,17 @@
>   package org.ofbiz.webapp.view;
>
>   import java.net.URL;
> +import java.util.HashMap;
>   import java.util.Map;
>   import java.util.Set;
>
>   import javax.servlet.ServletContext;
>
> -import javolution.util.FastMap;
> -
>   import org.ofbiz.base.util.Debug;
>   import org.ofbiz.base.util.GeneralRuntimeException;
>   import org.ofbiz.base.util.ObjectType;
>   import org.ofbiz.base.util.UtilValidate;
>   import org.ofbiz.webapp.control.ConfigXMLReader;
> -import org.ofbiz.webapp.control.WebAppConfigurationException;
>
>   /**
>    * ViewFactory - View Handler Factory
> @@ -40,46 +38,29 @@ public class ViewFactory {
>
>       public static final String module = ViewFactory.class.getName();
>
> -    private final URL controllerConfigURL;
> -    private final ServletContext context;
> -    protected Map<String, ViewHandler> handlers = null;
> +    private final Map<String, ViewHandler> handlers = new HashMap<String, ViewHandler>();
>
>       public ViewFactory(ServletContext context, URL controllerConfigURL) {
> -        this.handlers = FastMap.newInstance();
> -        this.controllerConfigURL = controllerConfigURL;
> -        this.context = context;
> -
> -        // pre-load all the view handlers
> +        // load all the view handlers
>           try {
> -            this.preLoadAll();
> -        } catch (ViewHandlerException e) {
> -            Debug.logError(e, module);
> -            throw new GeneralRuntimeException(e);
> -        }
> -    }
> -
> -    private void preLoadAll() throws ViewHandlerException {
> -        Set<String> handlers = null;
> -        try {
> -            handlers = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getViewHandlerMap().keySet();
> -        } catch (WebAppConfigurationException e) {
> -            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
> -        }
> -        if (handlers != null) {
> -            for (String type: handlers) {
> -                this.handlers.put(type, this.loadViewHandler(type));
> +            Set<Map.Entry<String,String>> handlerEntries = ConfigXMLReader.getControllerConfig(controllerConfigURL).getViewHandlerMap().entrySet();
> +            if (handlerEntries != null) {
> +                for (Map.Entry<String,String> handlerEntry: handlerEntries) {
> +                    ViewHandler handler = (ViewHandler) ObjectType.getInstance(handlerEntry.getValue());
> +                    handler.setName(handlerEntry.getKey());
> +                    handler.init(context);
> +                    this.handlers.put(handlerEntry.getKey(), handler);
> +                }
>               }
> -        }
> -
> -        // load the "default" type
> -        if (!this.handlers.containsKey("default")) {
> -            try {
> -                ViewHandler h = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
> -                h.init(context);
> -                this. handlers.put("default", h);
> -            } catch (Exception e) {
> -                throw new ViewHandlerException(e);
> +            // load the "default" type
> +            if (!this.handlers.containsKey("default")) {
> +                ViewHandler defaultHandler = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
> +                defaultHandler.init(context);
> +                this. handlers.put("default", defaultHandler);
>               }
> +        } catch (Exception e) {
> +            Debug.logError(e, module);
> +            throw new GeneralRuntimeException(e);
>           }
>       }
>
> @@ -87,60 +68,11 @@ public class ViewFactory {
>           if (UtilValidate.isEmpty(type)) {
>               type = "default";
>           }
> -
> -        // check if we are new / empty and add the default handler in
> -        if (handlers.size() == 0) {
> -            this.preLoadAll();
> -        }
> -
>           // get the view handler by type from the contextHandlers
>           ViewHandler handler = handlers.get(type);
> -
> -        // if none found lets create it and add it in
>           if (handler == null) {
> -            synchronized (ViewFactory.class) {
> -                handler = handlers.get(type);
> -                if (handler == null) {
> -                    handler = this.loadViewHandler(type);
> -                    handlers.put(type, handler);
> -                }
> -            }
> -            if (handler == null) {
> -                throw new ViewHandlerException("No handler found for type: " + type);
> -            }
> -        }
> -        return handler;
> -    }
> -
> -    public void clear() {
> -        handlers.clear();
> -    }
> -
> -    private ViewHandler loadViewHandler(String type) throws ViewHandlerException {
> -        ViewHandler handler = null;
> -        String handlerClass = null;
> -        try {
> -            handlerClass = ConfigXMLReader.getControllerConfig(this.controllerConfigURL).getViewHandlerMap().get(type);
> -        } catch (WebAppConfigurationException e) {
> -            Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
> -        }
> -        if (handlerClass == null) {
> -            throw new ViewHandlerException("Unknown handler type: " + type);
> +            throw new ViewHandlerException("No handler found for type: " + type);
>           }
> -
> -        try {
> -            handler = (ViewHandler) ObjectType.getInstance(handlerClass);
> -            handler.setName(type);
> -            handler.init(context);
> -        } catch (ClassNotFoundException cnf) {
> -            //throw new ViewHandlerException("Cannot load handler class", cnf);
> -            Debug.logWarning("Warning: could not load view handler class because it was not found; note that some views may not work: " + cnf.toString(), module);
> -        } catch (InstantiationException ie) {
> -            throw new ViewHandlerException("Cannot get instance of the handler", ie);
> -        } catch (IllegalAccessException iae) {
> -            throw new ViewHandlerException(iae.getMessage(), iae);
> -        }
> -
>           return handler;
>       }
>   }
>
>