8b98f5bf53028da8795756b395dfbf76745da72f
[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*1L; // 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                 return expired();
117         }
118
119         @Override
120         public byte[] getHash() {
121                 return hash;
122         }
123
124         @Override
125         public boolean match(byte[] hashIn) {
126                 if(hash==null || hashIn==null || hash.length!=hashIn.length) {
127                         return false;
128                 }
129                 for(int i=0;i<hashIn.length;++i) {
130                         if(hash[i]!=hashIn[i]) {
131                                 return false;
132                         }
133                 }
134                 return true;
135         }
136
137         @Override
138         public synchronized void inc() {
139                 ++count;
140         }
141
142         /* (non-Javadoc)
143          * @see org.onap.aaf.cadi.oauth.Cacheable#count()
144          */
145         @Override
146         public int count() {
147                 return count;
148         }
149
150         /* (non-Javadoc)
151          * @see org.onap.aaf.cadi.oauth.Persistable#clearCount()
152          */
153         @Override
154         public synchronized void clearCount() {
155                 count=0;
156         }
157
158         @Override
159         public Path path() {
160                 return path;
161         }
162
163 }