Skip to content

Commit 2772f25

Browse files
committed
Initial commit of SorterLoader
Started with basic functions, still untested!
1 parent 7b1f33e commit 2772f25

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package de.littlerolf.sav.loader;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
12+
/**
13+
* The Main Class of the Loader module, used for loading and accessing the specific Sorters
14+
* @author ole
15+
*
16+
*/
17+
public class SorterLoader {
18+
19+
private final String classpath;
20+
21+
private List<Class> classes = new ArrayList<Class>();
22+
23+
public SorterLoader(String path) {
24+
classpath = path;
25+
26+
}
27+
public void loadAllClasses() {
28+
// Create a File object on the root of the directory containing the class file
29+
File file = new File(classpath);
30+
31+
try {
32+
// Convert File to a URL
33+
URL url = file.toURI().toURL(); // file:/c:/myclasses/
34+
URL[] urls = new URL[]{url};
35+
36+
// Create a new class loader with the directory
37+
ClassLoader cl = new URLClassLoader(urls);
38+
39+
String[] directories = getSubdirectories();
40+
41+
for(String folder : directories) {
42+
classes.add(cl.loadClass(folder + ".Sorter"));
43+
}
44+
45+
46+
} catch (MalformedURLException e) {
47+
} catch (ClassNotFoundException e) {
48+
}
49+
}
50+
51+
public Class getClassByPackageName(String packageName) {
52+
for(Class c : classes) {
53+
if(c.getPackage().getName().equals(packageName)) {
54+
return c;
55+
}
56+
}
57+
return null;
58+
}
59+
60+
public String[] getAvailablePackagesList() {
61+
String[] packages = new String[classes.size()];
62+
for(int i = 0; i < classes.size()-1;i++) {
63+
packages[i] = classes.get(i).getPackage().getName();
64+
}
65+
return packages;
66+
}
67+
68+
private String[] getSubdirectories() {
69+
File file = new File(classpath);
70+
String[] directories = file.list(new FilenameFilter() {
71+
public boolean accept(File current, String name) {
72+
return new File(current, name).isDirectory();
73+
}
74+
});
75+
return directories;
76+
}
77+
}

0 commit comments

Comments
 (0)