1 package org.codehaus.mojo.jaxb2.shared.environment.logging;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.logging.Log;
23 import org.codehaus.mojo.jaxb2.AbstractJaxbMojo;
24 import org.codehaus.mojo.jaxb2.shared.Validate;
25 import org.codehaus.mojo.jaxb2.shared.environment.AbstractLogAwareFacet;
26
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.logging.Handler;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33
34
35
36
37
38
39 public class LoggingHandlerEnvironmentFacet extends AbstractLogAwareFacet {
40
41
42
43
44 public static final String[] DEFAULT_LOGGER_NAMES = new String[]{"com.sun", "javax.xml", "javax.tools"};
45
46
47 private boolean restored;
48 private Logger rootLogger;
49 private Level originalRootLoggerLevel;
50 private List<Handler> originalHandlers;
51 private MavenLogHandler mavenLogHandler;
52
53 private String logPrefix;
54 private String encoding;
55 private String[] loggerNamePrefixes;
56
57
58
59
60
61
62
63
64
65
66
67 public LoggingHandlerEnvironmentFacet(final String logPrefix,
68 final Log mavenLog,
69 final String encoding,
70 final String[] loggerNamePrefixes) {
71
72 super(mavenLog);
73
74
75 Validate.notEmpty(encoding, "encoding");
76 Validate.notNull(loggerNamePrefixes, "loggerNamePrefixes");
77
78
79 this.originalHandlers = new ArrayList<Handler>();
80 this.logPrefix = logPrefix;
81 rootLogger = Logger.getLogger("");
82 originalRootLoggerLevel = rootLogger.getLevel();
83 this.encoding = encoding;
84 this.loggerNamePrefixes = loggerNamePrefixes;
85 }
86
87
88
89
90
91 @Override
92 public void setup() {
93
94
95 rootLogger.setLevel(MavenLogHandler.getJavaUtilLoggingLevelFor(log));
96 this.mavenLogHandler = new MavenLogHandler(log, logPrefix, encoding, loggerNamePrefixes);
97
98 for (Handler current : rootLogger.getHandlers()) {
99
100
101 originalHandlers.add(current);
102
103
104 rootLogger.removeHandler(current);
105 }
106
107
108 rootLogger.addHandler(this.mavenLogHandler);
109 }
110
111
112
113
114 public void restore() {
115
116 if (!restored) {
117
118
119 rootLogger.removeHandler(mavenLogHandler);
120
121
122 rootLogger.setLevel(originalRootLoggerLevel);
123 for (Handler current : originalHandlers) {
124 rootLogger.addHandler(current);
125 }
126
127
128 restored = true;
129 }
130 }
131
132
133
134
135
136
137
138
139
140 public static LoggingHandlerEnvironmentFacet create(final Log mavenLog,
141 final Class<? extends AbstractJaxbMojo> caller,
142 final String encoding) {
143
144
145 Validate.notNull(mavenLog, "mavenLog");
146 Validate.notNull(caller, "caller");
147 Validate.notEmpty(encoding, "encoding");
148
149
150 final String logPrefix = caller.getClass().getCanonicalName().toUpperCase().contains("XJC")
151 ? "XJC"
152 : "SchemaGen";
153
154
155 return new LoggingHandlerEnvironmentFacet(logPrefix, mavenLog, encoding, DEFAULT_LOGGER_NAMES);
156 }
157 }