Example usage for java.lang.reflect InvocationTargetException getTargetException

List of usage examples for java.lang.reflect InvocationTargetException getTargetException

Introduction

In this page you can find the example usage for java.lang.reflect InvocationTargetException getTargetException.

Prototype

public Throwable getTargetException() 

Source Link

Document

Get the thrown target exception.

Usage

From source file:com.alibaba.wasp.util.JVMClusterUtil.java

/**
 * Creates a {@link MasterThread}./*from www .ja  v  a 2s. co m*/
 * Call 'start' on the returned thread to make it run.
 * @param c Configuration to use.
 * @param hmc Class to create.
 * @param index Used distinguishing the object returned.
 * @throws java.io.IOException
 * @return Master added.
 */
public static JVMClusterUtil.MasterThread createMasterThread(final Configuration c,
        final Class<? extends FMaster> hmc, final int index) throws IOException {
    FMaster server;
    try {
        server = hmc.getConstructor(Configuration.class).newInstance(c);
    } catch (InvocationTargetException ite) {
        Throwable target = ite.getTargetException();
        throw new RuntimeException("Failed construction of Master: " + hmc.toString()
                + ((target.getCause() != null) ? target.getCause().getMessage() : ""), target);
    } catch (Exception e) {
        IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
    return new JVMClusterUtil.MasterThread(server, index);
}

From source file:com.alibaba.wasp.util.JVMClusterUtil.java

/**
 * Creates a {@link FServerThread}.//  w ww. j a v a 2 s .co m
 * Call 'start' on the returned thread to make it run.
 * @param c Configuration to use.
 * @param hrsc Class to create.
 * @param index Used distinguishing the object returned.
 * @throws java.io.IOException
 * @return FServer added.
 */
public static JVMClusterUtil.FServerThread createFServerThread(final Configuration c,
        final Class<? extends FServer> hrsc, final int index) throws IOException {
    FServer server;
    try {
        Constructor<? extends FServer> ctor = hrsc.getConstructor(Configuration.class);
        ctor.setAccessible(true);
        server = ctor.newInstance(c);
    } catch (InvocationTargetException ite) {
        Throwable target = ite.getTargetException();
        throw new RuntimeException("Failed construction of FServer: " + hrsc.toString()
                + ((target.getCause() != null) ? target.getCause().getMessage() : ""), target);
    } catch (Exception e) {
        IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
    return new JVMClusterUtil.FServerThread(server, index);
}

From source file:controllers.GWT2Controller.java

/**
 * Invoke a Service /* w  w  w  .  j av  a2  s .  co  m*/
 * 
 * This way GWT2Service is allowed to use GWT2Chain
 * 
 * @param mod
 * @return
 * @throws Exception 
 */
private static String invoke(GWT2Service service, RPCRequest rpcRequest) throws Exception {
    String responseload = null;

    try {
        // Get GWt2Invoker
        GWT2Invoker invoker = new GWT2Invoker(service, request, response, session, rpcRequest);

        // run 
        boolean run = true;
        Object result = null;
        Object chainResult = null;
        boolean firstcall = true;
        GWT2ChainRuntime chain = null;
        while (run) {
            try {
                if (firstcall) {
                    firstcall = false;
                    // call service
                    result = invoker.invoke();
                    // get a result stop request
                    run = false;
                } else if (chain != null) {
                    // call callback
                    result = chain.getChain().chain(chainResult);
                    // get a result stop request
                    run = false;
                } else {
                    // no callback to call stop process
                    run = false;
                }
            } catch (GWT2ChainAsync casync) {
                // async chain call
                chain = casync;
                chainResult = chainAwait(chain);
            } catch (GWT2ChainSync csync) {
                chain = csync;
                chainResult = chainDo(chain);
            } catch (InvocationTargetException ite) {
                if (ite.getTargetException() instanceof GWT2ChainAsync) {
                    // async chain call
                    chain = (GWT2ChainAsync) ite.getTargetException();
                    chainResult = chainAwait(chain);
                } else if (ite.getTargetException() instanceof GWT2ChainSync) {
                    chain = (GWT2ChainSync) ite.getTargetException();
                    chainResult = chainDo(chain);
                } else
                    throw ite;
            }
        }

        // encode result
        responseload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result,
                rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        return responseload;

    } catch (Exception ex) {
        if (ex instanceof PlayException)
            // Rethrow the enclosed exception
            throw (PlayException) ex;

        // if not gwt rpc rethrow
        if (!isGWT())
            throw ex;

        try {
            // else encode error
            responseload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), ex,
                    rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        } catch (SerializationException e) {
            StackTraceElement element = PlayException.getInterestingStrackTraceElement(e);
            if (element != null) {
                throw new JavaExecutionException(Play.classes.getApplicationClass(element.getClassName()),
                        element.getLineNumber(), e);
            }
            throw new JavaExecutionException(e);
        }
    }

    return responseload;
}

From source file:TypeUtil.java

/** Convert String value to instance.
 * @param type The class of the instance, which may be a primitive TYPE field.
 * @param value The value as a string./*  ww w .  j  av  a  2 s .  c o m*/
 * @return The value as an Object.
 */
public static Object valueOf(Class type, String value) {
    try {
        if (type.equals(java.lang.String.class))
            return value;

        Method m = (Method) class2Value.get(type);
        if (m != null)
            return m.invoke(null, new Object[] { value });

        if (type.equals(java.lang.Character.TYPE) || type.equals(java.lang.Character.class))
            return new Character(value.charAt(0));

        Constructor c = type.getConstructor(stringArg);
        return c.newInstance(new Object[] { value });
    }

    catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof Error)
            throw (Error) (e.getTargetException());

    }
    return null;
}

