Changeset 492


Ignore:
Timestamp:
10/23/13 16:07:59 (11 years ago)
Author:
davidb
Message:

Replaced getClasses() with getClassesNew(), an alternative that is compatible with use in an Applet. After a bit more testing, this should be renamed and become the main one used in Expeditee

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Actions.java

    r480 r492  
    88import java.lang.reflect.Modifier;
    99import java.net.URL;
     10import java.net.URI;
     11import java.net.URLDecoder;
     12import java.net.JarURLConnection;
    1013import java.rmi.UnexpectedException;
    1114import java.util.ArrayList;
     
    1619import java.util.List;
    1720import java.util.Set;
     21import java.util.jar.JarEntry;
    1822import java.util.jar.JarFile;
    1923import java.util.zip.ZipEntry;
     24
    2025
    2126import org.expeditee.agents.Agent;
     
    2833import org.expeditee.gui.FreeItems;
    2934import org.expeditee.gui.MessageBay;
     35import org.expeditee.gui.UserSettings;
    3036import org.expeditee.io.Conversion;
    3137import org.expeditee.items.Item;
     
    8793            + "actions.NavigationActions";
    8894
     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
    89185    public static Class<?>[] getClasses(final String pckgname)
    90186            throws ClassNotFoundException {
     187
    91188        final ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
     189
    92190        // Must be a forward slash for loading resources
    93191        final String packagePath = pckgname.replace('.', '/');
    94         if(System.getProperty("expeditee.home") == null) {
     192
     193        if (System.getProperty("eclipse.expeditee.home") == null) {
    95194            final ClassLoader cld = Thread.currentThread().getContextClassLoader();
    96195            if (cld == null) {
     
    108207                        + System.getProperty("line.separator")
    109208                        + "Are you running from within Eclipse? (or just not with Jar)  Then make sure your"
    110                         + " 'expeditee.home' property is set correctly.  It is currently: '"
    111                         + System.getProperty("expeditee.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")
    112211                        + "You can set it by adding a VM argument.  "
    113                         + "Example: -Dexpeditee.home=D:\\Desktop\\Research\\expeditee-svn");
     212                        + "Example: -Declipse.expeditee.home=D:\\Desktop\\Research\\expeditee-svn");
    114213                e.printStackTrace();
    115214            }
     
    118217            }
    119218            final File directory = new File(resource.getFile());
     219
    120220            final int splitPoint = directory.getPath().indexOf('!');
    121221            if(splitPoint > 0) {
     
    141241                                    // The forward slash below is a forwards slash for
    142242                                    // 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));
    145248                                }
    146249                            }
     
    157260                    }
    158261            } else {
     262
    159263                System.err.println( "A Error has occured when trying to use a Jar file to find actions or agents."
    160264                        + System.getProperty("line.separator")
    161265                        + "Are you running from within Eclipse? (or just not with Jar)  Then make sure your"
    162                         + " 'expeditee.home' property is set correctly.  It is currently: '"
    163                         + System.getProperty("expeditee.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")
    164268                        + "You can set it by adding a VM argument.  "
    165                         + "Example: -Dexpeditee.home=D:\\Desktop\\Research\\expeditee-svn");
     269                        + "Example: -Declipse.expeditee.home=D:\\Desktop\\Research\\expeditee-svn");
    166270            }
    167271        } 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
    169278            if (directory.exists()) {
    170279                // Get the list of the files contained in the package
     
    312421
    313422        try {
    314             classes = getClasses(AGENTS_PACKAGE);
     423            classes = getClassesNew(AGENTS_PACKAGE);
    315424
    316425            for (int i = 0; i < classes.length; i++) {
     
    320429            }
    321430
    322             classes = getClasses(WIDGET_PACKAGE);
     431            classes = getClassesNew(WIDGET_PACKAGE);
    323432
    324433            for (int i = 0; i < classes.length; i++) {
     
    328437            }
    329438
    330             classes = getClasses(CHARTS_PACKAGE);
     439            classes = getClassesNew(CHARTS_PACKAGE);
    331440
    332441            for (int i = 0; i < classes.length; i++) {
     
    345454
    346455        try {
    347             classes = getClasses(ACTIONS_PACKAGE);
     456            classes = getClassesNew(ACTIONS_PACKAGE);
    348457
    349458            for (int i = 0; i < classes.length; i++) {
Note: See TracChangeset for help on using the changeset viewer.