Author: rahul
Date: Fri Dec 22 10:59:26 2006
New Revision: 489723

URL: http://svn.apache.org/viewvc?view=rev&rev=489723
Log:
Better design for serializability. Remove lazy inits, Commons SCXML v0.6 does 
all the work for us.
SHALE-368

Modified:
    
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java
    
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogManager.java

Modified: 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java
URL: 
http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java?view=diff&rev=489723&r1=489722&r2=489723
==============================================================================
--- 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java
 (original)
+++ 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogContext.java
 Fri Dec 22 10:59:26 2006
@@ -160,11 +160,8 @@
      * <p>The [EMAIL PROTECTED] SCXMLExecutor}, an instance of the state 
machine
      * defined for the SCXML document for this dialog.</p>
      *
-     * TODO: Won't need to be transient after next release of Commons SCXML
-     *       (the current release is v0.5).
-     *       Remove lazy initialization bits as well then.
      */
-    private transient SCXMLExecutor executor = null;
+    private SCXMLExecutor executor = null;
 
 
     /**
@@ -290,25 +287,24 @@
             return;
         }
 
-        SCXMLExecutor dialogExecutor = getExecutor(context);
-        ((ShaleDialogELEvaluator) dialogExecutor.getEvaluator()).
+        ((ShaleDialogELEvaluator) executor.getEvaluator()).
                     setFacesContext(context);
-        dialogExecutor.getRootContext().setLocal(Globals.POSTBACK_OUTCOME, 
outcome);
+        executor.getRootContext().setLocal(Globals.POSTBACK_OUTCOME, outcome);
 
         try {
-            dialogExecutor.triggerEvent(new 
TriggerEvent(Globals.POSTBACK_EVENT,
+            executor.triggerEvent(new TriggerEvent(Globals.POSTBACK_EVENT,
                                 TriggerEvent.SIGNAL_EVENT));
         } catch (ModelException me) {
             fireOnException(me);
         }
 
-        Iterator iterator = 
dialogExecutor.getCurrentStatus().getStates().iterator();
+        Iterator iterator = executor.getCurrentStatus().getStates().iterator();
         this.stateId = ((State) iterator.next()).getId();
-        DialogProperties dp = (DialogProperties) 
dialogExecutor.getRootContext().
+        DialogProperties dp = (DialogProperties) executor.getRootContext().
             get(Globals.DIALOG_PROPERTIES);
 
         // If done, stop context
-        if (dialogExecutor.getCurrentStatus().isFinal()) {
+        if (executor.getCurrentStatus().isFinal()) {
             stop(context);
         }
 
@@ -356,23 +352,22 @@
             fireOnException(e);
         }
 
-        SCXMLExecutor dialogExecutor = getExecutor(context);
         // set state machine in motion
-        ((ShaleDialogELEvaluator) dialogExecutor.getEvaluator()).
+        ((ShaleDialogELEvaluator) executor.getEvaluator()).
             setFacesContext(context);
         try {
-            dialogExecutor.go();
+            executor.go();
         } catch (ModelException me) {
             fireOnException(me);
         }
 
-        Iterator iterator = 
dialogExecutor.getCurrentStatus().getStates().iterator();
+        Iterator iterator = executor.getCurrentStatus().getStates().iterator();
         this.stateId = ((State) iterator.next()).getId();
-        DialogProperties dp = (DialogProperties) 
dialogExecutor.getRootContext().
+        DialogProperties dp = (DialogProperties) executor.getRootContext().
             get(Globals.DIALOG_PROPERTIES);
 
         // Might be done at the beginning itself, if so, stop context
-        if (dialogExecutor.getCurrentStatus().isFinal()) {
+        if (executor.getCurrentStatus().isFinal()) {
             stop(context);
         }
 
@@ -482,32 +477,6 @@
         }
     }
 
-    /**
-     * Get the [EMAIL PROTECTED] SCXMLExecutor} instance driving this dialog, 
replenish
-     * if needed.
-     *
-     * @param context The [EMAIL PROTECTED] FacesContext} for this request
-     * @return The [EMAIL PROTECTED] SCXMLExecutor} instance driving this 
dialog
-     */
-    private SCXMLExecutor getExecutor(FacesContext context) {
-
-        // We've just been deserialized, if null
-        if (this.executor == null) {
-            this.executor = new SCXMLExecutor(new ShaleDialogELEvaluator(),
-                new SimpleDispatcher(), new SimpleErrorReporter());
-            SCXML stateMachine = ((SCXMLDialogManager) 
manager).dialog(context, name).
-                getStateMachine();
-            this.executor.setStateMachine(stateMachine);
-            Context rootCtx = new ELContext();
-            rootCtx.setLocal(Globals.DIALOG_PROPERTIES, new 
DialogProperties());
-            this.executor.setRootContext(rootCtx);
-            this.executor.addListener(stateMachine, new 
DelegatingSCXMLListener());
-            
this.executor.getCurrentStatus().getStates().add(stateMachine.getTargets().get(stateId));
-        }
-
-        return this.executor;
-    }
-
 
     /**
      * <p>Return the <code>Log</code> instance for this dialog context,
@@ -527,7 +496,12 @@
      * A [EMAIL PROTECTED] SCXMLListener} that delegates to the Shale
      * [EMAIL PROTECTED] DialogContextListener}s attached to this [EMAIL 
PROTECTED] DialogContext}.
      */
