Project

General

Profile

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

    
29
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.InputStream;
32

    
33
import org.apache.commons.logging.Log;
34
import org.apache.commons.logging.LogFactory;
35
import org.dataone.configuration.Settings;
36
import org.dataone.service.types.v1.Identifier;
37
import org.dataone.service.types.v1.SystemMetadata;
38

    
39
import com.hazelcast.client.ClientConfig;
40
import com.hazelcast.client.HazelcastClient;
41
import com.hazelcast.config.Config;
42
import com.hazelcast.config.FileSystemXmlConfig;
43
import com.hazelcast.core.EntryEvent;
44
import com.hazelcast.core.EntryListener;
45
import com.hazelcast.core.IMap;
46

    
47
public class SystemMetadataEventListener implements EntryListener<Identifier, SystemMetadata> {
48
	
49
	private static Log log = LogFactory.getLog(SystemMetadataEventListener.class);
50
	
51
	private SolrIndex solrIndex = null;
52
	
53
	private HazelcastClient hzClient;
54

    
55
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
56
    
57
    private IMap<Identifier, String> objectPathMap;
58
    
59
    /**
60
     * Default constructor - caller needs to initialize manually
61
     * @see setSolrIndex()
62
     * @see start()
63
     */
64
    public SystemMetadataEventListener() {
65
    }
66
    
67
    /**
68
     * Sets the SolrIndex instance and initializes the listener 
69
     * @param solrIndex
70
     */
71
    public SystemMetadataEventListener(SolrIndex solrIndex) {
72
    	this.solrIndex = solrIndex;
73
    	start();
74
    }
75
    
76
    /**
77
     * Get the SolrIndex that this listener communicates with
78
     * @return a SolrIndex instance that indexes the content being listened to
79
     */
80
    public SolrIndex getSolrIndex() {
81
		return solrIndex;
82
	}
83

    
84
    /**
85
     * Set the SolrIndex instance that the listener modifies
86
     * @param solrIndex
87
     */
88
	public void setSolrIndex(SolrIndex solrIndex) {
89
		this.solrIndex = solrIndex;
90
	}
91

    
92
	/**
93
     * Registers this instance as a system metadata map event listener
94
     */
95
    public void start() {
96
    	
97
    	// get config values
98
        String hzSystemMetadata = Settings.getConfiguration().getString(
99
                "dataone.hazelcast.storageCluster.systemMetadataMap");
100
        String hzObjectPath = Settings.getConfiguration().getString(
101
                "dataone.hazelcast.storageCluster.objectPathMap");
102
        String configFileName = Settings.getConfiguration().getString(
103
        		"dataone.hazelcast.configFilePath");;
104
    	Config hzConfig = null;
105
		try {
106
    		hzConfig = new FileSystemXmlConfig(configFileName);
107
    	} catch (FileNotFoundException e) {
108
    		log.error("could not load hazelcast configuration file from: " + configFileName, e);
109
			//e.printStackTrace();
110
    	}
111
		
112
        String hzGroupName = hzConfig.getGroupConfig().getName();
113
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
114
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
115

    
116
    	log.info("starting index entry listener...");
117
    	log.info("System Metadata value: " + hzSystemMetadata);
118
    	log.info("Object path value: " + hzObjectPath);
119
    	log.info("Group Name: " + hzGroupName);
120
    	log.info("Group Password: " + "*****"); // don't show value
121
    	log.info("HZ Address: " + hzAddress);
122

    
123
    	// connect to the HZ cluster
124
        ClientConfig cc = new ClientConfig();
125
		cc.getGroupConfig().setName(hzGroupName);
126
		cc.getGroupConfig().setPassword(hzGroupPassword);
127
		cc.addAddress(hzAddress);
128
        try {
129
        	this.hzClient = HazelcastClient.newHazelcastClient(cc);
130
        } catch (Exception e) {
131
            log.error("Unable to create hazelcast client: ", e);
132
            e.printStackTrace();
133
        }
134
        
135
        // get shared structures and add listener
136
        this.systemMetadataMap = this.hzClient.getMap(hzSystemMetadata);
137
        this.objectPathMap = this.hzClient.getMap(hzObjectPath);
138
        this.systemMetadataMap.addEntryListener(this, true);
139

    
140
        log.info("System Metadata size: " + this.systemMetadataMap.size());
141
        log.info("Object path size:" + this.objectPathMap.size());
142
    }
143

    
144
    /**
145
     * Removes this instance as a system metadata map event listener
146
     */
147
    public void stop() {
148
    	log.info("stopping index entry listener...");
149
        this.systemMetadataMap.removeEntryListener(this);
150
    }
151

    
152
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
153
		// use the same implementation for insert/update for now
154
		this.entryUpdated(entryEvent);
155

    
156
	}
157

    
158
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> entryEvent) {
159
		// remove from the index for now, this may be a temporary eviction
160
		this.entryRemoved(entryEvent);
161
		
162
	}
163

    
164
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> entryEvent) {
165
		// remove from the index
166
		Identifier pid = entryEvent.getKey();		
167
		try {
168
			solrIndex.remove(pid.getValue());
169
		} catch (Exception e) {
170
			// TODO: need to track errors, retry later
171
			log.error(e.getMessage(), e);
172
		}
173
		
174
	}
175

    
176
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> entryEvent) {
177
		// add to the index
178
		Identifier pid = entryEvent.getKey();
179
		SystemMetadata systemMetadata = entryEvent.getValue();
180
		
181
		String objectPath = objectPathMap.get(pid);
182
		InputStream data = null;
183
		try {
184
			data = new FileInputStream(objectPath);
185
			solrIndex.insert(pid.getValue(), systemMetadata, data);
186
		} catch (Exception e) {
187
			// TODO: need to track errors, retry later
188
			log.error(e.getMessage(), e);
189
		}
190
	}
191
    
192
}
(5-5/5)