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;
29 import java.util.NoSuchElementException;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.local.DataFile.Token;
33 import org.onap.aaf.auth.local.DataFile.Token.Field;
34 import org.onap.aaf.misc.env.Env;
35 import org.onap.aaf.misc.env.TimeTaken;
37 public abstract class AbsData implements Iterable<String> {
38 protected DataFile data;
39 protected TextIndex ti;
40 private File dataf,idxf,lockf;
43 private int maxLineSize;
44 private int fieldOffset;
45 private int skipLines;
47 public AbsData(File dataf,char sepChar, int maxLineSize, int fieldOffset) {
48 File dir = dataf.getParentFile();
49 int dot = dataf.getName().lastIndexOf('.');
50 name = dataf.getName().substring(0,dot);
54 this.maxLineSize = maxLineSize;
55 this.fieldOffset = fieldOffset;
56 idxf = new File(dir,name.concat(".idx"));
57 lockf = new File(dir,name.concat(".lock"));
60 data = new DataFile(dataf,"r");
61 ti = new TextIndex(idxf);
65 public void skipLines(int lines) {
69 public String name() {
73 public void open(AuthzTrans trans, long timeout) throws IOException {
74 TimeTaken tt = trans.start("Open Data File", Env.SUB);
77 if (!dataf.exists()) {
78 throw new FileNotFoundException("Data File Missing:" + dataf.getCanonicalPath());
80 long begin = System.currentTimeMillis();
81 long end = begin+timeout;
83 while ((exists=lockf.exists()) && begin<end) {
85 trans.warn().log("Waiting for",lockf.getCanonicalPath(),"to close");
90 } catch (InterruptedException e) {
91 Thread.currentThread().interrupt();
93 begin = System.currentTimeMillis();
96 throw new IOException(lockf.getCanonicalPath() + "exists. May not open Datafile");
100 ensureIdxGood(trans);
101 } catch (IOException e) {
112 private synchronized void ensureIdxGood(AuthzTrans trans) throws IOException {
113 if (!idxf.exists() || idxf.length()==0 || dataf.lastModified()>idxf.lastModified()) {
114 trans.warn().log(idxf.getAbsolutePath(),"is missing, empty or out of date, creating");
115 RandomAccessFile raf = new RandomAccessFile(lockf, "rw");
117 ti.create(trans, data, maxLineSize, delim, fieldOffset, skipLines);
118 if (!idxf.exists() || (idxf.length()==0 && dataf.length()!=0)) {
119 throw new IOException("Data Index File did not create correctly");
128 public void close(AuthzTrans trans) throws IOException {
134 public Token tokenData;
135 private Field fieldData;
137 private Reuse(int size,char delim) {
138 tokenData = data.new Token(size);
139 fieldData = tokenData.new Field(delim);
142 public void reset() {
143 getFieldData().reset();
146 public void pos(int rec) {
147 getFieldData().reset();
151 public String next() {
152 return getFieldData().next();
155 public String at(int field) {
156 return getFieldData().at(field);
159 public String atToEnd(int field) {
160 return getFieldData().atToEnd(field);
163 public Field getFieldData() {
168 public Reuse reuse() {
169 return new Reuse(maxLineSize,delim);
172 public Iter iterator() {
176 public class Iter implements Iterator<String> {
178 private org.onap.aaf.auth.local.TextIndex.Iter tii;
186 public boolean hasNext() {
187 return tii.hasNext();
191 public String next() {
193 throw new NoSuchElementException();
196 int rec = tii.next();
202 public void remove() {