View Javadoc

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