001package org.kuali.common.util.execute.impl; 002 003import static com.google.common.base.Preconditions.checkArgument; 004import static com.google.common.base.Preconditions.checkNotNull; 005 006import java.util.List; 007 008import org.apache.commons.lang3.StringUtils; 009import org.kuali.common.util.execute.Executable; 010import org.kuali.common.util.log.LoggerContext; 011import org.kuali.common.util.log.LoggerExecutable; 012import org.kuali.common.util.log.LoggerUtils; 013import org.slf4j.Logger; 014 015import com.google.common.base.Optional; 016import com.google.common.collect.ImmutableList; 017 018public class SetSystemPropertyExecutable implements Executable { 019 020 private static final Logger logger = LoggerUtils.make(); 021 022 private final boolean skip; 023 private final Optional<LoggerContext> context; 024 private final String key; 025 private final String value; 026 027 @Override 028 public void execute() { 029 if (skip) { 030 return; 031 } 032 if (context.isPresent()) { 033 LoggerExecutable.create(context.get()).execute(); 034 } 035 System.setProperty(key, value); 036 } 037 038 private SetSystemPropertyExecutable(Builder builder) { 039 this.key = builder.key; 040 this.value = builder.value; 041 this.skip = builder.skip; 042 this.context = builder.context; 043 } 044 045 public static Builder builder(String key, String value) { 046 return new Builder(key, value); 047 } 048 049 public static class Builder { 050 051 // Required 052 private final String key; 053 private final String value; 054 055 // Optional 056 private Optional<LoggerContext> context = Optional.absent(); 057 private boolean skip = false; 058 059 public Builder(String key, String value) { 060 this.key = key; 061 this.value = value; 062 } 063 064 public Builder log(String msg) { 065 return log(msg, ImmutableList.of()); 066 } 067 068 public Builder log(String msg, List<Object> args) { 069 return context(LoggerContext.builder(logger, msg).args(args).build()); 070 } 071 072 public Builder context(LoggerContext context) { 073 this.context = Optional.of(context); 074 return this; 075 } 076 077 public Builder skip(boolean skip) { 078 this.skip = skip; 079 return this; 080 } 081 082 public SetSystemPropertyExecutable build() { 083 SetSystemPropertyExecutable instance = new SetSystemPropertyExecutable(this); 084 validate(instance); 085 return instance; 086 } 087 088 private static void validate(SetSystemPropertyExecutable instance) { 089 checkArgument(!StringUtils.isBlank(instance.key), "key cannot be blank"); 090 checkArgument(!StringUtils.isBlank(instance.value), "value cannot be blank"); 091 checkNotNull(instance.context, "context cannot be null"); 092 } 093 094 public Optional<LoggerContext> getContext() { 095 return context; 096 } 097 098 public void setContext(Optional<LoggerContext> context) { 099 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}