1/*
2 * Copyright (c) 2013, 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/*
27 *
28 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
29 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
30 */
31
32package sun.security.krb5.internal.rcache;
33
34import java.util.*;
35import sun.security.krb5.internal.KerberosTime;
36import sun.security.krb5.internal.KrbApErrException;
37import sun.security.krb5.internal.ReplayCache;
38
39/**
40 * This class stores replay caches. AuthTimeWithHash objects are categorized
41 * into AuthLists keyed by the names of client and server.
42 *
43 * @author Yanni Zhang
44 */
45public class MemoryCache extends ReplayCache {
46
47    // TODO: One day we'll need to read dynamic krb5.conf.
48    private static final int lifespan = KerberosTime.getDefaultSkew();
49    private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
50
51    private final Map<String,AuthList> content = new HashMap<>();
52
53    @Override
54    public synchronized void checkAndStore(KerberosTime currTime, AuthTimeWithHash time)
55            throws KrbApErrException {
56        String key = time.client + "|" + time.server;
57        AuthList rc = content.get(key);
58        if (DEBUG) {
59            System.out.println("MemoryCache: add " + time + " to " + key);
60        }
61        if (rc == null) {
62            rc = new AuthList(lifespan);
63            rc.put(time, currTime);
64            if (!rc.isEmpty()) {
65                content.put(key, rc);
66            }
67        } else {
68            if (DEBUG) {
69                System.out.println("MemoryCache: Existing AuthList:\n" + rc);
70            }
71            rc.put(time, currTime);
72            if (rc.isEmpty()) {
73                content.remove(key);
74            }
75        }
76    }
77
78    public String toString() {
79        StringBuilder sb = new StringBuilder();
80        for (AuthList rc: content.values()) {
81            sb.append(rc.toString());
82        }
83        return sb.toString();
84    }
85}
86