001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util.execute.impl; 017 018import static com.google.common.base.Preconditions.checkArgument; 019import static com.google.common.base.Preconditions.checkNotNull; 020 021import java.util.List; 022 023import org.apache.commons.lang3.StringUtils; 024import org.kuali.common.util.execute.Executable; 025import org.kuali.common.util.log.LoggerContext; 026import org.kuali.common.util.log.LoggerExecutable; 027import org.kuali.common.util.log.LoggerUtils; 028import org.slf4j.Logger; 029 030import com.google.common.base.Optional; 031import com.google.common.collect.ImmutableList; 032 033public class SetSystemPropertyExecutable implements Executable { 034 035 private static final Logger logger = LoggerUtils.make(); 036 037 private final boolean skip; 038 private final Optional<LoggerContext> context; 039 private final String key; 040 private final String value; 041 042 @Override 043 public void execute() { 044 if (skip) { 045 return; 046 } 047 if (context.isPresent()) { 048 LoggerExecutable.create(context.get()).execute(); 049 } 050 System.setProperty(key, value); 051 } 052 053 private SetSystemPropertyExecutable(Builder builder) { 054 this.key = builder.key; 055 this.value = builder.value; 056 this.skip = builder.skip; 057 this.context = builder.context; 058 } 059 060 public static Builder builder(String key, String value) { 061 return new Builder(key, value); 062 } 063 064 public static class Builder { 065 066 // Required 067 private final String key; 068 private final String value; 069 070 // Optional 071 private Optional<LoggerContext> context = Optional.absent(); 072 private boolean skip = false; 073 074 public Builder(String key, String value) { 075 this.key = key; 076 this.value = value; 077 } 078 079 public Builder log(String msg) { 080 return log(msg, ImmutableList.of()); 081 } 082 083 public Builder log(String msg, List<Object> args) { 084 return context(LoggerContext.builder(logger, msg).args(args).build()); 085 } 086 087 public Builder context(LoggerContext context) { 088 this.context = Optional.of(context); 089 return this; 090 } 091 092 public Builder skip(boolean skip) { 093 this.skip = skip; 094 return this; 095 } 096 097 public SetSystemPropertyExecutable build() { 098 SetSystemPropertyExecutable instance = new SetSystemPropertyExecutable(this); 099 validate(instance); 100 return instance; 101 } 102 103 private static void validate(SetSystemPropertyExecutable instance) { 104 checkArgument(!StringUtils.isBlank(instance.key), "key cannot be blank"); 105 checkArgument(!StringUtils.isBlank(instance.value), "value cannot be blank"); 106 checkNotNull(instance.context, "context cannot be null"); 107 } 108 109 public Optional<LoggerContext> getContext() { 110 return context; 111 } 112 113 public void setContext(Optional<LoggerContext> context) { 114 this.context = context; 115 } 116 117 public boolean isSkip() { 118 return skip; 119 } 120 121 public void setSkip(boolean skip) { 122 this.skip = skip; 123 } 124 125 public String getKey() { 126 return key; 127 } 128 129 public String getValue() { 130 return value; 131 } 132 } 133 134 public String getKey() { 135 return key; 136 } 137 138 public String getValue() { 139 return value; 140 } 141 142 public Optional<LoggerContext> getContext() { 143 return context; 144 } 145 146 public boolean isSkip() { 147 return skip; 148 } 149 150}