|
| 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.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import de.littlerolf.sav.data.BaseSorter; |
| 13 | + |
| 14 | +/** |
| 15 | + * The Main Class of the Loader module, used for loading and accessing the |
| 16 | + * specific Sorters |
| 17 | + * |
| 18 | + * @author ole |
| 19 | + * |
| 20 | + */ |
| 21 | +public class SorterLoader { |
| 22 | + |
| 23 | + private final String classpath; |
| 24 | + private boolean debug = false; |
| 25 | + |
| 26 | + private List<Class> classes = new ArrayList<Class>(); |
| 27 | + private List<BaseSorter> sorters = new ArrayList<BaseSorter>(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a new SorterLoader looking for classes in the given path |
| 31 | + * |
| 32 | + * @param path |
| 33 | + * The path to look for classes |
| 34 | + */ |
| 35 | + public SorterLoader(String path) { |
| 36 | + classpath = path; |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new SorterLoader looking for classes in the given path and |
| 42 | + * offers ability to enable debug output |
| 43 | + * |
| 44 | + * @param path |
| 45 | + * The path to look for classes |
| 46 | + * @param debug |
| 47 | + * Debug Flag |
| 48 | + */ |
| 49 | + public SorterLoader(String path, boolean debug) { |
| 50 | + classpath = path; |
| 51 | + this.debug = debug; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Loads all classes named 'Sorter' from the set folder from all the |
| 56 | + * subdirectories |
| 57 | + */ |
| 58 | + public void loadAllClasses() { |
| 59 | + // Create a File object on the root of the directory containing the |
| 60 | + // class file |
| 61 | + File file = new File(classpath); |
| 62 | + |
| 63 | + try { |
| 64 | + // Convert File to a URL |
| 65 | + URL url = file.toURI().toURL(); // file:/c:/myclasses/ |
| 66 | + URL[] urls = new URL[] { url }; |
| 67 | + |
| 68 | + // Create a new class loader with the directory |
| 69 | + ClassLoader cl = new URLClassLoader(urls); |
| 70 | + |
| 71 | + String[] directories = getSubdirectories(); |
| 72 | + |
| 73 | + for (String folder : directories) { |
| 74 | + classes.add(cl.loadClass(folder + ".Sorter")); |
| 75 | + } |
| 76 | + |
| 77 | + } catch (MalformedURLException e) { |
| 78 | + } catch (ClassNotFoundException e) { |
| 79 | + } |
| 80 | + |
| 81 | + if (debug) { |
| 82 | + System.out.println("[Debug] Loaded Classes:"); |
| 83 | + System.out.println("____________________"); |
| 84 | + for (Class c : classes) { |
| 85 | + System.out.println(c.getPackage().getName() + " " |
| 86 | + + c.getName()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public void instanciateAllClasses() { |
| 92 | + if(classes.size() == 0) { |
| 93 | + if(debug) { |
| 94 | + System.out.println("You have to call loadAllClasses() first!"); |
| 95 | + } |
| 96 | + return; |
| 97 | + } |
| 98 | + for(Class c : classes) { |
| 99 | + try { |
| 100 | + sorters.add((BaseSorter) c.newInstance()); |
| 101 | + } catch (InstantiationException | IllegalAccessException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public BaseSorter getSorterByName(String name) { |
| 108 | + for (BaseSorter bs : sorters) { |
| 109 | + if(bs.getName().equals(name)) { |
| 110 | + return bs; |
| 111 | + } |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + public String[] getAvailableSorters() { |
| 117 | + String[] packages = new String[sorters.size()]; |
| 118 | + for (int i = 0; i < sorters.size(); i++) { |
| 119 | + packages[i] = sorters.get(i).getName(); |
| 120 | + } |
| 121 | + if (debug) { |
| 122 | + System.out.println(Arrays.toString(packages)); |
| 123 | + } |
| 124 | + return packages; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Gets the Class of a Sorter via the Package name |
| 129 | + * |
| 130 | + * @param packageName |
| 131 | + * package name to search for |
| 132 | + * @return The found Class or null |
| 133 | + */ |
| 134 | + public Class getSorterClassByPackageName(String packageName) { |
| 135 | + for (Class c : classes) { |
| 136 | + if (c.getPackage().getName().equals(packageName)) { |
| 137 | + if (debug) { |
| 138 | + System.out.println("Found loaded class for package name: " |
| 139 | + + packageName); |
| 140 | + } |
| 141 | + return c; |
| 142 | + } |
| 143 | + } |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns a full list of all available package names |
| 149 | + * |
| 150 | + * @return The list of absolute awesomeness |
| 151 | + */ |
| 152 | + public String[] getAvailablePackagesList() { |
| 153 | + String[] packages = new String[classes.size()]; |
| 154 | + for (int i = 0; i < classes.size(); i++) { |
| 155 | + packages[i] = classes.get(i).getPackage().getName(); |
| 156 | + } |
| 157 | + if (debug) { |
| 158 | + System.out.println(Arrays.toString(packages)); |
| 159 | + } |
| 160 | + return packages; |
| 161 | + } |
| 162 | + |
| 163 | + private String[] getSubdirectories() { |
| 164 | + File file = new File(classpath); |
| 165 | + String[] directories = file.list(new FilenameFilter() { |
| 166 | + public boolean accept(File current, String name) { |
| 167 | + return new File(current, name).isDirectory(); |
| 168 | + } |
| 169 | + }); |
| 170 | + return directories; |
| 171 | + } |
| 172 | +} |
0 commit comments