View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.log;
17  
18  import static org.kuali.common.util.base.Precondition.checkNotNull;
19  
20  import org.kuali.common.util.execute.Executable;
21  
22  public class LoggerExecutable implements Executable {
23  
24  	private static final Object[] EMPTY_OBJECT_ARRAY = {};
25  
26  	private final LoggerContext context;
27  	private final boolean skip;
28  
29  	@Override
30  	public void execute() {
31  		if (!skip) {
32  			LoggerUtils.logMsg(context.getMsg(), context.getArgs().toArray(EMPTY_OBJECT_ARRAY), context.getLogger(), context.getLevel());
33  		}
34  	}
35  
36  	private LoggerExecutable(Builder builder) {
37  		this.context = builder.context;
38  		this.skip = builder.skip;
39  	}
40  
41  	public static LoggerExecutable create(LoggerContext context) {
42  		return builder(context).build();
43  	}
44  
45  	public static Builder builder(LoggerContext context) {
46  		return new Builder(context);
47  	}
48  
49  	public static class Builder implements org.apache.commons.lang3.builder.Builder<LoggerExecutable> {
50  
51  		// Required
52  		private final LoggerContext context;
53  
54  		// Optional
55  		private boolean skip = false;
56  
57  		public Builder(LoggerContext context) {
58  			this.context = context;
59  		}
60  
61  		public Builder skip(boolean skip) {
62  			this.skip = skip;
63  			return this;
64  		}
65  
66  		@Override
67  		public LoggerExecutable build() {
68  			LoggerExecutable instance = new LoggerExecutable(this);
69  			validate(instance);
70  			return instance;
71  		}
72  
73  		private static void validate(LoggerExecutable instance) {
74  			checkNotNull(instance.context, "context");
75  		}
76  	}
77  
78  	public LoggerContext getContext() {
79  		return context;
80  	}
81  
82  	public boolean isSkip() {
83  		return skip;
84  	}
85  
86  }