Java Code Examples for org.apache.tomcat.util.ExceptionUtils#unwrapInvocationTargetException()

The following examples show how to use org.apache.tomcat.util.ExceptionUtils#unwrapInvocationTargetException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebappLoader.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private String getClasspath( ClassLoader loader ) {
    try {
        Method m=loader.getClass().getMethod("getClasspath", new Class[] {});
        if( log.isTraceEnabled())
            log.trace("getClasspath " + m );
        if( m==null ) return null;
        Object o=m.invoke( loader, new Object[] {} );
        if( log.isDebugEnabled() )
            log.debug("gotClasspath " + o);
        if( o instanceof String )
            return (String)o;
        return null;
    } catch( Exception ex ) {
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(ex);
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled())
            log.debug("getClasspath ", ex);
    }
    return null;
}
 
Example 2
Source File: WebappLoader.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private String getClasspath( ClassLoader loader ) {
    try {
        Method m=loader.getClass().getMethod("getClasspath", new Class[] {});
        if( log.isTraceEnabled())
            log.trace("getClasspath " + m );
        Object o=m.invoke( loader, new Object[] {} );
        if( log.isDebugEnabled() )
            log.debug("gotClasspath " + o);
        if( o instanceof String )
            return (String)o;
        return null;
    } catch( Exception ex ) {
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(ex);
        ExceptionUtils.handleThrowable(t);
        if (log.isDebugEnabled())
            log.debug("getClasspath ", ex);
    }
    return null;
}
 
Example 3
Source File: WebappClassLoaderBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Deregister any JDBC drivers registered by the webapp that the webapp
 * forgot. This is made unnecessary complex because a) DriverManager
 * checks the class loader of the calling class (it would be much easier
 * if it checked the context class loader) b) using reflection would
 * create a dependency on the DriverManager implementation which can,
 * and has, changed.
 *
 * We can't just create an instance of JdbcLeakPrevention as it will be
 * loaded by the common class loader (since it's .class file is in the
 * $CATALINA_HOME/lib directory). This would fail DriverManager's check
 * on the class loader of the calling class. So, we load the bytes via
 * our parent class loader but define the class with this class loader
 * so the JdbcLeakPrevention looks like a webapp class to the
 * DriverManager.
 *
 * If only apps cleaned up after themselves...
 */
private final void clearReferencesJdbc() {
    // We know roughly how big the class will be (~ 1K) so allow 2k as a
    // starting point
    byte[] classBytes = new byte[2048];
    int offset = 0;
    try (InputStream is = getResourceAsStream(
            "org/apache/catalina/loader/JdbcLeakPrevention.class")) {
        int read = is.read(classBytes, offset, classBytes.length-offset);
        while (read > -1) {
            offset += read;
            if (offset == classBytes.length) {
                // Buffer full - double size
                byte[] tmp = new byte[classBytes.length * 2];
                System.arraycopy(classBytes, 0, tmp, 0, classBytes.length);
                classBytes = tmp;
            }
            read = is.read(classBytes, offset, classBytes.length-offset);
        }
        Class<?> lpClass =
            defineClass("org.apache.catalina.loader.JdbcLeakPrevention",
                classBytes, 0, offset, this.getClass().getProtectionDomain());
        Object obj = lpClass.getConstructor().newInstance();
        @SuppressWarnings("unchecked")
        List<String> driverNames = (List<String>) obj.getClass().getMethod(
                "clearJdbcDriverRegistrations").invoke(obj);
        for (String name : driverNames) {
            log.warn(sm.getString("webappClassLoader.clearJdbc",
                    getContextName(), name));
        }
    } catch (Exception e) {
        // So many things to go wrong above...
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
        ExceptionUtils.handleThrowable(t);
        log.warn(sm.getString(
                "webappClassLoader.jdbcRemoveFailed", getContextName()), t);
    }
}
 
Example 4
Source File: PojoMessageHandlerBase.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected final void handlePojoMethodException(Throwable t) {
    t = ExceptionUtils.unwrapInvocationTargetException(t);
    ExceptionUtils.handleThrowable(t);
    if (t instanceof RuntimeException) {
        throw (RuntimeException) t;
    } else {
        throw new RuntimeException(t.getMessage(), t);
    }
}
 
