KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > eclipse > console > utils > ClassLoaderHelper


1 /*
2  * Created on 15-Oct-2004
3  *
4  *
5  */

6 package org.hibernate.eclipse.console.utils;
7
8 import java.io.File JavaDoc;
9 import java.net.MalformedURLException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLClassLoader JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
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 /**
26  * @author max
27  *
28  */

29 public class ClassLoaderHelper {
30
31     static public URLClassLoader JavaDoc getProjectClassLoader (IJavaProject project)
32     {
33         List JavaDoc pathElements = getProjectClassPathURLs(project);
34         URL JavaDoc urlPaths[] = (URL JavaDoc[]) pathElements.toArray(new URL JavaDoc[pathElements.size()]);
35         
36 // for (int i = 0; i < urlPaths.length; i++)
37
// {
38
// System.out.println("[aop-core-plugin] class-loader-path: " + urlPaths[i]);
39
// }
40

41         return new URLClassLoader JavaDoc(urlPaths, Thread.currentThread().getContextClassLoader());
42     }
43     
44     static public List JavaDoc getProjectClassPathURLs (IJavaProject project)
45     {
46         ArrayList JavaDoc pathElements = new ArrayList JavaDoc();
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 JavaDoc url = getRawLocationURL(simplePath);
61                         
62                         pathElements.add(url);
63                     }
64                 }
65             }
66             
67             /*IPath aopPaths[] = AopClasspathContainer.getAopJarPaths();
68             for (int i = 0; i < aopPaths.length; i++)
69             {
70                 URL url = aopPaths[i].toFile().toURL();
71                 pathElements.add(url);
72             }*/

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 JavaDoc e) {
82             e.printStackTrace();
83         }
84         
85         return pathElements;
86     }
87     
88     private static URL JavaDoc getRawLocationURL(IPath simplePath) throws MalformedURLException JavaDoc {
89         File JavaDoc file = getRawLocationFile(simplePath);
90         return file.toURL();
91     }
92
93     private static File JavaDoc getRawLocationFile(IPath simplePath) {
94         IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(simplePath);
95         File JavaDoc 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     /**
114      * Convenience method to get access to the java model.
115      */

116     public static IJavaModel getJavaModel() {
117         return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
118     }
119
120     public static URL JavaDoc[] getRawLocationsURLForResources(IPath[] classpaths) throws MalformedURLException JavaDoc {
121         URL JavaDoc[] l = new URL JavaDoc[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