Read project markers and ignores from the indexing backend rather than from the hudson plugin's configuration
authorJan Lahoda <jlahoda@netbeans.org>
Sun, 08 Jul 2012 15:56:24 +0200
changeset 817e755b5974c6a
parent 816 97147b705fbe
child 818 264a9dc6dfaa
Read project markers and ignores from the indexing backend rather than from the hudson plugin's configuration
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java
remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly
remoting/server/indexer/impl/release/patterns/ignore-standard
remoting/server/indexer/impl/release/patterns/project-marker-ant-based
remoting/server/indexer/impl/release/patterns/project-marker-maven
     1.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Sun Jul 08 10:36:57 2012 +0200
     1.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Sun Jul 08 15:56:24 2012 +0200
     1.3 @@ -54,19 +54,24 @@
     1.4  import hudson.tasks.Builder;
     1.5  import hudson.util.ArgumentListBuilder;
     1.6  import hudson.util.FormValidation;
     1.7 +import java.io.BufferedReader;
     1.8  import java.io.File;
     1.9  import java.io.FileOutputStream;
    1.10  import java.io.IOException;
    1.11  import java.io.InputStream;
    1.12 +import java.io.InputStreamReader;
    1.13  import java.io.OutputStreamWriter;
    1.14  import java.io.Serializable;
    1.15  import java.io.Writer;
    1.16 +import java.util.ArrayList;
    1.17  import java.util.Arrays;
    1.18  import java.util.Collection;
    1.19  import java.util.Comparator;
    1.20  import java.util.HashSet;
    1.21  import java.util.List;
    1.22  import java.util.Set;
    1.23 +import java.util.logging.Level;
    1.24 +import java.util.logging.Logger;
    1.25  import java.util.regex.Matcher;
    1.26  import java.util.regex.Pattern;
    1.27  import java.util.regex.PatternSyntaxException;
    1.28 @@ -143,7 +148,7 @@
    1.29          listener.getLogger().println("Looking for projects in: " + build.getWorkspace().getRemote());
    1.30  
    1.31          FilePath base = indexSubDirectory == null || indexSubDirectory.isEmpty() ? build.getWorkspace() : build.getWorkspace().child(indexSubDirectory); //XXX: child also supports absolute paths! absolute paths should not be allowed here (for security)
    1.32 -        RemoteResult res = base.act(new FindProjects(getDescriptor().getProjectMarkers(), getDescriptor().getIgnorePattern(), getIgnorePatterns()));
    1.33 +        RemoteResult res = base.act(new FindProjects(t.getHome(), getIgnorePatterns()));
    1.34  
    1.35          listener.getLogger().println("Running: " + toolName + " on projects: " + res);
    1.36  
    1.37 @@ -200,15 +205,18 @@
    1.38          return null;
    1.39      }
    1.40  
    1.41 -    private static void findProjects(File root, Collection<String> result, Pattern markers, Pattern ignore, Pattern perProjectIgnore, StringBuilder relPath) {
    1.42 +    private static void findProjects(File root, Collection<String> result, Iterable<Pattern> markers, Iterable<Pattern> ignores, Pattern perProjectIgnore, StringBuilder relPath) {
    1.43          int len = relPath.length();
    1.44          boolean first = relPath.length() == 0;
    1.45  
    1.46 -        Matcher m = markers.matcher(relPath);
    1.47 +        for (Pattern marker : markers) {
    1.48 +            Matcher m = marker.matcher(relPath);
    1.49  
    1.50 -        if (m.matches()) {
    1.51 -            if (perProjectIgnore == null || !perProjectIgnore.matcher(relPath).matches()) {
    1.52 -                result.add(m.group(1));
    1.53 +            if (m.matches()) {
    1.54 +                if (perProjectIgnore == null || !perProjectIgnore.matcher(relPath).matches()) {
    1.55 +                    result.add(m.group(1));
    1.56 +                }
    1.57 +                break;
    1.58              }
    1.59          }
    1.60  
    1.61 @@ -220,12 +228,14 @@
    1.62                      return o1.getName().compareTo(o2.getName());
    1.63                  }
    1.64              });
    1.65 -            for (File c : children) {
    1.66 -                if (ignore.matcher(c.getName()).matches()) continue;
    1.67 +            OUTER: for (File c : children) {
    1.68 +                for (Pattern ignore : ignores) {
    1.69 +                    if (ignore.matcher(c.getName()).matches()) continue OUTER;
    1.70 +                }
    1.71                  if (!first)
    1.72                      relPath.append("/");
    1.73                  relPath.append(c.getName());
    1.74 -                findProjects(c, result, markers, ignore, perProjectIgnore, relPath);
    1.75 +                findProjects(c, result, markers, ignores, perProjectIgnore, relPath);
    1.76                  relPath.delete(len, relPath.length());
    1.77              }
    1.78          }
    1.79 @@ -244,10 +254,6 @@
    1.80      public static final class DescriptorImpl extends Descriptor<Builder> {
    1.81  
    1.82          private File cacheDir;
    1.83 -        private static final String DEFAULT_PROJECT_MARKERS = "(.*)/(nbproject/project.xml|pom.xml)";
    1.84 -        private String projectMarkers = DEFAULT_PROJECT_MARKERS;
    1.85 -        private static final String DEFAULT_IGNORE_PATTERN = "CVS|\\.hg|\\.svn";
    1.86 -        private String ignorePattern = DEFAULT_IGNORE_PATTERN;
    1.87  
    1.88          public DescriptorImpl() {
    1.89              Cache.setStandaloneCacheRoot(cacheDir = new File(Hudson.getInstance().getRootDir(), "index").getAbsoluteFile());
    1.90 @@ -258,22 +264,6 @@
    1.91              return cacheDir;
    1.92          }
    1.93  
    1.94 -        public String getProjectMarkers() {
    1.95 -            return projectMarkers;
    1.96 -        }
    1.97 -
    1.98 -        public  void setProjectMarkers(String projectMarkers) {
    1.99 -            this.projectMarkers = projectMarkers;
   1.100 -        }
   1.101 -
   1.102 -        public String getIgnorePattern() {
   1.103 -            return ignorePattern;
   1.104 -        }
   1.105 -
   1.106 -        public  void setIgnorePattern(String ignorePattern) {
   1.107 -            this.ignorePattern = ignorePattern;
   1.108 -        }
   1.109 -
   1.110          @Override
   1.111          public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormException {
   1.112              return new IndexingBuilder(req, formData);
   1.113 @@ -282,8 +272,6 @@
   1.114          @Override
   1.115          public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
   1.116              cacheDir = new File(json.getString("cacheDir"));
   1.117 -            projectMarkers = json.optString("projectMarkers", DEFAULT_PROJECT_MARKERS);
   1.118 -            ignorePattern = json.optString("ignorePattern", DEFAULT_IGNORE_PATTERN);
   1.119  
   1.120              save();
   1.121              
   1.122 @@ -329,21 +317,63 @@
   1.123      }
   1.124  
   1.125      private static class FindProjects implements FileCallable<RemoteResult> {
   1.126 -        private final String ignorePattern;
   1.127 -        private final String markers;
   1.128 +        private final String toolHome;
   1.129          private final String perProjectIgnore;
   1.130 -        public FindProjects(String markers, String ignorePattern, String perProjectIgnore) {
   1.131 -            this.markers = markers;
   1.132 -            this.ignorePattern = ignorePattern;
   1.133 +        public FindProjects(String toolHome, String perProjectIgnore) {
   1.134 +            this.toolHome = toolHome;
   1.135              this.perProjectIgnore = perProjectIgnore;
   1.136          }
   1.137          public RemoteResult invoke(File file, VirtualChannel vc) throws IOException, InterruptedException {
   1.138 +            List<Pattern> projectMarkers = new ArrayList<Pattern>();
   1.139 +            List<Pattern> ignorePatterns = new ArrayList<Pattern>();
   1.140 +            FilePath indexerPath = new FilePath(new File(toolHome, "indexer"));
   1.141 +            for (FilePath clusters : indexerPath.listDirectories()) {
   1.142 +                FilePath patternsDirectory = clusters.child("patterns");
   1.143 +                if (!patternsDirectory.isDirectory()) continue;
   1.144 +                for (FilePath patterns : patternsDirectory.list()) {
   1.145 +                    if (patterns.getName().startsWith("project-marker-")) {
   1.146 +                        projectMarkers.addAll(readPatterns(patterns));
   1.147 +                    } else if (patterns.getName().startsWith("ignore-")) {
   1.148 +                        ignorePatterns.addAll(readPatterns(patterns));
   1.149 +                    }
   1.150 +                }
   1.151 +            }
   1.152 +            
   1.153              Set<String> projects = new HashSet<String>();
   1.154  
   1.155 -            findProjects(file, projects, Pattern.compile(markers), Pattern.compile(ignorePattern), perProjectIgnore == null || perProjectIgnore.trim().isEmpty() ? null : Pattern.compile(perProjectIgnore), new StringBuilder());
   1.156 +            findProjects(file, projects, projectMarkers, ignorePatterns, perProjectIgnore == null || perProjectIgnore.trim().isEmpty() ? null : Pattern.compile(perProjectIgnore), new StringBuilder());
   1.157  
   1.158              return new RemoteResult(projects, file.getCanonicalPath()/*XXX: will resolve symlinks!!!*/);
   1.159          }
   1.160      }
   1.161  
   1.162 +    private static List<Pattern> readPatterns(FilePath source) {
   1.163 +        BufferedReader in = null;
   1.164 +
   1.165 +        List<Pattern> result = new ArrayList<Pattern>();
   1.166 +        try {
   1.167 +            in = new BufferedReader(new InputStreamReader(source.read(), "UTF-8"));
   1.168 +            String line;
   1.169 +
   1.170 +            while ((line = in.readLine()) != null) {
   1.171 +                line = line.trim();
   1.172 +                if (!line.isEmpty()) {
   1.173 +                    result.add(Pattern.compile(line));
   1.174 +                }
   1.175 +            }
   1.176 +        } catch (IOException ex) {
   1.177 +            Logger.getLogger(IndexingBuilder.class.getName()).log(Level.SEVERE, null, ex);
   1.178 +        } finally {
   1.179 +            if (in != null) {
   1.180 +                try {
   1.181 +                    in.close();
   1.182 +                } catch (IOException ex) {
   1.183 +                    Logger.getLogger(IndexingBuilder.class.getName()).log(Level.SEVERE, null, ex);
   1.184 +                }
   1.185 +            }
   1.186 +        }
   1.187 +
   1.188 +        return result;
   1.189 +    }
   1.190 +
   1.191  }
     2.1 --- a/remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly	Sun Jul 08 10:36:57 2012 +0200
     2.2 +++ b/remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly	Sun Jul 08 15:56:24 2012 +0200
     2.3 @@ -4,14 +4,6 @@
     2.4          <f:entry title="Cache directory:" field="cacheDir">
     2.5              <f:textbox />
     2.6          </f:entry>
     2.7 -        <f:advanced>
     2.8 -        <f:entry title="Project markers:" field="projectMarkers">
     2.9 -            <f:textbox />
    2.10 -        </f:entry>
    2.11 -        <f:entry title="Ignore pattern:" field="ignorePattern">
    2.12 -            <f:textbox />
    2.13 -        </f:entry>
    2.14 -        </f:advanced>
    2.15      </f:section>
    2.16      
    2.17  </j:jelly>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/remoting/server/indexer/impl/release/patterns/ignore-standard	Sun Jul 08 15:56:24 2012 +0200
     3.3 @@ -0,0 +1,1 @@
     3.4 +CVS|\\.hg|\\.svn
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/remoting/server/indexer/impl/release/patterns/project-marker-ant-based	Sun Jul 08 15:56:24 2012 +0200
     4.3 @@ -0,0 +1,1 @@
     4.4 +(.*)/nbproject/project.xml
     4.5 \ No newline at end of file
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/remoting/server/indexer/impl/release/patterns/project-marker-maven	Sun Jul 08 15:56:24 2012 +0200
     5.3 @@ -0,0 +1,1 @@
     5.4 +(.*)/pom.xml
     5.5 \ No newline at end of file