Example 5
Source File: PojoMessageHandlerBase.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected final void handlePojoMethodException(Throwable t) {
    t = ExceptionUtils.unwrapInvocationTargetException(t);
    ExceptionUtils.handleThrowable(t);
    if (t instanceof RuntimeException) {
        throw (RuntimeException) t;
    } else {
        throw new RuntimeException(t.getMessage(), t);
    }
}
 
Example 6
Source File: PojoMessageHandlerBase.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected final void handlePojoMethodException(Throwable t) {
    t = ExceptionUtils.unwrapInvocationTargetException(t);
    ExceptionUtils.handleThrowable(t);
    if (t instanceof RuntimeException) {
        throw (RuntimeException) t;
    } else {
        throw new RuntimeException(t.getMessage(), t);
    }
}
 
Example 7
Source File: WebappLoader.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Start associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.starting"));

    if (context.getResources() == null) {
        log.info("No resources for " + context);
        setState(LifecycleState.STARTING);
        return;
    }

    // Construct a class loader based on our current repositories list
    try {

        classLoader = createClassLoader();
        classLoader.setResources(context.getResources());
        classLoader.setDelegate(this.delegate);

        // Configure our repositories
        setClassPath();

        setPermissions();

        ((Lifecycle) classLoader).start();

        String contextName = context.getName();
        if (!contextName.startsWith("/")) {
            contextName = "/" + contextName;
        }
        ObjectName cloname = new ObjectName(context.getDomain() + ":type=" +
                classLoader.getClass().getSimpleName() + ",host=" +
                context.getParent().getName() + ",context=" + contextName);
        Registry.getRegistry(null, null)
            .registerComponent(classLoader, cloname, null);

    } catch (Throwable t) {
        t = ExceptionUtils.unwrapInvocationTargetException(t);
        ExceptionUtils.handleThrowable(t);
        log.error( "LifecycleException ", t );
        throw new LifecycleException("start: ", t);
    }

    setState(LifecycleState.STARTING);
}
 
Example 8
Source File: WebappClassLoaderBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private void clearReferencesStopTimerThread(Thread thread) {

        // Need to get references to:
        // in Sun/Oracle JDK:
        // - newTasksMayBeScheduled field (in java.util.TimerThread)
        // - queue field
        // - queue.clear()
        // in IBM JDK, Apache Harmony:
        // - cancel() method (in java.util.Timer$TimerImpl)

        try {

            try {
                Field newTasksMayBeScheduledField =
                    thread.getClass().getDeclaredField("newTasksMayBeScheduled");
                newTasksMayBeScheduledField.setAccessible(true);
                Field queueField = thread.getClass().getDeclaredField("queue");
                queueField.setAccessible(true);

                Object queue = queueField.get(thread);

                Method clearMethod = queue.getClass().getDeclaredMethod("clear");
                clearMethod.setAccessible(true);

                synchronized(queue) {
                    newTasksMayBeScheduledField.setBoolean(thread, false);
                    clearMethod.invoke(queue);
                    // In case queue was already empty. Should only be one
                    // thread waiting but use notifyAll() to be safe.
                    queue.notifyAll();
                }

            }catch (NoSuchFieldException nfe){
                Method cancelMethod = thread.getClass().getDeclaredMethod("cancel");
                synchronized(thread) {
                    cancelMethod.setAccessible(true);
                    cancelMethod.invoke(thread);
                }
            }

            log.warn(sm.getString("webappClassLoader.warnTimerThread",
                    getContextName(), thread.getName()));

        } catch (Exception e) {
            // So many things to go wrong above...
            Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(t);
            log.warn(sm.getString(
                    "webappClassLoader.stopTimerThreadFail",
                    thread.getName(), getContextName()), t);
        }
    }
 
Example 9
Source File: StatusTransformer.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Write the OS state.
 *
 * @param writer The output writer
 * @param mode Mode <code>0</code> will generate HTML.
 *             Mode <code>1</code> will generate XML.
 * @param args I18n labels for the OS state values
 */
