This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new 08e4140  Context API refactoring.
08e4140 is described below

commit 08e4140041f06c5f6d6219b3298e4063faecc44d
Author: JamesBognar <[email protected]>
AuthorDate: Thu Oct 21 11:53:45 2021 -0400

    Context API refactoring.
---
 .../org/apache/juneau/utils/MethodInvoker.java     | 21 +++++----
 .../java/org/apache/juneau/rest/RestContext.java   | 51 ++++++++++++++--------
 .../java/org/apache/juneau/rest/RestOpInvoker.java | 21 ++++++---
 .../java/org/apache/juneau/rest/RestOpSession.java |  9 ++--
 4 files changed, 67 insertions(+), 35 deletions(-)

diff --git 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
index 2d00a04..97545ac 100644
--- 
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
+++ 
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MethodInvoker.java
@@ -12,11 +12,12 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.utils;
 
+import static org.apache.juneau.internal.ExceptionUtils.*;
+
 import java.lang.reflect.*;
 import java.util.*;
 import java.util.stream.*;
 
-import org.apache.juneau.*;
 import org.apache.juneau.cp.*;
 import org.apache.juneau.mstat.*;
 import org.apache.juneau.reflect.*;
@@ -54,19 +55,21 @@ public class MethodInvoker {
         * @param o  The object the underlying method is invoked from.
         * @param args  The arguments used for the method call.
         * @return  The result of dispatching the method represented by this 
object on {@code obj} with parameters {@code args}
-        * @throws ExecutableException If error occurred trying to invoke the 
method.
+        * @throws IllegalAccessException If method cannot be accessed.
+        * @throws IllegalArgumentException If wrong arguments were passed to 
method.
+        * @throws InvocationTargetException If method threw an exception.
         */
-       public Object invoke(Object o, Object...args) throws 
ExecutableException {
+       public Object invoke(Object o, Object...args) throws 
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                long startTime = System.nanoTime();
                stats.started();
                try {
                        return m.inner().invoke(o, args);
                } catch (IllegalAccessException|IllegalArgumentException e) {
                        stats.error(e);
-                       throw new ExecutableException(e);
+                       throw e;
                } catch (InvocationTargetException e) {
                        stats.error(e.getTargetException());
-                       throw new ExecutableException(e.getTargetException());
+                       throw e;
                } finally {
                        stats.finished(System.nanoTime() - startTime);
                }
@@ -78,14 +81,16 @@ public class MethodInvoker {
         * @param beanStore The bean store to use to resolve parameters.
         * @param o The object to invoke the method on.
         * @return The result of invoking the method.
-        * @throws ExecutableException If error occurred trying to invoke the 
method.
+        * @throws IllegalAccessException If method cannot be accessed.
+        * @throws IllegalArgumentException If wrong arguments were passed to 
method.
+        * @throws InvocationTargetException If method threw an exception.
         */
-       public Object invokeUsingFactory(BeanStore beanStore, Object o) throws 
ExecutableException {
+       public Object invoke(BeanStore beanStore, Object o) throws 
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                List<ClassInfo> missing;
                missing = beanStore.getMissingParamTypes(m.getParams());
                if (missing.isEmpty())
                        return invoke(o, beanStore.getParams(m.getParams()));
-               throw new ExecutableException("Could not find prerequisites to 
invoke method ''{0}'': {1}", getFullName(), 
missing.stream().map(x->x.getSimpleName()).collect(Collectors.joining(",")));
+               throw illegalArgumentException("Could not find prerequisites to 
invoke method ''{0}'': {1}", getFullName(), 
missing.stream().map(x->x.getSimpleName()).collect(Collectors.joining(",")));
        }
 
        /**
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
index 35c0d23..2a1d58c 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestContext.java
@@ -7165,9 +7165,11 @@ public class RestContext extends Context {
        protected void startCall(RestSession session) throws BasicHttpException 
{
                for (MethodInvoker x : startCallMethods) {
                        try {
-                               
x.invokeUsingFactory(session.getBeanStore(),session.getContext().getResource());
-                       } catch (ExecutableException e) {
-                               throw toHttpException(e.unwrap(), 
InternalServerError.class);
+                               x.invoke(session.getBeanStore(), 
session.getContext().getResource());
+                       } catch 
(IllegalAccessException|IllegalArgumentException e) {
+                               throw 
InternalServerError.create().message("Error occurred invoking start-call method 
''{0}''.", x.getFullName()).causedBy(e).build();
+                       } catch (InvocationTargetException e) {
+                               throw toHttpException(e.getTargetException(), 
InternalServerError.class);
                        }
                }
        }
@@ -7176,9 +7178,9 @@ public class RestContext extends Context {
         * Called during a request to invoke all {@link HookEvent#PRE_CALL} 
methods.
         *
         * @param session The current request.
-        * @throws BasicHttpException If thrown from call methods.
+        * @throws Throwable If thrown from call methods.
         */
-       protected void preCall(RestOpSession session) throws BasicHttpException 
{
+       protected void preCall(RestOpSession session) throws Throwable {
                for (RestOpInvoker m : session.getContext().getPreCallMethods())
                        m.invoke(session);
        }
@@ -7187,9 +7189,9 @@ public class RestContext extends Context {
         * Called during a request to invoke all {@link HookEvent#POST_CALL} 
methods.
         *
         * @param session The current request.
-        * @throws BasicHttpException If thrown from call methods.
+        * @throws Throwable If thrown from call methods.
         */
-       protected void postCall(RestOpSession session) throws 
BasicHttpException {
+       protected void postCall(RestOpSession session) throws Throwable {
                for (RestOpInvoker m : 
session.getContext().getPostCallMethods())
                        m.invoke(session);
        }
@@ -7205,9 +7207,9 @@ public class RestContext extends Context {
        protected void finishCall(RestSession session) {
                for (MethodInvoker x : endCallMethods) {
                        try {
-                               x.invokeUsingFactory(session.getBeanStore(), 
session.getResource());
-                       } catch (ExecutableException e) {
-                               logger.log(Level.WARNING, e.unwrap(), 
()->format("Error occurred invoking finish-call method ''{0}''.", 
x.getFullName()));
+                               x.invoke(session.getBeanStore(), 
session.getResource());
+                       } catch (Exception e) {
+                               logger.log(Level.WARNING, unwrap(e), 
()->format("Error occurred invoking finish-call method ''{0}''.", 
x.getFullName()));
                        }
                }
        }
@@ -7232,9 +7234,9 @@ public class RestContext extends Context {
                }
                for (MethodInvoker x : postInitMethods) {
                        try {
-                               x.invokeUsingFactory(beanStore, getResource());
-                       } catch (ExecutableException e) {
-                               throw new ServletException(e.unwrap());
+                               x.invoke(beanStore, getResource());
+                       } catch (Exception e) {
+                               throw new ServletException(unwrap(e));
                        }
                }
                restChildren.postInit();
@@ -7253,9 +7255,9 @@ public class RestContext extends Context {
                restChildren.postInitChildFirst();
                for (MethodInvoker x : postInitChildFirstMethods) {
                        try {
-                               x.invokeUsingFactory(beanStore, getResource());
-                       } catch (ExecutableException e) {
-                               throw new ServletException(e.unwrap());
+                               x.invoke(beanStore, getResource());
+                       } catch (Exception e) {
+                               throw new ServletException(unwrap(e));
                        }
                }
                initialized.set(true);
@@ -7268,9 +7270,9 @@ public class RestContext extends Context {
        protected void destroy() {
                for (MethodInvoker x : destroyMethods) {
                        try {
-                               x.invokeUsingFactory(beanStore, getResource());
-                       } catch (ExecutableException e) {
-                               getLogger().log(Level.WARNING, e.unwrap(), 
()->format("Error occurred invoking servlet-destroy method ''{0}''.", 
x.getFullName()));
+                               x.invoke(beanStore, getResource());
+                       } catch (Exception e) {
+                               getLogger().log(Level.WARNING, unwrap(e), 
()->format("Error occurred invoking servlet-destroy method ''{0}''.", 
x.getFullName()));
                        }
                }
 
@@ -7326,6 +7328,17 @@ public class RestContext extends Context {
        }
 
        
//-----------------------------------------------------------------------------------------------------------------
+       // Helper methods
+       
//-----------------------------------------------------------------------------------------------------------------
+
+       private Throwable unwrap(Throwable t) {
+               if (t instanceof InvocationTargetException) {
+                       return 
((InvocationTargetException)t).getTargetException();
+               }
+               return t;
+       }
+
+       
//-----------------------------------------------------------------------------------------------------------------
        // Other methods
        
//-----------------------------------------------------------------------------------------------------------------
 
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
index fc734b8..010354a 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpInvoker.java
@@ -16,7 +16,8 @@ import static org.apache.juneau.rest.HttpRuntimeException.*;
 
 import java.lang.reflect.*;
 
-import org.apache.juneau.*;
+import org.apache.http.*;
+import org.apache.juneau.http.annotation.*;
 import org.apache.juneau.http.response.*;
 import org.apache.juneau.mstat.*;
 import org.apache.juneau.reflect.*;
@@ -46,9 +47,9 @@ public class RestOpInvoker extends MethodInvoker {
         *
         * @param opSession The REST call.
         * @return The results of the call.
-        * @throws BasicHttpException If an error occurred during either 
parameter resolution or method invocation.
+        * @throws Throwable If an error occurred during either parameter 
resolution or method invocation.
         */
-       public Object invoke(RestOpSession opSession) throws BasicHttpException 
{
+       public Object invoke(RestOpSession opSession) throws Throwable {
                Object[] args = new Object[opArgs.length];
                for (int i = 0; i < opArgs.length; i++) {
                        ParamInfo pi = inner().getParam(i);
@@ -60,8 +61,18 @@ public class RestOpInvoker extends MethodInvoker {
                }
                try {
                        return invoke(opSession.getRestSession().getResource(), 
args);
-               } catch (ExecutableException e) {
-                       throw toHttpException(e.unwrap(), 
InternalServerError.class, "Method ''{0}'' threw an unexpected exception.", 
getFullName());
+               } catch (IllegalAccessException|IllegalArgumentException e) {
+                       throw InternalServerError.create().message("Error 
occurred invoking method ''{0}''.", inner().getFullName()).causedBy(e).build();
+               } catch (InvocationTargetException e) {
+                       RestResponse res = opSession.getResponse();
+                       Throwable e2 = e.getTargetException();
+                       res.setStatus(500);  // May be overridden later.
+                       Class<?> c = e2.getClass();
+                       if (e2 instanceof HttpResponse || 
c.getAnnotation(Response.class) != null || c.getAnnotation(ResponseBody.class) 
!= null) {
+                               res.setOutput(e2);
+                               return null;
+                       }
+                       throw toHttpException(e2, InternalServerError.class, 
"Method ''{0}'' threw an unexpected exception.", getFullName());
                }
        }
 }
diff --git 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
index a7b4957..12e44f6 100644
--- 
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
+++ 
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/RestOpSession.java
@@ -15,6 +15,7 @@ package org.apache.juneau.rest;
 import static org.apache.juneau.rest.HttpRuntimeException.*;
 
 import java.io.*;
+import java.lang.reflect.*;
 import java.util.*;
 
 import org.apache.http.*;
@@ -188,14 +189,16 @@ public class RestOpSession extends ContextSession {
                                if (output != null || ! 
res.getOutputStreamCalled())
                                        res.setOutput(output);
                        }
-               } catch (ExecutableException e) {
-                       Throwable e2 = e.unwrap();  // Get the throwable thrown 
from the doX() method.
+               } catch (IllegalAccessException e) {
+                       throw InternalServerError.create().message("Error 
occurred invoking method ''{0}''.", 
methodInvoker.inner().getFullName()).causedBy(e).build();
+               } catch (InvocationTargetException e) {
+                       Throwable e2 = e.getTargetException();  // Get the 
throwable thrown from the doX() method.
                        res.setStatus(500);  // May be overridden later.
                        Class<?> c = e2.getClass();
                        if (e2 instanceof HttpResponse || 
c.getAnnotation(Response.class) != null || c.getAnnotation(ResponseBody.class) 
!= null) {
                                res.setOutput(e2);
                        } else {
-                               throw e.unwrap();
+                               throw e2;
                        }
                } catch (IllegalArgumentException e) {
                        MethodInfo mi = MethodInfo.of(m);

Reply via email to