2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi.persist;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.NoSuchFileException;
27 import java.nio.file.Path;
28 import java.nio.file.attribute.FileTime;
30 import org.onap.aaf.cadi.Access.Level;
32 public class Persisting<T> implements Persistable<T> {
33 private static final byte[] EMPTY = new byte[0];
34 private final byte[] hash; // need to be able to validate disk entry
36 private static final long SYNC_TIME = 1000*60*1L; // Checking File change max 1 min
37 private FileTime lastTouched;
40 private long nextCheck;
43 private Persist<T, ?> persist;
45 public Persisting(Persist<T,?> p, T t, long expiresSecsFrom1970, byte[] hash, Path path) {
48 expires = expiresSecsFrom1970;
51 lastTouched = Files.getLastModifiedTime(path);
52 } catch (IOException e) {
70 public long expires() {
75 public boolean expired() {
76 return System.currentTimeMillis()/1000>expires;
80 public boolean hasBeenTouched() {
82 FileTime modT = Files.getLastModifiedTime(path);
83 if (lastTouched==null) {
87 return !modT.equals(lastTouched);
89 } catch (NoSuchFileException e) {
90 persist.access.log(Level.DEBUG, "File not found " + e.getMessage() + ", this is ok, marking as touched.");
92 } catch (IOException e) {
93 persist.access.log(e, "Accessing File Time");
99 public synchronized boolean checkSyncTime() {
100 long temp=System.currentTimeMillis();
101 if (nextCheck==0 || nextCheck<temp) {
102 nextCheck = temp+SYNC_TIME;
109 * @see org.onap.aaf.cadi.oauth.Persistable#checkReloadTime()
112 public boolean checkReloadable() {
113 //TODO other elements to add here...
114 // Ideas: Is it valid?
115 // if not, How many times has it been checked in the last minute
120 public byte[] getHash() {
125 public boolean match(byte[] hashIn) {
126 if (hash==null || hashIn==null || hash.length!=hashIn.length) {
129 for (int i=0;i<hashIn.length;++i) {
130 if (hash[i]!=hashIn[i]) {
138 public synchronized void inc() {
143 * @see org.onap.aaf.cadi.oauth.Cacheable#count()
151 * @see org.onap.aaf.cadi.oauth.Persistable#clearCount()
154 public synchronized void clearCount() {