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.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * This class is for debugging the access keys.
33   */
34  public final class AccessKeyLogger extends HashMap<Character, List<String>> {
35  
36    private static final Logger LOG = LoggerFactory.getLogger(AccessKeyLogger.class);
37  
38    private static final char[] KEYS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
39        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
40        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
41  
42    private AccessKeyLogger() {
43    }
44  
45    private static AccessKeyLogger getInstance(final FacesContext facesContext) {
46      final Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
47      AccessKeyLogger keyMap = (AccessKeyLogger) requestMap.get(AccessKeyLogger.class.getName());
48      if (keyMap == null) {
49        keyMap = new AccessKeyLogger();
50        requestMap.put(AccessKeyLogger.class.getName(), keyMap);
51      }
52      return keyMap;
53    }
54  
55    public static void addAccessKey(final FacesContext facesContext, final Character key, String clientId) {
56      if (LOG.isDebugEnabled()) {
57        final AccessKeyLogger instance = getInstance(facesContext);
58        List<String> clientIds;
59        if (instance.containsKey(key)) {
60          clientIds = instance.get(key);
61        } else {
62          clientIds = new ArrayList<String>();
63          instance.put(key, clientIds);
64        }
65        clientIds.add(clientId);
66        // should only be called, when debug is enabled
67        LOG.debug("Using accessKey='{}' for clientId='{}'", key, clientId);
68      }
69    }
70  
71    public static void logStatus(final FacesContext facesContext) {
72      if (LOG.isDebugEnabled()) {
73        final StringBuilder builder = new StringBuilder();
74        final AccessKeyLogger instance = AccessKeyLogger.getInstance(facesContext);
75        builder.append("Used access keys:");
76        for (Map.Entry<Character, List<String>> entry : instance.entrySet()) {
77          builder.append("\n'");
78          builder.append(entry.getKey());
79          builder.append("' -> ");
80          builder.append(entry.getValue());
81        }
82        builder.append("\nFree access keys: ");
83        for (final char key : KEYS) {
84          builder.append(instance.containsKey(Character.valueOf(key)) ? '.' : key);
85        }
86        LOG.debug(builder.toString());
87      }
88    }
89  }