public static void writeOSState(PrintWriter writer, int mode, Object[] args) {
    long[] result = new long[16];
    boolean ok = false;
    try {
        String methodName = "info";
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = result.getClass();
        Object paramValues[] = new Object[1];
        paramValues[0] = result;
        Method method = Class.forName("org.apache.tomcat.jni.OS")
            .getMethod(methodName, paramTypes);
        method.invoke(null, paramValues);
        ok = true;
    } catch (Throwable t) {
        t = ExceptionUtils.unwrapInvocationTargetException(t);
        ExceptionUtils.handleThrowable(t);
    }

    if (ok) {
        if (mode == 0){
            writer.print("<h1>OS</h1>");

            writer.print("<p>");
            writer.print( args[0] );
            writer.print(' ');
            writer.print(formatSize(Long.valueOf(result[0]), true));
            writer.print(' ');
            writer.print(args[1]);
            writer.print(' ');
            writer.print(formatSize(Long.valueOf(result[1]), true));
            writer.print(' ');
            writer.print(args[2]);
            writer.print(' ');
            writer.print(formatSize(Long.valueOf(result[2]), true));
            writer.print(' ');
            writer.print(args[3]);
            writer.print(' ');
            writer.print(formatSize(Long.valueOf(result[3]), true));
            writer.print(' ');
            writer.print(args[4]);
            writer.print(' ');
            writer.print(Long.valueOf(result[6]));
            writer.print("<br>");
            writer.print(args[5]);
            writer.print(' ');
            writer.print(formatTime(Long.valueOf(result[11] / 1000), true));
            writer.print(' ');
            writer.print(args[6]);
            writer.print(' ');
            writer.print(formatTime(Long.valueOf(result[12] / 1000), true));
            writer.print("</p>");
        } else if (mode == 1){
            // NO-OP
        }
    }

}
 
Example 10
Source File: WebappClassLoaderBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Deregister any JDBC drivers registered by the webapp that the webapp
 * forgot. This is made unnecessary complex because a) DriverManager
 * checks the class loader of the calling class (it would be much easier
 * if it checked the context class loader) b) using reflection would
 * create a dependency on the DriverManager implementation which can,
 * and has, changed.
 *
 * We can't just create an instance of JdbcLeakPrevention as it will be
 * loaded by the common class loader (since it's .class file is in the
 * $CATALINA_HOME/lib directory). This would fail DriverManager's check
 * on the class loader of the calling class. So, we load the bytes via
 * our parent class loader but define the class with this class loader
 * so the JdbcLeakPrevention looks like a webapp class to the
 * DriverManager.
 *
 * If only apps cleaned up after themselves...
 */
private final void clearReferencesJdbc() {
    InputStream is = getResourceAsStream(
            "org/apache/catalina/loader/JdbcLeakPrevention.class");
    // We know roughly how big the class will be (~ 1K) so allow 2k as a
    // starting point
    byte[] classBytes = new byte[2048];
    int offset = 0;
    try {
        int read = is.read(classBytes, offset, classBytes.length-offset);
        while (read > -1) {
            offset += read;
            if (offset == classBytes.length) {
                // Buffer full - double size
                byte[] tmp = new byte[classBytes.length * 2];
                System.arraycopy(classBytes, 0, tmp, 0, classBytes.length);
                classBytes = tmp;
            }
            read = is.read(classBytes, offset, classBytes.length-offset);
        }
        Class<?> lpClass =
            defineClass("org.apache.catalina.loader.JdbcLeakPrevention",
                classBytes, 0, offset, this.getClass().getProtectionDomain());
        Object obj = lpClass.newInstance();
        @SuppressWarnings("unchecked") // clearJdbcDriverRegistrations() returns List<String>
        List<String> driverNames = (List<String>) obj.getClass().getMethod(
                "clearJdbcDriverRegistrations").invoke(obj);
        for (String name : driverNames) {
            log.error(sm.getString("webappClassLoader.clearJdbc",
                    contextName, name));
        }
    } catch (Exception e) {
        // So many things to go wrong above...
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
        ExceptionUtils.handleThrowable(t);
        log.warn(sm.getString(
                "webappClassLoader.jdbcRemoveFailed", contextName), t);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                log.warn(sm.getString(
                        "webappClassLoader.jdbcRemoveStreamError",
                        contextName), ioe);
            }
        }
    }
}
 
