Changeset 492
- Timestamp:
- 10/23/13 16:07:59 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/actions/Actions.java
r480 r492 8 8 import java.lang.reflect.Modifier; 9 9 import java.net.URL; 10 import java.net.URI; 11 import java.net.URLDecoder; 12 import java.net.JarURLConnection; 10 13 import java.rmi.UnexpectedException; 11 14 import java.util.ArrayList; … … 16 19 import java.util.List; 17 20 import java.util.Set; 21 import java.util.jar.JarEntry; 18 22 import java.util.jar.JarFile; 19 23 import java.util.zip.ZipEntry; 24 20 25 21 26 import org.expeditee.agents.Agent; … … 28 33 import org.expeditee.gui.FreeItems; 29 34 import org.expeditee.gui.MessageBay; 35 import org.expeditee.gui.UserSettings; 30 36 import org.expeditee.io.Conversion; 31 37 import org.expeditee.items.Item; … … 87 93 + "actions.NavigationActions"; 88 94 95 96 97 // The following is adapted from: 98 // http://stackoverflow.com/questions/1456930/how-do-i-read-all-classes-from-a-java-package-in-the-classpath 99 100 public static Class<?>[] getClassesNew(String packageName) 101 throws ClassNotFoundException { 102 103 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 104 105 ArrayList<String> names = new ArrayList<String>();; 106 final ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); 107 108 packageName = packageName.replace(".", "/"); 109 URL packageURL = classLoader.getResource(packageName); 110 111 if(packageURL.getProtocol().equals("jar")) { 112 113 // build jar file name, then loop through zipped entries 114 115 String jarFileNameUndecoded = packageURL.getFile(); 116 117 try { 118 String jarFileName = URLDecoder.decode(jarFileNameUndecoded, "UTF-8"); 119 120 121 JarURLConnection ju_connection=(JarURLConnection)packageURL.openConnection(); 122 JarFile jf =ju_connection.getJarFile(); 123 124 Enumeration<JarEntry> jarEntries = jf.entries(); 125 while(jarEntries.hasMoreElements()) { 126 String entryName = jarEntries.nextElement().getName(); 127 128 if (entryName.startsWith(packageName)) { 129 130 if (entryName.endsWith(".class") && !entryName.contains("$")) { 131 132 // Deal with situation where the class found might be a further sub-package 133 // e.g. for "org.expeditee.action" 134 // there is: org/expeditee/action/widgets/Chart.class 135 // which would produce a value of 'entryName' as widgets/Chart.class 136 137 entryName = entryName.substring(0,entryName.length() - 6); // 6 = '.class' 138 entryName = entryName.replace('/', '.'); 139 140 names.add(entryName); 141 classes.add(Class.forName(entryName)); 142 } 143 } 144 } 145 } 146 catch (Exception e) { 147 System.err.println("Failed to decode jar file: " + jarFileNameUndecoded); 148 e.printStackTrace(); 149 } 150 } 151 else { 152 // loop through files in classpath 153 154 String packageURLString = packageURL.toString(); 155 try { 156 URI uri = new URI(packageURLString); 157 File folder = new File(uri.getPath()); 158 159 // won't work with path which contains blank (%20) 160 // File folder = new File(packageURL.getFile()); 161 File[] contenuti = folder.listFiles(); 162 163 for (File actual: contenuti) { 164 String entryName = actual.getName(); 165 166 if (entryName.endsWith(".class") && !entryName.contains("$")) { 167 entryName = entryName.substring(0, entryName.lastIndexOf('.')); 168 169 names.add(entryName); 170 classes.add(Class.forName(entryName)); 171 } 172 } 173 } 174 catch (Exception e) { 175 System.err.println("Failed to process file: " + packageURLString); 176 e.printStackTrace(); 177 } 178 } 179 180 Class<?>[] classesA = new Class<?>[classes.size()]; 181 classes.toArray(classesA); 182 return classesA; 183 } 184 89 185 public static Class<?>[] getClasses(final String pckgname) 90 186 throws ClassNotFoundException { 187 91 188 final ArrayList<Class<?>> classes = new ArrayList<Class<?>>(); 189 92 190 // Must be a forward slash for loading resources 93 191 final String packagePath = pckgname.replace('.', '/'); 94 if(System.getProperty("expeditee.home") == null) { 192 193 if (System.getProperty("eclipse.expeditee.home") == null) { 95 194 final ClassLoader cld = Thread.currentThread().getContextClassLoader(); 96 195 if (cld == null) { … … 108 207 + System.getProperty("line.separator") 109 208 + "Are you running from within Eclipse? (or just not with Jar) Then make sure your" 110 + " 'e xpeditee.home' property is set correctly. It is currently: '"111 + System.getProperty("e xpeditee.home") + "'" + System.getProperty("line.separator")209 + " 'eclipse.expeditee.home' property is set correctly. It is currently: '" 210 + System.getProperty("eclipse.expeditee.home") + "'" + System.getProperty("line.separator") 112 211 + "You can set it by adding a VM argument. " 113 + "Example: -De xpeditee.home=D:\\Desktop\\Research\\expeditee-svn");212 + "Example: -Declipse.expeditee.home=D:\\Desktop\\Research\\expeditee-svn"); 114 213 e.printStackTrace(); 115 214 } … … 118 217 } 119 218 final File directory = new File(resource.getFile()); 219 120 220 final int splitPoint = directory.getPath().indexOf('!'); 121 221 if(splitPoint > 0) { … … 141 241 // The forward slash below is a forwards slash for 142 242 // both windows and linux 143 classes.add(Class.forName(className.substring(0, 144 className.length() - 6).replace('/', '.'))); 243 244 String class_forname = className.substring(0,className.length() - 6); 245 class_forname = class_forname.replace('/', '.'); 246 247 classes.add(Class.forName(class_forname)); 145 248 } 146 249 } … … 157 260 } 158 261 } else { 262 159 263 System.err.println( "A Error has occured when trying to use a Jar file to find actions or agents." 160 264 + System.getProperty("line.separator") 161 265 + "Are you running from within Eclipse? (or just not with Jar) Then make sure your" 162 + " 'e xpeditee.home' property is set correctly. It is currently: '"163 + System.getProperty("e xpeditee.home") + "'" + System.getProperty("line.separator")266 + " 'eclipse.expeditee.home' property is set correctly. It is currently: '" 267 + System.getProperty("eclipse.expeditee.home") + "'" + System.getProperty("line.separator") 164 268 + "You can set it by adding a VM argument. " 165 + "Example: -De xpeditee.home=D:\\Desktop\\Research\\expeditee-svn");269 + "Example: -Declipse.expeditee.home=D:\\Desktop\\Research\\expeditee-svn"); 166 270 } 167 271 } else { 168 final File directory = new File(System.getProperty("expeditee.home", "") + "\\bin\\src\\" + packagePath); 272 String eclipse_expeditee_home = System.getProperty("eclipse.expeditee.home", ""); 273 String full_package_path = eclipse_expeditee_home 274 + File.separator + "bin" + File.separator + "src" + File.separator + packagePath; 275 276 final File directory = new File(full_package_path); 277 169 278 if (directory.exists()) { 170 279 // Get the list of the files contained in the package … … 312 421 313 422 try { 314 classes = getClasses (AGENTS_PACKAGE);423 classes = getClassesNew(AGENTS_PACKAGE); 315 424 316 425 for (int i = 0; i < classes.length; i++) { … … 320 429 } 321 430 322 classes = getClasses (WIDGET_PACKAGE);431 classes = getClassesNew(WIDGET_PACKAGE); 323 432 324 433 for (int i = 0; i < classes.length; i++) { … … 328 437 } 329 438 330 classes = getClasses (CHARTS_PACKAGE);439 classes = getClassesNew(CHARTS_PACKAGE); 331 440 332 441 for (int i = 0; i < classes.length; i++) { … … 345 454 346 455 try { 347 classes = getClasses (ACTIONS_PACKAGE);456 classes = getClassesNew(ACTIONS_PACKAGE); 348 457 349 458 for (int i = 0; i < classes.length; i++) {
Note:
See TracChangeset
for help on using the changeset viewer.