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:org.openamf.invoker.EJBServiceInvoker.java

public Object invokeService() throws ServiceInvocationException {
    Object serviceResult = null;/*from w w w  . j  ava  2 s  . co  m*/
    try {
        Object ejb = null;

        if (persistService) {
            log.debug("Trying to get Handle from persistentObject");
            Handle handle = (Handle) getPersistentServiceObject();
            if (handle != null) {
                log.debug("Got Handle from persistentObject");
                ejb = handle.getEJBObject();
            }
        }

        if (ejb == null) {
            if (home == null) {
                log.warn("Home is NULL - unable to find EJB " + request.getServiceName());
                return null;
            }
            Method ejbCreateMethod = home.getClass().getMethod("create", new Class[0]);
            log.info("Calling Create Method: " + request.getServiceName());
            ejb = ejbCreateMethod.invoke(home, new Object[0]);
        }

        serviceResult = invokeServiceMethod(ejb, ejb.getClass(), request.getServiceMethodName(),
                request.getParameters());

        if (persistService) {
            if (ejb instanceof EJBObject) {
                log.debug("setting persistentObject = Handle");
                setPersistentServiceObject(((EJBObject) ejb).getHandle());
            }
        }

    } catch (InvocationTargetException e) {
        //use the cause since the exception is thrown when 
        //the service method throws an exception
        throw new ServiceInvocationException(request, e.getTargetException());
    } catch (Exception e) {
        throw new ServiceInvocationException(request, e);
    }
    return serviceResult;
}

From source file:org.openamf.invoker.JavaServiceInvoker.java

public Object invokeService() throws ServiceInvocationException {
    Class serviceClass = null;//  ww w.j a v  a 2  s .  c  o  m
    Object service = null;
    Object serviceResult = null;

    try {
        serviceClass = loadClass(request.getServiceName());
        service = getPersistentServiceObject();
        if (service == null) {
            service = serviceClass.newInstance();
        }
        serviceResult = invokeServiceMethod(service, serviceClass, request.getServiceMethodName(),
                request.getParameters());
        setPersistentServiceObject(service);
    } catch (InvocationTargetException e) {
        //use the cause since the exception is thrown when 
        //the service method throws an exception
        throw new ServiceInvocationException(request, e.getTargetException());
    } catch (Exception e) {
        throw new ServiceInvocationException(request, e);
    }

    return serviceResult;
}

From source file:org.opencms.workplace.CmsWidgetDialogParameter.java

/**
 * "Commits" (writes) the value of this widget back to the underlying base object.<p> 
 * //from  w ww. j  av a2  s. c o  m
 * @param dialog the widget dialog where the parameter is used on
 * 
 * @throws CmsException in case the String value of the widget is invalid for the base Object
 */
@SuppressWarnings("unchecked")
public void commitValue(CmsWidgetDialog dialog) throws CmsException {

    if (m_baseCollection == null) {
        PropertyUtilsBean bean = new PropertyUtilsBean();
        ConvertUtilsBean converter = new ConvertUtilsBean();
        Object value = null;
        try {
            Class<?> type = bean.getPropertyType(m_baseObject, m_baseObjectProperty);
            value = converter.convert(m_value, type);
            bean.setNestedProperty(m_baseObject, m_baseObjectProperty, value);
            setError(null);
        } catch (InvocationTargetException e) {
            setError(e.getTargetException());
            throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value,
                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()),
                    m_baseObject.getClass().getName()), e.getTargetException(), this);
        } catch (Exception e) {
            setError(e);
            throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value,
                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()),
                    m_baseObject.getClass().getName()), e, this);
        }
    } else if (m_baseCollection instanceof SortedMap) {
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) {
            int pos = m_value.indexOf('=');
            if ((pos > 0) && (pos < (m_value.length() - 1))) {
                String key = m_value.substring(0, pos);
                String value = m_value.substring(pos + 1);
                @SuppressWarnings("rawtypes")
                SortedMap map = (SortedMap) m_baseCollection;
                if (map.containsKey(key)) {
                    Object val = map.get(key);
                    CmsWidgetException error = new CmsWidgetException(
                            Messages.get().container(Messages.ERR_MAP_DUPLICATE_KEY_3,
                                    dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), key, val),
                            this);
                    setError(error);
                    throw error;
                }
                map.put(key, value);
            } else {
                CmsWidgetException error = new CmsWidgetException(
                        Messages.get().container(Messages.ERR_MAP_PARAMETER_FORM_1,
                                dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey())),
                        this);
                setError(error);
                throw error;
            }
        }
    } else if (m_baseCollection instanceof List) {
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) {
            @SuppressWarnings("rawtypes")
            List list = (List) m_baseCollection;
            list.add(m_value);
        }
    }
}

