View Javadoc
1   package org.kuali.common.devops.jenkins.archive;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static java.nio.file.Files.isDirectory;
5   import static java.nio.file.Files.isRegularFile;
6   import static java.nio.file.Files.isSymbolicLink;
7   import static org.kuali.common.core.io.Paths.getRealPath;
8   
9   import java.nio.file.Path;
10  import java.nio.file.Paths;
11  
12  import org.kuali.common.core.build.ValidatingBuilder;
13  import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
14  
15  import com.google.common.base.Predicate;
16  
17  @IdiotProofImmutable
18  public final class JenkinsBuildPredicate implements Predicate<Path> {
19  
20  	private final int requiredLength;
21  	private final String requiredPrefix;
22  	private final String buildXml;
23  
24  	@Override
25  	public boolean apply(Path dir) {
26  		checkNotNull(dir);
27  
28  		// skip symbolic links and anything that's not a directory
29  		if (isSymbolicLink(dir) || !isDirectory(dir)) {
30  			return false;
31  		}
32  
33  		// Extract the name of the directory
34  		String name = dir.getFileName().toString();
35  
36  		// Make sure it has the required prefix
37  		boolean hasPrefix = name.startsWith(requiredPrefix);
38  
39  		// Make sure it is the required length
40  		boolean properLength = name.length() == requiredLength;
41  
42  		// Make sure this directory contains a file called build.xml
43  		boolean hasBuildXml = isRegularFile(Paths.get(getRealPath(dir).toString() + "/" + buildXml));
44  
45  		// If it's got the right prefix, proper length, and has a build.xml, we are good to go
46  		return hasPrefix && properLength && hasBuildXml;
47  
48  	}
49  
50  	private JenkinsBuildPredicate(Builder builder) {
51  		this.requiredLength = builder.requiredLength;
52  		this.requiredPrefix = builder.requiredPrefix;
53  		this.buildXml = builder.buildXml;
54  	}
55  
56  	public static JenkinsBuildPredicate build() {
57  		return builder().build();
58  	}
59  
60  	public static Builder builder() {
61  		return new Builder();
62  	}
63  
64  	public static class Builder extends ValidatingBuilder<JenkinsBuildPredicate> {
65  
66  		private int requiredLength = "2014-08-20_18-34-13".length();
67  		private String requiredPrefix = "20";
68  		private String buildXml = "build.xml";
69  
70  		public Builder withRequiredLength(int requiredLength) {
71  			this.requiredLength = requiredLength;
72  			return this;
73  		}
74  
75  		public Builder withRequiredPrefix(String requiredPrefix) {
76  			this.requiredPrefix = requiredPrefix;
77  			return this;
78  		}
79  
80  		public Builder withBuildXml(String buildXml) {
81  			this.buildXml = buildXml;
82  			return this;
83  		}
84  
85  		@Override
86  		public JenkinsBuildPredicate build() {
87  			return validate(new JenkinsBuildPredicate(this));
88  		}
89  	}
90  
91  	public int getRequiredLength() {
92  		return requiredLength;
93  	}
94  
95  	public String getRequiredPrefix() {
96  		return requiredPrefix;
97  	}
98  
99  	public String getBuildXml() {
100 		return buildXml;
101 	}
102 
103 }