537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * The contents of this file are subject to the terms of the Common Development and
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Distribution License (the License). You may not use this file except in compliance with the
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * specific language governing permission and limitations under the License.
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * When distributing Covered Software, include this CDDL Header Notice in each file and include
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Header, with the fields enclosed by brackets [] replaced by your own identifying
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * information: "Portions copyright [year] [name of copyright owner]".
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpotts * Copyright 2014-2015 ForgeRock AS.
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Lunapackage org.forgerock.openam.cts.monitoring.impl.persistence;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.CTSPersistentStore;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.api.filter.TokenFilter;
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshottimport org.forgerock.openam.cts.api.filter.TokenFilterBuilder;
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpottsimport org.forgerock.openam.tokens.TokenType;
1b5cb8422c825dbdc6d7669e74f285cede6ab71cJames Phillpottsimport org.forgerock.openam.tokens.CoreTokenField;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Lunaimport org.forgerock.openam.cts.exceptions.CoreTokenException;
a093731116a8c24d49b903df7602cf586e499b45Phill Cunningtonimport org.forgerock.openam.sm.datalayer.api.query.PartialToken;
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * Used to query the CTS persistence store and return data to the monitoring service
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public CtsPersistenceOperationsDelegate(CTSPersistentStore store) {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Counts the number of tokens in the persistent store that match the requested type.
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @param tokenType The type of token for which we are gathering results
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * @return Zero or positive integer of the number of tokens in the store.
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @throws CoreTokenException
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public int countTokenEntries(TokenType tokenType) throws CoreTokenException {
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna //create the filter to restrict by token type
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final TokenFilter tokenFilter = new TokenFilterBuilder()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .withAttribute(CoreTokenField.TOKEN_TYPE, tokenType)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott return store.attributeQuery(tokenFilter).size();
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * Counts all Tokens within the CTS Persistent store. All tokens are considered regardless of TokenType.
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott * @return Zero or positive integer.
0f7c5b88fd04e25bea6113dfc783a05e4e2045f8David Luna * @throws CoreTokenException if there are issues talking with the CTS
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott public int countAllTokens() throws CoreTokenException {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott TokenFilter filter = new TokenFilterBuilder().returnAttribute(CoreTokenField.TOKEN_ID).build();
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * Gathers list of the durations of tokens in epoch'd seconds
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @param tokenType The type of token for which we are gathering results
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @return A collection of longs, each of which represents the duration of a token inside the CTS
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna * @throws CoreTokenException
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna public Collection<Long> listDurationOfTokens(TokenType tokenType) throws CoreTokenException {
dcaf67650b5323dbf22121c7bf12be9be903f8c5David Luna final Collection<Long> results = new ArrayList<Long>();
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final long unixTime = TimeUtils.currentUnixTime();
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott final TokenFilter filter = new TokenFilterBuilder()
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .returnAttribute(CoreTokenField.CREATE_TIMESTAMP)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott .withAttribute(CoreTokenField.TOKEN_TYPE, tokenType)
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott for (PartialToken token : store.attributeQuery(filter)) {
537857b204a4425e0ea257b0413a02c5c7c5e266Robert Wapshott Calendar timestamp = token.getValue(CoreTokenField.CREATE_TIMESTAMP);