1 package org.kuali.common.jute.project.maven;
2
3 import static com.google.common.base.Optional.fromNullable;
4 import static com.google.common.base.Preconditions.checkState;
5 import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
6
7 import java.util.Properties;
8
9 import javax.inject.Inject;
10 import javax.inject.Provider;
11
12 import org.kuali.common.jute.collect.ImmutableProperties;
13 import org.kuali.common.jute.env.Environment;
14 import org.kuali.common.jute.project.maven.annotation.EnvPrefix;
15 import org.kuali.common.jute.project.maven.annotation.ProjectProperties;
16
17 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
18 import com.google.common.base.Optional;
19
20
21
22
23
24
25 @JsonDeserialize(builder = MavenEnvironment.Builder.class)
26 public final class MavenEnvironment implements Environment {
27
28 private final Environment env;
29 private final ImmutableProperties properties;
30 private final String prefix;
31
32 @Override
33 public Optional<String> getProperty(String key) {
34 Optional<String> standard = env.getProperty(key);
35 if (standard.isPresent()) {
36 return standard;
37 }
38 Optional<String> prefixed = env.getProperty(prefix + "." + key);
39 if (prefixed.isPresent()) {
40 return prefixed;
41 }
42 return fromNullable(properties.getProperty(key));
43 }
44
45 @Override
46 public boolean containsProperty(String key) {
47 return getProperty(key).isPresent();
48 }
49
50 @Override
51 public String getProperty(String key, String defaultValue) {
52 Optional<String> value = getProperty(key);
53 if (value.isPresent()) {
54 return value.get();
55 } else {
56 return defaultValue;
57 }
58 }
59
60 @Override
61 public String getRequiredProperty(String key) {
62 Optional<String> value = getProperty(key);
63 checkState(value.isPresent(), "'%s' is required", key);
64 return value.get();
65 }
66
67 public Environment getEnv() {
68 return env;
69 }
70
71 public Properties getProperties() {
72 return properties;
73 }
74
75 public String getPrefix() {
76 return prefix;
77 }
78
79 private MavenEnvironment(Builder builder) {
80 this.env = builder.env;
81 this.properties = ImmutableProperties.copyOf(builder.properties);
82 this.prefix = builder.prefix;
83 }
84
85 public static class Builder implements org.apache.commons.lang3.builder.Builder<MavenEnvironment>, Provider<MavenEnvironment> {
86
87 private Environment env;
88 private Properties properties;
89 private String prefix;
90
91 @Inject
92 public Builder withEnv(Environment env) {
93 this.env = env;
94 return this;
95 }
96
97 @Inject
98 public Builder withProperties(@ProjectProperties Properties properties) {
99 this.properties = properties;
100 return this;
101 }
102
103 @Inject
104 public Builder withPrefix(@EnvPrefix String prefix) {
105 this.prefix = prefix;
106 return this;
107 }
108
109 @Override
110 public MavenEnvironment get() {
111 return build();
112 }
113
114 @Override
115 public MavenEnvironment build() {
116 return checkNoNulls(new MavenEnvironment(this));
117 }
118 }
119
120 }