From source file:org.openhie.openempi.util.ConvertingWrapDynaBean.java

/**
 * In addition to be able to retrieve properties stored in the record using
 * the built-in capabilities of the BeansUtils interface, we also added the
 * capability of retrieving a value from a property that has a one-to-many
 * relationship to the base object./*from ww  w  .  j av a 2  s .c o  m*/
 * 
 * For example, we want to be able to retrieve one of the identifiers associated
 * with a person object and person has a one-to-many association with person
 * identifiers. To access a specific identifier using the identifier domain
 * as the key to the search, you need to use the following syntax in the
 * addressing of the property.
 * 
 * basePropertyName:searchKey:property
 * basePropertyName is the name of the property in the base object that has
 *       the one-to-many association. This property should be of type Set.
 * searchKey: is the string to use to identify the one of multiple possible
 *       values in the set.
 * property: the attribute from the instance of the object in the set that
 *       should be returned.
 * 
 */
@SuppressWarnings("rawtypes")
public Object get(String name) {
    Object value = null;
    try {
        if (name.indexOf(':') >= 0) {
            int posOfColon = name.indexOf(':');
            String key = name.substring(0, posOfColon);
            value = PropertyUtils.getNestedProperty(instance, key);
            if (value instanceof java.util.Set) {
                java.util.Set set = (java.util.Set) value;
                int secondColon = name.indexOf(':', posOfColon + 1);
                String searchKey = name.substring(posOfColon + 1, secondColon);
                for (Object val : set) {
                    if (val.toString().indexOf(searchKey) >= 0) {
                        String propertyName = name.substring(secondColon + 1, name.length());
                        Object pval = PropertyUtils.getNestedProperty(val, propertyName);
                        return pval;
                    }
                }
                return null;
            }
        }
        value = PropertyUtils.getNestedProperty(instance, name);
    } catch (InvocationTargetException ite) {
        Throwable cause = ite.getTargetException();
        throw new IllegalArgumentException("Error reading property '" + name + "' nested exception - " + cause);
    } catch (Throwable t) {
        throw new IllegalArgumentException("Error reading property '" + name + "', exception - " + t);
    }
    return (value);
}

From source file:org.openqa.selenium.server.browserlaunchers.BrowserLauncherFactory.java

private BrowserLauncher createBrowserLauncher(Class<? extends BrowserLauncher> c, String browserStartCommand,
        String sessionId, RemoteControlConfiguration configuration,
        BrowserConfigurationOptions browserOptions) {
    try {//w w  w.j a v a  2s  . co  m
        try {
            final BrowserLauncher browserLauncher;
            final Constructor<? extends BrowserLauncher> ctor;
            ctor = c.getConstructor(BrowserConfigurationOptions.class, RemoteControlConfiguration.class,
                    String.class, String.class);
            browserLauncher = ctor.newInstance(browserOptions, configuration, sessionId, browserStartCommand);

            return browserLauncher;
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        }
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openspaces.core.jini.JiniServiceFactoryBean.java

/**
 * When using smart proxy, wraps the invocation of a service method and in case of failure will
 * try and perform another lookup for the service.
 *//*from  www.j  av a 2  s . co  m*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    int retries = retryCountOnFailure;
    while (true) {
        try {
            if (actualService == null) {
                synchronized (actualServiceMonitor) {
                    if (actualService == null) {
                        actualService = lookupService();
                    }
                }
            }
            return methodInvocation.getMethod().invoke(actualService, methodInvocation.getArguments());
        } catch (InvocationTargetException e) {
            if (logger.isTraceEnabled()) {
                logger.trace("Failed to execute [" + methodInvocation.getMethod().getName() + "] on ["
                        + actualService + "]", e);
            }
            synchronized (actualServiceMonitor) {
                actualService = null;
            }
            // we have an invocation exception, if we got to the reties
            // throw it, if not, lookup another service and try it
            if (--retries == 0) {
                throw e.getTargetException();
            }
        }
    }
}

From source file:org.opentaps.testsuit.web.IntegrTestsServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*w  w  w.j av  a  2  s. co m*/
        String serviceName = request.getParameter("service");
        String testName = request.getParameter("test");
        if (GenericValidator.isBlankOrNull(serviceName) || GenericValidator.isBlankOrNull(testName)) {
            throw new ServletException("Missing required parameters.");
        }

        InitialContext context = new InitialContext();
        Object testSrvc = context.lookup(String.format("osgi:service/%1$s", serviceName));
        if (testSrvc == null) {
            throw new ServletException("Test service is unavailable.");
        }

        PrintWriter out = response.getWriter();

        try {
            Method testCase = ClassUtils.getPublicMethod(testSrvc.getClass(), testName, new Class[0]);
            testCase.invoke(testSrvc, new Object[] {});

            out.println(BaseTestCase.SUCCESS_RET_CODE);

        } catch (InvocationTargetException e) {
            out.println(e.getTargetException().getLocalizedMessage());
            return;
        }

    } catch (NamingException e) {
        throw new ServletException(e);
    } catch (IllegalArgumentException e) {
        throw new ServletException(e);
    } catch (IllegalAccessException e) {
        throw new ServletException(e);
    } catch (SecurityException e) {
        throw new ServletException(e);
    } catch (NoSuchMethodException e) {
        throw new ServletException(e);
    }
}

