Project

General

Profile

« Previous | Next » 

Revision 7800

Added by Jing Tao over 11 years ago

Add a temporary file log for debugging.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/event/EventlogFactory.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class which creates a instance of an IndexEventLog.
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 org.dataone.configuration.Settings;
25

  
26

  
27
/**
28
 *  A class which creates a instance of an IndexEventLog.
29
 *  This class will read the metacat.properties file and create a instance of the EventLog.
30
 * @author tao
31
 *
32
 */
33
public class EventlogFactory {
34
    private static IndexEventLog eventLog = null;
35
    
36
    
37
    /**
38
     * Create an IndexEventLog. This is a singleton object.
39
     * @return
40
     * @throws ClassNotFoundException 
41
     * @throws IllegalAccessException 
42
     * @throws InstantiationException 
43
     */
44
    public static IndexEventLog createIndexEventLog() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
45
        if(eventLog == null) {
46
            String className = Settings.getConfiguration().getString("index.evenlog.classname");
47
            Class<?> classObj = Class.forName(className);
48
            eventLog = (IndexEventLog)classObj.newInstance();
49
        }
50
        return eventLog;
51
    }
52
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/event/IndexEventFileLog.java
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.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.apache.commons.io.IOUtils;
36
import org.dataone.configuration.Settings;
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 File logFile = null;
49
    private long index=1;
50
    
51
    /**
52
     * Constructor. Initialize the log file. The log file locates solr.homeDir. 
53
     * If the solr.homeDir doesn't exists in the metacat.properties, the it will locate the user's home directory.
54
     * @throws IOException 
55
     */
56
    public IndexEventFileLog() throws IOException {
57
        String path = Settings.getConfiguration().getString("solr.homeDir");
58
        if(path == null || path.trim().equals("")) {
59
            path = System.getProperty("user.home");
60
        }
61
        File pathDir = new File(path);
62
        if(!pathDir.exists()) {
63
            pathDir.mkdirs();
64
        }
65
        logFile = new File(pathDir, LOGFILENAME);
66
        if(!logFile.exists()) {
67
            logFile.createNewFile();
68
        }
69
    }
70
    /**
71
     * Write an IndexEvent into a file
72
     * @param event
73
     * @return the serial number for this event
74
     * @throws IndexEventLogException
75
     */
76
    public synchronized long write(IndexEvent event) throws IndexEventLogException {
77
        if(event != null) {
78
            Vector<String> lines = new Vector<String>();
79
            StringBuffer lineBuffer = new StringBuffer();
80
            lineBuffer.append("\""+index+"\""+FIELDSEPERATOR);
81
            int type = event.getType();
82
            lineBuffer.append("\""+type+"\""+FIELDSEPERATOR);
83
            Date date = event.getDate();
84
            if(date != null) {
85
                DateFormat formate = new SimpleDateFormat();
86
                lineBuffer.append("\""+formate.format(date)+"\""+FIELDSEPERATOR);
87
            }
88
            Identifier id = event.getPid();
89
            if(id != null) {
90
                lineBuffer.append("\""+id.getValue()+"\""+FIELDSEPERATOR);
91
            }
92
            String description = event.getDescription();
93
            if(description != null) {
94
                lineBuffer.append("\""+description+"\""+FIELDSEPERATOR);
95
            }
96
            lines.add(lineBuffer.toString());
97
            String lineEnding = null;//null means to use the system default one.
98
            boolean append = true;
99
            try {
100
                FileUtils.writeLines(logFile, "UTF-8", lines, append);
101
                //FileUtils.writeLines(logFile, "UTF-8", lines, lineEnding);
102
                //IOUtils.writeLines(lines, lineEnding, new FileOutputStream(logFile), "UTF-8");
103
            } catch (FileNotFoundException e) {
104
                // TODO Auto-generated catch block
105
                throw new IndexEventLogException(e.getMessage());
106
            } catch (IOException e) {
107
                // TODO Auto-generated catch block
108
                throw new IndexEventLogException(e.getMessage());
109
            }
110
            index++;
111
        }
112
        return index;
113
    }
114
    
115
    
116
    /**
117
     * Gets the list of IndexEvent matching the specified set of filters. The filter parameters can be null
118
     * @param type  the type of the event
119
     * @param pid   the identifier of the data object in the event
120
     * @param start the start time of the time range for query
121
     * @param end   the end time of the time range for query
122
     * @return
123
     * @throws IndexEventLogException
124
     */
125
    public List<IndexEvent> getEvents(int type, Identifier pid, boolean archvied, Date start, Date end) throws IndexEventLogException {
126
        List<IndexEvent> list = null;
127
        return list;
128
    }
129
    
130
    /**
131
     * Set the event with the specified serial number to be archived
132
     * @param serialNumber
133
     */
134
    public void setArchived(long serialNumber) throws IndexEventLogException {
135
        
136
    }
137
    
138
    /**
139
     * Set the all events with the specified identifier to be archived
140
     * @param pid
141
     */
142
    public void setArchived(Identifier pid) throws IndexEventLogException {
143
        
144
    }
145
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrIndex.java
31 31
import java.io.IOException;
32 32
import java.io.InputStream;
33 33
import java.util.ArrayList;
34
import java.util.Calendar;
34 35
import java.util.HashMap;
35 36
import java.util.Iterator;
36 37
import java.util.List;
......
79 80
import org.w3c.dom.NameList;
80 81
import org.xml.sax.SAXException;
81 82

  
83
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
84
import edu.ucsb.nceas.metacat.index.event.IndexEvent;
82 85
import edu.ucsb.nceas.metacat.index.resourcemap.ResourceMapSubprocessor;
83 86

  
84 87
/**
......
392 395
            if(!solrDoc.isEmpty()) {
393 396
                UpdateResponse response = solrServer.add(solrDoc);
394 397
                solrServer.commit();
398
                IndexEvent event = new IndexEvent();
399
                event.setType(IndexEvent.SUCCESSINSERT);
400
                event.setDate(Calendar.getInstance().getTime());
401
                Identifier pid = new Identifier();
402
                pid.setValue(doc.getIdentifier());
403
                event.setPid(pid);
404
                event.setDescription("Successfully insert the solr index for the id "+pid.getValue());
405
                try {
406
                    EventlogFactory.createIndexEventLog().write(event);
407
                } catch (Exception e) {
408
                    log.error("SolrIndex.insertToIndex - IndexEventLog can't insert the solr doc to the solr server :"+e.getMessage());
409
                }
410
                
395 411
                //System.out.println("=================the response is:\n"+response.toString());
396 412
            }
397 413
        }
......
590 606
        if(pid != null && !pid.trim().equals("")) {
591 607
            solrServer.deleteById(pid);
592 608
            solrServer.commit();
609
            IndexEvent event = new IndexEvent();
610
            event.setType(IndexEvent.SUCCESSINSERT);
611
            event.setDate(Calendar.getInstance().getTime());
612
            Identifier identifier = new Identifier();
613
            identifier.setValue(pid);
614
            event.setPid(identifier);
615
            event.setDescription("Successfully remove the solr index for the id "+identifier.getValue());
616
            try {
617
                EventlogFactory.createIndexEventLog().write(event);
618
            } catch (Exception e) {
619
                log.error("SolrIndex.insertToIndex - IndexEventLog can't insert the solr doc to the solr server :"+e.getMessage());
620
            }
593 621
        }
594 622
    }
595 623

  

Also available in: Unified diff