Project

General

Profile

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

    
28
import java.sql.SQLException;
29
import java.util.Collection;
30
import java.util.Map;
31
import java.util.Set;
32
import java.util.TreeMap;
33

    
34
import org.apache.log4j.Logger;
35
import org.dataone.service.types.v1.Identifier;
36

    
37
import com.hazelcast.core.EntryEvent;
38
import com.hazelcast.core.EntryListener;
39
import com.hazelcast.core.MapLoader;
40
import com.hazelcast.core.MapStore;
41

    
42
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
43

    
44
public class IndexEventEntryListener implements MapStore<Identifier, IndexEvent>, MapLoader<Identifier, IndexEvent>, EntryListener<Identifier, IndexEvent> {
45

    
46
	private Logger logMetacat = Logger.getLogger(IndexEventEntryListener.class);
47

    
48
	/**
49
	 * The map store/loader methods
50
	 */
51
	
52
	@Override
53
	public IndexEvent load(Identifier identifier) {
54
		try {
55
			return IndexEventDAO.getInstance().get(identifier);
56
		} catch (SQLException e) {
57
			logMetacat.error(e.getMessage(), e);
58
		}
59
		return null;
60
	}
61

    
62
	@Override
63
	public Map<Identifier, IndexEvent> loadAll(Collection<Identifier> identifiers) {
64
		Map<Identifier, IndexEvent> eventMap = new TreeMap<Identifier, IndexEvent>();
65
		for (Identifier identifier: identifiers) {
66
			IndexEvent event = null;
67
			try {
68
				event = IndexEventDAO.getInstance().get(identifier);
69
				eventMap.put(identifier, event);
70
			} catch (SQLException e) {
71
				logMetacat.error(e.getMessage(), e);
72
			}
73
		}
74
		return eventMap;
75
	}
76

    
77
	@Override
78
	public Set<Identifier> loadAllKeys() {
79
		try {
80
			return IndexEventDAO.getInstance().getAllIdentifiers();
81
		} catch (SQLException e) {
82
			logMetacat.error(e.getMessage(), e);
83
		}
84
		return null;
85
	}
86

    
87
	@Override
88
	public void delete(Identifier identifier) {
89
		try {
90
			IndexEventDAO.getInstance().remove(identifier);
91
		} catch (SQLException e) {
92
			logMetacat.error(e.getMessage(), e);
93
		}		
94
	}
95

    
96
	@Override
97
	public void deleteAll(Collection<Identifier> identifiers) {
98
		for (Identifier identifier: identifiers) {
99
			try {
100
				IndexEventDAO.getInstance().remove(identifier);
101
			} catch (SQLException e) {
102
				logMetacat.error(e.getMessage(), e);
103
			}
104
		}
105
	}
106

    
107
	@Override
108
	public void store(Identifier identifier, IndexEvent event) {
109
		try {
110
			IndexEventDAO.getInstance().add(event);
111
		} catch (SQLException e) {
112
			logMetacat.error(e.getMessage(), e);
113
		}		
114
	}
115

    
116
	@Override
117
	public void storeAll(Map<Identifier, IndexEvent> indexEventMap) {
118
		for (IndexEvent event: indexEventMap.values()) {
119
			try {
120
				IndexEventDAO.getInstance().add(event);
121
			} catch (SQLException e) {
122
				logMetacat.error(e.getMessage(), e);
123
			}
124
		}
125
	}
126
	
127
	/**
128
	 * The EntryListener methods below
129
	 */
130

    
131
	@Override
132
	public void entryAdded(EntryEvent<Identifier, IndexEvent> event) {
133
		try {
134
			IndexEventDAO.getInstance().add(event.getValue());
135
		} catch (SQLException e) {
136
			logMetacat.error(e.getMessage(), e);
137
		}		
138
	}
139

    
140
	@Override
141
	public void entryEvicted(EntryEvent<Identifier, IndexEvent> arg0) {
142
		// do nothing
143
		
144
	}
145

    
146
	@Override
147
	public void entryRemoved(EntryEvent<Identifier, IndexEvent> event) {
148
		try {
149
			IndexEventDAO.getInstance().remove(event.getKey());
150
		} catch (SQLException e) {
151
			logMetacat.error(e.getMessage(), e);
152
		}		
153
	}
154

    
155
	@Override
156
	public void entryUpdated(EntryEvent<Identifier, IndexEvent> event) {
157
		try {
158
			IndexEventDAO.getInstance().add(event.getValue());
159
		} catch (SQLException e) {
160
			logMetacat.error(e.getMessage(), e);
161
		}		
162
	}
163

    
164
}
(2-2/4)