Skip to content

Commit 12c8dba

Browse files
committed
Added a new class - LegacyStoreObjManager to handle the Metacat old style storage system.
1 parent 565e45b commit 12c8dba

File tree

5 files changed

+330
-86
lines changed

5 files changed

+330
-86
lines changed

src/main/java/org/dataone/cn/indexer/object/ObjectManager.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.dataone.cn.indexer.object;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.File;
45
import java.io.FileNotFoundException;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import java.security.NoSuchAlgorithmException;
89

910
import org.apache.commons.io.FileUtils;
11+
import org.apache.commons.io.output.ByteArrayOutputStream;
1012
import org.apache.commons.logging.Log;
1113
import org.apache.commons.logging.LogFactory;
1214
import org.dataone.client.auth.AuthTokenSession;
@@ -23,6 +25,7 @@
2325
import org.dataone.service.exceptions.NotFound;
2426
import org.dataone.service.exceptions.NotImplemented;
2527
import org.dataone.service.exceptions.ServiceFailure;
28+
import org.dataone.service.types.v1.Identifier;
2629
import org.dataone.service.types.v1.Session;
2730
import org.dataone.service.types.v2.SystemMetadata;
2831
import org.dataone.service.util.TypeMarshaller;
@@ -75,30 +78,11 @@ public abstract InputStream getSystemMetadataStream(String id)
7578
* @throws MarshallingException
7679
* @throws NoSuchAlgorithmException
7780
*/
78-
public org.dataone.service.types.v1.SystemMetadata getSystemMetadata(String id)
81+
public abstract org.dataone.service.types.v1.SystemMetadata getSystemMetadata(String id)
7982
throws InvalidToken, NotAuthorized, NoSuchAlgorithmException,
8083
NotImplemented, ServiceFailure, NotFound,
8184
InstantiationException, IllegalAccessException,
82-
IOException, MarshallingException {
83-
org.dataone.service.types.v1.SystemMetadata sysmeta = null;
84-
try (InputStream input = getSystemMetadataStream(id)) {
85-
if (input != null) {
86-
try {
87-
SystemMetadata sysmeta2 = TypeMarshaller
88-
.unmarshalTypeFromStream(SystemMetadata.class, input);
89-
sysmeta = sysmeta2;
90-
} catch (Exception e) {
91-
try (InputStream input2 = getSystemMetadataStream(id)) {
92-
if (input2 != null) {
93-
sysmeta = TypeMarshaller.unmarshalTypeFromStream(
94-
org.dataone.service.types.v1.SystemMetadata.class, input2);
95-
}
96-
}
97-
}
98-
}
99-
}
100-
return sysmeta;
101-
}
85+
IOException, MarshallingException;
10286

10387
/**
10488
* Get the input stream of the content of the given pid
@@ -108,10 +92,11 @@ public org.dataone.service.types.v1.SystemMetadata getSystemMetadata(String id)
10892
* @throws FileNotFoundException
10993
* @throws NoSuchAlgorithmException
11094
* @throws IOException
95+
* @throws NotFound
11196
*/
11297
public abstract InputStream getObject(String pid)
11398
throws IllegalArgumentException, FileNotFoundException, NoSuchAlgorithmException,
114-
IOException;
99+
IOException, NotFound;
115100

