View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.util;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.faces.context.FacesContext;
26  import java.lang.invoke.MethodHandles;
27  import java.util.ArrayList;
28  import java.util.HashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * This class is for debugging the access keys.
34   */
35  public final class AccessKeyLogger extends HashMap<Character, List<String>> {
36  
37    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
38  
39    private static final char[] KEYS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
40        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
41        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
42  
43    private AccessKeyLogger() {
44    }
45  
46    private static AccessKeyLogger getInstance(final FacesContext facesContext) {
47      final Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
48      AccessKeyLogger keyMap = (AccessKeyLogger) requestMap.get(AccessKeyLogger.class.getName());
49      if (keyMap == null) {
50        keyMap = new AccessKeyLogger();
51        requestMap.put(AccessKeyLogger.class.getName(), keyMap);
52      }
53      return keyMap;
54    }
55  
56    public static void addAccessKey(final FacesContext facesContext, final Character key, final String clientId) {
57      if (LOG.isDebugEnabled()) {
58        final AccessKeyLogger instance = getInstance(facesContext);
59        final List<String> clientIds;
60        if (instance.containsKey(key)) {
61          clientIds = instance.get(key);
62        } else {
63          clientIds = new ArrayList<>();
64          instance.put(key, clientIds);
65        }
66        clientIds.add(clientId);
67        // should only be called, when debug is enabled
68        LOG.debug("Using accessKey='{}' for clientId='{}'", key, clientId);
69      }
70    }
71  
72    public static void logStatus(final FacesContext facesContext) {
73      if (LOG.isDebugEnabled()) {
74        final StringBuilder builder = new StringBuilder();
75        final AccessKeyLogger instance = AccessKeyLogger.getInstance(facesContext);
76        builder.append("Used access keys:");
77        for (final Map.Entry<Character, List<String>> entry : instance.entrySet()) {
78          builder.append("\n'");
79          builder.append(entry.getKey());
80          builder.append("' -> ");
81          builder.append(entry.getValue());
82        }
83        builder.append("\nFree access keys: ");
84        for (final char key : KEYS) {
85          builder.append(instance.containsKey(Character.valueOf(key)) ? '.' : key);
86        }
87        LOG.debug(builder.toString());
88      }
89    }
90  }