commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r917247 - in /commons/proper/jxpath/trunk/src: java/org/apache/commons/jxpath/ java/org/apache/commons/jxpath/ri/ java/org/apache/commons/jxpath/ri/model/ java/org/apache/commons/jxpath/ri/model/beans/ test/org/apache/commons/jxpath/ri/
Date Sun, 28 Feb 2010 18:47:01 GMT
Author: mbenson
Date: Sun Feb 28 18:47:00 2010
New Revision: 917247

URL: http://svn.apache.org/viewvc?rev=917247&view=rev
Log:
[JXPATH-131] Exception handling

Added:
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java 
 (with props)
    commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java
  (with props)
Modified:
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/NodePointer.java
    commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/beans/PropertyIterator.java

Added: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java?rev=917247&view=auto
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java (added)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java Sun
Feb 28 18:47:00 2010
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jxpath;
+
+/**
+ * Exception handler interface.  Actually handles Throwables.
+ * @since 1.4
+ */
+public interface ExceptionHandler {
+    /**
+     * Handle an encountered Throwable.
+     * @param t to handle
+     * @param ptr specific context
+     */
+    void handle(Throwable t, Pointer ptr);
+}

Propchange: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ExceptionHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java?rev=917247&r1=917246&r2=917247&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java (original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/JXPathContext.java Sun
Feb 28 18:47:00 2010
@@ -944,4 +944,13 @@
                 "Namespace registration is not implemented by " + getClass());
     }
 
