Search in sources :

Example 1 with ConsoleConfigClassLoader

use of org.hibernate.console.ConsoleConfigClassLoader in project jbosstools-hibernate by jbosstools.

the class HibernateExtension method cleanUpClassLoader.

protected boolean cleanUpClassLoader() {
    boolean resetted = false;
    if (executionContext != null) {
        executionContext.execute(new Command() {

            public Object execute() {
                Iterator<FakeDelegatingDriver> it = fakeDrivers.values().iterator();
                while (it.hasNext()) {
                    try {
                        DriverManager.deregisterDriver(it.next());
                    } catch (SQLException e) {
                    // ignore
                    }
                }
                return null;
            }
        });
    }
    if (fakeDrivers.size() > 0) {
        fakeDrivers.clear();
        resetted = true;
    }
    ClassLoader classLoaderTmp = classLoader;
    while (classLoaderTmp != null) {
        if (classLoaderTmp instanceof ConsoleConfigClassLoader) {
            ((ConsoleConfigClassLoader) classLoaderTmp).close();
            resetted = true;
        }
        classLoaderTmp = classLoaderTmp.getParent();
    }
    if (classLoader != null) {
        classLoader = null;
        resetted = true;
    }
    return resetted;
}
Also used : Command(org.hibernate.console.execution.ExecutionContext.Command) SQLException(java.sql.SQLException) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader) Iterator(java.util.Iterator) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader)

Example 2 with ConsoleConfigClassLoader

use of org.hibernate.console.ConsoleConfigClassLoader in project jbosstools-hibernate by jbosstools.

the class DriverDeleteTest method createJarClassLoader.

private ConsoleConfigClassLoader createJarClassLoader() {
    final URL[] customClassPathURLs = getCustomClassPathURLs();
    ConsoleConfigClassLoader urlClassLoader = AccessController.doPrivileged(new PrivilegedAction<ConsoleConfigClassLoader>() {

        public ConsoleConfigClassLoader run() {
            return new ConsoleConfigClassLoader(customClassPathURLs, getParentClassLoader()) {

                public InputStream getResourceAsStream(String name) {
                    InputStream is = super.getResourceAsStream(name);
                    return is;
                }

                public URL findResource(final String name) {
                    URL res = super.findResource(name);
                    return res;
                }

                public Enumeration<URL> findResources(final String name) throws IOException {
                    Enumeration<URL> res = super.findResources(name);
                    return res;
                }

                protected Class<?> findClass(String name) throws ClassNotFoundException {
                    Class<?> res = null;
                    try {
                        res = super.findClass(name);
                    } catch (ClassNotFoundException cnfe) {
                        throw cnfe;
                    }
                    return res;
                }

                protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    Class<?> res = null;
                    try {
                        res = super.loadClass(name, resolve);
                    } catch (ClassNotFoundException cnfe) {
                        throw cnfe;
                    }
                    return res;
                }

                public Class<?> loadClass(String name) throws ClassNotFoundException {
                    Class<?> res = null;
                    try {
                        res = super.loadClass(name);
                    } catch (ClassNotFoundException cnfe) {
                        throw cnfe;
                    }
                    return res;
                }
            };
        }
    });
    return urlClassLoader;
}
Also used : Enumeration(java.util.Enumeration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with ConsoleConfigClassLoader

use of org.hibernate.console.ConsoleConfigClassLoader in project jbosstools-hibernate by jbosstools.

the class ConsoleConfigurationUtils method getClassLoader.

public static ClassLoader getClassLoader(ConsoleConfiguration cc) {
    try {
        Field loaderField = cc.getClass().getDeclaredField("classLoader");
        loaderField.setAccessible(true);
        return (ConsoleConfigClassLoader) loaderField.get(cc);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader)

Example 4 with ConsoleConfigClassLoader

use of org.hibernate.console.ConsoleConfigClassLoader in project jbosstools-hibernate by jbosstools.

the class DriverDeleteTest method initExecutionContext.

private void initExecutionContext() {
    final ConsoleConfigClassLoader urlClassLoader = createJarClassLoader();
    DefaultExecutionContext dec = new DefaultExecutionContext(testName.getMethodName(), urlClassLoader);
    ExecutionContext.Command command = new ExecutionContext.Command() {

        public Object execute() {
            try {
                Class<?> driverClass = null;
                driverClass = (Class<?>) ReflectHelper.classForName(DRIVER_TEST_CLASS);
                Driver driver2 = (Driver) driverClass.newInstance();
                ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                if (contextClassLoader != null) {
                    driverClass = (Class<?>) contextClassLoader.loadClass(DRIVER_TEST_CLASS);
                }
                java.util.Properties info = new java.util.Properties();
                info.put("user", CONNECTION_USERNAME);
                info.put("password", CONNECTION_PASSWORD);
                try {
                    Connection connection = driver2.connect(CONNECTION_URL, info);
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    dec.execute(command);
    command = null;
    dec = null;
    urlClassLoader.close();
}
Also used : DefaultExecutionContext(org.hibernate.console.execution.DefaultExecutionContext) SQLException(java.sql.SQLException) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader) Connection(java.sql.Connection) Driver(java.sql.Driver) DefaultExecutionContext(org.hibernate.console.execution.DefaultExecutionContext) ExecutionContext(org.hibernate.console.execution.ExecutionContext) ConsoleConfigClassLoader(org.hibernate.console.ConsoleConfigClassLoader)

Aggregations

ConsoleConfigClassLoader (org.hibernate.console.ConsoleConfigClassLoader)4 SQLException (java.sql.SQLException)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 Connection (java.sql.Connection)1 Driver (java.sql.Driver)1 Enumeration (java.util.Enumeration)1 Iterator (java.util.Iterator)1 DefaultExecutionContext (org.hibernate.console.execution.DefaultExecutionContext)1 ExecutionContext (org.hibernate.console.execution.ExecutionContext)1 Command (org.hibernate.console.execution.ExecutionContext.Command)1