Project

General

Profile

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.FileOutputStream;
27
import java.io.IOException;
28
import java.text.DateFormat;
29
import java.text.ParseException;
30
import java.text.SimpleDateFormat;
31
import java.util.Date;
32
import java.util.List;
33
import java.util.Vector;
34

    
35
import org.apache.commons.io.FileUtils;
36
import org.apache.commons.io.IOUtils;
37
import org.dataone.configuration.Settings;
38
import org.dataone.service.types.v1.Identifier;
39

    
40

    
41
/**
42
 * A class represents a file log for the index events.
43
 * @author tao
44
 *
45
 */
46
public class IndexEventFileLog implements IndexEventLog {
47
    private static final String FIELDSEPERATOR = " ";
48
    private static final String LOGFILENAME = "solr-index.log";
49
    private static final String LASTPROCESSEDDATEFILENAME = "solr-last-proccessed-date";
50
    private File logFile = null;
51
    private File lastProcessedDateFile = null;
52
    private long index=1;
53
    private SimpleDateFormat format = new SimpleDateFormat();
54
    
55
    /**
56
     * Constructor. Initialize the log file. The log file locates solr.homeDir. 
57
     * If the solr.homeDir doesn't exists in the metacat.properties, the it will locate the user's home directory.
58
     * @throws IOException 
59
     */
60
    public IndexEventFileLog() throws IOException {
61
        String path = Settings.getConfiguration().getString("solr.homeDir");
62
        if(path == null || path.trim().equals("")) {
63
            path = System.getProperty("user.home");
64
        }
65
        File pathDir = new File(path);
66
        if(!pathDir.exists()) {
67
            pathDir.mkdirs();
68
        }
69
        logFile = new File(pathDir, LOGFILENAME);
70
        if(!logFile.exists()) {
71
            logFile.createNewFile();
72
        }
73
        lastProcessedDateFile = new File(pathDir, LASTPROCESSEDDATEFILENAME);
74
        if(!lastProcessedDateFile.exists()) {
75
            lastProcessedDateFile.createNewFile();
76
        }
77
    }
78
    /**
79
     * Write an IndexEvent into a file
80
     * @param event
81
     * @return the serial number for this event
82
     * @throws IndexEventLogException
83
     */
84
    public synchronized long write(IndexEvent event) throws IndexEventLogException {
85
        if(event != null) {
86
            Vector<String> lines = new Vector<String>();
87
            StringBuffer lineBuffer = new StringBuffer();
88
            lineBuffer.append("\""+index+"\""+FIELDSEPERATOR);
89
            int type = event.getType();
90
            lineBuffer.append("\""+type+"\""+FIELDSEPERATOR);
91
            Date date = event.getDate();
92
            if(date != null) {
93
                DateFormat formate = new SimpleDateFormat();
94
                lineBuffer.append("\""+formate.format(date)+"\""+FIELDSEPERATOR);
95
            }
96
            Identifier id = event.getPid();
97
            if(id != null) {
98
                lineBuffer.append("\""+id.getValue()+"\""+FIELDSEPERATOR);
99
            }
100
            String description = event.getDescription();
101
            if(description != null) {
102
                lineBuffer.append("\""+description+"\""+FIELDSEPERATOR);
103
            }
104
            lines.add(lineBuffer.toString());
105
            String lineEnding = null;//null means to use the system default one.
106
            boolean append = true;
107
            try {
108
                FileUtils.writeLines(logFile, "UTF-8", lines, append);
109
                //FileUtils.writeLines(logFile, "UTF-8", lines, lineEnding);
110
                //IOUtils.writeLines(lines, lineEnding, new FileOutputStream(logFile), "UTF-8");
111
            } catch (FileNotFoundException e) {
112
                // TODO Auto-generated catch block
113
                throw new IndexEventLogException(e.getMessage());
114
            } catch (IOException e) {
115
                // TODO Auto-generated catch block
116
                throw new IndexEventLogException(e.getMessage());
117
            }
118
            index++;
119
        }
120
        return index;
121
    }
122
    
123
    
124
    /**
125
     * Gets the list of IndexEvent matching the specified set of filters. The filter parameters can be null
126
     * @param type  the type of the event
127
     * @param pid   the identifier of the data object in the event
128
     * @param start the start time of the time range for query
129
     * @param end   the end time of the time range for query
130
     * @return
131
     * @throws IndexEventLogException
132
     */
133
    public List<IndexEvent> getEvents(int type, Identifier pid, Date start, Date end) throws IndexEventLogException {
134
        List<IndexEvent> list = null;
135
        return list;
136
    }
137
    
138
   
139
    /**
140
     * Get the list of identifiers which were failed to build the solr index since the 
141
     * previous timed indexing (including the ones in the timed indexing).
142
     * @return the list of failure identifiers. The null will be returned if no failure. 
143
     */
144
    public List<String> getFailedPids() throws IndexEventLogException {
145
        return null;
146
    }
147
    
148
    
149
    /**
150
     * Get the latest SystemMetadata modification Date of the objects that were built
151
     * the solr index during the previous timed indexing process.
152
     * @return the date. The null will be returned if there is no such date.
153
     * @throws IndexEventLogException
154
     */
155
    public Date getLastProcessDate() throws IndexEventLogException {
156
        Date date = null;
157
        try {
158
            String dateStr = FileUtils.readFileToString(lastProcessedDateFile, "UTF-8");
159
            if(dateStr != null && !dateStr.trim().equals("")) {
160
                date = format.parse(dateStr);
161
            }
162
        } catch (IOException e) {
163
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date :", e);
164
        } catch (ParseException e) {
165
            // TODO Auto-generated catch block
166
            throw new IndexEventLogException("IndexEventFileLog.getLastProcessedDate - couldn't read the last processed date since the content of file is not date :", e);
167
        }
168
        return date;
169
    }
170
    
171
    
172
    /**
173
     * Set the SystemMetadata modification Date of the objects that were built
174
     * the solr index during the previous timed indexing process.
175
     * @throws IndexEventLogException
176
     */
177
    public void setLastProcessDate(Date date) throws IndexEventLogException {
178
        String dateStr = format.format(date);
179
        try {
180
            FileUtils.writeStringToFile(lastProcessedDateFile, dateStr, "UTF-8");
181
        } catch (IOException e) {
182
            // TODO Auto-generated catch block
183
           throw new IndexEventLogException("IndexEventFileLog.setLastProcessedDate - couldn't set the last processed date :", e);
184
        }
185
    }
186
}
(3-3/5)