jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ra...@apache.org
Subject svn commit: r320669 - /jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java
Date Thu, 13 Oct 2005 05:35:13 GMT
Author: rahul
Date: Wed Oct 12 22:35:10 2005
New Revision: 320669

URL: http://svn.apache.org/viewcvs?rev=320669&view=rev
Log:
For rdc:group, cache DM strategy instance, use a better classloading scheme and provide more
informative error messages.

Modified:
    jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java

Modified: jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java
URL: http://svn.apache.org/viewcvs/jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java?rev=320669&r1=320668&r2=320669&view=diff
==============================================================================
--- jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java (original)
+++ jakarta/taglibs/proper/rdc/trunk/src/org/apache/taglibs/rdc/core/GroupTag.java Wed Oct
12 22:35:10 2005
@@ -1,5 +1,5 @@
 /*
- *    
+ *
  *   Copyright 2004 The Apache Software Foundation.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,22 +29,29 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.taglibs.rdc.RDCUtils;
 
 /**
  * <p>This is the implementation of the RDC helper tag group.
  * Group is a container with pluggable DM strategies.</p>
- * 
+ *
  * @author Rahul Akolkar
  */
 public class GroupTag extends SimpleTagSupport {
-    
+
     // Error messages (to be i18n'zed)
+    private static final String ERR_NO_STRATEGY = "<!-- GroupTag: " +
+        "strategy attribute value - \"{0}\" contains whitespace only -->\n";
     private static final String ERR_NO_SUCH_STRATEGY = "<!-- GroupTag: No " +
-            "strategy class found - \"{0}\" -->\n";
-    
+        "strategy class found - \"{0}\" -->\n";
+    private static final String ERR_NOT_A_DM_STRATEGY = "<!-- GroupTag: " +
+        "Class - \"{0}\" does not implement the DialogManager interface -->\n";
+    private static final String ERR_INSTANTIATING_STRATEGY = "<!-- GroupTag:" +
+        " Error instantiating the specified strategy class - \"{0}\" -->\n";
+
     // Logging
     private static Log log = LogFactory.getLog(GroupTag.class);
-    
+
     // CLASS PROPERTIES
     // The unique identifier associated with the group
     private String id;
@@ -58,6 +65,8 @@
     private Boolean confirm;
     // The class name that implements the dialog management strategy
     private String strategy;
+    // The DialogManager overseeing this group instance
+    private DialogManager dm = null;
 
     /* Constructor */
     public GroupTag() {
@@ -67,6 +76,7 @@
         config = null;
         strategy = null;
         confirm = Boolean.FALSE;
+        dm = null;
     }
 
     /**
@@ -122,10 +132,10 @@
     public void setConfig(String config) {
         this.config = config;
     }
-    
+
     /**
      * Get the group level confirmation
-     * 
+     *
      * @return confirm
      */
     public Boolean getConfirm() {
@@ -134,16 +144,16 @@
 
     /**
      * Set the group level confirmation
-     * 
+     *
      * @param Boolean confirm
      */
     public void setConfirm(Boolean confirm) {
         this.confirm = confirm;
     }
-    
+
     /**
      * Get the class name that implements the DM strategy
-     * 
+     *
      * @return strategy the class name
      */
     public String getStrategy() {
@@ -152,61 +162,123 @@
 
     /**
      * Set the class name that implements the DM strategy
-     * 
+     *
      * @param strategy the class name
      */
     public void setStrategy(String strategy) {
         this.strategy = strategy;
-    }    
+    }
 
     /**
      * Make JspContext visible to the DM strategy
-     * 
+     *
      * @return JspContext
-     */    
+     */
     public JspContext getJspContext() {
         return super.getJspContext();
     }
-    
+
     /**
      * Has the state machine for the group
      *
      * Uses a pluggable dialog management strategy
      */
     public void doTag() throws JspException, IOException {
-        
-        DialogManager dm = null;
-        try {
-            dm = (DialogManager) Class.forName(strategy).newInstance();
-        } catch (Exception e) {
-            MessageFormat msgFormat = new MessageFormat(ERR_NO_SUCH_STRATEGY);
-            String errMsg = msgFormat.format(new Object[] {strategy});
-            // Log error and send comment to client
-            log.error(errMsg);
-            ((PageContext) getJspContext()).getOut().write(errMsg);
-            return;
+
+        if (dm == null) {
+            PageContext ctx = (PageContext) getJspContext();
+            try {
+                dm = getStrategyInstance(ctx);
+            } catch (ClassNotFoundException cnfe) {
+                write(ctx, ERR_NO_SUCH_STRATEGY,
+                    new Object[] {strategy}, cnfe);
+            }
         }
-        
+
         if (submit == null || submit.length() == 0) {
-            // to-do - investigate why servlet path won't work in VTK,
-            // and if session id needs to be appended 
             submit = ((HttpServletRequest) ((PageContext) getJspContext()).
                 getRequest()).getRequestURI();
         }
 
         dm.setGroupTag(this);
-        
+
         if (!dm.initialize(getJspContext(), getJspBody())) {
-            return;    
+            return;
         }
 
         dm.collect(getJspContext(), getJspBody());
-        
+
         // Deprecated (since RDC 1.1)
         dm.confirm();
-        
+
         dm.finish(getJspContext());
-        
+
+    }
+
+    /**
+     * Instantiate the strategy for this group instance.
+     *
+     * @param ctx The host JSP PageContext
+     * @return DialogManager The DM strategy instance
+     */
+    private DialogManager getStrategyInstance(PageContext ctx)
+    throws ClassNotFoundException {
+        final Object[] errMsgArgs = new Object[] {strategy};
+        if (RDCUtils.isStringEmpty(strategy)) {
+            write(ctx, ERR_NO_STRATEGY, errMsgArgs, null);
+            return null;
+        }
+        Class dmClass = null;
+        DialogManager dialogManager = null;
+        try {
+            dmClass = Class.forName(strategy);
+        } catch (ClassNotFoundException cnfe) {
+            // Try the classloader for this thread
+            dmClass = Class.forName(strategy, true, Thread.currentThread().
+                        getContextClassLoader());
+        } catch (Exception e) {
+            // Log error
+            log.error(e.getMessage(), e);
+            write(ctx, ERR_INSTANTIATING_STRATEGY, errMsgArgs, null);
+        }
+
+        if (dmClass != null) {
+            try {
+                dialogManager = (DialogManager) dmClass.newInstance();
+            } catch (ClassCastException cce) {
+                write(ctx, ERR_NOT_A_DM_STRATEGY, errMsgArgs, cce);
+            } catch (Exception e) {
+                // Log error
+                log.error(e.getMessage(), e);
+                write(ctx, ERR_INSTANTIATING_STRATEGY, errMsgArgs, e);
+            }
+        }
+        return dialogManager;
+    }
+    
+    /**
+     * Attempt to write given message to JSP writer.
+     *
+     * @param ctx The host JSP PageContext
+     * @param msg The message to be sent to the JSP writer
+     * @return boolean Outcome of write attempt
+     */
+    private void write(final PageContext ctx, final String errMsgName,
+            final Object[] msgArgs, final Exception e) {
+        MessageFormat msgFormat = new MessageFormat(errMsgName);
+        String errMsg = msgFormat.format(msgArgs);
+        // Log error
+        if (e == null) {
+            log.error(errMsg);
+        } else {
+            log.error(errMsg, e);
+        }
+        // Attempt to send message to client
+        try {
+            ctx.getOut().write(errMsg);
+        } catch (IOException ioe) {
+            log.error(ioe.getMessage(),ioe);
+        }
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org


Mime
View raw message