1 package org.kuali.common.util.execute.impl;
2
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static com.google.common.base.Preconditions.checkNotNull;
5
6 import java.util.List;
7
8 import org.apache.commons.lang3.StringUtils;
9 import org.kuali.common.util.execute.Executable;
10 import org.kuali.common.util.log.LoggerContext;
11 import org.kuali.common.util.log.LoggerExecutable;
12 import org.kuali.common.util.log.LoggerUtils;
13 import org.slf4j.Logger;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.ImmutableList;
17
18 public class SetSystemPropertyExecutable implements Executable {
19
20 private static final Logger logger = LoggerUtils.make();
21
22 private final boolean skip;
23 private final Optional<LoggerContext> context;
24 private final String key;
25 private final String value;
26
27 @Override
28 public void execute() {
29 if (skip) {
30 return;
31 }
32 if (context.isPresent()) {
33 LoggerExecutable.create(context.get()).execute();
34 }
35 System.setProperty(key, value);
36 }
37
38 private SetSystemPropertyExecutable(Builder builder) {
39 this.key = builder.key;
40 this.value = builder.value;
41 this.skip = builder.skip;
42 this.context = builder.context;
43 }
44
45 public static Builder builder(String key, String value) {
46 return new Builder(key, value);
47 }
48
49 public static class Builder {
50
51
52 private final String key;
53 private final String value;
54
55
56 private Optional<LoggerContext> context = Optional.absent();
57 private boolean skip = false;
58
59 public Builder(String key, String value) {
60 this.key = key;
61 this.value = value;
62 }
63
64 public Builder log(String msg) {
65 return log(msg, ImmutableList.of());
66 }
67
68 public Builder log(String msg, List<Object> args) {
69 return context(LoggerContext.builder(logger, msg).args(args).build());
70 }
71
72 public Builder context(LoggerContext context) {
73 this.context = Optional.of(context);
74 return this;
75 }
76
77 public Builder skip(boolean skip) {
78 this.skip = skip;
79 return this;
80 }
81
82 public SetSystemPropertyExecutable build() {
83 SetSystemPropertyExecutable instance = new SetSystemPropertyExecutable(this);
84 validate(instance);
85 return instance;
86 }
87
88 private static void validate(SetSystemPropertyExecutable instance) {
89 checkArgument(!StringUtils.isBlank(instance.key), "key cannot be blank");
90 checkArgument(!StringUtils.isBlank(instance.value), "value cannot be blank");
91 checkNotNull(instance.context, "context cannot be null");
92 }
93
94 public Optional<LoggerContext> getContext() {
95 return context;
96 }
97
98 public void setContext(Optional<LoggerContext> context) {
99 this.context = context;
100 }
101
102 public boolean isSkip() {
103 return skip;
104 }
105
106 public void setSkip(boolean skip) {
107 this.skip = skip;
108 }
109
110 public String getKey() {
111 return key;
112 }
113
114 public String getValue() {
115 return value;
116 }
117 }
118
119 public String getKey() {
120 return key;
121 }
122
123 public String getValue() {
124 return value;
125 }
126
127 public Optional<LoggerContext> getContext() {
128 return context;
129 }
130
131 public boolean isSkip() {
132 return skip;
133 }
134
135 }