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
    				Settings.getConfiguration().getString("application.context") + 
113
    				"/WEB-INF/hazelcast.xml";
114
    		try {
115
				hzConfig = new FileSystemXmlConfig(configFileName);
116
			} catch (FileNotFoundException fnfe) {
117
				// TODO: anything we can do here?
118
				fnfe.printStackTrace();
119
			}
120
    	}
121
		
122
        String hzGroupName = hzConfig.getGroupConfig().getName();
123
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
124
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
125

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

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

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

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

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

    
166
	}
167

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

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

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