Author: vyemialyanchyk
Date: 2009-11-18 08:51:35 -0500 (Wed, 18 Nov 2009)
New Revision: 18733
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/ConsoleConfigUnloadJarChange.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/DeleteResourceParticipant.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/GarbageCollectionUtil.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
trunk/hibernatetools/plugins/org.hibernate.eclipse/META-INF/MANIFEST.MF
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleConfiguration.java
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/execution/DefaultExecutionContext.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java
Log:
http://opensource.atlassian.com/projects/hibernate/browse/HBX-936 - fixed
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse/META-INF/MANIFEST.MF
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse/META-INF/MANIFEST.MF 2009-11-18
12:21:32 UTC (rev 18732)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse/META-INF/MANIFEST.MF 2009-11-18
13:51:35 UTC (rev 18733)
@@ -484,6 +484,7 @@
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui.console,
org.eclipse.jface,
+ org.eclipse.jdt.apt.core,
org.eclipse.datatools.connectivity
Bundle-ActivationPolicy: lazy
Eclipse-BuddyPolicy: registered
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleConfiguration.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleConfiguration.java 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleConfiguration.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -30,12 +30,15 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -44,8 +47,11 @@
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.DOMWriter;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.ProfileManager;
+import org.eclipse.jdt.apt.core.internal.JarClassLoader;
import org.eclipse.osgi.util.NLS;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
@@ -71,9 +77,11 @@
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
+@SuppressWarnings("restriction")
public class ConsoleConfiguration implements ExecutionContextHolder {
private ExecutionContext executionContext;
+ private ClassLoader classLoader = null;
private Map<String, FakeDelegatingDriver> fakeDrivers = new HashMap<String,
FakeDelegatingDriver>();
@@ -106,8 +114,20 @@
// reseting state
configuration = null;
closeSessionFactory();
+ cleanUpClassLoader();
}
+ public void cleanUpClassLoader() {
+ ClassLoader classLoaderTmp = classLoader;
+ while (classLoaderTmp != null) {
+ if (classLoaderTmp instanceof JarClassLoader) {
+ ((JarClassLoader)classLoaderTmp).close();
+ }
+ classLoaderTmp = classLoaderTmp.getParent();
+ }
+ classLoader = null;
+ }
+
public void build() {
configuration = buildWith(null, true);
fireConfigurationBuilt();
@@ -237,39 +257,102 @@
}
return customClassPathURLs;
}
+
+ public URL[] getCustomClassPathURLs_Jars() {
+ URL[] classPathURLs = getCustomClassPathURLs();
+ ArrayList<URL> res = new ArrayList<URL>();
+ for (int i = 0; i < classPathURLs.length; i++) {
+ String fileName = classPathURLs[i].getFile();
+ fileName = fileName.toLowerCase();
+ if (fileName.endsWith(".jar")) { //$NON-NLS-1$
+ res.add(classPathURLs[i]);
+ }
+ }
+ return res.toArray(new URL[0]);
+ }
+
+ protected URL[] getCustomClassPathURLs_NotJars() {
+ URL[] classPathURLs = getCustomClassPathURLs();
+ ArrayList<URL> res = new ArrayList<URL>();
+ for (int i = 0; i < classPathURLs.length; i++) {
+ String fileName = classPathURLs[i].getFile();
+ fileName = fileName.toLowerCase();
+ if (!fileName.endsWith(".jar")) { //$NON-NLS-1$
+ res.add(classPathURLs[i]);
+ }
+ }
+ return res.toArray(new URL[0]);
+ }
+
+ protected ClassLoader createJarClassLoader() {
+ URL[] customClassPathURLs_Jars = getCustomClassPathURLs_Jars();
+ List<File> filesTmp = new ArrayList<File>();
+ for (int i = 0; i < customClassPathURLs_Jars.length; i++) {
+ String filePath = customClassPathURLs_Jars[i].getPath();
+ IPath resourcePath = new Path(filePath);
+ File resourceFile = resourcePath.toFile();
+ //if (resourceFile.exists()) {
+ filesTmp.add(resourceFile);
+ //}
+ }
+ final List<File> files = filesTmp;
+ JarClassLoader jarClassLoader = AccessController.doPrivileged(new
PrivilegedAction<JarClassLoader>() {
+ public JarClassLoader run() {
+ return new JarClassLoader(files, getParentClassLoader()) {
+ @Override
+ public Enumeration<URL> getResources(String name) throws IOException {
+ return null;
+ }
+ };
+ }
+ });
+ return jarClassLoader;
+ }
+
+ protected ClassLoader createURLClassLoader() {
+ final URL[] customClassPathURLs = getCustomClassPathURLs_NotJars();
+ URLClassLoader urlClassLoader = AccessController.doPrivileged(new
PrivilegedAction<URLClassLoader>() {
+ public URLClassLoader run() {
+ return new URLClassLoader(customClassPathURLs, createJarClassLoader()) {
+ protected Class<?> findClass(String name) throws ClassNotFoundException {
+ try {
+ return super.findClass(name);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ }
+
+ protected synchronized Class<?> loadClass(String name, boolean resolve) throws
ClassNotFoundException {
+ try {
+ return super.loadClass(name, resolve);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ }
+
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ try {
+ return super.loadClass(name);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ }
+ };
+ }
+ });
+ return urlClassLoader;
+ }
/**
* @return
*
*/
public Configuration buildWith(final Configuration cfg, final boolean includeMappings)
{
- URL[] customClassPathURLs = getCustomClassPathURLs();
- executionContext = new DefaultExecutionContext( getName(), new URLClassLoader(
customClassPathURLs, getParentClassLoader() ) {
- protected Class<?> findClass(String name) throws ClassNotFoundException {
- try {
- return super.findClass( name );
- } catch(ClassNotFoundException cnfe) {
- throw cnfe;
- }
- }
+ if (classLoader == null) {
+ classLoader = createURLClassLoader();
+ }
+ executionContext = new DefaultExecutionContext(getName(), classLoader);
- protected synchronized Class<?> loadClass(String name, boolean resolve) throws
ClassNotFoundException {
- try {
- return super.loadClass( name, resolve );
- } catch(ClassNotFoundException cnfe) {
- throw cnfe;
- }
- }
-
- public Class<?> loadClass(String name) throws ClassNotFoundException {
- try {
- return super.loadClass( name );
- } catch(ClassNotFoundException cnfe) {
- throw cnfe;
- }
- }
- });
-
Configuration result = (Configuration) executionContext.execute(new
ExecutionContext.Command() {
public Object execute() {
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/execution/DefaultExecutionContext.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/execution/DefaultExecutionContext.java 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/execution/DefaultExecutionContext.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -21,7 +21,6 @@
*/
package org.hibernate.console.execution;
-import java.net.URLClassLoader;
import java.util.Map;
import java.util.WeakHashMap;
@@ -31,13 +30,13 @@
public class DefaultExecutionContext implements ExecutionContext {
- final private URLClassLoader configurationClassLoader;
+ final private ClassLoader configurationClassLoader;
private volatile int installs;
private Map<Thread, ClassLoader> previousLoaders = new WeakHashMap<Thread,
ClassLoader>();
final String key;
- public DefaultExecutionContext(String key, URLClassLoader loader) {
+ public DefaultExecutionContext(String key, ClassLoader loader) {
configurationClassLoader = loader;
this.key = key;
}
Modified: trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml
===================================================================
--- trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml 2009-11-18
12:21:32 UTC (rev 18732)
+++ trunk/hibernatetools/plugins/org.hibernate.eclipse.console/plugin.xml 2009-11-18
13:51:35 UTC (rev 18733)
@@ -640,6 +640,19 @@
label="hql editor colors coloring highlighting query"/>
</extension>
<extension
+ point="org.eclipse.ltk.core.refactoring.deleteParticipants">
+ <deleteParticipant
+
class="org.hibernate.eclipse.launch.core.refactoring.DeleteResourceParticipant"
+
id="org.hibernate.eclipse.launch.core.refactoring.DeleteResourceParticipant"
+ name="Console Configuration Resource Delete Participant">
+ <enablement>
+ <with variable="element">
+ <instanceof
value="org.eclipse.core.resources.IResource"/>
+ </with>
+ </enablement>
+ </deleteParticipant>
+ </extension>
+ <extension
point="org.eclipse.ltk.core.refactoring.renameParticipants">
<renameParticipant
class="org.hibernate.eclipse.launch.core.refactoring.ConsoleConfigurationITypeRenameParticipant"
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -475,6 +475,7 @@
public static String
LaunchConfigurationResourceNameChange_update_resource_path_in_launch_cfg;
public static String MoveResourceParticipant_launch_configurations_updates;
public static String RenameResourceParticipant_launch_configurations_updates;
+ public static String DeleteResourceParticipant_hibernate_configurations_updates;
public static String ConsoleConfigurationITypeRenameParticipant_update;
public static String ConsoleConfigurationITypeRenameParticipant_update_names;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/HibernateConsoleMessages.properties 2009-11-18
13:51:35 UTC (rev 18733)
@@ -482,6 +482,7 @@
LaunchConfigurationResourceNameChange_update_resource_path_in_launch_cfg=Update resource
path in launch configuration
MoveResourceParticipant_launch_configurations_updates=Launch Configurations updates
RenameResourceParticipant_launch_configurations_updates=Launch Configurations updates
+DeleteResourceParticipant_hibernate_configurations_updates=Hibernate Configurations
updates
DeleteProjectParticipant_console_configurations_updates=Delete related Console
Configurations
DeleteProjectParticipant_delete_console_configuration=Delete Console Configuration
''{0}''
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/ConsoleConfigUnloadJarChange.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/ConsoleConfigUnloadJarChange.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/ConsoleConfigUnloadJarChange.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.launch.core.refactoring;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.hibernate.console.ConsoleConfiguration;
+
+/**
+ * @author Vitali Yemialyanchyk
+ */
+public class ConsoleConfigUnloadJarChange extends Change {
+
+ protected ConsoleConfiguration config;
+
+ public ConsoleConfigUnloadJarChange(ConsoleConfiguration config) {
+ this.config = config;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Change#getModifiedElement()
+ */
+ @Override
+ public Object getModifiedElement() {
+ return config;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Change#getName()
+ */
+ @Override
+ public String getName() {
+ return config.getName();
+ }
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public void initializeValidationData(IProgressMonitor pm) {
+ }
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException,
+ OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ @Override
+ public Change perform(IProgressMonitor pm) throws CoreException {
+ config.cleanUpClassLoader();
+ return null;
+ }
+
+}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/DeleteResourceParticipant.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/DeleteResourceParticipant.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/core/refactoring/DeleteResourceParticipant.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.launch.core.refactoring;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.ltk.core.refactoring.Change;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
+import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant;
+import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.KnownConfigurations;
+import org.hibernate.eclipse.console.HibernateConsoleMessages;
+
+/**
+ * @author Vitali Yemialyanchyk
+ */
+public class DeleteResourceParticipant extends DeleteParticipant {
+
+ private String path2Del = null;
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#checkConditions(org.eclipse.core.runtime.IProgressMonitor,
org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
+ */
+ @Override
+ public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext
context)
+ throws OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ @Override
+ public Change createPreChange(IProgressMonitor pm) throws CoreException,
OperationCanceledException {
+ if (path2Del == null) {
+ return null;
+ }
+ List<Change> changes = new ArrayList<Change>();
+ ConsoleConfiguration[] configs =
KnownConfigurations.getInstance().getConfigurations();
+ for (int i = 0; i < configs.length; i++) {
+ final ConsoleConfiguration config = configs[i];
+ final URL[] classPathURLs_Jars = config.getCustomClassPathURLs_Jars();
+ for (int j = 0; j < classPathURLs_Jars.length; j++) {
+ String path2Jar = classPathURLs_Jars[i].getPath();
+ if (path2Jar.startsWith(path2Del)) {
+ changes.add(new ConsoleConfigUnloadJarChange(config));
+ break;
+ }
+ }
+ }
+ return HibernateRefactoringUtil.createChangesFromList(changes, getName());
+ }
+
+ @Override
+ public Change createChange(IProgressMonitor pm) throws CoreException,
OperationCanceledException {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#getName()
+ */
+ @Override
+ public String getName() {
+ return
HibernateConsoleMessages.DeleteResourceParticipant_hibernate_configurations_updates;
+ }
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#initialize(java.lang.Object)
+ */
+ @Override
+ protected boolean initialize(Object element) {
+ boolean res = false;
+ IResource res2Del = (IResource)element;
+ String path2Del = res2Del.getLocationURI().getPath();
+ ConsoleConfiguration[] configs =
KnownConfigurations.getInstance().getConfigurations();
+ for (int i = 0; i < configs.length; i++) {
+ final ConsoleConfiguration config = configs[i];
+ final URL[] classPathURLs_Jars = config.getCustomClassPathURLs_Jars();
+ for (int j = 0; j < classPathURLs_Jars.length; j++) {
+ String path2Jar = classPathURLs_Jars[i].getPath();
+ if (path2Jar.startsWith(path2Del)) {
+ res = true;
+ }
+ }
+ }
+ this.path2Del = res ? path2Del : null;
+ return res;
+ }
+
+}
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/META-INF/MANIFEST.MF 2009-11-18
13:51:35 UTC (rev 18733)
@@ -53,6 +53,7 @@
org.eclipse.ui.views,
org.jboss.tools.hibernate.ui,
org.jboss.tools.tests,
+ org.eclipse.jdt.apt.core,
org.eclipse.debug.ui,
org.eclipse.ui.workbench.texteditor;bundle-version="3.5.0"
Bundle-ActivationPolicy: lazy
Added:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/GarbageCollectionUtil.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/GarbageCollectionUtil.java
(rev 0)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/GarbageCollectionUtil.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -0,0 +1,31 @@
+/*******************************************************************************
+ * Copyright (c) 2007-2009 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.console.test.utils;
+
+/**
+ * @author Vitali Yemialyanchyk
+ */
+public class GarbageCollectionUtil {
+
+ static public int forceCollectGarbage() {
+ int numGCCall = 0;
+ long freeMemory = 0;
+ long freeMemoryAfter = freeMemory;
+ do {
+ freeMemory = Runtime.getRuntime().freeMemory();
+ System.gc();
+ numGCCall++;
+ freeMemoryAfter = Runtime.getRuntime().freeMemory();
+ } while (freeMemoryAfter - freeMemory != 0);
+ return numGCCall;
+ }
+
+}
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java 2009-11-18
12:21:32 UTC (rev 18732)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java 2009-11-18
13:51:35 UTC (rev 18733)
@@ -14,6 +14,7 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.ref.WeakReference;
@@ -25,14 +26,19 @@
import java.security.PrivilegedAction;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
import java.util.Vector;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
+import org.eclipse.jdt.apt.core.internal.JarClassLoader;
import org.hibernate.console.execution.DefaultExecutionContext;
import org.hibernate.console.execution.ExecutionContext;
import org.hibernate.eclipse.console.test.HibernateConsoleTestPlugin;
+import org.hibernate.eclipse.console.test.utils.GarbageCollectionUtil;
import org.hibernate.util.ReflectHelper;
import junit.framework.TestCase;
@@ -44,6 +50,7 @@
*
* @author Vitali Yemialyanchyk
*/
+@SuppressWarnings("restriction")
public class DriverDeleteTest extends TestCase {
public static final String DRIVER_TEST_NAME =
"mysql-connector-java-5.0.7-bin.jar"; //$NON-NLS-1$
@@ -69,6 +76,18 @@
return customClassPathURLs;
}
+ protected List<File> getCustomClassPathFiles() {
+ final List<File> files = new ArrayList<File>();
+ File driverJar2;
+ try {
+ driverJar2 = getResourceItem(DRIVER_PUT_PATH);
+ files.add(driverJar2);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return files;
+ }
+
protected File getResourceItem(String strResPath) throws IOException {
IPath resourcePath = new Path(strResPath);
File resourceFolder = resourcePath.toFile();
@@ -124,28 +143,49 @@
URLClassLoader urlClassLoader = AccessController.doPrivileged(new
PrivilegedAction<URLClassLoader>() {
public URLClassLoader run() {
return new URLClassLoader( customClassPathURLs, getParentClassLoader() ) {
- protected Class<?> findClass(String name) throws ClassNotFoundException {
+ 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 {
- return super.findClass(name);
+ 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 {
- return super.loadClass(name, resolve);
+ res = super.loadClass(name, resolve);
} catch (ClassNotFoundException cnfe) {
throw cnfe;
}
+ return res;
}
public Class<?> loadClass(String name) throws ClassNotFoundException {
+ Class<?> res = null;
try {
- return super.loadClass(name);
+ res = super.loadClass(name);
} catch (ClassNotFoundException cnfe) {
throw cnfe;
}
+ return res;
}
};
}
@@ -153,6 +193,17 @@
return urlClassLoader;
}
+ public JarClassLoader createJarClassLoader() {
+ final List<File> files = getCustomClassPathFiles();
+ JarClassLoader urlClassLoader = AccessController.doPrivileged(new
PrivilegedAction<JarClassLoader>() {
+ public JarClassLoader run() {
+ return new JarClassLoader(files, getParentClassLoader()) {
+ };
+ }
+ });
+ return urlClassLoader;
+ }
+
public class StringWriter extends Writer {
public String res = new String();
@@ -170,11 +221,36 @@
res += cbuf.toString() + "\r\n"; //$NON-NLS-1$
}
}
+
+ @SuppressWarnings("unchecked")
+ public void forceDeleteDrivers() {
+ Class<DriverManager> clazz = DriverManager.class;
+ Vector drivers = null;
+ try {
+ Field field = clazz.getDeclaredField("drivers"); //$NON-NLS-1$
+ field.setAccessible(true);
+ drivers = (Vector)field.get(null);
+ } catch (IllegalArgumentException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
+ }
+ if (drivers != null && drivers.size() > 0) {
+ drivers.clear();
+ }
+ clazz = null;
+ drivers = null;
+ }
@SuppressWarnings("unchecked")
public void initExecutionContext() {
/**/
- URLClassLoader urlClassLoader = createClassLoader();
+ //URLClassLoader urlClassLoader = createClassLoader();
+ JarClassLoader urlClassLoader = createJarClassLoader();
/**/
Class<Driver> driverClass = null;
try {
@@ -192,7 +268,6 @@
/**/
try {
driver = driverClass.newInstance();
- /**/
DriverManager.registerDriver(driver);
//DriverManager.deregisterDriver(driver);
java.util.Enumeration<Driver> drEnum = DriverManager.getDrivers();
@@ -201,7 +276,6 @@
DriverManager.deregisterDriver(driver);
numRedDrivers++;
}
- /**/
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
@@ -210,79 +284,55 @@
e.printStackTrace();
}
/**/
- Class<DriverManager> clazz = DriverManager.class;
- Vector drivers = null;
- try {
- Field field = clazz.getDeclaredField("drivers"); //$NON-NLS-1$
- field.setAccessible(true);
- drivers = (Vector)field.get(null);
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchFieldException e) {
- e.printStackTrace();
- }
- if (drivers != null && drivers.size() > 0) {
- drivers.clear();
- }
System.out.print(wr.res);
/** /
DefaultExecutionContext dec = new DefaultExecutionContext(getName(), urlClassLoader);
executionContext = new WeakReference<ExecutionContext>(dec);
- dec = null;
/** /
- dec.execute(new ExecutionContext.Command() {
-
- @SuppressWarnings("unchecked")
+ ExecutionContext.Command command = new ExecutionContext.Command() {
public Object execute() {
try {
Class<Driver> driverClass = null;
//if (driverClass != null) {
- driverClass = ReflectHelper.classForName("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ //driverClass = ReflectHelper.classForName("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if (contextClassLoader != null) {
+ driverClass =
(Class<Driver>)contextClassLoader.loadClass("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ }
//if (driverClass == null) {
- DriverManager.registerDriver(driverClass.newInstance());
+ //driverClass.newInstance();
+ //DriverManager.registerDriver(driverClass.newInstance());
//}
//}
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
+ //contextClassLoader = null;
+ //driverClass = null;
+ //} catch (InstantiationException e) {
+ // e.printStackTrace();
+ //} catch (IllegalAccessException e) {
+ // e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
+ //} catch (SQLException e) {
+ // e.printStackTrace();
}
return null;
}
- });
- /**/
+ };
+ dec.execute(command);
+ command = null;
+ dec = null;
+ /** /
+ forceDeleteDrivers();
//
- clazz = null;
- drivers = null;
// obligatory in other case gc can't collect these variables!!!
driver = null;
driverClass = null;
urlClassLoader = null;
forceCollectGarbage();
/**/
+ urlClassLoader.close();
}
- public int forceCollectGarbage() {
- int numGCCall = 0;
- long freeMemory = 0;
- long freeMemoryAfter = freeMemory;
- do {
- freeMemory = Runtime.getRuntime().freeMemory();
- System.gc();
- numGCCall++;
- freeMemoryAfter = Runtime.getRuntime().freeMemory();
- } while (freeMemoryAfter - freeMemory != 0);
- return numGCCall;
- }
-
public void cleanupExecutionContext() {
if (executionContext != null && executionContext.get() != null) {
executionContext.get().execute(new ExecutionContext.Command() {
@@ -307,7 +357,7 @@
executionContext = null;
}
//
- forceCollectGarbage();
+ GarbageCollectionUtil.forceCollectGarbage();
}
public void testDelete() {