Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/pkcs11/Session.java
38919 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.pkcs11;2627import java.lang.ref.*;28import java.util.*;29import java.util.concurrent.atomic.AtomicInteger;3031import java.security.*;3233import sun.security.pkcs11.wrapper.*;3435/**36* A session object. Sessions are obtained via the SessionManager,37* see there for details. Most code will only ever need one method in38* this class, the id() method to obtain the session id.39*40* @author Andreas Sterbenz41* @since 1.542*/43final class Session implements Comparable<Session> {4445// time after which to close idle sessions, in milliseconds (3 minutes)46private final static long MAX_IDLE_TIME = 3 * 60 * 1000;4748// token instance49final Token token;5051// session id52private final long id;5354// number of objects created within this session55private final AtomicInteger createdObjects;5657// time this session was last used58// not synchronized/volatile for performance, so may be unreliable59// this could lead to idle sessions being closed early, but that is harmless60private long lastAccess;6162private final SessionRef sessionRef;6364Session(Token token, long id) {65this.token = token;66this.id = id;67createdObjects = new AtomicInteger();68id();69sessionRef = new SessionRef(this, id, token);70}7172public int compareTo(Session other) {73if (this.lastAccess == other.lastAccess) {74return 0;75} else {76return (this.lastAccess < other.lastAccess) ? -1 : 1;77}78}7980boolean isLive(long currentTime) {81return currentTime - lastAccess < MAX_IDLE_TIME;82}8384long idInternal() {85return id;86}8788long id() {89if (token.isPresent(this.id) == false) {90throw new ProviderException("Token has been removed");91}92lastAccess = System.currentTimeMillis();93return id;94}9596void addObject() {97int n = createdObjects.incrementAndGet();98// XXX update statistics in session manager if n == 199}100101void removeObject() {102int n = createdObjects.decrementAndGet();103if (n == 0) {104token.sessionManager.demoteObjSession(this);105} else if (n < 0) {106throw new ProviderException("Internal error: objects created " + n);107}108}109110boolean hasObjects() {111return createdObjects.get() != 0;112}113114void close() {115if (hasObjects()) {116throw new ProviderException(117"Internal error: close session with active objects");118}119sessionRef.dispose();120}121}122123/*124* NOTE: Use PhantomReference here and not WeakReference125* otherwise the sessions maybe closed before other objects126* which are still being finalized.127*/128final class SessionRef extends PhantomReference<Session>129implements Comparable<SessionRef> {130131private static ReferenceQueue<Session> refQueue =132new ReferenceQueue<Session>();133134private static Set<SessionRef> refList =135Collections.synchronizedSortedSet(new TreeSet<SessionRef>());136137static ReferenceQueue<Session> referenceQueue() {138return refQueue;139}140141static int totalCount() {142return refList.size();143}144145private static void drainRefQueueBounded() {146while (true) {147SessionRef next = (SessionRef) refQueue.poll();148if (next == null) break;149next.dispose();150}151}152153// handle to the native session154private long id;155private Token token;156157SessionRef(Session session, long id, Token token) {158super(session, refQueue);159this.id = id;160this.token = token;161refList.add(this);162// TBD: run at some interval and not every time?163drainRefQueueBounded();164}165166void dispose() {167refList.remove(this);168try {169if (token.isPresent(id)) {170token.p11.C_CloseSession(id);171}172} catch (PKCS11Exception e1) {173// ignore174} catch (ProviderException e2) {175// ignore176} finally {177this.clear();178}179}180181public int compareTo(SessionRef other) {182if (this.id == other.id) {183return 0;184} else {185return (this.id < other.id) ? -1 : 1;186}187}188}189190191