[jboss-cvs] JBossAS SVN: r58212 - branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Nov 8 17:26:24 EST 2006
Author: weston.price at jboss.com
Date: 2006-11-08 17:26:23 -0500 (Wed, 08 Nov 2006)
New Revision: 58212
Added:
branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeHandler.java
branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeLoader.java
Log:
[JBAS-3511] More ASF improvements.
Added: branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeHandler.java
===================================================================
--- branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeHandler.java 2006-11-08 22:26:01 UTC (rev 58211)
+++ branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeHandler.java 2006-11-08 22:26:23 UTC (rev 58212)
@@ -0,0 +1,244 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.jms.asf;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.jms.JMSException;
+
+import org.jboss.logging.Logger;
+
+public class JMSSupportCodeHandler implements JMSExceptionSorter
+{
+ private static final Logger log = Logger.getLogger(JMSSupportCodeLoader.class);
+
+ private boolean validateLinkedExcpetion;
+ private List fatalCodes = new ArrayList();
+ private JMSExceptionSorter sorter;
+ private String supportCodeUrl;
+ private static final String NO_SORTER = "NoSorter";
+ private static final String NO_URL = "NoUrl";
+
+ public boolean hasSupportCodes()
+ {
+ return fatalCodes.size() > 0;
+
+ }
+
+ //For testing only.
+ public JMSExceptionSorter getUnderlyingSorter()
+ {
+ return this.sorter;
+
+ }
+
+ public JMSSupportCodeHandler(final String fileName, final String sorterClassName)
+ {
+ this(fileName, sorterClassName, false);
+
+ }
+ public JMSSupportCodeHandler(final String fileName, final String sorterClassName, boolean validateLinkedException)
+ {
+ this.validateLinkedExcpetion = validateLinkedException;
+
+ if(sorterClassName == null || sorterClassName.equalsIgnoreCase(NO_SORTER))
+ {
+
+ if(fileName != null && !fileName.equalsIgnoreCase("") && !fileName.equalsIgnoreCase(NO_URL))
+ {
+ log.trace("Exception sorter class name is null. Attempting to load codes from external file " + fileName);
+ this.supportCodeUrl = fileName;
+ fatalCodes = JMSSupportCodeLoader.loadJMSSupportCodes(fileName);
+
+ }else
+ {
+ log.trace("No external url or exception sorter classname was provided. JMSSessions will not be validated.");
+ }
+
+
+ }else
+ {
+
+ final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+
+ try
+ {
+ final Class sorterClass = cl.loadClass(sorterClassName);
+ sorter = (JMSExceptionSorter)sorterClass.newInstance();
+ sorter.setValidateLinkedException(validateLinkedException);
+
+ }catch(Exception e)
+ {
+ log.error("Sorter class " + sorterClassName + " could not be loaded. Using default exception sorter");
+ sorter = new AllNonFatalJMSExceptionSorter();
+ sorter.setValidateLinkedException(validateLinkedException);
+ }
+
+
+ }
+
+ }
+
+ public JMSSupportCodeHandler(final String fileName)
+ {
+ this(fileName, null, false);
+
+ }
+
+ public void reloadSupportFile()
+ {
+ if(fatalCodes != null)
+ log.trace("Attempting to reload support code file " + supportCodeUrl);
+
+ synchronized (this)
+ {
+ if(fatalCodes != null)
+ {
+ fatalCodes = null;
+ fatalCodes = JMSSupportCodeLoader.loadJMSSupportCodes(supportCodeUrl);
+
+ }
+ }
+ }
+
+ public void addSupportCodes(String fileName)
+ {
+ if(fatalCodes != null)
+ log.trace("Attempting to reload support code file " + fileName);
+
+ List newCodes = JMSSupportCodeLoader.loadJMSSupportCodes(fileName);
+
+ synchronized (fatalCodes)
+ {
+ fatalCodes.addAll(newCodes);
+
+ }
+
+ }
+
+ public boolean isCodeInUse(int code)
+ {
+ return (fatalCodes != null && fatalCodes.contains(new Integer(code)));
+
+ }
+ public void reloadSupportFile(String fileName)
+ {
+ if(fatalCodes != null)
+ log.trace("Attempting to reload support code file " + fileName);
+
+ synchronized (this)
+ {
+ fatalCodes = JMSSupportCodeLoader.loadJMSSupportCodes(fileName);
+
+ }
+
+ }
+
+ public void clearSupportCodes()
+ {
+ synchronized (fatalCodes)
+ {
+ fatalCodes.clear();
+
+ }
+
+ }
+ public Integer[] getCurrentCodes()
+ {
+ synchronized (fatalCodes)
+ {
+ return (Integer[]) fatalCodes.toArray(new Integer[fatalCodes.size()]);
+
+ }
+ }
+
+ public boolean isJMSExceptionFatal(final JMSException e)
+ {
+
+ if(sorter != null)
+ {
+ return sorter.isJMSExceptionFatal(e);
+
+ }
+
+ boolean fatal = false;
+ final String errorCode = e.getErrorCode();
+
+ if(errorCode != null)
+ {
+ for(Iterator iter = fatalCodes.iterator(); iter.hasNext();)
+ {
+ Integer code = (Integer)iter.next();
+
+ if(code != null)
+ {
+
+
+ if(JMSExceptionCodeMatcher.matches(errorCode, code))
+ {
+ log.trace("JMS error code " + errorCode + " contains value " + code + " and will be processed as a fatal exception.");
+ fatal = true;
+ break;
+ }
+ }
+
+ }
+ }
+
+ if(fatal || !fatal && !validateLinkedExcpetion)
+ {
+ return fatal;
+ }
+
+ if(e.getLinkedException() instanceof JMSException)
+ {
+ JMSException linked = (JMSException)e.getLinkedException();
+ final String linkedErrorCode = linked.getErrorCode();
+
+ if(linkedErrorCode != null)
+ {
+ for(Iterator iter = fatalCodes.iterator(); iter.hasNext();)
+ {
+ Integer code = (Integer)iter.next();
+
+ if(linkedErrorCode != null)
+ {
+ if(JMSExceptionCodeMatcher.matches(linkedErrorCode,code))
+ {
+ log.trace("JMS error code from linked exception " + linkedErrorCode + " contains value " + code + " and will be processed as a fatal exception.");
+ fatal = true;
+ break;
+ }
+
+ }
+ }
+
+ }
+ }
+
+ return fatal;
+ }
+
+ public boolean getValidateLinkedException()
+ {
+ return validateLinkedExcpetion;
+
+ }
+
+ public void setValidateLinkedException(boolean vle)
+ {
+ this.validateLinkedExcpetion = vle;
+
+ }
+
+
+
+}
Added: branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeLoader.java
===================================================================
--- branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeLoader.java 2006-11-08 22:26:01 UTC (rev 58211)
+++ branches/JBoss_4_0_3_SP1_JBAS_3511/server/src/main/org/jboss/jms/asf/JMSSupportCodeLoader.java 2006-11-08 22:26:23 UTC (rev 58212)
@@ -0,0 +1,141 @@
+package org.jboss.jms.asf;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
+import org.jboss.logging.Logger;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+
+public class JMSSupportCodeLoader
+{
+ private static final Logger log = Logger.getLogger(JMSSupportCodeLoader.class);
+ private static final String FATAL_CODE_ELEM_NAME = "jms-fatal-code";
+
+ public static List loadJMSSupportCodes(final String fileName)
+ {
+ final List supportCodes = new ArrayList();
+ InputStream is = null;
+ DocumentBuilder builder = null;
+ InputSource source = null;
+ boolean loaded = false;
+
+ if(fileName == null)
+ return supportCodes;
+
+ try
+ {
+ if(fileName != null)
+ {
+
+ try
+ {
+ builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+
+ }catch(Exception e)
+ {
+ log.trace("Could not instantiate DocumentBuilder.", e);
+ return supportCodes;
+
+ }
+
+ //First try the file system
+ try
+ {
+ source = new InputSource(new FileReader(new File(fileName)));
+ loaded = true;
+
+ }catch(Exception e)
+ {
+ log.trace("Could not load filesystem resource", e);
+ }
+
+ if(source == null || loaded == false)
+ {
+ log.debug("Could not load filesystem resource " + fileName + " attempting to load from classpath");
+ final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ is = cl.getResourceAsStream(fileName);
+
+ if(is == null)
+ {
+ log.debug("Could not load system resource " + fileName + " from classpath");
+ return supportCodes;
+ }
+
+ source = new InputSource(is);
+ }
+
+ try
+ {
+ Document document = builder.parse(source);
+ //Get nodes and populate
+ NodeList nl = document.getElementsByTagName(FATAL_CODE_ELEM_NAME);
+ log.debug("Found " + nl.getLength() + " codes for processing.");
+
+ for(int i = 0; i < nl.getLength(); i++)
+ {
+ Node fatal = (Node)nl.item(i);
+ String txt = fatal.getFirstChild().getNodeValue();
+
+ try
+ {
+
+ if(txt != null)
+ {
+ Integer code = Integer.valueOf(txt);
+ supportCodes.add(code);
+
+ }
+
+ }catch(Exception e)
+ {
+ log.trace("Could not format or add supoprt code " + txt + " please validate support code file " + fileName);
+
+ }
+
+ }
+
+ log.trace("Loaded " + supportCodes.size() + " codes " + "from " + fileName);
+
+ return supportCodes;
+
+ }catch(Exception e)
+ {
+ log.trace("Could not parse support code file.", e);
+ return supportCodes;
+
+ }
+
+ }
+
+
+ }finally
+ {
+ try
+ {
+ if(is != null)
+ is.close();
+
+ }catch(Exception ignore)
+ {
+
+ }
+ }
+
+ return supportCodes;
+
+
+ }
+
+
+
+}
More information about the jboss-cvs-commits
mailing list