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.a.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 LoggerA implements System.Logger {
  35 
  36     // ---- test utility fields and methods ----
  37 
  38     private static Map<String, LoggerA> map = new HashMap<>();
  39 
  40     public static LoggerA getLogger(String name) {
  41         return map.computeIfAbsent(name, (n) -> new LoggerA());
  42     }
  43 
  44     public static boolean checkLog(String name, Level level, ResourceBundle bundle,
  45                                    String format, Throwable throwable, Object... params) {
  46         LoggerA 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
  61     public String getName() {
  62         return this.getClass().getName();
  63     }
  64 
  65     @Override
  66     public boolean isLoggable(Level level) {
  67         return true;
  68     }
  69 
  70     @Override
  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
  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 
  82     void log(LogEvent l) {
  83         print(l);
  84         queue.add(l);
  85     }
  86 
  87     private void print(LogEvent l) {
  88         System.err.println("LoggerA Message"+ l);
  89     }
  90 
  91     public Queue<LogEvent> getLogEvent() {
  92         return queue;
  93     }
  94 
  95     public static class LogEvent {
  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
 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         @Override
 118         public String toString() {
 119             return String.format("[level: %s, bundle: %s, format: %s, throwable: %s, object: %s]",
 120                     level, bundle, format, throwable, params);
 121         }
 122 
 123         private Level level;
 124         private ResourceBundle bundle;
 125         private String format;
 126         private Throwable throwable;
 127         private Object[] params;
 128     }
 129 }