Project

General

Profile

1 6414 rnahf
package edu.ucsb.nceas.metacat.dataone.hazelcast;
2
3 6708 leinfelder
import java.io.IOException;
4 6414 rnahf
import java.util.Collection;
5 6471 jones
import java.util.Collections;
6
import java.util.HashSet;
7 6414 rnahf
import java.util.Hashtable;
8 6471 jones
import java.util.List;
9 6414 rnahf
import java.util.Map;
10
import java.util.Set;
11
12 6481 rnahf
import org.dataone.service.types.v1.Identifier;
13 6708 leinfelder
import org.jibx.runtime.JiBXException;
14
import org.xml.sax.SAXException;
15 6481 rnahf
16 6414 rnahf
import com.hazelcast.core.MapLoader;
17
18
import edu.ucsb.nceas.metacat.IdentifierManager;
19
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
20 6708 leinfelder
import edu.ucsb.nceas.metacat.McdbException;
21
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
22 6414 rnahf
import edu.ucsb.nceas.metacat.properties.PropertyService;
23 6708 leinfelder
import edu.ucsb.nceas.metacat.replication.ReplicationService;
24
import edu.ucsb.nceas.metacat.shared.HandlerException;
25 6420 rnahf
import edu.ucsb.nceas.metacat.shared.ServiceException;
26 6414 rnahf
import edu.ucsb.nceas.utilities.FileUtil;
27
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
28
29
30
/**
31 6420 rnahf
 * MapLoader implementation for a hazelcast hzObjectPath.  This class is called
32 6440 rnahf
 * when the IMap get methods needs to refresh against the persistent data-store.
33 6420 rnahf
 * The use case for this class is to communicate the filepath between JVMs on
34
 * the same machine, specifically between the metacat instance and the d1_indexer.
35 6414 rnahf
 *
36 6420 rnahf
 * d1_indexer will get Identifiers from elsewhere, but use this class to get
37 6440 rnahf
 * the paths to their associated files.  The getAllKeys() method will
38
 * return null in a live setting, to avoid possibly expensive preloading
39 6420 rnahf
 *
40 6414 rnahf
 * @author rnahf
41
 */
42 6481 rnahf
public class ObjectPathMap implements MapLoader<Identifier, String> {
43 6414 rnahf
	private static IdentifierManager im;
44
	private static String dataPath;
45
	private static String metadataPath;
46 6424 rnahf
47 6414 rnahf
48
	/**
49 6424 rnahf
	 * creates an ObjectPathMap
50 6414 rnahf
	 */
51 6420 rnahf
	public ObjectPathMap() {
52 6440 rnahf
		try {
53
			PropertyService ps = PropertyService.getInstance();
54
			dataPath = PropertyService.getProperty("application.datafilepath");
55
			metadataPath = PropertyService.getProperty("application.documentfilepath");
56
		} catch (PropertyNotFoundException e) {
57
			// TODO Auto-generated catch block
58
			e.printStackTrace();
59
		} catch (ServiceException e) {
60
			// TODO Auto-generated catch block
61
			e.printStackTrace();
62
		}
63 6414 rnahf
		im = IdentifierManager.getInstance();
64
	}
65
66
67
	/*
68
	 * Metadata is stored in a different place on the filesystem than
69
	 * the data.  The doctype value for metadata can vary, but for data
70
	 * is always 'BIN', so using a simple if-then-else to separate
71
	 */
72 6708 leinfelder
	private String pathToDocid(String localid) throws AccessControlException, HandlerException, JiBXException, IOException, McdbException, SAXException
73 6440 rnahf
	{
74 6708 leinfelder
		Hashtable<String, String> ht = ReplicationService.getDocumentInfoMap(localid);
75 6440 rnahf
		if (ht.get("doctype").equals("BIN")) {
76
			return dataPath + FileUtil.getFS() + localid;
77
		} else {
78
			return metadataPath + FileUtil.getFS() + localid;
79
		}
80 6414 rnahf
	}
81
82
83
	/**
84
	 *  Implementation of hazelcast MapLoader interface method.
85 6416 rnahf
	 *  For the provided Identifier (as key), returns the path to the
86
	 *  document on the local filesystem.  Returns null if it can't
87
	 *  create the path.
88 6414 rnahf
	 */
89 6471 jones
	@Override
90 6481 rnahf
	public String load(Identifier key)
91 6415 rnahf
	{
92 6440 rnahf
93
		String docid = null;
94
		String path = null;
95
		try {
96 6481 rnahf
			docid = im.getLocalId(key.getValue());
97 6440 rnahf
			path = pathToDocid(docid);
98 6708 leinfelder
		} catch (Exception e) {
99 6440 rnahf
			// TODO Auto-generated catch block
100
			e.printStackTrace();
101 6414 rnahf
			return null;
102
		}
103 6440 rnahf
		return path;
104 6414 rnahf
	}
105
106
107
	/**
108 6416 rnahf
	 *  Implementation of hazelcast MapLoader interface method.  This method loads
109
	 *  mappings for all Identifiers in the parameters.  Any Identifier not found
110
	 *  is not included in the resulting map.
111 6414 rnahf
	 */
112 6471 jones
	@Override
113 6481 rnahf
	public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) {
114 6414 rnahf
115 6440 rnahf
116 6481 rnahf
		Hashtable<Identifier,String> map = new Hashtable<Identifier,String>();
117
		for (Identifier id : identifiers) {
118 6440 rnahf
			try {
119 6481 rnahf
				String docid = im.getLocalId(id.getValue());
120 6440 rnahf
				map.put(id, pathToDocid(docid));
121
122 6708 leinfelder
			} catch (Exception e) {
123 6440 rnahf
				// TODO should the map load an empty path instead of
124
				// leaving out the entire entry?
125
				e.printStackTrace();
126 6414 rnahf
			}
127
		}
128 6440 rnahf
		return map;
129 6414 rnahf
	}
130
131
132 6415 rnahf
	/**
133 6416 rnahf
	 * Return the full set of guids in the local metacat repository as
134
	 * dataone Identifiers.
135
	 *
136 6440 rnahf
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so will
137
	 * do this to avoid pre-loading a very long list unnecessarily)
138 6415 rnahf
	 */
139 6471 jones
	@Override
140 6481 rnahf
	public Set<Identifier> loadAllKeys()
141 6440 rnahf
	{
142
		return null;
143
144
//		List<String> guids = im.getAllGUIDs();
145
//
146 6481 rnahf
//		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
147 6440 rnahf
//		for (String guid : guids) {
148 6481 rnahf
//			Identifier id = new Identifier();
149
//			id.setValue(guid);
150
//			set.add(id);
151 6440 rnahf
//		}
152
//		return set;
153 6414 rnahf
	}
154
}