DefaultJMQSAML2Repository.java revision f97667c80d9dd8155ff61164f82b9ad6deae5d95
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2008 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: DefaultJMQSAML2Repository.java,v 1.5 2008/08/01 22:23:47 hengming Exp $
*
*/
/**
*/
public class DefaultJMQSAML2Repository extends GeneralTaskRunnable
implements JMQSAML2Repository {
/* Operations */
// Private data members
/* Config data */
private static boolean isDatabaseUp = true;
/**
* grace period before expired session records are removed from the
* repository
*/
private static final String CLEANUP_GRACE_PERIOD =
"com.sun.identity.session.repository.cleanupGracePeriod";
private static final String BRIEF_DB_ERROR_MSG =
"SAML2 failover service is not functional due to DB unavailability.";
private static final String DB_ERROR_MSG =
"SAML2 database is not available at this moment."
+ "Please check with the system administrator " +
"for appropriate actions";
private static final String LOG_MSG_DB_BACK_ONLINE =
"SESSION_DATABASE_BACK_ONLINE";
private static final String LOG_MSG_DB_UNAVAILABLE =
"SESSION_DATABASE_UNAVAILABLE";
private static boolean lastLoggedDBStatusIsUp = true;
/**
* Time period between two successive runs of repository cleanup thread
* which checks and removes expired records
*/
private static long cleanUpValue = 0;
public static final String CLEANUP_RUN_PERIOD =
"com.sun.identity.saml2.repository.cleanupRunPeriod";
/**
* Time period between two successive runs of DBHealthChecker thread which
* checks for Database availability.
*/
public static final String HEALTH_CHECK_RUN_PERIOD =
"com.sun.identity.saml2.repository.healthCheckRunPeriod";
/**
* This period is actual one that is used by the thread. The value is set to
* the smallest value of cleanUPPeriod and healthCheckPeriod.
*/
static {
try {
} catch (Exception e) {
+ ", using default");
}
try {
} catch (Exception e) {
+ ", using default");
}
try {
} catch (Exception e) {
+ ", using default");
}
}
// Message queues
/**
*
* Constructs new JMQSAML2Repository
* @exception Exception when cannot create a new SAML2 repository
*
*/
public DefaultJMQSAML2Repository() throws Exception {
if (thisSessionServerProtocol == null
|| thisSessionServer == null) {
"propertyMustBeSet", null);
}
}
/**
*
* Initialize new FAMRecord persister
*/
private void initPersistSession() {
try {
isDatabaseUp = true;
} catch (Exception e) {
isDatabaseUp = false;
if (debug.messageEnabled()) {
}
}
}
/**
* Retrives existing SAML2 object from persistent datastore
* @param samlKey primary key
* @return SAML2 object, if failed, return null.
*/
if (!isDatabaseUp) {
return null;
}
try {
return retObj;
} catch (IllegalStateException e) {
isDatabaseUp = false;
logDBStatus();
if (debug.messageEnabled()) {
}
return null;
} catch (Exception e) {
+ "SAML2 object", e);
return null;
}
}
/**
* Retrives a list of existing SAML2 object from persistent datastore with
* secodaryKey
*
* @param secKey Secondary Key
* @return SAML2 object, if failed, return null.
*/
if (!isDatabaseUp) {
return null;
}
try {
}
return list;
}
}
return null;
} catch (IllegalStateException e) {
isDatabaseUp = false;
logDBStatus();
if (debug.messageEnabled()) {
}
return null;
} catch (Exception e) {
+ "SAML2 object", e);
return null;
}
}
/**
* Deletes the SAML2 object by given primary key from the repository
* @param samlKey primary key
*/
if (!isDatabaseUp) {
return;
}
try {
} catch (IllegalStateException e) {
isDatabaseUp = false;
logDBStatus();
if (debug.messageEnabled()) {
}
} catch (Exception e) {
+ "SAML2 object", e);
}
}
/**
* Deletes expired SAML2 object from the repository
* @exception When Unable to delete the expired SAML2 object
*/
public void deleteExpired() {
if (!isDatabaseUp) {
return;
}
try {
} catch (IllegalStateException e) {
isDatabaseUp = false;
logDBStatus();
if (debug.messageEnabled()) {
}
} catch (Exception e) {
+ "deleting Expired saml2 object", e);
}
}
/**
* Saves SAML2 data into the SAML2 Repository
* @param samlKey primary key
* @param samlObj saml object such as Response, IDPSession
* @param expirationTime expiration time
* @param secKey Secondary Key
*/
if (!isDatabaseUp) {
return;
}
try {
} catch (IllegalStateException e) {
isDatabaseUp = false;
logDBStatus();
if (debug.messageEnabled()) {
}
} catch (Exception e) {
+ "to save SAML2 object", e);
}
}
/**
* This method is invoked to log a message in the following two cases:
*
* (1) the DB is detected down by either the user requests
* Log message: HA_DATABASE_UNAVAILABLE (2) the DB is detected
* available again by the background health checker thread => Log message:
* HA_DATABASE_BACK_ONLINE
*
* The flag "lastLoggedDBStatusIsUp" is used to avoid logging the same DB
* status again and again if the status actually doesn't change over time.
*
* Please also note that if the DB is already down in the very beginning
* when starting the AM instance, there will be no message being logged
* since at this time the session service is not fully initialized yet
* therefore no sso token can be generated and used for the logging purpose.
* Nevertheless, the appropriate logging will be done later when the
* background thread kicks in.
*
*/
private void logDBStatus() {
if (!isDatabaseUp && lastLoggedDBStatusIsUp) {
lastLoggedDBStatusIsUp = false;
}
if (isDatabaseUp && !lastLoggedDBStatusIsUp) {
lastLoggedDBStatusIsUp = true;
}
}
public long getRunPeriod() {
return runPeriod;
}
return false;
}
return false;
}
public boolean isEmpty() {
return true;
}
/**
* Monitoring logic used by background thread This thread is used for both
* clenup expired sessions in the repository and for the Database health
* checking. The thread always runs with smallest value of cleanUpPeriod and
* healthCheckPeriod.
*/
public void run() {
try {
if (debug.messageEnabled()) {
}
/*
* Clean up is done based on the cleanUpPeriod even though the
* thread runs based on the runPeriod.
*/
}
/*
* HealthChecking is done based on the runPeriod but only when
* the Database is down.
*/
logDBStatus();
}
} catch (Exception e) {
e);
}
}
}