From source file:org.ops4j.gaderian.examples.impl.ProxyLoggingInvocationHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    boolean debug = _log.isDebugEnabled();

    if (debug)//from  ww w.  j a  va  2  s  .co m
        LoggingUtils.entry(_log, method.getName(), args);

    try {
        Object result = method.invoke(_delegate, args);

        if (debug) {
            if (method.getReturnType() == void.class)
                LoggingUtils.voidExit(_log, method.getName());
            else
                LoggingUtils.exit(_log, method.getName(), result);
        }

        return result;
    } catch (InvocationTargetException ex) {
        Throwable targetException = ex.getTargetException();

        if (debug)
            LoggingUtils.exception(_log, method.getName(), targetException);

        throw targetException;
    }
}

From source file:org.ops4j.gaderian.service.impl.AssemblyInstructionImpl.java

private void invokeInitializer(Object service, AssemblyParameters factoryParameters) {
    String methodName = getInitializeMethod();

    boolean allowMissing = Gaderian.isBlank(methodName);

    String searchMethodName = allowMissing ? "initializeService" : methodName;

    try {/*from  w w w  . j av a  2 s .c  o m*/
        findAndInvokeInitializerMethod(service, searchMethodName, allowMissing);
    } catch (InvocationTargetException ex) {
        Throwable cause = ex.getTargetException();

        factoryParameters.getErrorLog()
                .error(ServiceMessages.unableToInitializeService(factoryParameters.getServiceId(),
                        searchMethodName, service.getClass(), cause), getLocation(), cause);
    } catch (Exception ex) {
        factoryParameters.getErrorLog().error(ServiceMessages.unableToInitializeService(
                factoryParameters.getServiceId(), searchMethodName, service.getClass(), ex), getLocation(), ex);
    }
}

From source file:org.plasma.sdo.DataConverterTest.java

/**
 * Checks the given data type as a target for all other SDO datatypes, expecting
 * conversion errors thrown from the DataConverter based on the DataConverter.getAllowableTargetTypes()
 * method. //from  w  w w  .  ja  va 2 s . c  om
 * @param testDataType
 * @throws Throwable
 */
private void checkAsTarget(DataType testDataType) throws Throwable {
    DataType[] allTypes = DataType.values();
    for (int i = 0; i < allTypes.length; i++) {
        DataType currentDataType = allTypes[i];
        Type currentType = TypeHelper.INSTANCE.getType(
                PlasmaConfig.getInstance().getSDODataTypesNamespace().getUri(), currentDataType.name());

        List<DataType> allowedFromTestDataType = DataConverter.INSTANCE.getAllowableTargetTypes(testDataType);
        // check the current type as a target type
        Class[] fromArgsTypes = { Type.class, DataConverter.INSTANCE.toPrimitiveJavaClass(testDataType) };
        String fromMethodName = "from" + testDataType.name();
        Method fromMethod = DataConverter.INSTANCE.getClass().getMethod(fromMethodName, fromArgsTypes);
        Object[] fromArgs = { currentType, testValues[testDataType.ordinal()][0] };

        if (findDataType(currentDataType, allowedFromTestDataType)) {

            try {
                Object result = fromMethod.invoke(DataConverter.INSTANCE, fromArgs);
                log.info("(" + testDataType.name() + ") converted to " + currentDataType.toString() + " from "
                        + testDataType.toString() + " (" + String.valueOf(result) + ")");
            } catch (InvocationTargetException e) {
                throw e.getTargetException();
            }
        } else {
            try {
                fromMethod.invoke(DataConverter.INSTANCE, fromArgs);
                assertTrue(
                        "expected error on conversion " + testDataType.name() + "->" + currentDataType.name(),
                        false); // force assert
            } catch (InvocationTargetException e) {
                assertTrue("expected class-cast-exception",
                        e.getTargetException() instanceof ClassCastException);
                log.debug("(" + testDataType.name() + ") prevented conversion to " + currentDataType.toString()
                        + " from " + testDataType.toString() + " ");
            }
        }
    }
}