summaryrefslogtreecommitdiffstats
path: root/authz-core/src/main/java/org/onap/aaf/authz/local/AbsData.java
blob: 30231b897f099a89e3ed82ddbef698a88b790b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.authz.local;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Iterator;

import org.onap.aaf.authz.env.AuthzTrans;
import org.onap.aaf.authz.local.DataFile.Token;
import org.onap.aaf.authz.local.DataFile.Token.Field;

import org.onap.aaf.inno.env.Env;
import org.onap.aaf.inno.env.TimeTaken;

public abstract class AbsData implements Iterable<String> {
	protected DataFile data;
	protected TextIndex ti;
	private File dataf,idxf,lockf;
	private String name;
	private char delim;
	private int maxLineSize;
	private int fieldOffset;
	private int skipLines;

	public AbsData(File dataf,char sepChar, int maxLineSize, int fieldOffset) {
		File dir = dataf.getParentFile();
		int dot = dataf.getName().lastIndexOf('.');
		if(dot>=0) {
			name = dataf.getName().substring(0,dot);
		}

		this.dataf=dataf;
		this.delim = sepChar;
		this.maxLineSize = maxLineSize;
		this.fieldOffset = fieldOffset;
		idxf = new File(dir,name.concat(".idx"));
		lockf = new File(dir,name.concat(".lock"));
		
		
		data = new DataFile(dataf,"r");
		ti = new TextIndex(idxf);
		skipLines=0;
	}
	
	public void skipLines(int lines) {
		skipLines=lines;
	}
	
	public String name() {
		return name;
	}
	
	public void open(AuthzTrans trans, long timeout) throws IOException {
		TimeTaken tt = trans.start("Open Data File", Env.SUB);
		boolean opened = false, first = true;
		try {
				if(!dataf.exists()) {
					throw new FileNotFoundException("Data File Missing:" + dataf.getCanonicalPath());
				}
				long begin = System.currentTimeMillis();
				long end = begin+timeout;
				boolean exists;
				while((exists=lockf.exists()) && begin<end) {
					if(first) {
						trans.warn().log("Waiting for",lockf.getCanonicalPath(),"to close");
						first = false;
					} 
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						break;
					}
					begin = System.currentTimeMillis();
				}
				if(exists) {
					throw new IOException(lockf.getCanonicalPath() + "exists.  May not open Datafile");
				}
				data.open();
				try {
					ensureIdxGood(trans);
				} catch (IOException e) {
					data.close();
					throw e;
				}
				ti.open();
				opened = true;
			
		} finally {
			tt.done();
		}
		if(!opened) {
			throw new IOException("DataFile pair for " + name + " was not able to be opened in " + timeout + "ms");
		}
	}
	
	private synchronized void ensureIdxGood(AuthzTrans trans) throws IOException {
		if(!idxf.exists() || idxf.length()==0 || dataf.lastModified()>idxf.lastModified()) {
			trans.warn().log(idxf.getCanonicalPath(),"is missing, empty or out of date, creating");
			RandomAccessFile raf = new RandomAccessFile(lockf, "rw");
			try {
				ti.create(trans, data, maxLineSize, delim, fieldOffset, skipLines);
				if(!idxf.exists() || (idxf.length()==0 && dataf.length()!=0)) {
					throw new IOException("Data Index File did not create correctly");
				}
			} finally {
				raf.close();
				lockf.delete();
			}
		}
	}

	public void close(AuthzTrans trans) throws IOException {
		ti.close();
		data.close();
	}
	
	public class Reuse {
		private Token tokenData;
		private Field fieldData;

		private Reuse(int size,char delim) {
			tokenData = data.new Token(size);
			fieldData = getTokenData().new Field(delim);
		}
		
		public void reset() {
			getFieldData().reset();
		}

		public void pos(int rec) {
			getFieldData().reset();
			getTokenData().pos(rec);
		}

		public String next() {
			return getFieldData().next();
		}
		
		public String at(int field) {
			return getFieldData().at(field);
		}

		public String atToEnd(int field) {
			return getFieldData().atToEnd(field);
		}

		public Field getFieldData() {
			return fieldData;
		}

		public Token getTokenData() {
			return tokenData;
		}

	}
	
	public Reuse reuse() {
		return new Reuse(maxLineSize,delim);
	}

	public Iter iterator() {
		return new Iter();
	}
	
	public class Iter implements Iterator<String> {
		private Reuse reuse;
		private org.onap.aaf.authz.local.TextIndex.Iter tii;

		public Iter() {
			reuse = reuse();
			tii = ti.new Iter();
		}

		@Override
		public boolean hasNext() {
			return tii.hasNext();
		}

		@Override
		public String next() {
			reuse.reset();
			int rec = tii.next();
			reuse.pos(rec);
			return reuse.at(0);
		}

		@Override
		public void remove() {
			// read only
		}
	}
}