|
| 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