Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.index;
20

    
21
import java.io.FileInputStream;
22
import java.io.FileNotFoundException;
23
import java.io.InputStream;
24
import java.util.ArrayList;
25
import java.util.Calendar;
26
import java.util.List;
27

    
28
import org.apache.commons.logging.Log;
29
import org.apache.commons.logging.LogFactory;
30
import org.dataone.service.exceptions.ServiceFailure;
31
import org.dataone.service.types.v1.Event;
32
import org.dataone.service.types.v1.Identifier;
33
import org.dataone.service.types.v1.SystemMetadata;
34

    
35
import com.hazelcast.core.IMap;
36
import com.hazelcast.core.ISet;
37
import com.hazelcast.core.ItemEvent;
38
import com.hazelcast.core.ItemListener;
39

    
40
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
41
import edu.ucsb.nceas.metacat.index.event.EventlogFactory;
42

    
43
public class SystemMetadataEventListener implements ItemListener<SystemMetadata> {
44
	
45
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
46
	
47
	private SolrIndex solrIndex = null;
48
	
49
	private ISet<SystemMetadata> source = null;
50
	        
51
    /**
52
     * Default constructor - caller needs to initialize manually
53
     * @see setSolrIndex()
54
     * @see start()
55
     */
56
    public SystemMetadataEventListener() {
57
    }
58
    
59
    /**
60
     * Sets the SolrIndex instance and initializes the listener 
61
     * @param solrIndex
62
     */
63
    public SystemMetadataEventListener(SolrIndex solrIndex) {
64
    	this.solrIndex = solrIndex;
65
    	try {
66
			start();
67
		} catch (Exception e) {
68
			log.error(e.getMessage(), e);
69
		}
70
    }
71
    
72
    /**
73
     * Get the SolrIndex that this listener communicates with
74
     * @return a SolrIndex instance that indexes the content being listened to
75
     */
76
    public SolrIndex getSolrIndex() {
77
		return solrIndex;
78
	}
79

    
80
    /**
81
     * Set the SolrIndex instance that the listener modifies
82
     * @param solrIndex
83
     */
84
	public void setSolrIndex(SolrIndex solrIndex) {
85
		this.solrIndex = solrIndex;
86
	}
87

    
88
	/**
89
     * Registers this instance as a system metadata map event listener
90
	 * @throws ServiceFailure 
91
	 * @throws FileNotFoundException 
92
     */
93
    public void start() throws FileNotFoundException, ServiceFailure {
94
    	
95
        // get shared structures and add listener
96
        IMap<Identifier, String> objectPathMap = DistributedMapsFactory.getObjectPathMap();
97
        ISet<SystemMetadata> indexQueue = DistributedMapsFactory.getIndexQueue();
98
        indexQueue.addItemListener(this, true);
99
        this.source = indexQueue;
100
        log.info("System Metadata size: " + indexQueue.size());
101
        log.info("Object path size:" + objectPathMap.size());
102
    }
103

    
104
    /**
105
     * Removes this instance as a system metadata map event listener
106
     * @throws ServiceFailure 
107
     * @throws FileNotFoundException 
108
     */
109
    public void stop() throws FileNotFoundException, ServiceFailure {
110
    	log.info("stopping index entry listener...");
111
    	DistributedMapsFactory.getIndexQueue().removeItemListener(this);
112
    }
113
    
114
    /**
115
     * Get the obsoletes chain of the specified id. The returned list doesn't include
116
     * the specified id itself. The newer version has the lower index number in the list.
117
     * Empty list will be returned if there is no document to be obsoleted by this id.
118
     * @param id
119
     * @return
120
     * @throws ServiceFailure
121
     * @throws FileNotFoundException 
122
     */
123
    private List<String> getObsoletes(String id) throws FileNotFoundException, ServiceFailure {
124
        List<String> obsoletes = new ArrayList<String>();
125
        while (id != null) {
126
            SystemMetadata metadata = DistributedMapsFactory.getSystemMetadata(id);
127
            id = null;//set it to be null in order to stop the while loop if the id can't be assinged to a new value in the following code.
128
            if(metadata != null) {
129
                Identifier identifier = metadata.getObsoletes();
130
                if(identifier != null && identifier.getValue() != null && !identifier.getValue().trim().equals("")) {
131
                    obsoletes.add(identifier.getValue());
132
                    id = identifier.getValue();
133
                } 
134
            } 
135
        }
136
        return obsoletes;
137
    }
138
    
139

    
140
	public void itemRemoved(ItemEvent<SystemMetadata> entryEvent) {
141
		// do nothing - indexing acts on added objects, even if they need to be deleted
142
	}
143

    
144
	public void itemAdded(ItemEvent<SystemMetadata> entryEvent) {
145
	    //System.out.println("===================================calling entryUpdated method ");
146
	    log.info("===================================calling SystemMetadataEventListener.itemAdded method ");
147
		// add to the index
148
		Identifier pid = entryEvent.getItem().getIdentifier();
149
		//System.out.println("===================================update the document "+pid.getValue());
150
		log.info("===================================adding the document "+pid.getValue());
151
		SystemMetadata systemMetadata = entryEvent.getItem();
152
		
153
		/*if(systemMetadata == null) {
154
		    writeEventLog(systemMetadata, pid, "SystemMetadataEventListener.itemAdded -could not get the SystemMetadata");
155
		    return;
156
		}*/
157
		
158
		// make sure we remove this object so that it can be re-added in the future
159
		if (source != null && systemMetadata != null) {
160
			source.remove(systemMetadata);
161
		}
162
		solrIndex.update(pid, systemMetadata);
163
				
164
		//Identifier obsoletes = systemMetadata.getObsoletes();
165
		//List<String> obsoletesChain = null;
166
		/*if (obsoletes != null) {
167
		    try {
168
				obsoletesChain = getObsoletes(pid.getValue());
169
			} catch (Exception e) {
170
			    String error = "SystemMetadataEventListener.itemAdded -could not look up revision history " + e.getMessage();
171
			    writeEventLog(systemMetadata, pid, error);
172
	            log.error(error, e);
173
	            return;
174
			}
175
		}*/
176
		
177
		/*if(objectPath != null) {
178
		    InputStream data = null;
179
	        try {
180
	            data = new FileInputStream(objectPath);
181
	            solrIndex.update(pid.getValue(), systemMetadata, data);
182
	            EventlogFactory.createIndexEventLog().remove(pid);
183
	        } catch (Exception e) {
184
	            String error = "SystemMetadataEventListener.itemAdded - could not comit the index into the solr server since " + e.getMessage();
185
	            writeEventLog(systemMetadata, pid, error);
186
	            log.error(error, e);
187
	        }
188
		}*/
189
		
190
	}
191
	
192
	
193
    
194
}
(6-6/6)