From source file:com.espertech.esper.event.bean.BeanInstantiatorFactory.java

public static BeanInstantiator makeInstantiator(BeanEventType beanEventType,
        EngineImportService engineImportService) throws EventBeanManufactureException {

    // see if we use a factory method
    if (beanEventType.getFactoryMethodName() != null) {
        return resolveFactoryMethod(beanEventType, engineImportService);
    }/*from w w w  . j a  v  a2  s .  c om*/

    // find public ctor
    EngineImportException ctorNotFoundEx;
    try {
        engineImportService.resolveCtor(beanEventType.getUnderlyingType(), new Class[0]);
        if (beanEventType.getFastClass() != null) {
            return new BeanInstantiatorByNewInstanceFastClass(beanEventType.getFastClass());
        } else {
            return new BeanInstantiatorByNewInstanceReflection(beanEventType.getUnderlyingType());
        }
    } catch (EngineImportException ex) {
        ctorNotFoundEx = ex;
    }

    // not found ctor, see if FastClass can handle
    if (beanEventType.getFastClass() != null) {
        FastClass fastClass = beanEventType.getFastClass();
        try {
            fastClass.newInstance();
            return new BeanInstantiatorByNewInstanceFastClass(beanEventType.getFastClass());
        } catch (InvocationTargetException e) {
            String message = "Failed to instantiate class '" + fastClass.getJavaClass().getName()
                    + "', define a factory method if the class has no suitable constructors: "
                    + e.getTargetException().getMessage();
            log.debug(message);
        } catch (IllegalArgumentException e) {
            String message = "Failed to instantiate class '" + fastClass.getJavaClass().getName()
                    + "', define a factory method if the class has no suitable constructors";
            log.debug(message, e);
        }
    }

    // see if JVM ReflectionFactory (specific to JVM) may handle it
    if (SUN_JVM_OBJECT_CONSTRUCTOR != null) {
        Constructor ctor = OnDemandSunReflectionFactory.getConstructor(beanEventType.getUnderlyingType(),
                SUN_JVM_OBJECT_CONSTRUCTOR);
        return new BeanInstantiatorByCtor(ctor);
    }

    throw new EventBeanManufactureException(
            "Failed to find no-arg constructor and no factory method has been configured and cannot use Sun-JVM reflection to instantiate object of type "
                    + beanEventType.getUnderlyingType().getName(),
            ctorNotFoundEx);
}

From source file:com.espertech.esper.core.ResultDeliveryStrategyImpl.java

