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