提交 7607e559 编写于 作者: V valeriep

7001094: Can't initialize SunPKCS11 more times than PKCS11 driver maxSessionCount

Summary: Changed SessionManager to keep track of session count for each instance
Reviewed-by: mullan
上级 f6a5d308
/* /*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -76,6 +76,9 @@ final class SessionManager { ...@@ -76,6 +76,9 @@ final class SessionManager {
// maximum number of sessions to open with this token // maximum number of sessions to open with this token
private final int maxSessions; private final int maxSessions;
// total number of active sessions
private int activeSessions;
// pool of available object sessions // pool of available object sessions
private final Pool objSessions; private final Pool objSessions;
...@@ -115,11 +118,6 @@ final class SessionManager { ...@@ -115,11 +118,6 @@ final class SessionManager {
return (maxSessions <= DEFAULT_MAX_SESSIONS); return (maxSessions <= DEFAULT_MAX_SESSIONS);
} }
// returns the total number of active sessions
int totalSessionCount() {
return SessionRef.totalCount();
}
synchronized Session getObjSession() throws PKCS11Exception { synchronized Session getObjSession() throws PKCS11Exception {
Session session = objSessions.poll(); Session session = objSessions.poll();
if (session != null) { if (session != null) {
...@@ -141,7 +139,7 @@ final class SessionManager { ...@@ -141,7 +139,7 @@ final class SessionManager {
// create a new session rather than re-using an obj session // create a new session rather than re-using an obj session
// that avoids potential expensive cancels() for Signatures & RSACipher // that avoids potential expensive cancels() for Signatures & RSACipher
if (maxSessions == Integer.MAX_VALUE || if (maxSessions == Integer.MAX_VALUE ||
totalSessionCount() < maxSessions) { activeSessions < maxSessions) {
session = openSession(); session = openSession();
return ensureValid(session); return ensureValid(session);
} }
...@@ -164,7 +162,7 @@ final class SessionManager { ...@@ -164,7 +162,7 @@ final class SessionManager {
if (debug != null) { if (debug != null) {
String location = new Exception().getStackTrace()[2].toString(); String location = new Exception().getStackTrace()[2].toString();
System.out.println("Killing session (" + location + ") active: " System.out.println("Killing session (" + location + ") active: "
+ totalSessionCount()); + activeSessions);
} }
closeSession(session); closeSession(session);
return null; return null;
...@@ -189,7 +187,7 @@ final class SessionManager { ...@@ -189,7 +187,7 @@ final class SessionManager {
} }
if (debug != null) { if (debug != null) {
System.out.println("Demoting session, active: " + System.out.println("Demoting session, active: " +
totalSessionCount()); activeSessions);
} }
boolean present = objSessions.remove(session); boolean present = objSessions.remove(session);
if (present == false) { if (present == false) {
...@@ -202,16 +200,16 @@ final class SessionManager { ...@@ -202,16 +200,16 @@ final class SessionManager {
private Session openSession() throws PKCS11Exception { private Session openSession() throws PKCS11Exception {
if ((maxSessions != Integer.MAX_VALUE) && if ((maxSessions != Integer.MAX_VALUE) &&
(totalSessionCount() >= maxSessions)) { (activeSessions >= maxSessions)) {
throw new ProviderException("No more sessions available"); throw new ProviderException("No more sessions available");
} }
long id = token.p11.C_OpenSession long id = token.p11.C_OpenSession
(token.provider.slotID, openSessionFlags, null, null); (token.provider.slotID, openSessionFlags, null, null);
Session session = new Session(token, id); Session session = new Session(token, id);
activeSessions++;
if (debug != null) { if (debug != null) {
int currTotal = totalSessionCount(); if (activeSessions > maxActiveSessions) {
if (currTotal > maxActiveSessions) { maxActiveSessions = activeSessions;
maxActiveSessions = currTotal;
if (maxActiveSessions % 10 == 0) { if (maxActiveSessions % 10 == 0) {
System.out.println("Open sessions: " + maxActiveSessions); System.out.println("Open sessions: " + maxActiveSessions);
} }
...@@ -222,6 +220,7 @@ final class SessionManager { ...@@ -222,6 +220,7 @@ final class SessionManager {
private void closeSession(Session session) { private void closeSession(Session session) {
session.close(); session.close();
activeSessions--;
} }
private static final class Pool { private static final class Pool {
...@@ -275,7 +274,7 @@ final class SessionManager { ...@@ -275,7 +274,7 @@ final class SessionManager {
} }
if (debug != null) { if (debug != null) {
System.out.println("Closing " + i + " idle sessions, active: " System.out.println("Closing " + i + " idle sessions, active: "
+ mgr.totalSessionCount()); + mgr.activeSessions);
} }
List<Session> subList = pool.subList(0, i); List<Session> subList = pool.subList(0, i);
subList.clear(); subList.clear();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册