Project

General

Profile

« Previous | Next » 

Revision 7550

add first pass at listening for SM events on HZ. Note that the configuration and even the call to solrIndex.insert() are not actually working.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SystemMetadataEventListener.java
26 26
 */
27 27
package edu.ucsb.nceas.metacat.index;
28 28

  
29
public class SystemMetadataEventListener {
29
import java.io.FileInputStream;
30
import java.io.InputStream;
31

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

  
38
import com.hazelcast.client.ClientConfig;
39
import com.hazelcast.client.HazelcastClient;
40
import com.hazelcast.core.EntryEvent;
41
import com.hazelcast.core.EntryListener;
42
import com.hazelcast.core.IMap;
43

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

  
52
    private IMap<Identifier, SystemMetadata> systemMetadataMap;
30 53
    
54
    private IMap<Identifier, String> objectPathMap;
55
    
56
    /**
57
     * Register this instance as a system metadata map event listener.
58
     */
59
    public void start() {
60
    	
61
    	// get config values
62
        String hzSystemMetadata = Settings.getConfiguration().getString(
63
                "dataone.hazelcast.systemMetadata");
64
        String hzObjectPath = Settings.getConfiguration().getString(
65
                "dataone.hazelcast.objectPath");
66
        String hzGroupName = Settings.getConfiguration().getString(
67
                "dataone.hazelcast.groupName");
68
        String hzGroupPassword = Settings.getConfiguration().getString(
69
                "dataone.hazelcast.groupPassword");
70
        String hzAddress = Settings.getConfiguration().getString(
71
                "dataone.hazelcast.address");
72

  
73
    	log.info("starting index entry listener...");
74
    	log.info("System Metadata value: " + hzSystemMetadata);
75
    	log.info("Object path value: " + hzObjectPath);
76
    	log.info("Group Name: " + hzGroupName);
77
    	log.info("Group Password: " + "*****"); // don't show value
78
    	log.info("HZ Address: " + hzAddress);
79

  
80
    	// connect to the HZ cluster
81
        ClientConfig cc = new ClientConfig();
82
		cc.getGroupConfig().setName(hzGroupName);
83
		cc.getGroupConfig().setPassword(hzGroupPassword);
84
		cc.addAddress(hzAddress );
85
        try {
86
        	this.hzClient = HazelcastClient.newHazelcastClient(cc);
87
        } catch (Exception e) {
88
            log.error("Unable to create hazelcast client: ", e);
89
            e.printStackTrace();
90
        }
91
        
92
        // get shared structures and add listener
93
        this.systemMetadataMap = this.hzClient.getMap(hzSystemMetadata);
94
        this.objectPathMap = this.hzClient.getMap(hzObjectPath);
95
        this.systemMetadataMap.addEntryListener(this, true);
96

  
97
        log.info("System Metadata size: " + this.systemMetadataMap.size());
98
        log.info("Object path size:" + this.objectPathMap.size());
99
    }
100

  
101
    /**
102
     * Removes this instance as a system metadata map event listener.
103
     */
104
    public void stop() {
105
    	log.info("stopping index entry listener...");
106
        this.systemMetadataMap.removeEntryListener(this);
107
    }
108

  
109
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
110
		// add to the index
111
		Identifier pid = entryEvent.getKey();
112
		SystemMetadata systemMetadata = entryEvent.getValue();
113
		
114
		String objectPath = objectPathMap.get(pid);
115
		InputStream data = null;
116
		try {
117
			data = new FileInputStream(objectPath);
118
			// TODO: pass the SM object
119
			solrIndex.insert(pid.getValue(), null, data);
120
		} catch (Exception e) {
121
			log.error(e.getMessage(), e);
122
		}
123

  
124
	}
125

  
126
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> entryEvent) {
127
		// TODO Auto-generated method stub
128
		
129
	}
130

  
131
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> entryEvent) {
132
		// TODO Auto-generated method stub
133
		
134
	}
135

  
136
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> entryEvent) {
137
		// TODO Auto-generated method stub
138
		
139
	}
140
    
31 141
}

Also available in: Unified diff