JMQTokenRepo.java revision e70418658b6daa84fc8a1f13677d2cb616a66725
/*
* Copyright (c) 2012 ForgeRock AS. 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 legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at 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]".
*
*/
/**
* This class is a repository capable of storing and retrieving tokens in the JMQ/BDB session repository.
*
* @author Jonathan Scudder
*/
private static boolean isDatabaseUp = true;
// Time period between two successive runs of repository cleanup thread which checks and removes expired records
private static long CLEANUPVALUE = 0;
// TODO rename
// Time period between two successive runs of DBHealthChecker thread which checks for Database availability
// TODO rename
public static final String HEALTH_CHECK_RUN_PERIOD = "org.forgerock.ext.cts.repo.healthCheckRunPeriod";
// This period is actual one that is used by the thread, smallest value of cleanUpPeriod and healthCheckPeriod
//
static {
/*
try {
gracePeriod = Integer.parseInt(SystemPropertiesManager.get(CLEANUP_GRACE_PERIOD, String.valueOf(gracePeriod)));
} catch (Exception e) {
debug.error("Invalid value for " + CLEANUP_GRACE_PERIOD + ", using default");
}
try {
cleanUpPeriod = Integer.parseInt(SystemPropertiesManager.get(CLEANUP_RUN_PERIOD, String.valueOf(cleanUpPeriod)));
} catch (Exception e) {
debug.error("Invalid value for " + CLEANUP_RUN_PERIOD + ", using default");
}
try {
healthCheckPeriod = Integer.parseInt(SystemPropertiesManager.get(HEALTH_CHECK_RUN_PERIOD, String.valueOf(healthCheckPeriod)));
} catch (Exception e) {
debug.error("Invalid value for " + HEALTH_CHECK_RUN_PERIOD + ", using default");
}
*/
}
// The OpenAM message queue used for session and SAML2
/**
* Create the JMQ connection based on settings in system properties, and start the timer for cleanup operations.
*
* @throws Exception
*/
public JMQTokenRepo() throws Exception {
if (thisSessionServerProtocol == null
|| thisSessionServer == null) {
throw new IllegalStateException("JMQ session failover not correctly configured");
}
}
/**
* Initialize new FAMRecord persister.
*/
private void initPersistSession() {
try {
isDatabaseUp = true;
} catch (Exception e) {
isDatabaseUp = false;
if (debug.messageEnabled()) {
}
}
}
/**
* Creates a new token, stores it and returns the token ID. If the request includes an ID then that is the ID that is used for
* the created object; if the ID is in the payload then that is used, and failing that an ID will be generated by
* this component.
*
* @param request the JSON request
* @return the JSON response, including the ID that may have been generated during creation
* @throws JsonResourceException
*/
if (!isDatabaseUp) {
}
// TODO: validate request
}
throw new JsonResourceException(JsonResourceException.BAD_REQUEST, "No value to store was found in the request");
}
// Get id from the request, not the value
// Generate the token ID or set to the value provided in the request or payload
} else {
//TODO request.get("value").put("id", primaryKey);
}
// Set expiry time to default
// TODO make default expiry time configurable
if (expiryTime <= 0) {
}
try {
FAMRecord famRec = new FAMRecord(cts, FAMRecord.WRITE, primaryKey, expiryTime, secondaryKey, 0, null, blob);
return retValue;
} catch (IllegalStateException e) {
isDatabaseUp = false;
// TODO revamp logging
//logDBStatus();
} catch (Exception e) {
}
}
/**
* Reads a token from the JMQ based on the request provided, locating token by id. If a token is to be retrieved
* using the secondary key then query must be used instead.
*
* @param request the json request object
* @return the json response including a value representing the entire token
* @throws JsonResourceException
*/
if (!isDatabaseUp) {
}
// Read the object using the primary key
// If trying to read based on the secondary key, use a query instead
try {
throw new JsonResourceException(JsonResourceException.NOT_FOUND, "Object not found with id: " + primaryKey);
}
// TODO: confirm Json response object
return retValue;
} catch (IllegalStateException e) {
isDatabaseUp = false;
//logDBStatus();
} catch (Exception e) {
}
}
/**
* Updates a token in the store. Note that this will overwrite the previous token completely. In the event that the
* original token has not been stored, the token will be created.
*
* @param request the JSON request object including the entire token object to be stored
* @return JSON response including the ID of the token
* @throws JsonResourceException
*/
// Update is run in the same way as a create
}
/**
* Deletes a token from the store based on the ID in the request.
*
* @param request the JSON request object including the ID of the token to delete
* @return null
* @throws JsonResourceException
*/
if (!isDatabaseUp) {
}
try {
return retValue;
} catch (IllegalStateException e) {
isDatabaseUp = false;
//logDBStatus();
} catch (Exception e) {
}
}
/**
* Applies a patch to a token in the store by retrieving, patching, then updating. This method does not currently
* support changing the primary or secondary key of the token.
*
* @param request the JSON request including the patch object
* @return simple JSON response of _id and _rev
* @throws JsonResourceException
*/
// Read the token
// Patch it
// Update the token
// TODO handle case where primary key or secondary key changes
return retValue;
}
// From interface // TODO: annotate
if (!isDatabaseUp) {
}
}
// From interface // TODO: annotate
if (!isDatabaseUp) {
}
// TODO TODO TODO
}
/**
* Triggers deletion of all expired resources in the store.
*
* @throws JsonResourceException
*/
protected void deleteExpired() throws JsonResourceException {
if (!isDatabaseUp) {
}
try {
} catch (IllegalStateException e) {
isDatabaseUp = false;
//logDBStatus();
} catch (Exception e) {
}
}
/**
* Handles a JSON resource request by dispatching to the method corresponding with the
* method member of the request. If the request method is not one of the standard JSON
* resource request methods, a {@code JsonResourceException} is thrown.
* <p/>
* This method catches any thrown {@code JsonValueException}, and rethrows it as a
* {@link JsonResourceException#BAD_REQUEST}. This allows the use of JsonValue methods
* to validate the content of the request.
*
* @param request the JSON resource request.
* @return the JSON resource response.
* @throws if there is an exception handling the request.
*/
try {
try {
case create:
case read:
case update:
case delete:
case patch:
case query:
case action:
default:
}
} catch (JsonValueException jve) {
}
try {
//onException(e1); // give handler opportunity to throw its own exception
throw e1;
throw (JsonResourceException) e2;
} else { // need to rethrow as resource exception
}
}
}
}
// Overrides from GeneralTaskRunnable
// TODO javadoc
public boolean addElement(Object o) {
throw new IllegalStateException("Not supported");
}
public boolean removeElement(Object o) {
throw new IllegalStateException("Not supported");
}
public boolean isEmpty() {
throw new IllegalStateException("Not supported");
}
public long getRunPeriod() {
return RUNPERIOD;
}
public void run() {
try {
if (debug.messageEnabled()) {
}
// TODO synchronize??
/*
* Clean up is done based on the cleanUpPeriod even though the
* thread runs based on the runPeriod.
*/
if (CLEANUPVALUE <= 0) {
}
/*
* HealthChecking is done based on the runPeriod but only when
* the Database is down.
*/
if (!isDatabaseUp) {
//logDBStatus();
}
} catch (Exception e) {
}
}
}