Skip to content

Commit

Permalink
Refactored PatternsEntityMatcher.pattern field from Pattern to String…
Browse files Browse the repository at this point in the history
…, 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
fcamblor committed Jul 21, 2012
1 parent 6751db9 commit 561266a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 121 deletions.
@@ -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;
}

}
Expand Up @@ -21,27 +21,14 @@ public class JenkinsConfigScmSyncStrategy extends AbstractScmSyncStrategy {
add(new PageMatcher("^newView$", "form[name='createView']"));
} };

private static final Pattern [] PATTERNS = new Pattern[]{
Pattern.compile("^config\\.xml$"),
Pattern.compile("^hudson[^\\/]+\\.xml$")
private static final String[] PATTERNS = new String[]{
"config.xml",
"hudson*.xml"
};

private static final ConfigurationEntityMatcher CONFIG_ENTITY_MATCHER = new PatternsEntityMatcher(PATTERNS);

public JenkinsConfigScmSyncStrategy(){
super(CONFIG_ENTITY_MATCHER, PAGE_MATCHERS);
}

public List<File> createInitializationSynchronizedFileset() {
return new ArrayList<File>(){{
File root = new File(Hudson.getInstance().getRootDir().getAbsolutePath());
for(String f : root.list()) {
for(Pattern pattern : PATTERNS) {
if (pattern.matcher(f).matches()) {
add(new File(root.getAbsolutePath()+File.separator+f));
}
}
}
}};
}
}
Expand Up @@ -21,24 +21,12 @@ public class JobConfigScmSyncStrategy extends AbstractScmSyncStrategy {
// Only saving config.xml file located in job directory
// Some plugins (like maven release plugin) could add their own configuration files in the job directory that we don't want to synchronize
// ... at least in the current strategy !
private static final Pattern [] PATTERNS = new Pattern[] {
Pattern.compile("^jobs/[^/]+/config\\.xml$")
private static final String [] PATTERNS = new String[] {
"jobs/*/config.xml"
};
private static final ConfigurationEntityMatcher CONFIG_ENTITY_MANAGER = new ClassAndFileConfigurationEntityMatcher(Job.class, PATTERNS);

public JobConfigScmSyncStrategy(){
super(CONFIG_ENTITY_MANAGER, PAGE_MATCHERS);
}

public List<File> createInitializationSynchronizedFileset() {
List<File> syncedFiles = new ArrayList<File>();
File hudsonJobsDirectory = new File(Hudson.getInstance().getRootDir().getAbsolutePath()+File.separator+"jobs");
for(File hudsonJob : hudsonJobsDirectory.listFiles()){
if(hudsonJob.isDirectory()){
File hudsonJobConfig = new File(hudsonJob.getAbsoluteFile()+File.separator+"config.xml");
syncedFiles.add(hudsonJobConfig);
}
}
return syncedFiles;
}
}
@@ -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;
}

}
@@ -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);
}
@@ -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();
}
}

0 comments on commit 561266a

Please sign in to comment.