1 6 package org.hibernate.eclipse.console.utils; 7 8 import java.io.File ; 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 import java.net.URLClassLoader ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.jdt.core.IClasspathEntry; 20 import org.eclipse.jdt.core.IJavaModel; 21 import org.eclipse.jdt.core.IJavaProject; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.core.JavaModelException; 24 25 29 public class ClassLoaderHelper { 30 31 static public URLClassLoader getProjectClassLoader (IJavaProject project) 32 { 33 List pathElements = getProjectClassPathURLs(project); 34 URL urlPaths[] = (URL []) pathElements.toArray(new URL [pathElements.size()]); 35 36 41 return new URLClassLoader (urlPaths, Thread.currentThread().getContextClassLoader()); 42 } 43 44 static public List getProjectClassPathURLs (IJavaProject project) 45 { 46 ArrayList pathElements = new ArrayList (); 47 48 try { 49 IClasspathEntry paths[] = project.getResolvedClasspath(true); 50 51 if (paths != null) 52 { 53 for (int i = 0; i < paths.length; i++) 54 { 55 IClasspathEntry path = paths[i]; 56 if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) 57 { 58 System.out.println("Path: " + path); 59 IPath simplePath = path.getPath(); 60 URL url = getRawLocationURL(simplePath); 61 62 pathElements.add(url); 63 } 64 } 65 } 66 67 73 74 IPath location = getProjectLocation(project.getProject()); 75 IPath outputPath = location.append( 76 project.getOutputLocation().removeFirstSegments(1)); 77 78 pathElements.add(outputPath.toFile().toURL()); 79 } catch (JavaModelException e) { 80 e.printStackTrace(); 81 } catch (MalformedURLException e) { 82 e.printStackTrace(); 83 } 84 85 return pathElements; 86 } 87 88 private static URL getRawLocationURL(IPath simplePath) throws MalformedURLException { 89 File file = getRawLocationFile(simplePath); 90 return file.toURL(); 91 } 92 93 private static File getRawLocationFile(IPath simplePath) { 94 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(simplePath); 95 File file = null; 96 if(resource!=null) { 97 file = ResourcesPlugin.getWorkspace().getRoot().findMember(simplePath).getRawLocation().toFile(); 98 } else { 99 file = simplePath.toFile(); 100 } 101 return file; 102 } 103 104 static public IPath getProjectLocation (IProject project) 105 { 106 if (project.getRawLocation() == null) 107 { 108 return project.getLocation(); 109 } 110 else return project.getRawLocation(); 111 } 112 113 116 public static IJavaModel getJavaModel() { 117 return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); 118 } 119 120 public static URL [] getRawLocationsURLForResources(IPath[] classpaths) throws MalformedURLException { 121 URL [] l = new URL [classpaths.length]; 122 for (int i = 0; i < classpaths.length; i++) { 123 l[i] = getRawLocationURL(classpaths[i]); 124 } 125 return l; 126 } 127 } 128 | Popular Tags |