001package org.kuali.common.devops.archive.sweep;
002
003import static java.lang.System.currentTimeMillis;
004import static org.kuali.common.util.Encodings.UTF8;
005import static org.kuali.common.util.FormatUtils.getMillis;
006
007import java.io.File;
008
009import javax.validation.constraints.Min;
010
011import org.kuali.common.aws.model.ImmutableAWSCredentials;
012import org.kuali.common.core.build.ValidatingBuilder;
013import org.kuali.common.core.file.scan.ScanRequest;
014import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
015
016import com.amazonaws.auth.AWSCredentials;
017import com.fasterxml.jackson.annotation.JsonIgnore;
018import com.google.common.base.Predicate;
019
020@IdiotProofImmutable
021public final class SweepContext {
022
023        private final String bucket;
024        private final File mount;
025        private final String hostname;
026        private final ScanRequest scan;
027        @JsonIgnore
028        private final Predicate<File> excludes;
029        private final PathAttributes rootDirAttributes;
030        @JsonIgnore
031        private final ImmutableAWSCredentials credentials;
032        private final int threads;
033        private final String encoding;
034        @Min(0)
035        private final long minimumAge;
036
037        private SweepContext(Builder builder) {
038                this.bucket = builder.bucket;
039                this.mount = builder.mount;
040                this.hostname = builder.hostname;
041                this.scan = builder.scan;
042                this.excludes = builder.excludes;
043                this.rootDirAttributes = builder.rootDirAttributes;
044                this.threads = builder.threads;
045                this.encoding = builder.encoding;
046                this.minimumAge = builder.minimumAge;
047                this.credentials = ImmutableAWSCredentials.copyOf(builder.credentials);
048        }
049
050        public static Builder builder() {
051                return new Builder();
052        }
053
054        public static class Builder extends ValidatingBuilder<SweepContext> {
055
056                private String hostname;
057                private String bucket;
058                private ScanRequest scan;
059                private File mount;
060                private Predicate<File> excludes;
061                private AWSCredentials credentials;
062                // this equates to "drwxr-xr-x root root"
063                // it is used when creating artificial top level directories from the hostname
064                // eg ci.kuali.org -> /org/kuali/ci
065                // this won't work universally, but on most linux systems there is a root user with gid/uid=0
066                private PathAttributes rootDirAttributes = PathAttributes.builder().withGid(0).withUid(0).withMode(16877).withMtime(currentTimeMillis()).build();
067                private int threads = 8;
068                private String encoding = UTF8;
069                private long minimumAge = getMillis("12h");
070
071                public Builder withMinimumAge(long minimumAge) {
072                        this.minimumAge = minimumAge;
073                        return this;
074                }
075
076                public Builder withEncoding(String encoding) {
077                        this.encoding = encoding;
078                        return this;
079                }
080
081                public Builder withThreads(int threads) {
082                        this.threads = threads;
083                        return this;
084                }
085
086                public Builder withAWSCredentials(AWSCredentials credentials) {
087                        this.credentials = credentials;
088                        return this;
089                }
090
091                public Builder withRootDirAttributes(PathAttributes rootDirAttributes) {
092                        this.rootDirAttributes = rootDirAttributes;
093                        return this;
094                }
095
096                public Builder withBucket(String bucket) {
097                        this.bucket = bucket;
098                        return this;
099                }
100
101                public Builder withMount(File mount) {
102                        this.mount = mount;
103                        return this;
104                }
105
106                public Builder withHostname(String hostname) {
107                        this.hostname = hostname;
108                        return this;
109                }
110
111                public Builder withScan(ScanRequest scan) {
112                        this.scan = scan;
113                        return this;
114                }
115
116                public Builder withExcludes(Predicate<File> excludes) {
117                        this.excludes = excludes;
118                        return this;
119                }
120
121                @Override
122                public SweepContext build() {
123                        return validate(new SweepContext(this));
124                }
125        }
126
127        public String getBucket() {
128                return bucket;
129        }
130
131        public File getMount() {
132                return mount;
133        }
134
135        public String getHostname() {
136                return hostname;
137        }
138
139        public ScanRequest getScan() {
140                return scan;
141        }
142
143        public Predicate<File> getExcludes() {
144                return excludes;
145        }
146
147        public PathAttributes getRootDirAttributes() {
148                return rootDirAttributes;
149        }
150
151        public ImmutableAWSCredentials getCredentials() {
152                return credentials;
153        }
154
155        public int getThreads() {
156                return threads;
157        }
158
159        public String getEncoding() {
160                return encoding;
161        }
162
163        public long getMinimumAge() {
164                return minimumAge;
165        }
166
167}