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 7319 cjones
import org.apache.log4j.Logger;
13 6481 rnahf
import org.dataone.service.types.v1.Identifier;
14 6708 leinfelder
import org.jibx.runtime.JiBXException;
15
import org.xml.sax.SAXException;
16 6481 rnahf
17 6414 rnahf
import com.hazelcast.core.MapLoader;
18
19
import edu.ucsb.nceas.metacat.IdentifierManager;
20
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
21 6708 leinfelder
import edu.ucsb.nceas.metacat.McdbException;
22
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
23 6414 rnahf
import edu.ucsb.nceas.metacat.properties.PropertyService;
24 6708 leinfelder
import edu.ucsb.nceas.metacat.replication.ReplicationService;
25
import edu.ucsb.nceas.metacat.shared.HandlerException;
26 6420 rnahf
import edu.ucsb.nceas.metacat.shared.ServiceException;
27 6414 rnahf
import edu.ucsb.nceas.utilities.FileUtil;
28
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
29
30
31
/**
32 6420 rnahf
 * MapLoader implementation for a hazelcast hzObjectPath.  This class is called
33 6440 rnahf
 * when the IMap get methods needs to refresh against the persistent data-store.
34 6420 rnahf
 * 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 6414 rnahf
 *
37 6420 rnahf
 * d1_indexer will get Identifiers from elsewhere, but use this class to get
38 6440 rnahf
 * the paths to their associated files.  The getAllKeys() method will
39
 * return null in a live setting, to avoid possibly expensive preloading
40 6420 rnahf
 *
41 6414 rnahf
 * @author rnahf
42
 */
43 6481 rnahf
public class ObjectPathMap implements MapLoader<Identifier, String> {
44 6414 rnahf
	private static IdentifierManager im;
45
	private static String dataPath;
46
	private static String metadataPath;
47 7319 cjones
  private Logger logMetacat = Logger.getLogger(ObjectPathMap.class);
48
49 6424 rnahf
50 6414 rnahf
	/**
51 6424 rnahf
	 * creates an ObjectPathMap
52 6414 rnahf
	 */
53 6420 rnahf
	public ObjectPathMap() {
54 6440 rnahf
		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 6414 rnahf
		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 6708 leinfelder
	private String pathToDocid(String localid) throws AccessControlException, HandlerException, JiBXException, IOException, McdbException, SAXException
75 6440 rnahf
	{
76 6708 leinfelder
		Hashtable<String, String> ht = ReplicationService.getDocumentInfoMap(localid);
77 6440 rnahf
		if (ht.get("doctype").equals("BIN")) {
78
			return dataPath + FileUtil.getFS() + localid;
79
		} else {
80
			return metadataPath + FileUtil.getFS() + localid;
81
		}
82 6414 rnahf
	}
83
84
85
	/**
86
	 *  Implementation of hazelcast MapLoader interface method.
87 6416 rnahf
	 *  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 6414 rnahf
	 */
91 6471 jones
	@Override
92 6481 rnahf
	public String load(Identifier key)
93 6415 rnahf
	{
94 6440 rnahf
95
		String docid = null;
96
		String path = null;
97
		try {
98 6481 rnahf
			docid = im.getLocalId(key.getValue());
99 6440 rnahf
			path = pathToDocid(docid);
100 6708 leinfelder
		} catch (Exception e) {
101 7319 cjones
			if (logMetacat.isDebugEnabled()) {
102
        e.printStackTrace();
103
    }
104
            return null;
105 6414 rnahf
		}
106 6440 rnahf
		return path;
107 6414 rnahf
	}
108
109
110
	/**
111 6416 rnahf
	 *  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 6414 rnahf
	 */
115 6471 jones
	@Override
116 6481 rnahf
	public Map<Identifier, String> loadAll(Collection<Identifier> identifiers) {
117 6414 rnahf
118 6440 rnahf
119 6481 rnahf
		Hashtable<Identifier,String> map = new Hashtable<Identifier,String>();
120
		for (Identifier id : identifiers) {
121 6440 rnahf
			try {
122 6481 rnahf
				String docid = im.getLocalId(id.getValue());
123 6440 rnahf
				map.put(id, pathToDocid(docid));
124
125 6708 leinfelder
			} catch (Exception e) {
126 7319 cjones
		      if (logMetacat.isDebugEnabled()) {
127
		          e.printStackTrace();
128
		      }
129 6414 rnahf
			}
130
		}
131 6440 rnahf
		return map;
132 6414 rnahf
	}
133
134
135 6415 rnahf
	/**
136 6416 rnahf
	 * Return the full set of guids in the local metacat repository as
137
	 * dataone Identifiers.
138
	 *
139 6440 rnahf
	 * (Hazelcast allows avoiding pre-loading by returning NULL, so will
140
	 * do this to avoid pre-loading a very long list unnecessarily)
141 6415 rnahf
	 */
142 6471 jones
	@Override
143 6481 rnahf
	public Set<Identifier> loadAllKeys()
144 6440 rnahf
	{
145
		return null;
146
147
//		List<String> guids = im.getAllGUIDs();
148
//
149 6481 rnahf
//		Set<Identifier> set = Collections.synchronizedSet(new HashSet<Identifier>());
150 6440 rnahf
//		for (String guid : guids) {
151 6481 rnahf
//			Identifier id = new Identifier();
152
//			id.setValue(guid);
153
//			set.add(id);
154 6440 rnahf
//		}
155
//		return set;
156 6414 rnahf
	}
157
}