1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package pkg.b.l;
27 
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.Map;
31 import java.util.Queue;
32 import java.util.ResourceBundle;
33 
34 public class LoggerB implements System.Logger {
35 
36     // ---- test utility fields and methods ----
37 
38     private static Map<String, LoggerB> map = new HashMap<>();
39 
getLogger(String name)40     public static LoggerB getLogger(String name) {
41         return map.computeIfAbsent(name, (n) -> new LoggerB());
42     }
43 
checkLog(String name, Level level, ResourceBundle bundle, String format, Throwable throwable, Object... params)44     public static boolean checkLog(String name, Level level, ResourceBundle bundle,
45                                    String format, Throwable throwable, Object... params) {
46         LoggerB logger = map.get(name);
47         LogEvent event = new LogEvent(level, bundle, format, null, params);
48         for (LogEvent l : logger.queue) {
49             if (l.equals(event)) {
50                 return true;
51             }
52         }
53         return false;
54     }
55 
56     // ---- logger implementation ----
57 
58     private Queue<LogEvent> queue = new LinkedList<>();
59 
60     @Override
getName()61     public String getName() {
62         return this.getClass().getName();
63     }
64 
65     @Override
isLoggable(Level level)66     public boolean isLoggable(Level level) {
67         return true;
68     }
69 
70     @Override
log(Level level, ResourceBundle bundle, String format, Object... params)71     public void log(Level level, ResourceBundle bundle, String format, Object... params) {
72         String msg = bundle != null ? bundle.getString(format) : format;
73         log(new LogEvent(level, bundle, msg, null, params));
74     }
75 
76     @Override
log(Level level, ResourceBundle bundle, String format, Throwable throwable)77     public void log(Level level, ResourceBundle bundle, String format, Throwable throwable) {
78         String msg = bundle != null ? bundle.getString(format) : format;
79         log(new LogEvent(level, bundle, msg, throwable, (Object)null));
80     }
81 
log(LogEvent l)82     void log(LogEvent l) {
83         print(l);
84         queue.add(l);
85     }
86 
print(LogEvent l)87     private void print(LogEvent l) {
88         System.err.println("LoggerB Message"+ l);
89     }
90 
getLogEvent()91     public Queue<LogEvent> getLogEvent() {
92         return queue;
93     }
94 
95     public static class LogEvent {
LogEvent(Level level, ResourceBundle bundle, String format, Throwable throwable, Object... params)96         public LogEvent(Level level, ResourceBundle bundle, String format,
97                         Throwable throwable, Object... params) {
98             this.level = level;
99             this.bundle = bundle;
100             this.format = format;
101             this.throwable = throwable;
102             this.params = params;
103         }
104 
105         @Override
equals(Object o)106         public boolean equals(Object o) {
107             if (o instanceof LogEvent) {
108                 LogEvent e = (LogEvent)o;
109                 return level == e.level
110                     && bundle == e.bundle
111                     && format == e.format
112                     && params == e.params;
113             }
114             return false;
115         }
116 
117         private Level level;
118         private ResourceBundle bundle;
119         private String format;
120         private Throwable throwable;
121         private Object[] params;
122     }
123 }
124