aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/e1/pdj/PDJClassLoader.java
blob: ee54e71c7a5e456218934db5c19dfb5d8277d9a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.e1.pdj;

import java.net.URLClassLoader;
import java.net.*;
import java.util.*;
import java.io.*;

import com.cycling74.max.MaxSystem;

public class PDJClassLoader extends URLClassLoader {
    /** The folder where the class (and java files) are */
	static File fclasses;
    
    /** if this is set to true, all classloader operation will be logged */
    static boolean verboseCL = false;
    
	static private PDJClassLoader instance;
    
	static {
	    String prop = System.getProperty("pdj.classes-dir");

	    if ( prop == null ) {
	        fclasses = new File(System.getProperty("pdj.home") + "/classes");
	    } else {
	        fclasses = new File(prop);
	    }
	    instance = new PDJClassLoader();
        
	    verboseCL = PDJSystem.isSystemPropertyTrue("pdj.verbose-classloader");
	}

	static public PDJClassLoader getInstance() {
		return instance;
	}

	public PDJClassLoader() {
		super(resolvClasspath());
	}

    private static void findJars(File f, Collection v) throws MalformedURLException {
        File files[] = f.listFiles();

        for(int i = 0;i<files.length;i++) {
            String filename = files[i].toString(); 
            if ( filename.endsWith(".jar") || filename.endsWith(".zip") ) {
				v.add(new URL("jar:" + files[i].toURL().toString() + "!/"));
            }
        }
    }

	private static URL[] resolvClasspath() {
		String cp = System.getProperty("pdj.classpath");
		ArrayList list = new ArrayList();
		String pathSeparator = "" + File.pathSeparator;
        
        try {
            if ( fclasses.isDirectory() )
                list.add(fclasses.toURL());
            
            File lib = new File(System.getProperty("pdj.home") + "/lib");
            if ( lib.isDirectory() )
                findJars(lib, list);
            
            StringTokenizer st = new StringTokenizer(cp, pathSeparator);
            while( st.hasMoreTokens() ) {
                String token = st.nextToken(pathSeparator);
                File f = new File(token);
                if ( f.isFile() && (f.toString().endsWith(".jar") || f.toString().endsWith(".zip")) )
                    list.add(new URL("jar:" + f.toURL().toString() + "!/"));
                if ( f.isDirectory() ) {
                    findJars(f, list);
                    list.add(f.toURL());
                }
	        }
        } catch ( MalformedURLException mue ) {
            throw new Error("pdj: unable to add to classpath: " + mue);
        }
        
        URL ret[] = (URL[]) list.toArray(new URL[list.size()]);
        
        if ( verboseCL ) {
            MaxSystem.post("pdj: verbose classloader: system classpath: " + System.getProperty("java.class.path"));
            MaxSystem.post("pdj: verbose classloader: dynamic classpath:");
            for(int i=0;i<ret.length;i++) {
                MaxSystem.post("\t" + ret[i].toString());
            }
        }

		return ret;
	}

	public static String[] getCurrentClassPath() {
		URL url[] = instance.getURLs();
		String ret[] = new String[url.length];

		for(int i=0;i<url.length;i++) {
			ret[i] = url[i].toString();
		}
		return ret;
	}

	public static String getReadableClassPath() {
		PDJClassLoader cloader = getInstance();
		StringBuffer sb = new StringBuffer();

		URL url[] = cloader.getURLs();
		for(int i=0;i<url.length;i++) {
			sb.append("\t" + url[i].toString() + "\n");
		}

		return sb.toString();
	}

	/**
	 * Returns a readable classpath for javac
	 * @return the classpath for javac / jikes
	 */
	public static String getConfigurationClassPath() {
		StringBuffer sb = new StringBuffer();

		sb.append(System.getProperty("java.class.path") + File.pathSeparatorChar);

		URL url[] = getInstance().getURLs();
		for(int i=0;i<url.length;i++) {
			String path = url[i].toString();

			if ( path.startsWith("file:") ) { 
			    sb.append(path.substring(6) + File.pathSeparatorChar);
                continue;
            }
            
            if ( path.startsWith("jar:") ) {
                sb.append(path.substring(10, path.lastIndexOf("!")) + File.pathSeparatorChar);
                continue;
            }
                
			sb.append(path + File.pathSeparatorChar);
		}

		return sb.toString();
	}

	public static void resetClassloader() {
		instance = new PDJClassLoader();
		System.gc();
	}

	/**
	 * Dynamic resolv will try to load .java file from date with .class
	 * @param name classname
	 * @return class definition
	 * @throws ClassNotFoundException
	 */
	public static Class dynamicResolv(String name) throws PDJClassLoaderException {
        if ( System.getProperty("pdj.compiler").equals("null") ) {
            resetClassloader();
            try {
                return instance.loadClass(name);
            } catch (ClassNotFoundException e) {
                throw new PDJClassLoaderException(e);
            }
        }

		String pathName = name.replace('.', File.pathSeparatorChar);

		File classFile = new File(fclasses, pathName + ".class");
		File javaFile = new File(fclasses, pathName + ".java");

		if ( javaFile.exists() ) {
            if ( classFile.exists() ) {
                if ( javaFile.lastModified() > classFile.lastModified() ) {
                    compileClass(javaFile);
                } else {
                    if ( verboseCL )
                        MaxSystem.post("class: " + name + " is already compiled and younger than the source .java file");
                }
            } else {
                compileClass(javaFile);
            }
		}

        resetClassloader();                
        
		try {
			return instance.loadClass(name);
		} catch (ClassNotFoundException e) {
			throw new PDJClassLoaderException(e);
		}
	}

	protected static void compileClass(File javaFile) throws PDJClassLoaderException {
		String str_compiler = System.getProperty("pdj.compiler");
		GenericCompiler compiler = null;

		if ( str_compiler.equals("jikes") ) {
			compiler = new JikesCompiler();
		} else {
            if ( !str_compiler.equals("javac") ) {
                System.err.println("pdj: unknown compiler:" + str_compiler + ", using javac.");
            }
            compiler = new JavacCompiler();
        }
		compiler.javaFile = javaFile;
		compiler.compileClass();
	}
}