]> source.dussan.org Git - vaadin-framework.git/commitdiff
Create widgetset in first suitable classpath directory - should be a source directory
authorHenri Sara <henri.sara@itmill.com>
Wed, 28 Oct 2009 13:50:35 +0000 (13:50 +0000)
committerHenri Sara <henri.sara@itmill.com>
Wed, 28 Oct 2009 13:50:35 +0000 (13:50 +0000)
svn changeset:9430/svn branch:6.2

src/com/vaadin/terminal/gwt/widgetsetutils/ClassPathExplorer.java
src/com/vaadin/terminal/gwt/widgetsetutils/WidgetSetBuilder.java

index 6a458608038e3e9af23f35b33f8df960e7c8cb52..269d911ad1368b4c4c3f2d03da3b7dbbf146f682 100644 (file)
@@ -7,10 +7,14 @@ import java.net.JarURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.jar.Attributes;
@@ -50,7 +54,8 @@ public class ClassPathExplorer {
         }
     };
 
-    private static Map<URL, String> classpathLocations = getClasspathLocations();
+    private static List<String> rawClasspathEntries = getRawClasspathEntries();
+    private static Map<URL, String> classpathLocations = getClasspathLocations(rawClasspathEntries);
 
     private ClassPathExplorer() {
     }
@@ -150,8 +155,9 @@ public class ClassPathExplorer {
      * Determine every URL location defined by the current classpath, and it's
      * associated package name.
      */
-    private final static Map<URL, String> getClasspathLocations() {
-        Map<URL, String> locations = new HashMap<URL, String>();
+    private final static List<String> getRawClasspathEntries() {
+        // try to keep the order of the classpath
+        List<String> locations = new ArrayList<String>();
 
         String pathSep = System.getProperty("path.separator");
         String classpath = System.getProperty("java.class.path");
@@ -169,14 +175,28 @@ public class ClassPathExplorer {
         for (int i = 0; i < split.length; i++) {
             String classpathEntry = split[i];
             if (acceptClassPathEntry(classpathEntry)) {
-                File file = new File(classpathEntry);
-                include(null, file, locations);
+                locations.add(classpathEntry);
             }
         }
 
         return locations;
     }
 
+    /**
+     * Determine every URL location defined by the current classpath, and it's
+     * associated package name.
+     */
+    private final static Map<URL, String> getClasspathLocations(
+            List<String> rawClasspathEntries) {
+        // try to keep the order of the classpath
+        Map<URL, String> locations = new LinkedHashMap<URL, String>();
+        for (String classpathEntry : rawClasspathEntries) {
+            File file = new File(classpathEntry);
+            include(null, file, locations);
+        }
+        return locations;
+    }
+
     private static boolean acceptClassPathEntry(String classpathEntry) {
         if (!classpathEntry.endsWith(".jar")) {
             // accept all non jars (practically directories)
@@ -351,6 +371,41 @@ public class ClassPathExplorer {
         }
     }
 
+    /**
+     * Find and return the default source directory where to create new
+     * widgetsets.
+     *
+     * Return the first directory (not a JAR file etc.) on the classpath by
+     * default.
+     *
+     * TODO this could be done better...
+     *
+     * @return URL
+     */
+    public static URL getDefaultSourceDirectory() {
+        System.err.println("classpathLocations keys:  "
+                + classpathLocations.keySet());
+        Iterator<String> it = rawClasspathEntries.iterator();
+        while (it.hasNext()) {
+            String entry = it.next();
+
+            File directory = new File(entry);
+            if (directory.exists() && !directory.isHidden()
+                    && directory.isDirectory()) {
+                try {
+                    return new URL("file://" + directory.getCanonicalPath());
+                } catch (MalformedURLException e) {
+                    e.printStackTrace();
+                    // ignore: continue to the next classpath entry
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    // ignore: continue to the next classpath entry
+                }
+            }
+        }
+        return null;
+    }
+
     /**
      * Test method for helper tool
      */
index 6ce60972699c36c15a01eed60f2e95553e914b32..c6e9dd22753d018ce101c9387a7116b196b3668d 100644 (file)
@@ -41,9 +41,14 @@ public class WidgetSetBuilder {
                 .getAvailableWidgetSets();
 
         URL sourceUrl = availableWidgetSets.get(widgetset);
+        if (sourceUrl == null) {
+            // find first/default source directory
+            sourceUrl = ClassPathExplorer.getDefaultSourceDirectory();
+        }
 
         String widgetsetfilename = sourceUrl.getFile() + "/"
                 + widgetset.replace(".", "/") + ".gwt.xml";
+
         File widgetsetFile = new File(widgetsetfilename);
         if (!widgetsetFile.exists()) {
             // create empty gwt module file