File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\logging\WeakLogHandler.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.logging;
25
26 import java.io.UnsupportedEncodingException;
27 import java.lang.ref.WeakReference;
28 import java.util.logging.ErrorManager;
29 import java.util.logging.Filter;
30 import java.util.logging.Formatter;
31 import java.util.logging.Handler;
32 import java.util.logging.Level;
33 import java.util.logging.LogRecord;
34 import java.util.logging.Logger;
35
36 /**
37 * Delegating {@link Handler} that uses {@link WeakReference},
38 * which de-registers itself when an object disappears via GC.
39 */
40 public final class WeakLogHandler extends Handler {
41 private final WeakReference<Handler> target;
42 private final Logger logger;
43
44 public WeakLogHandler(Handler target, Logger logger) {
45 this.logger = logger;
46 logger.addHandler(this);
47 this.target = new WeakReference<Handler>(target);
48 }
49
50 public void publish(LogRecord record) {
51 Handler t = resolve();
52 if(t!=null)
53 t.publish(record);
54 }
55
56 public void flush() {
57 Handler t = resolve();
58 if(t!=null)
59 t.flush();
60 }
61
62 public void close() throws SecurityException {
63 Handler t = resolve();
64 if(t!=null)
65 t.close();
66 }
67
68 private Handler resolve() {
69 Handler r = target.get();
70 if(r==null)
71 logger.removeHandler(this);
72 return r;
73 }
74
75 @Override
76 public void setFormatter(Formatter newFormatter) throws SecurityException {
77 super.setFormatter(newFormatter);
78 Handler t = resolve();
79 if(t!=null)
80 t.setFormatter(newFormatter);
81 }
82
83 @Override
84 public void setEncoding(String encoding) throws SecurityException, UnsupportedEncodingException {
85 super.setEncoding(encoding);
86 Handler t = resolve();
87 if(t!=null)
88 t.setEncoding(encoding);
89 }
90
91 @Override
92 public void setFilter(Filter newFilter) throws SecurityException {
93 super.setFilter(newFilter);
94 Handler t = resolve();
95 if(t!=null)
96 t.setFilter(newFilter);
97 }
98
99 @Override
100 public void setErrorManager(ErrorManager em) {
101 super.setErrorManager(em);
102 Handler t = resolve();
103 if(t!=null)
104 t.setErrorManager(em);
105 }
106
107 @Override
108 public void setLevel(Level newLevel) throws SecurityException {
109 super.setLevel(newLevel);
110 Handler t = resolve();
111 if(t!=null)
112 t.setLevel(newLevel);
113 }
114
115 @Override
116 public boolean isLoggable(LogRecord record) {
117 Handler t = resolve();
118 if(t!=null)
119 return t.isLoggable(record);
120 else
121 return super.isLoggable(record);
122 }
123 }
124