Example 11
Source File: WebappClassLoaderBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private void clearReferencesStopTimerThread(Thread thread) {

        // Need to get references to:
        // in Sun/Oracle JDK:
        // - newTasksMayBeScheduled field (in java.util.TimerThread)
        // - queue field
        // - queue.clear()
        // in IBM JDK, Apache Harmony:
        // - cancel() method (in java.util.Timer$TimerImpl)

        try {

            try {
                Field newTasksMayBeScheduledField =
                    thread.getClass().getDeclaredField("newTasksMayBeScheduled");
                newTasksMayBeScheduledField.setAccessible(true);
                Field queueField = thread.getClass().getDeclaredField("queue");
                queueField.setAccessible(true);

                Object queue = queueField.get(thread);

                Method clearMethod = queue.getClass().getDeclaredMethod("clear");
                clearMethod.setAccessible(true);

                synchronized(queue) {
                    newTasksMayBeScheduledField.setBoolean(thread, false);
                    clearMethod.invoke(queue);
                    queue.notify();  // In case queue was already empty.
                }

            }catch (NoSuchFieldException nfe){
                Method cancelMethod = thread.getClass().getDeclaredMethod("cancel");
                synchronized(thread) {
                    cancelMethod.setAccessible(true);
                    cancelMethod.invoke(thread);
                }
            }

            log.error(sm.getString("webappClassLoader.warnTimerThread",
                    contextName, thread.getName()));

        } catch (Exception e) {
            // So many things to go wrong above...
            Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(t);
            log.warn(sm.getString(
                    "webappClassLoader.stopTimerThreadFail",
                    thread.getName(), contextName), t);
        }
    }
 
Example 12
Source File: StatusTransformer.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Write the OS state. Mode 0 will generate HTML.
 * Mode 1 will generate XML.
 */
public static void writeOSState(PrintWriter writer, int mode) {
    long[] result = new long[16];
    boolean ok = false;
    try {
        String methodName = "info";
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = result.getClass();
        Object paramValues[] = new Object[1];
        paramValues[0] = result;
        Method method = Class.forName("org.apache.tomcat.jni.OS")
            .getMethod(methodName, paramTypes);
        method.invoke(null, paramValues);
        ok = true;
    } catch (Throwable t) {
        t = ExceptionUtils.unwrapInvocationTargetException(t);
        ExceptionUtils.handleThrowable(t);
    }
    
    if (ok) {
        if (mode == 0){
            writer.print("<h1>OS</h1>");

            writer.print("<p>");
            writer.print(" Physical memory: ");
            writer.print(formatSize(Long.valueOf(result[0]), true));
            writer.print(" Available memory: ");
            writer.print(formatSize(Long.valueOf(result[1]), true));
            writer.print(" Total page file: ");
            writer.print(formatSize(Long.valueOf(result[2]), true));
            writer.print(" Free page file: ");
            writer.print(formatSize(Long.valueOf(result[3]), true));
            writer.print(" Memory load: ");
            writer.print(Long.valueOf(result[6]));
            writer.print("<br>");
            writer.print(" Process kernel time: ");
            writer.print(formatTime(Long.valueOf(result[11] / 1000), true));
            writer.print(" Process user time: ");
            writer.print(formatTime(Long.valueOf(result[12] / 1000), true));
            writer.print("</p>");
        } else if (mode == 1){
            // NO-OP
        }
    }
    
}
 
Example 13
Source File: WebappClassLoaderBase.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Deregister any JDBC drivers registered by the webapp that the webapp
 * forgot. This is made unnecessary complex because a) DriverManager
 * checks the class loader of the calling class (it would be much easier
 * if it checked the context class loader) b) using reflection would
 * create a dependency on the DriverManager implementation which can,
 * and has, changed.
 *
 * We can't just create an instance of JdbcLeakPrevention as it will be
 * loaded by the common class loader (since it's .class file is in the
 * $CATALINA_HOME/lib directory). This would fail DriverManager's check
 * on the class loader of the calling class. So, we load the bytes via
 * our parent class loader but define the class with this class loader
 * so the JdbcLeakPrevention looks like a webapp class to the
 * DriverManager.
 *
 * If only apps cleaned up after themselves...
 */
