1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class represents a file log for the index events.
|
4
|
* Copyright: 2013 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* This program is free software; you can redistribute it and/or modify
|
9
|
* it under the terms of the GNU General Public License as published by
|
10
|
* the Free Software Foundation; either version 2 of the License, or
|
11
|
* (at your option) any later version.
|
12
|
*
|
13
|
* This program is distributed in the hope that it will be useful,
|
14
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
* GNU General Public License for more details.
|
17
|
*
|
18
|
* You should have received a copy of the GNU General Public License
|
19
|
* along with this program; if not, write to the Free Software
|
20
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
21
|
*/
|
22
|
package edu.ucsb.nceas.metacat.index.event;
|
23
|
|
24
|
import java.io.File;
|
25
|
import java.io.FileNotFoundException;
|
26
|
import java.io.IOException;
|
27
|
import java.text.DateFormat;
|
28
|
import java.text.ParseException;
|
29
|
import java.text.SimpleDateFormat;
|
30
|
import java.util.Date;
|
31
|
import java.util.List;
|
32
|
import java.util.Vector;
|
33
|
|
34
|
import org.apache.commons.io.FileUtils;
|
35
|
import org.dataone.configuration.Settings;
|
36
|
import org.dataone.service.types.v1.Event;
|
37
|
import org.dataone.service.types.v1.Identifier;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* A class represents a file log for the index events.
|
42
|
* @author tao
|
43
|
*
|
44
|
*/
|
45
|
public class IndexEventFileLog implements IndexEventLog {
|
46
|
private static final String FIELDSEPERATOR = " ";
|
47
|
private static final String LOGFILENAME = "solr-index.log";
|
48
|
private static final String LASTPROCESSEDDATEFILENAME = "solr-last-proccessed-date";
|
49
|
private File logFile = null;
|
50
|
private File lastProcessedDateFile = null;
|
51
|
private long index=1;
|
52
|
private SimpleDateFormat format = new SimpleDateFormat();
|
53
|
|
54
|
/**
|
55
|
* Constructor. Initialize the log file. The log file locates solr.homeDir.
|
56
|
* If the solr.homeDir doesn't exists in the metacat.properties, the it will locate the user's home directory.
|
57
|
* @throws IOException
|
58
|
*/
|
59
|
public IndexEventFileLog() throws IOException {
|
60
|
String path = Settings.getConfiguration().getString("solr.homeDir");
|
61
|
if(path == null || path.trim().equals("")) {
|
62
|
path = System.getProperty("user.home");
|
63
|
}
|
64
|
File pathDir = new File(path);
|
65
|
if(!pathDir.exists()) {
|
66
|
pathDir.mkdirs();
|
67
|
}
|
68
|
logFile = new File(pathDir, LOGFILENAME);
|
69
|
if(!logFile.exists()) {
|
70
|
logFile.createNewFile();
|
71
|
}
|
72
|
lastProcessedDateFile = new File(pathDir, LASTPROCESSEDDATEFILENAME);
|
73
|
if(!lastProcessedDateFile.exists()) {
|
74
|
lastProcessedDateFile.createNewFile();
|
75
|
}
|
76
|
}
|
77
|
/**
|
78
|
* Write an IndexEvent into a file
|
79
|
* @param event
|
80
|
* @throws IndexEventLogException
|
81
|
*/
|
82
|
public synchronized void write(IndexEvent event) throws IndexEventLogException {
|
83
|
if(event != null) {
|
84
|
Vector<String> lines = new Vector<String>();
|
85
|
StringBuffer lineBuffer = new StringBuffer();
|
86
|
lineBuffer.append("\""+index+"\""+FIELDSEPERATOR);
|
87
|
Event type = event.getAction();
|
88
|
lineBuffer.append("\"" + type.xmlValue() + "\"" + FIELDSEPERATOR);
|
89
|
Date date = event.getDate();
|
90
|
if(date != null) {
|
91
|
DateFormat formate = new SimpleDateFormat();
|
92
|
lineBuffer.append("\""+formate.format(date)+"\""+FIELDSEPERATOR);
|
93
|
}
|
94
|
Identifier id = event.getIdentifier();
|
95
|
if(id != null) {
|
96
|
lineBuffer.append("\""+id.getValue()+"\""+FIELDSEPERATOR);
|
97
|
}
|
98
|
String description = event.getDescription();
|
99
|
if(description != null) {
|
100
|
lineBuffer.append("\""+description+"\""+FIELDSEPERATOR);
|
101
|
}
|
102
|
lines.add(lineBuffer.toString());
|
103
|
String lineEnding = null;//null means to use the system default one.
|
104
|
boolean append = true;
|
105
|
try {
|
106
|
FileUtils.writeLines(logFile, "UTF-8", lines, append);
|
107
|
//FileUtils.writeLines(logFile, "UTF-8", lines, lineEnding);
|
108
|
//IOUtils.writeLines(lines, lineEnding, new FileOutputStream(logFile), "UTF-8");
|
109
|
} catch (FileNotFoundException e) {
|
110
|
// TODO Auto-generated catch block
|
111
|
throw new IndexEventLogException(e.getMessage());
|
112
|
} catch (IOException e) {
|
113
|
// TODO Auto-generated catch block
|
114
|
throw new IndexEventLogException(e.getMessage());
|
115
|
}
|
116
|
index++;
|
117
|
}
|
118
|
return;
|
119
|
}
|
120
|
|
121
|
|
122
|
/**
|
123
|
* Gets the list of IndexEvent matching the specified set of filters. The filter parameters can be null
|
124
|
* @param action the action of the event
|
125
|
* @param pid the identifier of the data object in the event
|
126
|
* @param start the start time of the time range for query
|
127
|
* @param end the end time of the time range for query
|
128
|
* @return
|
129
|
* @throws IndexEventLogException
|
130
|
*/
|
131
|
public List<IndexEvent> getEvents(Event action, Identifier pid, Date start, Date end) throws IndexEventLogException {
|
132
|
List<IndexEvent> list = null;
|
133
|
return list;
|
134
|
}
|
135
|
|
136
|
|
137
|
/**
|
138
|
* Get the latest SystemMetadata modification Date of the objects that were built
|
139
|
* the solr index during the previous timed indexing process.
|
140
|
* @return the date. The null will be returned if there is no such date.
|
141
|
* @throws IndexEventLogException
|
142
|
*/
|
143
|
public Date getLastProcessDate() throws IndexEventLogException {
|
144
|
Date date = null;
|
145
|
try {
|
146
|
String dateStr = FileUtils.readFileToString(lastProcessedDateFile, "UTF-8");
|
147
|
if(dateStr != null && !dateStr.trim().equals("")) {
|
148
|
date = format.parse(dateStr);
|
149
|
}
|
150
|
} catch (IOException e) {
|
151
|
throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date :", e);
|
152
|
} catch (ParseException e) {
|
153
|
// TODO Auto-generated catch block
|
154
|
throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date since the content of file is not date :", e);
|
155
|
}
|
156
|
return date;
|
157
|
}
|
158
|
|
159
|
|
160
|
/**
|
161
|
* Set the SystemMetadata modification Date of the objects that were built
|
162
|
* the solr index during the previous timed indexing process.
|
163
|
* @throws IndexEventLogException
|
164
|
*/
|
165
|
public void setLastProcessDate(Date date) throws IndexEventLogException {
|
166
|
String dateStr = format.format(date);
|
167
|
try {
|
168
|
FileUtils.writeStringToFile(lastProcessedDateFile, dateStr, "UTF-8");
|
169
|
} catch (IOException e) {
|
170
|
// TODO Auto-generated catch block
|
171
|
throw new IndexEventLogException("IndexEventFileLog.setLastProcessedDate - couldn't set the last processed date :", e);
|
172
|
}
|
173
|
}
|
174
|
|
175
|
@Override
|
176
|
public void remove(Identifier identifier) throws IndexEventLogException {
|
177
|
// TODO Auto-generated method stub
|
178
|
|
179
|
}
|
180
|
}
|