001package org.kuali.common.devops.logic;
002
003import static com.google.common.base.Stopwatch.createStarted;
004import static java.util.concurrent.TimeUnit.MILLISECONDS;
005import static org.kuali.common.util.log.Loggers.newLogger;
006
007import java.util.List;
008import java.util.Set;
009import java.util.concurrent.Callable;
010
011import javax.validation.ConstraintViolation;
012
013import org.kuali.common.core.build.ViolationsBuilder;
014import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
015import org.kuali.common.devops.metadata.logic.EnvironmentMetadataService;
016import org.kuali.common.devops.model.Environment;
017import org.kuali.common.util.inform.PercentCompleteInformer;
018import org.slf4j.Logger;
019
020import com.google.common.base.Stopwatch;
021import com.google.common.collect.ImmutableList;
022
023@IdiotProofImmutable
024public final class BuilderFillerCallable implements Callable<Long> {
025
026        private final ImmutableList<Environment.Builder> builders;
027        private final EnvironmentMetadataService service;
028        private final PercentCompleteInformer informer;
029
030        private static final Logger logger = newLogger();
031
032        @Override
033        public Long call() {
034                Stopwatch sw = createStarted();
035                for (Environment.Builder builder : builders) {
036                        logger.debug(builder.getFqdn());
037                        Environments2.fillIn(builder, service);
038                        informer.incrementProgress();
039                }
040                return sw.elapsed(MILLISECONDS);
041        }
042
043        private BuilderFillerCallable(Builder builder) {
044                this.builders = ImmutableList.copyOf(builder.builders);
045                this.service = builder.service;
046                this.informer = builder.informer;
047        }
048
049        public static Builder builder() {
050                return new Builder();
051        }
052
053        public static class Builder extends ViolationsBuilder<BuilderFillerCallable> {
054
055                private List<Environment.Builder> builders;
056                private EnvironmentMetadataService service;
057                private PercentCompleteInformer informer;
058
059                public Builder informer(PercentCompleteInformer informer) {
060                        this.informer = informer;
061                        return this;
062                }
063
064                public Builder builders(List<Environment.Builder> builders) {
065                        this.builders = builders;
066                        return this;
067                }
068
069                public Builder service(EnvironmentMetadataService service) {
070                        this.service = service;
071                        return this;
072                }
073
074                @Override
075                public BuilderFillerCallable build() {
076                        return validate(make());
077                }
078
079                @Override
080                public Set<ConstraintViolation<BuilderFillerCallable>> violations() {
081                        return violations(make());
082                }
083
084                private BuilderFillerCallable make() {
085                        return new BuilderFillerCallable(this);
086                }
087
088                public List<Environment.Builder> getBuilders() {
089                        return builders;
090                }
091
092                public void setBuilders(List<Environment.Builder> builders) {
093                        this.builders = builders;
094                }
095
096                public EnvironmentMetadataService getService() {
097                        return service;
098                }
099
100                public void setService(EnvironmentMetadataService service) {
101                        this.service = service;
102                }
103
104                public PercentCompleteInformer getInformer() {
105                        return informer;
106                }
107
108                public void setInformer(PercentCompleteInformer informer) {
109                        this.informer = informer;
110                }
111
112        }
113
114        public List<Environment.Builder> getBuilders() {
115                return builders;
116        }
117
118        public EnvironmentMetadataService getService() {
119                return service;
120        }
121
122        public PercentCompleteInformer getInformer() {
123                return informer;
124        }
125
126}