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.auth.local;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.RandomAccessFile;
28 import java.util.Iterator;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.local.DataFile.Token;
32 import org.onap.aaf.auth.local.DataFile.Token.Field;
33 import org.onap.aaf.misc.env.Env;
34 import org.onap.aaf.misc.env.TimeTaken;
36 public abstract class AbsData implements Iterable<String> {
37 protected DataFile data;
38 protected TextIndex ti;
39 private File dataf,idxf,lockf;
42 private int maxLineSize;
43 private int fieldOffset;
44 private int skipLines;
46 public AbsData(File dataf,char sepChar, int maxLineSize, int fieldOffset) {
47 File dir = dataf.getParentFile();
48 int dot = dataf.getName().lastIndexOf('.');
49 name = dataf.getName().substring(0,dot);
53 this.maxLineSize = maxLineSize;
54 this.fieldOffset = fieldOffset;
55 idxf = new File(dir,name.concat(".idx"));
56 lockf = new File(dir,name.concat(".lock"));
59 data = new DataFile(dataf,"r");
60 ti = new TextIndex(idxf);
64 public void skipLines(int lines) {
68 public String name() {
72 public void open(AuthzTrans trans, long timeout) throws IOException {
73 TimeTaken tt = trans.start("Open Data File", Env.SUB);
74 boolean opened = false, first = true;
77 throw new FileNotFoundException("Data File Missing:" + dataf.getCanonicalPath());
79 long begin = System.currentTimeMillis();
80 long end = begin+timeout;
82 while((exists=lockf.exists()) && begin<end) {
84 trans.warn().log("Waiting for",lockf.getCanonicalPath(),"to close");
89 } catch (InterruptedException e) {
92 begin = System.currentTimeMillis();
95 throw new IOException(lockf.getCanonicalPath() + "exists. May not open Datafile");
100 } catch (IOException e) {
111 throw new IOException("DataFile pair for " + name + " was not able to be opened in " + timeout + "ms");
115 private synchronized void ensureIdxGood(AuthzTrans trans) throws IOException {
116 if(!idxf.exists() || idxf.length()==0 || dataf.lastModified()>idxf.lastModified()) {
117 trans.warn().log(idxf.getAbsolutePath(),"is missing, empty or out of date, creating");
118 RandomAccessFile raf = new RandomAccessFile(lockf, "rw");
120 ti.create(trans, data, maxLineSize, delim, fieldOffset, skipLines);
121 if(!idxf.exists() || (idxf.length()==0 && dataf.length()!=0)) {
122 throw new IOException("Data Index File did not create correctly");
131 public void close(AuthzTrans trans) throws IOException {
137 public Token tokenData;
138 private Field fieldData;
140 private Reuse(int size,char delim) {
141 tokenData = data.new Token(size);
142 fieldData = tokenData.new Field(delim);
145 public void reset() {
146 getFieldData().reset();
149 public void pos(int rec) {
150 getFieldData().reset();
154 public String next() {
155 return getFieldData().next();
158 public String at(int field) {
159 return getFieldData().at(field);
162 public String atToEnd(int field) {
163 return getFieldData().atToEnd(field);
166 public Field getFieldData() {
171 public Reuse reuse() {
172 return new Reuse(maxLineSize,delim);
175 public Iter iterator() {
179 public class Iter implements Iterator<String> {
181 private org.onap.aaf.auth.local.TextIndex.Iter tii;
189 public boolean hasNext() {
190 return tii.hasNext();
194 public String next() {
196 int rec = tii.next();
202 public void remove() {