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.log; 017 018import static org.kuali.common.util.base.Precondition.checkNotNull; 019 020import org.kuali.common.util.execute.Executable; 021 022public class LoggerExecutable implements Executable { 023 024 private static final Object[] EMPTY_OBJECT_ARRAY = {}; 025 026 private final LoggerContext context; 027 private final boolean skip; 028 029 @Override 030 public void execute() { 031 if (!skip) { 032 LoggerUtils.logMsg(context.getMsg(), context.getArgs().toArray(EMPTY_OBJECT_ARRAY), context.getLogger(), context.getLevel()); 033 } 034 } 035 036 private LoggerExecutable(Builder builder) { 037 this.context = builder.context; 038 this.skip = builder.skip; 039 } 040 041 public static LoggerExecutable create(LoggerContext context) { 042 return builder(context).build(); 043 } 044 045 public static Builder builder(LoggerContext context) { 046 return new Builder(context); 047 } 048 049 public static class Builder implements org.apache.commons.lang3.builder.Builder<LoggerExecutable> { 050 051 // Required 052 private final LoggerContext context; 053 054 // Optional 055 private boolean skip = false; 056 057 public Builder(LoggerContext context) { 058 this.context = context; 059 } 060 061 public Builder skip(boolean skip) { 062 this.skip = skip; 063 return this; 064 } 065 066 @Override 067 public LoggerExecutable build() { 068 LoggerExecutable instance = new LoggerExecutable(this); 069 validate(instance); 070 return instance; 071 } 072 073 private static void validate(LoggerExecutable instance) { 074 checkNotNull(instance.context, "context"); 075 } 076 } 077 078 public LoggerContext getContext() { 079 return context; 080 } 081 082 public boolean isSkip() { 083 return skip; 084 } 085 086}