cc00efffee587cc79d84d9391a1064b865229b80
[aaf/authz.git] / authz-core / src / main / java / com / att / authz / local / AbsData.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.authz.local;\r
25 \r
26 import java.io.File;\r
27 import java.io.FileNotFoundException;\r
28 import java.io.IOException;\r
29 import java.io.RandomAccessFile;\r
30 import java.util.Iterator;\r
31 \r
32 import com.att.authz.env.AuthzTrans;\r
33 import com.att.authz.local.DataFile.Token;\r
34 import com.att.authz.local.DataFile.Token.Field;\r
35 import com.att.inno.env.Env;\r
36 import com.att.inno.env.TimeTaken;\r
37 \r
38 public abstract class AbsData implements Iterable<String> {\r
39         protected DataFile data;\r
40         protected TextIndex ti;\r
41         private File dataf,idxf,lockf;\r
42         private String name;\r
43         private char delim;\r
44         private int maxLineSize;\r
45         private int fieldOffset;\r
46         private int skipLines;\r
47 \r
48         public AbsData(File dataf,char sepChar, int maxLineSize, int fieldOffset) {\r
49                 File dir = dataf.getParentFile();\r
50                 int dot = dataf.getName().lastIndexOf('.');\r
51                 if(dot>=0) {\r
52                         name = dataf.getName().substring(0,dot);\r
53                 }\r
54 \r
55                 this.dataf=dataf;\r
56                 this.delim = sepChar;\r
57                 this.maxLineSize = maxLineSize;\r
58                 this.fieldOffset = fieldOffset;\r
59                 idxf = new File(dir,name.concat(".idx"));\r
60                 lockf = new File(dir,name.concat(".lock"));\r
61                 \r
62                 \r
63                 data = new DataFile(dataf,"r");\r
64                 ti = new TextIndex(idxf);\r
65                 skipLines=0;\r
66         }\r
67         \r
68         public void skipLines(int lines) {\r
69                 skipLines=lines;\r
70         }\r
71         \r
72         public String name() {\r
73                 return name;\r
74         }\r
75         \r
76         public void open(AuthzTrans trans, long timeout) throws IOException {\r
77                 TimeTaken tt = trans.start("Open Data File", Env.SUB);\r
78                 boolean opened = false, first = true;\r
79                 try {\r
80                                 if(!dataf.exists()) {\r
81                                         throw new FileNotFoundException("Data File Missing:" + dataf.getCanonicalPath());\r
82                                 }\r
83                                 long begin = System.currentTimeMillis();\r
84                                 long end = begin+timeout;\r
85                                 boolean exists;\r
86                                 while((exists=lockf.exists()) && begin<end) {\r
87                                         if(first) {\r
88                                                 trans.warn().log("Waiting for",lockf.getCanonicalPath(),"to close");\r
89                                                 first = false;\r
90                                         } \r
91                                         try {\r
92                                                 Thread.sleep(200);\r
93                                         } catch (InterruptedException e) {\r
94                                                 break;\r
95                                         }\r
96                                         begin = System.currentTimeMillis();\r
97                                 }\r
98                                 if(exists) {\r
99                                         throw new IOException(lockf.getCanonicalPath() + "exists.  May not open Datafile");\r
100                                 }\r
101                                 data.open();\r
102                                 try {\r
103                                         ensureIdxGood(trans);\r
104                                 } catch (IOException e) {\r
105                                         data.close();\r
106                                         throw e;\r
107                                 }\r
108                                 ti.open();\r
109                                 opened = true;\r
110                         \r
111                 } finally {\r
112                         tt.done();\r
113                 }\r
114                 if(!opened) {\r
115                         throw new IOException("DataFile pair for " + name + " was not able to be opened in " + timeout + "ms");\r
116                 }\r
117         }\r
118         \r
119         private synchronized void ensureIdxGood(AuthzTrans trans) throws IOException {\r
120                 if(!idxf.exists() || idxf.length()==0 || dataf.lastModified()>idxf.lastModified()) {\r
121                         trans.warn().log(idxf.getCanonicalPath(),"is missing, empty or out of date, creating");\r
122                         RandomAccessFile raf = new RandomAccessFile(lockf, "rw");\r
123                         try {\r
124                                 ti.create(trans, data, maxLineSize, delim, fieldOffset, skipLines);\r
125                                 if(!idxf.exists() || (idxf.length()==0 && dataf.length()!=0)) {\r
126                                         throw new IOException("Data Index File did not create correctly");\r
127                                 }\r
128                         } finally {\r
129                                 raf.close();\r
130                                 lockf.delete();\r
131                         }\r
132                 }\r
133         }\r
134 \r
135         public void close(AuthzTrans trans) throws IOException {\r
136                 ti.close();\r
137                 data.close();\r
138         }\r
139         \r
140         public class Reuse {\r
141                 private Token tokenData;\r
142                 private Field fieldData;\r
143 \r
144                 private Reuse(int size,char delim) {\r
145                         tokenData = data.new Token(size);\r
146                         fieldData = getTokenData().new Field(delim);\r
147                 }\r
148                 \r
149                 public void reset() {\r
150                         getFieldData().reset();\r
151                 }\r
152 \r
153                 public void pos(int rec) {\r
154                         getFieldData().reset();\r
155                         getTokenData().pos(rec);\r
156                 }\r
157 \r
158                 public String next() {\r
159                         return getFieldData().next();\r
160                 }\r
161                 \r
162                 public String at(int field) {\r
163                         return getFieldData().at(field);\r
164                 }\r
165 \r
166                 public String atToEnd(int field) {\r
167                         return getFieldData().atToEnd(field);\r
168                 }\r
169 \r
170                 public Field getFieldData() {\r
171                         return fieldData;\r
172                 }\r
173 \r
174                 public Token getTokenData() {\r
175                         return tokenData;\r
176                 }\r
177 \r
178         }\r
179         \r
180         public Reuse reuse() {\r
181                 return new Reuse(maxLineSize,delim);\r
182         }\r
183 \r
184         public Iter iterator() {\r
185                 return new Iter();\r
186         }\r
187         \r
188         public class Iter implements Iterator<String> {\r
189                 private Reuse reuse;\r
190                 private com.att.authz.local.TextIndex.Iter tii;\r
191 \r
192                 public Iter() {\r
193                         reuse = reuse();\r
194                         tii = ti.new Iter();\r
195                 }\r
196 \r
197                 @Override\r
198                 public boolean hasNext() {\r
199                         return tii.hasNext();\r
200                 }\r
201 \r
202                 @Override\r
203                 public String next() {\r
204                         reuse.reset();\r
205                         int rec = tii.next();\r
206                         reuse.pos(rec);\r
207                         return reuse.at(0);\r
208                 }\r
209 \r
210                 @Override\r
211                 public void remove() {\r
212                         // read only\r
213                 }\r
214         }\r
215 }\r