Changeset 669 for trunk/java-utils/src/main/java/de
- Timestamp:
- 08/28/12 14:04:11 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/java-utils/src/main/java/de/ugoe/cs/util/console/CommandExecuter.java
r665 r669 9 9 import java.security.InvalidParameterException; 10 10 import java.util.ArrayList; 11 import java.util.Collections;12 11 import java.util.Comparator; 13 12 import java.util.Enumeration; 14 13 import java.util.List; 14 import java.util.SortedSet; 15 import java.util.TreeSet; 15 16 import java.util.jar.JarEntry; 16 17 import java.util.jar.JarInputStream; … … 176 177 /** 177 178 * <p> 178 * reads all available commands from the registered command packages and returns a list of 179 * theirnames180 * </p> 181 * 179 * reads all available commands from the registered command packages and returns a list of their 180 * names 181 * </p> 182 * 182 183 * @return an array containing the names of the available commands. 183 184 */ … … 187 188 packages.addAll(commandPackageList); 188 189 packages.add(defaultPackage); 189 190 190 191 FilenameFilter filter = new FilenameFilter() { 191 192 @Override … … 194 195 } 195 196 }; 196 197 List<String> classNames = new ArrayList<String>(); 197 198 SortedSet<String> classNames = new TreeSet<String>(new Comparator<String>() { 199 @Override 200 public int compare(String arg1, String arg2) { 201 String str1 = arg1.substring(arg1.lastIndexOf('.') + cmdPrefix.length() + 1); 202 String str2 = arg2.substring(arg2.lastIndexOf('.') + cmdPrefix.length() + 1); 203 return str1.compareTo(str2); 204 } 205 206 }); 207 198 208 for (String packageName : packages) { 199 209 String path = packageName.replace('.', '/'); 200 210 try { 201 211 Enumeration<URL> resources = ClassLoader.getSystemResources(path); 202 212 203 213 while (resources.hasMoreElements()) { 204 214 URL resource = resources.nextElement(); 205 215 File packageDir = new File(resource.getFile()); 206 216 207 217 if (packageDir.isDirectory()) { 208 218 for (File classFile : packageDir.listFiles(filter)) { 209 String className = classFile.getName().substring 210 (0, classFile.getName().lastIndexOf('.')); 219 String className = 220 classFile.getName().substring(0, 221 classFile.getName().lastIndexOf('.')); 211 222 classNames.add(packageName + "." + className); 212 223 } … … 219 230 { 220 231 String jarFile = resource.getFile().substring("file:".length(), index); 221 232 222 233 // we have to read the package content from a jar file 223 234 JarInputStream jarInputStream = null; … … 227 238 catch (Exception e) { 228 239 e.printStackTrace(); 229 Console.traceln 230 (Level.WARNING, "could not read contents of jar " +jarFile);240 Console.traceln(Level.WARNING, "could not read contents of jar " + 241 jarFile); 231 242 } 232 243 … … 237 248 (entry.getName().startsWith(path))) 238 249 { 239 String className = entry.getName().substring 240 (path.length(), entry.getName().lastIndexOf('.')); 250 String className = 251 entry.getName().substring(path.length(), 252 entry.getName().lastIndexOf('.')); 241 253 classNames.add(packageName + "." + className); 242 254 } … … 251 263 } 252 264 } 253 254 Collections.sort(classNames, new Comparator<String>() { 255 @Override 256 public int compare(String arg1, String arg2) { 257 String str1 = arg1.substring(arg1.lastIndexOf('.') + cmdPrefix.length() + 1); 258 String str2 = arg2.substring(arg2.lastIndexOf('.') + cmdPrefix.length() + 1); 259 return str1.compareTo(str2); 260 } 261 262 }); 265 263 266 for (String className : classNames) { 264 String commandStr = 265 className.substring(className.lastIndexOf('.') + cmdPrefix.length() + 1); 266 267 // commands may be found twice as a package may be available twice on the 268 // class path. Therefore check, if the command was already dumped before 269 // dumping it. 270 if (!commands.contains(commandStr)) { 271 // class may still be inner classes. Therefore load the command, to 272 // see if it is really available and a command. 273 Command cmd = loadCMD(className); 274 if (cmd != null) { 275 commands.add(cmd); 276 } 277 } 278 } 279 267 // class may still be inner classes. Therefore load the command, to 268 // see if it is really available and a command. 269 Command cmd = loadCMD(className); 270 if (cmd != null) { 271 commands.add(cmd); 272 } 273 } 274 280 275 Command[] commandArray = commands.toArray(new Command[commands.size()]); 281 276 return commandArray;
Note: See TracChangeset
for help on using the changeset viewer.