pzampino commented on a change in pull request #350:
URL: https://github.com/apache/knox/pull/350#discussion_r442812289



##########
File path: 
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/JournalBasedTokenStateService.java
##########
@@ -0,0 +1,190 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations 
under
+ * the License.
+ *
+ */
+package org.apache.knox.gateway.services.token.impl;
+
+import org.apache.knox.gateway.config.GatewayConfig;
+import org.apache.knox.gateway.services.ServiceLifecycleException;
+import org.apache.knox.gateway.services.security.token.UnknownTokenException;
+import 
org.apache.knox.gateway.services.token.impl.state.TokenStateJournalFactory;
+import org.apache.knox.gateway.services.token.state.JournalEntry;
+import org.apache.knox.gateway.services.token.state.TokenStateJournal;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class JournalBasedTokenStateService extends DefaultTokenStateService {
+
+    private TokenStateJournal journal;
+
+    @Override
+    public void init(final GatewayConfig config, final Map<String, String> 
options) throws ServiceLifecycleException {
+        super.init(config, options);
+
+        try {
+            // Initialize the token state journal
+            journal = TokenStateJournalFactory.create(config);
+
+            // Load any persisted journal entries, and add them to the 
unpersisted state collection
+            List<JournalEntry> entries = journal.get();
+            for (JournalEntry entry : entries) {
+                String id = entry.getTokenId();
+                try {
+                    long issueTime   = Long.parseLong(entry.getIssueTime());
+                    long expiration  = Long.parseLong(entry.getExpiration());
+                    long maxLifetime = Long.parseLong(entry.getMaxLifetime());
+
+                    // Add the token state to memory
+                    super.addToken(id, issueTime, expiration, maxLifetime);
+
+                } catch (Exception e) {
+                    log.failedToLoadJournalEntry(id, e);
+                }
+            }
+        } catch (IOException e) {
+            throw new ServiceLifecycleException("Failed to load persisted 
state from the token state journal", e);
+        }
+    }
+
+    @Override
+    public void addToken(final String tokenId, long issueTime, long 
expiration, long maxLifetimeDuration) {
+        super.addToken(tokenId, issueTime, expiration, maxLifetimeDuration);
+
+        try {
+            journal.add(tokenId, issueTime, expiration, maxLifetimeDuration);
+        } catch (IOException e) {
+            log.failedToAddJournalEntry(tokenId, e);
+        }
+    }
+
+    @Override
+    public long getTokenExpiration(final String tokenId, boolean validate) 
throws UnknownTokenException {
+        // Check the in-memory collection first, to avoid costly keystore 
access when possible

Review comment:
       +1




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to