Changeset 669


Ignore:
Timestamp:
08/28/12 14:04:11 (12 years ago)
Author:
sherbold
Message:
  • modified command executer to ensure that each command is only displayed once
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/java-utils/src/main/java/de/ugoe/cs/util/console/CommandExecuter.java

    r665 r669  
    99import java.security.InvalidParameterException; 
    1010import java.util.ArrayList; 
    11 import java.util.Collections; 
    1211import java.util.Comparator; 
    1312import java.util.Enumeration; 
    1413import java.util.List; 
     14import java.util.SortedSet; 
     15import java.util.TreeSet; 
    1516import java.util.jar.JarEntry; 
    1617import java.util.jar.JarInputStream; 
     
    176177    /** 
    177178     * <p> 
    178      * reads all available commands from the registered command packages and returns a list of 
    179      * their names 
    180      * </p> 
    181      * 
     179     * reads all available commands from the registered command packages and returns a list of their 
     180     * names 
     181     * </p> 
     182     *  
    182183     * @return an array containing the names of the available commands. 
    183184     */ 
     
    187188        packages.addAll(commandPackageList); 
    188189        packages.add(defaultPackage); 
    189          
     190 
    190191        FilenameFilter filter = new FilenameFilter() { 
    191192            @Override 
     
    194195            } 
    195196        }; 
    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 
    198208        for (String packageName : packages) { 
    199209            String path = packageName.replace('.', '/'); 
    200210            try { 
    201211                Enumeration<URL> resources = ClassLoader.getSystemResources(path); 
    202                  
     212 
    203213                while (resources.hasMoreElements()) { 
    204214                    URL resource = resources.nextElement(); 
    205215                    File packageDir = new File(resource.getFile()); 
    206                      
     216 
    207217                    if (packageDir.isDirectory()) { 
    208218                        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('.')); 
    211222                            classNames.add(packageName + "." + className); 
    212223                        } 
     
    219230                        { 
    220231                            String jarFile = resource.getFile().substring("file:".length(), index); 
    221                              
     232 
    222233                            // we have to read the package content from a jar file 
    223234                            JarInputStream jarInputStream = null; 
     
    227238                            catch (Exception e) { 
    228239                                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); 
    231242                            } 
    232243 
     
    237248                                    (entry.getName().startsWith(path))) 
    238249                                { 
    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('.')); 
    241253                                    classNames.add(packageName + "." + className); 
    242254                                } 
     
    251263            } 
    252264        } 
    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 
    263266        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 
    280275        Command[] commandArray = commands.toArray(new Command[commands.size()]); 
    281276        return commandArray; 
Note: See TracChangeset for help on using the changeset viewer.