-    class DelegatingSCXMLListener implements SCXMLListener {
+    class DelegatingSCXMLListener implements SCXMLListener, Serializable {
+
+        /**
+         * Serial version UID.
+         */
+        private static final long serialVersionUID = 1L;
 
         /**
          * Handle entry callbacks.

Modified: 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogManager.java
URL: 
http://svn.apache.org/viewvc/shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogManager.java?view=diff&rev=489723&r1=489722&r2=489723
==============================================================================
--- 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogManager.java
 (original)
+++ 
shale/framework/trunk/shale-dialog-scxml/src/main/java/org/apache/shale/dialog/scxml/SCXMLDialogManager.java
 Fri Dec 22 10:59:26 2006
@@ -18,12 +18,9 @@
 package org.apache.shale.dialog.scxml;
 
 import java.io.Serializable;
-import java.net.URL;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
-import javax.faces.FacesException;
 import javax.faces.context.FacesContext;
 
 import org.apache.commons.logging.Log;
@@ -33,7 +30,6 @@
 import org.apache.shale.dialog.DialogContext;
 import org.apache.shale.dialog.DialogContextManager;
 import org.apache.shale.dialog.base.AbstractDialogContextManager;
-import org.apache.shale.dialog.scxml.config.ConfigurationParser;
 import org.apache.shale.dialog.scxml.config.DialogMetadata;
 
 /**
@@ -56,22 +52,6 @@
 
 
     /**
-     * <p>The default configuration resource we will process (if present),
-     * even if it is not explicitly configured.</p>
-     */
-    private static final String DEFAULT_CONFIGURATION_RESOURCE =
-            "/WEB-INF/dialog-config.xml";
-
-
-    /**
-     * <p>Resource name for dialog configuration resource(s) embedded in
-     * JAR files inside the application.</p>
-     */
-    private static final String EMBEDDED_CONFIGURATION_RESOURCE =
-            "META-INF/dialog-config.xml";
-
-
-    /**
      * <p><code>Map</code> of [EMAIL PROTECTED] SCXML} configurations, keyed
      * by dialog name.  This value is lazily instantiated.</p>
      */
@@ -113,7 +93,11 @@
     public DialogContext create(FacesContext context, String name, 
DialogContext parent) {
 
         // Obtain the dialog metadata for the specified dialog
-        DialogMetadata dialog = dialog(context, name);
+        DialogMetadata dialog = (DialogMetadata) dialogs(context).get(name);
+        if (dialog == null) {
+            throw new IllegalArgumentException("No definition for dialog name 
'"
+                                               + name + "' can be found");
+        }
 
         // Validate the specified parent (if any)
         String parentDialogId = null;
@@ -171,168 +155,26 @@
     }
 
 
-    //  ------------------------------------------------- Default Scoped 
Methods
-
-    /**
-     * <p>Return the dialog metadata for the dialog with the specified name.
-     * If the metadata isn't already loaded from the configuration files,
-     * it will get parsed now.</p>
-     *
-     * @param context FacesContext for the current request
-     * @param name The dialog name
-     * @return The [EMAIL PROTECTED] DialogMetadata} for the specified dialog 
name
-     */
-    DialogMetadata dialog(FacesContext context, String name) {
-
-        // Look up the specified dialog configuration
-        Map dialogs = dialogs(context, false);
-        DialogMetadata dialog = (DialogMetadata) dialogs.get(name);
-        // No such dialog
-        if (dialog == null) {
-            throw new IllegalArgumentException("No definition for dialog name 
'"
-                                               + name + "' can be found");
-        }
-
-        // We've just been deserialized
-        if (dialog.getStateMachine() == null) {
-            dialogs = dialogs(context, true);
-            dialog = (DialogMetadata) dialogs.get(name);
-            // Shouldn't happen
-            if (dialog.getStateMachine() == null) {
-                throw new IllegalStateException("Null statemachine for dialog 
name '"
-                    + name + "'");
-            }
-        }
-
-        return dialog;
-
-    }
-
-
     // --------------------------------------------------------- Private 
Methods
 
 
     /**
      * <p>Return a <code>Map</code> of the configured [EMAIL PROTECTED] SCXML} 
instances,
-     * keyed by logical dialog name.  Upon the first request, the
-     * configuration resources will be processed.</p>
+     * keyed by logical dialog name.</p>
      *
      * @param context FacesContext for the current request
-     * @param refresh Whether to force a refresh of the dialog configurations
      * @return Map of [EMAIL PROTECTED] SCXML} instances, keyed by logical 
dialog name
      */
-    private Map dialogs(FacesContext context, boolean refresh) {
+    private Map dialogs(FacesContext context) {
 
         // Return the cached instance (if any)
-        if (this.dialogs != null && !refresh) {
+        if (this.dialogs != null) {
             return this.dialogs;
         }
 
         // Return the previously configured application scope instance (if any)
         this.dialogs = (Map)
           
context.getExternalContext().getApplicationMap().get(Globals.DIALOGS);
-        if (this.dialogs != null && !refresh) {
-            return this.dialogs;
-        }
-
-        // Set up to parse our specified configuration resources and cache the 
results
-        boolean didDefault = false;
-        ConfigurationParser parser = new ConfigurationParser();
-        parser.setDialogs(new HashMap());
-
-        // Parse implicitly specified resources embedded in JAR files
-        ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        if (loader == null) {
-            loader = this.getClass().getClassLoader();
-        }
-        try {
-            Enumeration resources = 
loader.getResources(EMBEDDED_CONFIGURATION_RESOURCE);
-            while (resources.hasMoreElements()) {
-                URL resource = (URL) resources.nextElement();
-                if (log().isDebugEnabled()) {
-                    log().debug("Parsing configuration resource '"
-                            + resource + "'");
-                }
-                parser.setResource(resource);
-                parser.parse();
-            }
-        } catch (RuntimeException e) {
-            throw e;
-        } catch (Exception e) {
-            throw new FacesException(e);
-        }
-
-        // Parse explicitly specified resources
-        String resources =
-          context.getExternalContext().getInitParameter(Globals.CONFIGURATION);
-        if (resources == null) {
-            resources = "";
-        }
-        int comma = 0;
-        String path = null;
-        try {
-            while (true) {
-                comma = resources.indexOf(",");
-                if (comma < 0) {
-                    path = resources.trim();
-                    resources = "";
-                } else {
-                    path = resources.substring(0, comma).trim();
-                    resources = resources.substring(comma + 1);
-                }
-                if (path.length() < 1) {
-                    break;
-                }
-                URL resource = context.getExternalContext().getResource(path);
-                if (log().isDebugEnabled()) {
-                    log().debug("Parsing configuration resource '"
-                            + resource + "'");
-                }
-                parser.setResource(resource);
-                parser.parse();
-                if (DEFAULT_CONFIGURATION_RESOURCE.equals(path)) {
-                    didDefault = true;
-                }
-            }
-        } catch (RuntimeException e) {
-            throw e;
-        } catch (Exception e) {
-            throw new FacesException(e);
-        }
-
-        // Parse the default configuration resource if it exists and has not
-        // already been parsed
-        if (!didDefault) {
-            try {
-                URL resource =
-                  
context.getExternalContext().getResource(DEFAULT_CONFIGURATION_RESOURCE);
-                if (resource != null) {
-                    if (log().isDebugEnabled()) {
-                        log().debug("Parsing configuration resource '"
-                                + resource + "'");
-                    }
-                    parser.setResource(resource);
-                    parser.parse();
-                }
-            } catch (RuntimeException e) {
-                throw e;
-            } catch (Exception e) {
-                throw new FacesException(e);
-            }
-        }
-
-        // Cache the results both locally and in application scope
-        this.dialogs = parser.getDialogs();
-
-        if (this.dialogs.size() == 0) {
-            if (log().isWarnEnabled()) {
-                log.warn("No dialog configuration information present.  No 
default configuration found at: " +
-                        DEFAULT_CONFIGURATION_RESOURCE + ".  No embedded 
configuration found at: " +
-                        EMBEDDED_CONFIGURATION_RESOURCE);
-            }
-        }
-
-        context.getExternalContext().getApplicationMap().put(Globals.DIALOGS, 
this.dialogs);
         return this.dialogs;
 
     }


Reply via email to