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.log4j.model;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAttribute;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
27  
28  import org.kuali.common.util.Assert;
29  import org.kuali.common.util.CollectionUtils;
30  import org.kuali.common.util.ListUtils;
31  import org.kuali.common.util.log.log4j.jaxb.DebugAdapter;
32  import org.kuali.common.util.log.log4j.jaxb.RepositoryThresholdAdapter;
33  import org.kuali.common.util.xml.jaxb.adapter.OmitFalseAdapter;
34  
35  @XmlRootElement(name = "log4j:configuration")
36  @XmlAccessorType(XmlAccessType.FIELD)
37  public final class Log4JConfiguration {
38  
39  	@XmlAttribute(name = "xmlns:log4j")
40  	private final String namespace;
41  
42  	@XmlElement(name = "appender")
43  	private final List<Appender> appenders;
44  
45  	@XmlElement
46  	private final Logger root;
47  
48  	@XmlElement(name = "logger")
49  	private final List<Logger> loggers;
50  
51  	@XmlAttribute
52  	@XmlJavaTypeAdapter(OmitFalseAdapter.class)
53  	private final Boolean reset;
54  
55  	@XmlAttribute
56  	@XmlJavaTypeAdapter(DebugAdapter.class)
57  	private final Debug debug;
58  
59  	@XmlAttribute
60  	@XmlJavaTypeAdapter(RepositoryThresholdAdapter.class)
61  	private final Threshold threshold;
62  
63  	// Only expose an unmodifiable version of our internal list
64  	public List<Logger> getLoggers() {
65  		return Collections.unmodifiableList(loggers);
66  	}
67  
68  	// Only expose an unmodifiable version of our internal list
69  	public List<Appender> getAppenders() {
70  		return Collections.unmodifiableList(appenders);
71  	}
72  
73  	public boolean getReset() {
74  		return reset;
75  	}
76  
77  	public Debug getDebug() {
78  		return debug;
79  	}
80  
81  	public Threshold getThreshold() {
82  		return threshold;
83  	}
84  
85  	public Logger getRoot() {
86  		return root;
87  	}
88  
89  	public String getNamespace() {
90  		return namespace;
91  	}
92  
93  	public static class Builder {
94  
95  		// Required
96  		private final Logger root;
97  
98  		// Optional
99  		private String namespace = "http://jakarta.apache.org/log4j/";
100 		private List<Appender> appenders = Appender.EMPTY;
101 		private List<Logger> loggers = Logger.EMPTY;
102 		private boolean reset = false;
103 		private Debug debug = Debug.DEFAULT_VALUE;
104 		private Threshold threshold = Threshold.DEFAULT_REPOSITORY_VALUE;
105 
106 		public Builder(Logger root) {
107 			Assert.noNulls(root);
108 			this.root = root;
109 		}
110 
111 		public Builder appenders(List<Appender> appenders) {
112 			this.appenders = appenders;
113 			return this;
114 		}
115 
116 		public Builder appender(Appender appender) {
117 			this.appenders = CollectionUtils.singletonList(appender);
118 			return this;
119 		}
120 
121 		public Builder namespace(String namespace) {
122 			this.namespace = namespace;
123 			return this;
124 		}
125 
126 		public Builder logger(Logger logger) {
127 			this.loggers = CollectionUtils.singletonList(logger);
128 			return this;
129 		}
130 
131 		public Builder loggers(List<Logger> loggers) {
132 			this.loggers = loggers;
133 			return this;
134 		}
135 
136 		public Builder reset(boolean reset) {
137 			this.reset = reset;
138 			return this;
139 		}
140 
141 		public Builder debug(Debug debug) {
142 			this.debug = debug;
143 			return this;
144 		}
145 
146 		public Builder threshold(Threshold threshold) {
147 			this.threshold = threshold;
148 			return this;
149 		}
150 
151 		private Builder finish() {
152 
153 			// Ensure we are being configured correctly
154 			Assert.noNulls(root, appenders, loggers, debug, threshold);
155 			Assert.isFalse(Logger.isThresholdNull(root), "root logging threshold is null");
156 			Assert.noBlanks(namespace);
157 
158 			// Defensive copies of the 2 lists we were passed
159 			this.appenders = ListUtils.newArrayList(appenders);
160 			this.loggers = ListUtils.newArrayList(loggers);
161 
162 			// Return the fully configured Builder
163 			return this;
164 		}
165 
166 		public Log4JConfiguration build() {
167 			finish(); // Finish setting things up
168 			return new Log4JConfiguration(this);
169 		}
170 	}
171 
172 	// This is a concession to JAXB so it can unmarshal the object from XML
173 	private Log4JConfiguration() {
174 		this(new Builder(Logger.DEFAULT).finish());
175 	}
176 
177 	private Log4JConfiguration(Builder builder) {
178 		this.root = builder.root;
179 		this.appenders = builder.appenders;
180 		this.loggers = builder.loggers;
181 		this.reset = builder.reset;
182 		this.debug = builder.debug;
183 		this.threshold = builder.threshold;
184 		this.namespace = builder.namespace;
185 	}
186 
187 }