AT&T 2.0.19 Code drop, stage 2
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / persist / Persisting.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.persist;
23
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;
29
30 import org.onap.aaf.cadi.Access.Level;
31
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
35
36         private static final long SYNC_TIME = 1000*60*1; // Checking File change max 1 min
37         private FileTime lastTouched;
38         private int count;
39         private long expires;
40         private long nextCheck;
41         private T t;
42         private Path path;
43         private Persist<T, ?> persist;
44         
45         public Persisting(Persist<T,?> p, T t, long expiresSecsFrom1970, byte[] hash, Path path) {
46                 persist = p;
47                 this.t=t;
48                 expires = expiresSecsFrom1970;
49                 this.path = path;
50                 try {
51                         lastTouched = Files.getLastModifiedTime(path);
52                 } catch (IOException e) {
53                         lastTouched = null;
54                 }
55                 count=0;
56                 nextCheck=0;
57                 if(hash==null) {
58                         this.hash = EMPTY;
59                 } else {
60                         this.hash = hash;
61                 }
62         }
63
64         @Override
65         public T get() {
66                 return t;
67         }
68
69         @Override
70         public long expires() {
71                 return expires;
72         }
73
74         @Override
75         public boolean expired() {
76                 return System.currentTimeMillis()/1000>expires;
77         }
78
79         @Override
80         public boolean hasBeenTouched() {
81                 try {
82                         FileTime modT = Files.getLastModifiedTime(path);
83                         if(lastTouched==null) {
84                                 lastTouched = modT;
85                                 return true;
86                         } else {
87                                 return !modT.equals(lastTouched);
88                         }
89                 } catch (NoSuchFileException e) {
90                         persist.access.log(Level.DEBUG, "File not found " +  e.getMessage() + ", this is ok, marking as touched.");
91                         return true;
92                 } catch (IOException e) {
93                         persist.access.log(e, "Accessing File Time");
94                         return true;
95                 }
96         }
97
98         @Override
99         public synchronized boolean checkSyncTime() {
100                 long temp=System.currentTimeMillis();
101                 if(nextCheck==0 || nextCheck<temp) {
102                         nextCheck = temp+SYNC_TIME;
103                         return true;
104                 }
105                 return false;
106         }
107
108         /* (non-Javadoc)
109          * @see org.onap.aaf.cadi.oauth.Persistable#checkReloadTime()
110          */
111         @Override
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
116                 if(expired()) {
117                         return true;
118                 } else {
119                         return false;
120                 }
121         }
122
123         @Override
124         public byte[] getHash() {
125                 return hash;
126         }
127
128         @Override
129         public boolean match(byte[] hashIn) {
130                 if(hash==null || hashIn==null || hash.length!=hashIn.length) {
131                         return false;
132                 }
133                 for(int i=0;i<hashIn.length;++i) {
134                         if(hash[i]!=hashIn[i]) {
135                                 return false;
136                         }
137                 }
138                 return true;
139         }
140
141         @Override
142         public synchronized void inc() {
143                 ++count;
144         }
145
146         /* (non-Javadoc)
147          * @see org.onap.aaf.cadi.oauth.Cacheable#count()
148          */
149         @Override
150         public int count() {
151                 return count;
152         }
153
154         /* (non-Javadoc)
155          * @see org.onap.aaf.cadi.oauth.Persistable#clearCount()
156          */
157         @Override
158         public synchronized void clearCount() {
159                 count=0;
160         }
161
162         @Override
163         public Path path() {
164                 return path;
165         }
166
167 }