116101
/**
117102
* Set the d1 node for this object manager.
@@ -129,7 +114,7 @@ public static void setD1Node(MultipartD1Node node) {
129114
protected static void refreshD1Node() throws ServiceFailure {
130115
//get the token
131116
DataONEauthToken = System.getenv(TOKEN_VARIABLE_NAME);
132-
if (DataONEauthToken == null || DataONEauthToken.trim().equals("")) {
117+
if (DataONEauthToken == null || DataONEauthToken.isBlank()) {
133118
//can't get the token from the env variable. So try to get it from a file specified
134119
// in the property
135120
String tokenFilePath = Settings.getConfiguration().getString(TOKEN_FILE_PATH_PROP_NAME);
@@ -144,14 +129,14 @@ protected static void refreshD1Node() throws ServiceFailure {
144129
logger.warn("Can NOT get the authen token from the file " + tokenFilePath +
145130
" since " + e.getMessage());
146131
}
147-
if (DataONEauthToken != null && !DataONEauthToken.trim().equals("")) {
132+
if (DataONEauthToken != null && !DataONEauthToken.isBlank()) {
148133
logger.info("Got the auth token from the file "+ tokenFilePath);
149134
}
150135
}
151136
} else {
152137
logger.info("Got the auth token from an env. variable");
153138
}
154-
if (DataONEauthToken == null || DataONEauthToken.trim().equals("")) {
139+
if (DataONEauthToken == null || DataONEauthToken.isBlank()) {
155140
logger.warn(
156141
"Could NOT get an auth token from either an env. variable or the properties file."
157142
+ " So it will act as the public user.");
@@ -245,4 +230,18 @@ private static Boolean isCN(String nodeStr) {
245230
return isCN;
246231
}
247232

233+
protected SystemMetadata getSystemMetadataByAPI(String id)
234+
throws ServiceFailure, InvalidToken, NotImplemented, NotAuthorized, NotFound {
235+
if (d1Node != null) {
236+
// Metacat can't find the system metadata from the storage system.
237+
// So try to get it from the dataone api
238+
Identifier identifier = new Identifier();
239+
identifier.setValue(id);
240+
return d1Node.getSystemMetadata(session, identifier);
241+
} else {
242+
throw new ServiceFailure("0000", "The d1Node is null and Indexer cannot get the "
243+
+ "systemmetadata by a API call.");
244+
}
245+
}
246+
248247
}

src/main/java/org/dataone/cn/indexer/object/hashstore/HashStoreObjManager.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ public InputStream getSystemMetadataStream(String id) throws InvalidToken, NotAu
6060
if (d1Node != null) {
6161
// Metacat can't find the system metadata from the storage system.
6262
// So try to get it from the dataone api
63-
SystemMetadata sysmeta = null;
64-
Identifier identifier = new Identifier();
65-
identifier.setValue(id);
66-
sysmeta = d1Node.getSystemMetadata(session, identifier);
67-
logger.debug("Finish getting the system metadata via the DataONE API call for the pid "
68-
+ id);
63+
SystemMetadata sysmeta = getSystemMetadataByAPI(id);
64+
logger.debug("Finish getting the system metadata via the DataONE API call for the"
65+
+ " pid " + id);
6966
if (sysmeta != null) {
7067
ByteArrayOutputStream systemMetadataOutputStream = new ByteArrayOutputStream();
7168
TypeMarshaller.marshalTypeToOutputStream(sysmeta, systemMetadataOutputStream);
@@ -80,6 +77,32 @@ public InputStream getSystemMetadataStream(String id) throws InvalidToken, NotAu
8077
return sysmetaInputStream;
8178
}
8279

80+
@Override
81+
public org.dataone.service.types.v1.SystemMetadata getSystemMetadata(String id)
82+
throws InvalidToken, NotAuthorized, NoSuchAlgorithmException,
83+
NotImplemented, ServiceFailure, NotFound,
84+
InstantiationException, IllegalAccessException,
85+
IOException, MarshallingException {
86+
org.dataone.service.types.v1.SystemMetadata sysmeta = null;
87+
try (InputStream input = getSystemMetadataStream(id)) {
88+
if (input != null) {
89+
try {
90+
SystemMetadata sysmeta2 = TypeMarshaller
91+
.unmarshalTypeFromStream(SystemMetadata.class, input);
92+
sysmeta = sysmeta2;
93+
} catch (Exception e) {
94+
try (InputStream input2 = getSystemMetadataStream(id)) {
95+
if (input2 != null) {
96+
sysmeta = TypeMarshaller.unmarshalTypeFromStream(
97+
org.dataone.service.types.v1.SystemMetadata.class, input2);
98+
}
99+
}
100+
}
101+
}
102+
}
103+
return sysmeta;
104+
}
105+
83106
@Override
84107
public InputStream getObject(String pid)
85108
throws IllegalArgumentException, NoSuchAlgorithmException, IOException {
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package org.dataone.cn.indexer.object.legacystore;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileNotFoundException;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.nio.file.FileSystems;
10+
import java.nio.file.Files;
11+
12+
import org.apache.commons.io.output.ByteArrayOutputStream;
13+
import org.apache.log4j.Logger;
14+
import org.dataone.client.v2.formats.ObjectFormatCache;
15+
import org.dataone.cn.indexer.object.ObjectManager;
16+
import org.dataone.configuration.Settings;
17+
import org.dataone.exceptions.MarshallingException;
18+
import org.dataone.service.exceptions.InvalidToken;
19+
import org.dataone.service.exceptions.NotAuthorized;
20+
import org.dataone.service.exceptions.NotFound;
21+
import org.dataone.service.exceptions.NotImplemented;
22+
import org.dataone.service.exceptions.ServiceFailure;
23+
import org.dataone.service.types.v2.ObjectFormat;
24+
import org.dataone.service.types.v2.SystemMetadata;
25+
import org.dataone.service.util.TypeMarshaller;
26+
27+
/**
28+
* The class to get objects and system metadata from Metacat legacy store
29+
* @author Tao
30+
*/
31+
public class LegacyStoreObjManager extends ObjectManager {
32+
33+
private static String dataRootDir =
34+
Settings.getConfiguration().getString("index.data.root.directory");
35+
private static String documentRootDir =
36+
Settings.getConfiguration().getString("index.document.root.directory");
37+
private static Logger logger = Logger.getLogger(LegacyStoreObjManager.class);
38+
39+
private static boolean ifDataAndDocRootSame = false;
40+
41+
/**
42+
* Constructor
43+
* @throws ServiceFailure
44+
*/
45+
public LegacyStoreObjManager() throws ServiceFailure {
46+
if (dataRootDir == null || dataRootDir.trim().equals("")) {
47+
throw new ServiceFailure(
48+
"0000",
49+
"The data root directory specified by the property index.data.root.directory is "
50+
+ "blank in the properties file");
51+
}
52+
if (documentRootDir == null || documentRootDir.trim().equals("")) {
53+
throw new ServiceFailure(
54+
"0000",
55+
"The metadata root directory specified by the property index.document.root"
56+
+ ".directory is blank in the properties file");
57+
}
58+
if (!Files.exists(FileSystems.getDefault().getPath(dataRootDir))) {
59+
throw new ServiceFailure("0000", "The data root directory " + dataRootDir +
60+
" specified in the properties file doesn't exist");
61+
}
62+
if (!Files.exists(FileSystems.getDefault().getPath(documentRootDir))) {
63+
throw new ServiceFailure("0000", "The document root directory " + documentRootDir +
64+
" specified in the properties file doesn't exist");
65+
}
66+
if (!dataRootDir.endsWith("/")) {
67+
dataRootDir = dataRootDir + "/";
68+
}
69+
if (!documentRootDir.endsWith("/")) {
70+
documentRootDir = documentRootDir + "/";
71+
}
72+
73+
if (documentRootDir.equals(dataRootDir)) {
74+
ifDataAndDocRootSame = true;
75+
}
76+
logger.info("ObjectManager.constructor - the root document directory is " +
77+
documentRootDir + " and the root data directory is " + dataRootDir +
78+
" Are they same?" + ifDataAndDocRootSame);
79+
80+
}
81+
82+
83+
@Override
84+
public InputStream getObject(String pid)
85+
throws IllegalArgumentException, IOException, NotFound {
86+
File object = new File(documentRootDir + pid);
87+
if (!object.exists()) {
88+
object = new File(dataRootDir + pid);
89+
if (object.exists()) {
90+
return new FileInputStream(object);
91+
} else {
92+
throw new FileNotFoundException(
93+
"Neither " + documentRootDir + " nor " + dataRootDir + " have the docid "
94+
+ pid);
95+
}
96+
} else {
97+
return new FileInputStream(object);
98+
}
99+
100+
}
101+
102+
/**
103+
* Get the absolute file path for a given relative path. If the relativePath is null or blank,
104+
* null will be returned
105+
* @param relativePath
106+
* @return the absolute file path
107+
* @throws NotFound
108+
*/
109+
private String getFilePath(String relativePath, String objectFormat) throws NotFound {
110+
String absolutePath = null;
111+
if (relativePath != null && !relativePath.isBlank()) {
112+
if (ifDataAndDocRootSame) {
113+
absolutePath = documentRootDir + relativePath;
114+
} else if (objectFormat != null && !objectFormat.isBlank()) {
115+
ObjectFormat format =ObjectFormatCache.getInstance().getFormat(objectFormat);
116+
if (format.getFormatType().equals("METADATA")) {
117+
absolutePath = documentRootDir + relativePath;
118+
} else {
119+
absolutePath = dataRootDir + relativePath;
120+
}
121+
}
122+
}
123+
logger.debug("The absolute file path for the relative file path " +
124+
relativePath + " is " + absolutePath);
125+
return absolutePath;
126+
}
127+
128+
@Override
129+
public org.dataone.service.types.v1.SystemMetadata getSystemMetadata(String id)
130+
throws InvalidToken, NotAuthorized, NotImplemented, ServiceFailure, NotFound {
131+
SystemMetadata sysmeta = null;
132+
long start = System.currentTimeMillis();
133+
try {
134+
for (int i=0; i<5; i++) {
135+
try {
136+
sysmeta = getSystemMetadataByAPI(id);
137+
break;
138+
} catch (ServiceFailure ee) {
139+
logger.warn("The DataONE api call doesn't get the system metadata since "
140+
+ ee.getMessage() + ". This is " + i
141+
+ " try and Indexer will try again.");
142+
try {
143+
Thread.sleep(300);
144+
} catch (InterruptedException ie) {
145+
logger.info("The sleep of the thread was interrupted.");
146+
}
147+
}
148+
}
149+
logger.debug(
150+
"ObjectManager.getSystemMetadata - finish getting the system metadata via the "
151+
+ "DataONE API call for the pid " + id);
152+
} catch (NotAuthorized e) {
153+
logger.info(
154+
"ObjectManager.getSystemMetadata - failed to get the system metadata via the "
155+
+ "DataONE API call for the pid "
156+
+ id + " since it is not authorized. We will refresh the token and try again");
157+
refreshD1Node();
158+
sysmeta = getSystemMetadataByAPI(id);
159+
}
160+
long end = System.currentTimeMillis();
161+
logger.info(
162+
"ObjectManager.getSystemMetadata - finish getting the system metadata via DataONE API"
163+
+ " for the pid "
164+
+ id + " and it took " + (end - start) + "milliseconds");
165+
166+
return sysmeta;
167+
}
168+
169+
@Override
170+
public InputStream getSystemMetadataStream(String id) throws InvalidToken, NotAuthorized,
171+
NotImplemented, ServiceFailure, NotFound, IOException, MarshallingException {
172+
long start = System.currentTimeMillis();
173+
//try to get the system metadata from the storage system first
174+
InputStream sysmetaInputStream = null;
175+
SystemMetadata sysmeta = getSystemMetadataByAPI(id);
176+
logger.debug("Finish getting the system metadata via the DataONE API call for the"
177+
+ " pid " + id);
178+
if (sysmeta != null) {
179+
ByteArrayOutputStream systemMetadataOutputStream = new ByteArrayOutputStream();
180+
TypeMarshaller.marshalTypeToOutputStream(sysmeta, systemMetadataOutputStream);
181+
sysmetaInputStream =
182+
new ByteArrayInputStream(systemMetadataOutputStream.toByteArray());
183+
}
184+
long end = System.currentTimeMillis();
185+
logger.info("Finish getting the system metadata via DataONE API for the pid " + id
186+
+ " and it took " + (end - start) + "milliseconds");
187+
return sysmetaInputStream;
188+
}
189+
190+
191+
}

0 commit comments

Comments
 (0)