Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored PatternsEntityMatcher.pattern field from Pattern to String…
…, using ant-like includes instead of regexes, for sync'ed files => ScmSyncStrategy.createInitializationSynchronizedFileset() can be simplified by using these includes. Will be a first step for implementing JENKINS-8225
- Loading branch information
Showing
6 changed files
with
134 additions
and
121 deletions.
There are no files selected for viewing
107 changes: 62 additions & 45 deletions
107
src/main/java/hudson/plugins/scm_sync_configuration/strategies/AbstractScmSyncStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,62 @@ | ||
package hudson.plugins.scm_sync_configuration.strategies; | ||
|
||
import hudson.model.Saveable; | ||
import hudson.model.Hudson; | ||
import hudson.plugins.scm_sync_configuration.strategies.model.ConfigurationEntityMatcher; | ||
import hudson.plugins.scm_sync_configuration.strategies.model.PageMatcher; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public abstract class AbstractScmSyncStrategy implements ScmSyncStrategy { | ||
|
||
private ConfigurationEntityMatcher configEntityMatcher; | ||
private List<PageMatcher> pageMatchers; | ||
|
||
protected AbstractScmSyncStrategy(ConfigurationEntityMatcher _configEntityMatcher, List<PageMatcher> _pageMatchers){ | ||
this.configEntityMatcher = _configEntityMatcher; | ||
this.pageMatchers = _pageMatchers; | ||
} | ||
|
||
public boolean isSaveableApplicable(Saveable saveable, File file) { | ||
return configEntityMatcher.matches(saveable, file); | ||
} | ||
|
||
public PageMatcher getPageMatcherMatching(String url){ | ||
String rootUrl = Hudson.getInstance().getRootUrl(); | ||
String cleanedUrl = null; | ||
if(url.startsWith(rootUrl)){ | ||
cleanedUrl = url.substring(rootUrl.length()); | ||
} else { | ||
cleanedUrl = url; | ||
} | ||
for(PageMatcher pm : pageMatchers){ | ||
if(pm.getUrlRegex().matcher(cleanedUrl).matches()){ | ||
return pm; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean isCurrentUrlApplicable(String url) { | ||
return getPageMatcherMatching(url)!=null; | ||
} | ||
|
||
} | ||
package hudson.plugins.scm_sync_configuration.strategies; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.Collections2; | ||
import com.sun.istack.internal.Nullable; | ||
import hudson.model.Saveable; | ||
import hudson.model.Hudson; | ||
import hudson.plugins.scm_sync_configuration.strategies.model.ConfigurationEntityMatcher; | ||
import hudson.plugins.scm_sync_configuration.strategies.model.PageMatcher; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public abstract class AbstractScmSyncStrategy implements ScmSyncStrategy { | ||
|
||
private static final Function<String,File> PATH_TO_FILE_IN_HUDSON = new Function<String, File>() { | ||
public File apply(@Nullable String path) { | ||
return new File(Hudson.getInstance().getRootDir()+File.separator+path); | ||
} | ||
}; | ||
|
||
private ConfigurationEntityMatcher configEntityMatcher; | ||
private List<PageMatcher> pageMatchers; | ||
|
||
protected AbstractScmSyncStrategy(ConfigurationEntityMatcher _configEntityMatcher, List<PageMatcher> _pageMatchers){ | ||
this.configEntityMatcher = _configEntityMatcher; | ||
this.pageMatchers = _pageMatchers; | ||
} | ||
|
||
public boolean isSaveableApplicable(Saveable saveable, File file) { | ||
return configEntityMatcher.matches(saveable, file); | ||
} | ||
|
||
public PageMatcher getPageMatcherMatching(String url){ | ||
String rootUrl = Hudson.getInstance().getRootUrl(); | ||
String cleanedUrl = null; | ||
if(url.startsWith(rootUrl)){ | ||
cleanedUrl = url.substring(rootUrl.length()); | ||
} else { | ||
cleanedUrl = url; | ||
} | ||
for(PageMatcher pm : pageMatchers){ | ||
if(pm.getUrlRegex().matcher(cleanedUrl).matches()){ | ||
return pm; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public List<File> createInitializationSynchronizedFileset() { | ||
File hudsonRoot = Hudson.getInstance().getRootDir(); | ||
String[] matchingFilePaths = configEntityMatcher.matchingFilesFrom(hudsonRoot); | ||
return new ArrayList(Collections2.transform(Arrays.asList(matchingFilePaths), PATH_TO_FILE_IN_HUDSON)); | ||
} | ||
|
||
public boolean isCurrentUrlApplicable(String url) { | ||
return getPageMatcherMatching(url)!=null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 29 additions & 29 deletions
58
...ugins/scm_sync_configuration/strategies/model/ClassAndFileConfigurationEntityMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
package hudson.plugins.scm_sync_configuration.strategies.model; | ||
|
||
import hudson.model.Saveable; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
|
||
public class ClassAndFileConfigurationEntityMatcher extends PatternsEntityMatcher { | ||
|
||
private Class<? extends Saveable> saveableClazz; | ||
|
||
public ClassAndFileConfigurationEntityMatcher(Class<? extends Saveable> clazz, Pattern [] patterns){ | ||
super(patterns); | ||
this.saveableClazz = clazz; | ||
} | ||
|
||
public boolean matches(Saveable saveable, File file) { | ||
if(saveableClazz.isAssignableFrom(saveable.getClass())){ | ||
if(file == null){ | ||
return true; | ||
} else { | ||
return super.matches(saveable, file); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} | ||
package hudson.plugins.scm_sync_configuration.strategies.model; | ||
|
||
import hudson.model.Saveable; | ||
|
||
import java.io.File; | ||
import java.util.regex.Pattern; | ||
|
||
public class ClassAndFileConfigurationEntityMatcher extends PatternsEntityMatcher { | ||
|
||
private Class<? extends Saveable> saveableClazz; | ||
|
||
public ClassAndFileConfigurationEntityMatcher(Class<? extends Saveable> clazz, String[] patterns){ | ||
super(patterns); | ||
this.saveableClazz = clazz; | ||
} | ||
|
||
public boolean matches(Saveable saveable, File file) { | ||
if(saveableClazz.isAssignableFrom(saveable.getClass())){ | ||
if(file == null){ | ||
return true; | ||
} else { | ||
return super.matches(saveable, file); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
21 changes: 11 additions & 10 deletions
21
...va/hudson/plugins/scm_sync_configuration/strategies/model/ConfigurationEntityMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package hudson.plugins.scm_sync_configuration.strategies.model; | ||
|
||
import hudson.model.Saveable; | ||
|
||
import java.io.File; | ||
|
||
public interface ConfigurationEntityMatcher { | ||
|
||
public boolean matches(Saveable saveable, File file); | ||
} | ||
package hudson.plugins.scm_sync_configuration.strategies.model; | ||
|
||
import hudson.model.Saveable; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public interface ConfigurationEntityMatcher { | ||
public boolean matches(Saveable saveable, File file); | ||
public String[] matchingFilesFrom(File rootDirectory); | ||
} |
34 changes: 27 additions & 7 deletions
34
...in/java/hudson/plugins/scm_sync_configuration/strategies/model/PatternsEntityMatcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
package hudson.plugins.scm_sync_configuration.strategies.model; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.collect.Collections2; | ||
import hudson.model.Hudson; | ||
import hudson.model.Saveable; | ||
import hudson.plugins.scm_sync_configuration.JenkinsFilesHelper; | ||
import org.apache.tools.ant.DirectoryScanner; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.File; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
public class PatternsEntityMatcher implements ConfigurationEntityMatcher { | ||
|
||
private Pattern [] patterns; | ||
private static final Function<String,String> FILE_SEPARATOR_TRANSFORMER = new Function<String, String>() { | ||
public String apply(@Nullable String s) { | ||
return s.replaceAll("/", File.separator); | ||
} | ||
}; | ||
|
||
public PatternsEntityMatcher(Pattern [] patterns) { | ||
this.patterns = patterns; | ||
private String[] patterns; | ||
|
||
public PatternsEntityMatcher(String[] patterns) { | ||
this.patterns = Collections2.transform(Arrays.asList(patterns), FILE_SEPARATOR_TRANSFORMER).toArray(new String[0]); | ||
} | ||
|
||
public boolean matches(Saveable saveable, File file) { | ||
if (file == null) { | ||
return false; | ||
} | ||
String filePathRelativeToHudsonRoot = JenkinsFilesHelper.buildPathRelativeToHudsonRoot(file); | ||
for(Pattern pattern : patterns) { | ||
if (pattern.matcher(filePathRelativeToHudsonRoot).matches()) { | ||
return true; | ||
} | ||
for(String pattern : patterns) { | ||
if(DirectoryScanner.match(pattern, filePathRelativeToHudsonRoot)){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public String[] matchingFilesFrom(File rootDirectory) { | ||
DirectoryScanner scanner = new DirectoryScanner(); | ||
scanner.setIncludes(patterns); | ||
scanner.setBasedir(rootDirectory); | ||
scanner.scan(); | ||
return scanner.getIncludedFiles(); | ||
} | ||
} |