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