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
        
103
        String configFileName = null;
104
    	Config hzConfig = null;
105
		try {
106
    		configFileName = Settings.getConfiguration().getString("dataone.hazelcast.configFilePath");
107
    		hzConfig = new FileSystemXmlConfig(configFileName);
108
    	} catch (Exception e) {
109
    		// use default metacat hazelcast.xml file in metacat deployment
110
    		configFileName = 
111
    				Settings.getConfiguration().getString("application.deployDir") +
112
    				"/" +
113
    				Settings.getConfiguration().getString("application.context") + 
114
    				"/WEB-INF/hazelcast.xml";
115
    		try {
116
				hzConfig = new FileSystemXmlConfig(configFileName);
117
			} catch (FileNotFoundException fnfe) {
118
				// TODO: anything we can do here?
119
				fnfe.printStackTrace();
120
			}
121
    	}
122
		
123
        String hzGroupName = hzConfig.getGroupConfig().getName();
124
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
125
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
126

    
127
    	log.info("starting index entry listener...");
128
    	log.info("System Metadata value: " + hzSystemMetadata);
129
    	log.info("Object path value: " + hzObjectPath);
130
    	log.info("Group Name: " + hzGroupName);
131
    	log.info("Group Password: " + "*****"); // don't show value
132
    	log.info("HZ Address: " + hzAddress);
133

    
134
    	// connect to the HZ cluster
135
        ClientConfig cc = new ClientConfig();
136
		cc.getGroupConfig().setName(hzGroupName);
137
		cc.getGroupConfig().setPassword(hzGroupPassword);
138
		cc.addAddress(hzAddress);
139
        try {
140
        	this.hzClient = HazelcastClient.newHazelcastClient(cc);
141
        } catch (Exception e) {
142
            log.error("Unable to create hazelcast client: ", e);
143
            e.printStackTrace();
144
        }
145
        
146
        // get shared structures and add listener
147
        this.systemMetadataMap = this.hzClient.getMap(hzSystemMetadata);
148
        this.objectPathMap = this.hzClient.getMap(hzObjectPath);
149
        this.systemMetadataMap.addEntryListener(this, true);
150

    
151
        log.info("System Metadata size: " + this.systemMetadataMap.size());
152
        log.info("Object path size:" + this.objectPathMap.size());
153
    }
154

    
155
    /**
156
     * Removes this instance as a system metadata map event listener
157
     */
158
    public void stop() {
159
    	log.info("stopping index entry listener...");
160
        this.systemMetadataMap.removeEntryListener(this);
161
    }
162

    
163
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
164
		// use the same implementation for insert/update for now
165
		this.entryUpdated(entryEvent);
166

    
167
	}
168

    
169
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> entryEvent) {
170
		// remove from the index for now, this may be a temporary eviction
171
		this.entryRemoved(entryEvent);
172
		
173
	}
174

    
175
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> entryEvent) {
176
		// remove from the index
177
		Identifier pid = entryEvent.getKey();		
178
		try {
179
			solrIndex.remove(pid.getValue());
180
		} catch (Exception e) {
181
			// TODO: need to track errors, retry later
182
			log.error(e.getMessage(), e);
183
		}
184
		
185
	}
186

    
187
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> entryEvent) {
188
		// add to the index
189
		Identifier pid = entryEvent.getKey();
190
		SystemMetadata systemMetadata = entryEvent.getValue();
191
		
192
		String objectPath = objectPathMap.get(pid);
193
		InputStream data = null;
194
		try {
195
			data = new FileInputStream(objectPath);
196
			solrIndex.insert(pid.getValue(), systemMetadata, data);
197
		} catch (Exception e) {
198
			// TODO: need to track errors, retry later
199
			log.error(e.getMessage(), e);
200
		}
201
	}
202
    
203
}
(4-4/4)