+    /**
+     * Set the ExceptionHandler used by this context, if any.
+     * @param exceptionHandler to set
+     * @since 1.4
+     */
+    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
+        throw new UnsupportedOperationException(
+                "ExceptionHandler registration is not implemented by " + getClass());
+    }
 }

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java?rev=917247&r1=917246&r2=917247&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
(original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java
Sun Feb 28 18:47:00 2010
@@ -28,6 +28,7 @@
 import java.util.Map.Entry;
 
 import org.apache.commons.jxpath.CompiledExpression;
+import org.apache.commons.jxpath.ExceptionHandler;
 import org.apache.commons.jxpath.Function;
 import org.apache.commons.jxpath.Functions;
 import org.apache.commons.jxpath.JXPathContext;
@@ -802,6 +803,15 @@
     }
 
     /**
+     * {@inheritDoc}
+     */
+    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
+        if (rootPointer instanceof NodePointer) {
+            ((NodePointer) rootPointer).setExceptionHandler(exceptionHandler);
+        }
+    }
+
+    /**
      * Checks if existenceCheckClass exists on the class path. If so, allocates
      * an instance of the specified class, otherwise returns null.
      * @param className to instantiate

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/NodePointer.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/NodePointer.java?rev=917247&r1=917246&r2=917247&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/NodePointer.java
(original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/NodePointer.java
Sun Feb 28 18:47:00 2010
@@ -20,6 +20,7 @@
 import java.util.Locale;
 
 import org.apache.commons.jxpath.AbstractFactory;
+import org.apache.commons.jxpath.ExceptionHandler;
 import org.apache.commons.jxpath.JXPathContext;
 import org.apache.commons.jxpath.JXPathException;
 import org.apache.commons.jxpath.NodeSet;
@@ -44,6 +45,9 @@
  */
 public abstract class NodePointer implements Pointer {
 
+    /** Serialization version */
+    private static final long serialVersionUID = 8117201322861007777L;
+
     /** Whole collection index. */
     public static final int WHOLE_COLLECTION = Integer.MIN_VALUE;
 
@@ -55,6 +59,7 @@
 
     private boolean attribute = false;
     private NamespaceResolver namespaceResolver;
+    private ExceptionHandler exceptionHandler;
     private transient Object rootNode;
 
     /**
@@ -806,6 +811,39 @@
     }
 
     /**
+     * Set the exceptionHandler of this NodePointer.
+     * @param exceptionHandler the ExceptionHandler to set
+     */
+    public void setExceptionHandler(ExceptionHandler exceptionHandler) {
+        this.exceptionHandler = exceptionHandler;
+    }
+
+    /**
+     * Handle a Throwable using an installed ExceptionHandler, if available.
+     * Public to facilitate calling for RI support; not truly intended for public consumption.
+     * @param t to handle
+     * @param originator context
+     */
+    public void handle(Throwable t, NodePointer originator) {
+        if (exceptionHandler != null) {
+            exceptionHandler.handle(t, originator);
+            return;
+        }
+        if (parent != null) {
+            parent.handle(t, originator);
+        }
+    }
+
+    /**
+     * Handle a Throwable using an installed ExceptionHandler, if available.
+     * Public to facilitate calling for RI support; not truly intended for public consumption.
+     * @param t to handle
+     */
+    public void handle(Throwable t) {
+        handle(t, this);
+    }
+
+    /**
      * Return a string escaping single and double quotes.
      * @param string string to treat
      * @return string with any necessary changes made.

Modified: commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/beans/PropertyIterator.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/beans/PropertyIterator.java?rev=917247&r1=917246&r2=917247&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/beans/PropertyIterator.java
(original)
+++ commons/proper/jxpath/trunk/src/java/org/apache/commons/jxpath/ri/model/beans/PropertyIterator.java
Sun Feb 28 18:47:00 2010
@@ -120,8 +120,8 @@
         try {
             return propertyNodePointer.getValuePointer();
         }
-        catch (Throwable ex) {
-            // @todo: should this exception be reported in any way?
+        catch (Throwable t) {
+            propertyNodePointer.handle(t);
             NullPropertyPointer npp =
                 new NullPropertyPointer(
                         propertyNodePointer.getImmediateParentPointer());
@@ -318,7 +318,7 @@
             length = propertyNodePointer.getLength(); // TBD: cache length
         }
         catch (Throwable t) {
-            // @todo: should this exception be reported in any way?
+            propertyNodePointer.handle(t);
             length = 0;
         }
         return length;

Added: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java?rev=917247&view=auto
==============================================================================
--- commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java
(added)
+++ commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java
Sun Feb 28 18:47:00 2010
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jxpath.ri;
+
+import org.apache.commons.jxpath.ExceptionHandler;
+import org.apache.commons.jxpath.JXPathContext;
+import org.apache.commons.jxpath.JXPathTestCase;
+import org.apache.commons.jxpath.Pointer;
+
+/**
+ * Test simple ExceptionHandler.
+ */
+public class ExceptionHandlerTest extends JXPathTestCase {
+    public static class Bar {
+        public Object getBaz() {
+            throw new IllegalStateException("baz unavailable");
+        }
+    }
+
+    private JXPathContext context;
+    private Bar bar = new Bar();
+
+    public void setUp() throws Exception {
+        context = JXPathContext.newContext(this);
+        context.setExceptionHandler(new ExceptionHandler() {
+            
+            public void handle(Throwable t, Pointer ptr) {
+                if (t instanceof Error) {
+                    throw (Error) t;
+                }
+                if (t instanceof RuntimeException) {
+                    throw (RuntimeException) t;
+                }
+                throw new RuntimeException(t);
+            }
+        });
+    }
+    
+    public Object getFoo() {
+        throw new IllegalStateException("foo unavailable");
+    }
+    
+    public void testHandleFoo() throws Exception {
+        try {
+            context.getValue("foo");
+            fail("expected Throwable");
+        } catch (Throwable t) {
+            while (t != null) {
+                if ("foo unavailable".equals(t.getMessage())) {
+                    return;
+                }
+                t = t.getCause();
+            }
+            fail("expected \"foo unavailable\" in throwable chain");
+        }
+    }
+
+    public void testHandleBarBaz() throws Exception {
+        try {
+            context.getValue("bar/baz");
+            fail("expected Throwable");
+        } catch (Throwable t) {
+            while (t != null) {
+                if ("baz unavailable".equals(t.getMessage())) {
+                    return;
+                }
+                t = t.getCause();
+            }
+            fail("expected \"baz unavailable\" in throwable chain");
+        }
+    }
+
+    public Bar getBar() {
+        return bar;
+    }
+}

Propchange: commons/proper/jxpath/trunk/src/test/org/apache/commons/jxpath/ri/ExceptionHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message