/**
 * Handle the exception, displaying a nice message and converting to {@link EPException}.
 * @param logger is the logger to use for error logging
 * @param e is the exception/*w  w  w . ja va2  s  .  c  om*/
 * @param params the method parameters
 * @param subscriber the object to deliver to
 * @param method the method to call
 * @throws EPException converted from the passed invocation exception
 */
protected static void handle(String statementName, Log logger, InvocationTargetException e, Object[] params,
        Object subscriber, FastMethod method) {
    String message = JavaClassHelper.getMessageInvocationTarget(statementName, method.getJavaMethod(),
            subscriber.getClass().getName(), params, e);
    logger.error(message, e.getTargetException());
}

From source file:com.espertech.esper.core.service.ResultDeliveryStrategyImpl.java

/**
 * Handle the exception, displaying a nice message and converting to {@link EPException}.
 * @param logger is the logger to use for error logging
 * @param e is the exception//from  w  ww  . ja  v a 2 s .  c  o m
 * @param parameters the method parameters
 * @param subscriber the object to deliver to
 * @param method the method to call
 * @throws EPException converted from the passed invocation exception
 */
protected static void handle(String statementName, Log logger, InvocationTargetException e, Object[] parameters,
        Object subscriber, FastMethod method) {
    String message = JavaClassHelper.getMessageInvocationTarget(statementName, method.getJavaMethod(),
            subscriber.getClass().getName(), parameters, e);
    logger.error(message, e.getTargetException());
}

From source file:lucee.commons.net.http.httpclient4.HTTPEngine4Impl.java

public static HttpResponse execute(HttpClient client, HttpUriRequest req, HttpContext context)
        throws ClientProtocolException, IOException {
    try {/*from  w w  w .  j ava 2  s.com*/
        Method exe = client.getClass().getMethod("execute",
                new Class[] { HttpUriRequest.class, HttpContext.class });
        return (HttpResponse) exe.invoke(client, new Object[] { req, context });
    } catch (InvocationTargetException ite) {
        Throwable t = ite.getTargetException();
        if (t instanceof IOException)
            throw (IOException) t;
        if (t instanceof ClientProtocolException)
            throw (ClientProtocolException) t;
        throw new RuntimeException(t);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.espertech.esper.event.vaevent.PropertyUtility.java

public static PropertyAccessException getInvocationTargetException(Method method, InvocationTargetException e) {
    Class declaring = method.getDeclaringClass();
    String message = "Failed to invoke method " + method.getName() + " on class "
            + JavaClassHelper.getClassNameFullyQualPretty(declaring) + ": "
            + e.getTargetException().getMessage();
    throw new PropertyAccessException(message, e);
}

From source file:br.gov.lexml.server.LexMLOAIHandler.java

public static String getResult(final HashMap attributes, final HttpServletRequest request,
        final HttpServletResponse response, final Transformer serverTransformer, final HashMap serverVerbs,
        final HashMap extensionVerbs, final String extensionPath) throws Throwable {
    try {/*  ww w .j  a va  2  s.co m*/
        boolean isExtensionVerb = extensionPath.equals(request.getPathInfo());
        String verb = request.getParameter("verb");
        if (debug) {
            log.debug("OAIHandler.getResult: verb=>" + verb + "<");
        }
        String result;
        Class verbClass = null;
        if (isExtensionVerb) {
            verbClass = (Class) extensionVerbs.get(verb);
        } else {
            verbClass = (Class) serverVerbs.get(verb);
        }
        if (verbClass == null) {
            verbClass = (Class) attributes.get("OAIHandler.missingVerbClass");
        }
        Method construct = verbClass.getMethod("construct", new Class[] { HashMap.class,
                HttpServletRequest.class, HttpServletResponse.class, Transformer.class });
        try {
            result = (String) construct.invoke(null,
                    new Object[] { attributes, request, response, serverTransformer });
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
        if (debug) {
            log.debug(result);
        }
        return result;
    } catch (NoSuchMethodException e) {
        throw new OAIInternalServerError(e.getMessage());
    } catch (IllegalAccessException e) {
        throw new OAIInternalServerError(e.getMessage());
    }
}