private final void clearReferencesJdbc() {
    InputStream is = getResourceAsStream(
            "org/apache/catalina/loader/JdbcLeakPrevention.class");
    // We know roughly how big the class will be (~ 1K) so allow 2k as a
    // starting point
    byte[] classBytes = new byte[2048];
    int offset = 0;
    try {
        int read = is.read(classBytes, offset, classBytes.length-offset);
        while (read > -1) {
            offset += read;
            if (offset == classBytes.length) {
                // Buffer full - double size
                byte[] tmp = new byte[classBytes.length * 2];
                System.arraycopy(classBytes, 0, tmp, 0, classBytes.length);
                classBytes = tmp;
            }
            read = is.read(classBytes, offset, classBytes.length-offset);
        }
        Class<?> lpClass =
            defineClass("org.apache.catalina.loader.JdbcLeakPrevention",
                classBytes, 0, offset, this.getClass().getProtectionDomain());
        Object obj = lpClass.newInstance();
        @SuppressWarnings("unchecked") // clearJdbcDriverRegistrations() returns List<String>
        List<String> driverNames = (List<String>) obj.getClass().getMethod(
                "clearJdbcDriverRegistrations").invoke(obj);
        for (String name : driverNames) {
            log.error(sm.getString("webappClassLoader.clearJdbc",
                    contextName, name));
        }
    } catch (Exception e) {
        // So many things to go wrong above...
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
        ExceptionUtils.handleThrowable(t);
        log.warn(sm.getString(
                "webappClassLoader.jdbcRemoveFailed", contextName), t);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ioe) {
                log.warn(sm.getString(
                        "webappClassLoader.jdbcRemoveStreamError",
                        contextName), ioe);
            }
        }
    }
}
 
Example 14
Source File: WebappClassLoaderBase.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private void clearReferencesStopTimerThread(Thread thread) {

        // Need to get references to:
        // in Sun/Oracle JDK:
        // - newTasksMayBeScheduled field (in java.util.TimerThread)
        // - queue field
        // - queue.clear()
        // in IBM JDK, Apache Harmony:
        // - cancel() method (in java.util.Timer$TimerImpl)

        try {

            try {
                Field newTasksMayBeScheduledField =
                    thread.getClass().getDeclaredField("newTasksMayBeScheduled");
                newTasksMayBeScheduledField.setAccessible(true);
                Field queueField = thread.getClass().getDeclaredField("queue");
                queueField.setAccessible(true);

                Object queue = queueField.get(thread);

                Method clearMethod = queue.getClass().getDeclaredMethod("clear");
                clearMethod.setAccessible(true);

                synchronized(queue) {
                    newTasksMayBeScheduledField.setBoolean(thread, false);
                    clearMethod.invoke(queue);
                    queue.notify();  // In case queue was already empty.
                }

            }catch (NoSuchFieldException nfe){
                Method cancelMethod = thread.getClass().getDeclaredMethod("cancel");
                synchronized(thread) {
                    cancelMethod.setAccessible(true);
                    cancelMethod.invoke(thread);
                }
            }

            log.error(sm.getString("webappClassLoader.warnTimerThread",
                    contextName, thread.getName()));

        } catch (Exception e) {
            // So many things to go wrong above...
            Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
            ExceptionUtils.handleThrowable(t);
            log.warn(sm.getString(
                    "webappClassLoader.stopTimerThreadFail",
                    thread.getName(), contextName), t);
        }
    }
 
Example 15
Source File: StatusTransformer.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Write the OS state. Mode 0 will generate HTML.
 * Mode 1 will generate XML.
 */
public static void writeOSState(PrintWriter writer, int mode) {
    long[] result = new long[16];
    boolean ok = false;
    try {
        String methodName = "info";
        Class<?> paramTypes[] = new Class[1];
        paramTypes[0] = result.getClass();
        Object paramValues[] = new Object[1];
        paramValues[0] = result;
        Method method = Class.forName("org.apache.tomcat.jni.OS")
            .getMethod(methodName, paramTypes);
        method.invoke(null, paramValues);
        ok = true;
    } catch (Throwable t) {
        t = ExceptionUtils.unwrapInvocationTargetException(t);
        ExceptionUtils.handleThrowable(t);
    }
    
    if (ok) {
        if (mode == 0){
            writer.print("<h1>OS</h1>");

            writer.print("<p>");
            writer.print(" Physical memory: ");
            writer.print(formatSize(Long.valueOf(result[0]), true));
            writer.print(" Available memory: ");
            writer.print(formatSize(Long.valueOf(result[1]), true));
            writer.print(" Total page file: ");
            writer.print(formatSize(Long.valueOf(result[2]), true));
            writer.print(" Free page file: ");
            writer.print(formatSize(Long.valueOf(result[3]), true));
            writer.print(" Memory load: ");
            writer.print(Long.valueOf(result[6]));
            writer.print("<br>");
            writer.print(" Process kernel time: ");
            writer.print(formatTime(Long.valueOf(result[11] / 1000), true));
            writer.print(" Process user time: ");
            writer.print(formatTime(Long.valueOf(result[12] / 1000), true));
            writer.print("</p>");
        } else if (mode == 1){
            // NO-OP
        }
    }
    
}