|
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.List; |
10 | 10 |
|
11 | | - |
12 | 11 | /** |
13 | | - * The Main Class of the Loader module, used for loading and accessing the specific Sorters |
| 12 | + * The Main Class of the Loader module, used for loading and accessing the |
| 13 | + * specific Sorters |
| 14 | + * |
14 | 15 | * @author ole |
15 | | - * |
| 16 | + * |
16 | 17 | */ |
17 | 18 | public class SorterLoader { |
18 | 19 |
|
19 | 20 | private final String classpath; |
20 | | - |
| 21 | + private boolean debug = false; |
| 22 | + |
21 | 23 | private List<Class> classes = new ArrayList<Class>(); |
22 | | - |
| 24 | + |
23 | 25 | /** |
24 | 26 | * Creates a new SorterLoader looking for classes in the given path |
25 | | - * @param path The path to look for classes |
| 27 | + * |
| 28 | + * @param path |
| 29 | + * The path to look for classes |
26 | 30 | */ |
27 | 31 | public SorterLoader(String path) { |
28 | 32 | classpath = path; |
29 | | - |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new SorterLoader looking for classes in the given path and |
| 38 | + * offers ability to enable debug output |
| 39 | + * |
| 40 | + * @param path |
| 41 | + * The path to look for classes |
| 42 | + * @param debug |
| 43 | + * Debug Flag |
| 44 | + */ |
| 45 | + public SorterLoader(String path, boolean debug) { |
| 46 | + classpath = path; |
| 47 | + this.debug = debug; |
30 | 48 | } |
31 | | - |
| 49 | + |
32 | 50 | /** |
33 | | - * Loads all classes named 'Sorter' from the set folder from all the subdirectories |
| 51 | + * Loads all classes named 'Sorter' from the set folder from all the |
| 52 | + * subdirectories |
34 | 53 | */ |
35 | 54 | public void loadAllClasses() { |
36 | | - // Create a File object on the root of the directory containing the class file |
| 55 | + // Create a File object on the root of the directory containing the |
| 56 | + // class file |
37 | 57 | File file = new File(classpath); |
38 | 58 |
|
39 | 59 | try { |
40 | | - // Convert File to a URL |
41 | | - URL url = file.toURI().toURL(); // file:/c:/myclasses/ |
42 | | - URL[] urls = new URL[]{url}; |
| 60 | + // Convert File to a URL |
| 61 | + URL url = file.toURI().toURL(); // file:/c:/myclasses/ |
| 62 | + URL[] urls = new URL[] { url }; |
43 | 63 |
|
44 | | - // Create a new class loader with the directory |
45 | | - ClassLoader cl = new URLClassLoader(urls); |
| 64 | + // Create a new class loader with the directory |
| 65 | + ClassLoader cl = new URLClassLoader(urls); |
46 | 66 |
|
47 | | - String[] directories = getSubdirectories(); |
48 | | - |
49 | | - |
50 | | - for(String folder : directories) { |
51 | | - classes.add(cl.loadClass(folder + ".Sorter")); |
52 | | - } |
| 67 | + String[] directories = getSubdirectories(); |
| 68 | + |
| 69 | + for (String folder : directories) { |
| 70 | + classes.add(cl.loadClass(folder + ".Sorter")); |
| 71 | + } |
53 | 72 |
|
54 | | - |
55 | 73 | } catch (MalformedURLException e) { |
56 | 74 | } catch (ClassNotFoundException e) { |
57 | 75 | } |
| 76 | + |
| 77 | + if (debug) { |
| 78 | + System.out.println("[Debug] Loaded Classes:"); |
| 79 | + System.out.println("____________________"); |
| 80 | + for (Class c : classes) { |
| 81 | + System.out.println(c.getPackage().getName() + " " |
| 82 | + + c.getName()); |
| 83 | + } |
| 84 | + } |
58 | 85 | } |
59 | | - |
60 | | - public Class getClassByPackageName(String packageName) { |
61 | | - for(Class c : classes) { |
62 | | - if(c.getPackage().getName().equals(packageName)) { |
| 86 | + |
| 87 | + public Class getSorterByPackageName(String packageName) { |
| 88 | + for (Class c : classes) { |
| 89 | + if (c.getPackage().getName().equals(packageName)) { |
63 | 90 | return c; |
64 | 91 | } |
65 | 92 | } |
66 | 93 | return null; |
67 | 94 | } |
68 | | - |
| 95 | + |
69 | 96 | public String[] getAvailablePackagesList() { |
70 | 97 | String[] packages = new String[classes.size()]; |
71 | | - for(int i = 0; i < classes.size()-1;i++) { |
| 98 | + for (int i = 0; i < classes.size() - 1; i++) { |
72 | 99 | packages[i] = classes.get(i).getPackage().getName(); |
73 | 100 | } |
74 | 101 | return packages; |
75 | 102 | } |
76 | | - |
| 103 | + |
77 | 104 | private String[] getSubdirectories() { |
78 | | - File file = new File(classpath); |
79 | | - String[] directories = file.list(new FilenameFilter() { |
80 | | - public boolean accept(File current, String name) { |
81 | | - return new File(current, name).isDirectory(); |
82 | | - } |
83 | | - }); |
84 | | - return directories; |
| 105 | + File file = new File(classpath); |
| 106 | + String[] directories = file.list(new FilenameFilter() { |
| 107 | + public boolean accept(File current, String name) { |
| 108 | + return new File(current, name).isDirectory(); |
| 109 | + } |
| 110 | + }); |
| 111 | + return directories; |
85 | 112 | } |
86 | 113 | } |
0 commit comments