001    package org.kuali.common.deploy;
002    
003    import org.apache.commons.lang3.StringUtils;
004    import org.kuali.common.util.Assert;
005    import org.kuali.common.util.LocationUtils;
006    import org.kuali.common.util.nullify.NullUtils;
007    
008    import com.google.common.base.Optional;
009    
010    public final class Deployable {
011    
012            private final String local;
013            private final String remote;
014            private final boolean exists;
015    
016            private final boolean filter;
017            private final boolean required;
018            private final Optional<String> permissions;
019    
020            public boolean isFilter() {
021                    return filter;
022            }
023    
024            public String getLocal() {
025                    return local;
026            }
027    
028            public String getRemote() {
029                    return remote;
030            }
031    
032            public Optional<String> getPermissions() {
033                    return permissions;
034            }
035    
036            public boolean isRequired() {
037                    return required;
038            }
039    
040            public boolean isExists() {
041                    return exists;
042            }
043    
044            public static class Builder {
045    
046                    private final String local;
047                    private final String remote;
048                    private final boolean exists;
049    
050                    private boolean filter = false;
051                    private boolean required = true;
052                    private Optional<String> permissions = Optional.absent();
053    
054                    public Builder(String local, String remote) {
055                            this.local = local;
056                            this.remote = remote;
057                            this.exists = LocationUtils.exists(local);
058                    }
059    
060                    public Builder filter(boolean filter) {
061                            this.filter = filter;
062                            return this;
063                    }
064    
065                    public Builder required(boolean required) {
066                            this.required = required;
067                            return this;
068                    }
069    
070                    /**
071                     * Can't be the empty string or all whitespace. <code>null</code> and the magic strings "NONE" and "NULL" are all allowed and mean the same thing. (ie no explicit
072                     * permissions will be set on the remote machine for this deployable).
073                     */
074                    public Builder permissions(String permissions) {
075                            if (permissions != null) {
076                                    Assert.noBlanks(permissions);
077                            }
078                            String trimmed = StringUtils.trimToNull(permissions);
079                            String converted = NullUtils.isNullOrNone(trimmed) ? null : trimmed;
080                            this.permissions = Optional.fromNullable(converted);
081                            return this;
082                    }
083    
084                    public Deployable build() {
085                            Assert.noBlanks(local, remote);
086                            Assert.noNulls(permissions);
087                            if (required) {
088                                    Assert.isTrue(exists, "[" + local + "] does not exist");
089                            }
090                            return new Deployable(this);
091                    }
092    
093            }
094    
095            private Deployable(Builder builder) {
096                    this.local = builder.local;
097                    this.remote = builder.remote;
098                    this.exists = builder.exists;
099                    this.filter = builder.filter;
100                    this.required = builder.required;
101                    this.permissions = builder.permissions;
102            }
103    
104    }