|
| 1 | +package gov.nasa.jpl.edrn.labcas; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +import org.apache.commons.lang.WordUtils; |
| 12 | +import org.apache.oodt.cas.filemgr.structs.ProductType; |
| 13 | +import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient; |
| 14 | +import org.apache.oodt.cas.filemgr.util.XmlStructFactory; |
| 15 | +import org.apache.oodt.cas.metadata.Metadata; |
| 16 | +import org.apache.oodt.cas.metadata.SerializableMetadata; |
| 17 | +import gov.nasa.jpl.edrn.labcas.Constants; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class that contains common functionality to interact with the FileManager |
| 21 | + * both through its XML/RPC interface (for dynamic updates) |
| 22 | + * and its XML repository (for persisting the updates to XML). |
| 23 | + * |
| 24 | + * @author luca |
| 25 | + * |
| 26 | + */ |
| 27 | +public class FileManagerUtils { |
| 28 | + |
| 29 | + // default value for FileManager URL |
| 30 | + private static String FILEMANAGER_URL = "http://localhost:9000/"; |
| 31 | + static { |
| 32 | + if (System.getenv(Constants.ENV_FILEMGR_URL)!=null) { |
| 33 | + FILEMANAGER_URL = System.getenv(Constants.ENV_FILEMGR_URL); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static final Logger LOG = Logger.getLogger(FileManagerUtils.class.getName()); |
| 38 | + |
| 39 | + /** |
| 40 | + * Method to add a new dataset, or update an existing dataset, into the File Manager. |
| 41 | + * |
| 42 | + * @param dataset |
| 43 | + */ |
| 44 | + public static void updateDataset(String dataset) throws Exception { |
| 45 | + |
| 46 | + XmlRpcFileManagerClient client = new XmlRpcFileManagerClient(new URL(FILEMANAGER_URL)); |
| 47 | + |
| 48 | + // build product type from dataset |
| 49 | + String productTypeName = FileManagerUtils.getProductTypeName(dataset); |
| 50 | + |
| 51 | + // retrieve additional dataset metadata from file DatasetMetadata.xml |
| 52 | + Metadata datasetMetadata = FileManagerUtils.readDatasetMetadata( dataset ); |
| 53 | + |
| 54 | + |
| 55 | + // transfer metadata field 'Description' to dataset description |
| 56 | + String datasetDescription = dataset; // default |
| 57 | + if (datasetMetadata.containsKey(Constants.METADATA_KEY_DESCRIPTION)) { |
| 58 | + datasetDescription = datasetMetadata.getMetadata(Constants.METADATA_KEY_DESCRIPTION); |
| 59 | + datasetMetadata.removeMetadata(Constants.METADATA_KEY_DESCRIPTION); |
| 60 | + } |
| 61 | + |
| 62 | + // query File Manager for product type object |
| 63 | + ProductType productType = client.getProductTypeByName(productTypeName); |
| 64 | + LOG.info("Retrieved product type id="+productType.getProductTypeId()+" from File Manager"); |
| 65 | + |
| 66 | + // update the product type object |
| 67 | + //productType.setDescription(datasetDescription); |
| 68 | + |
| 69 | + // loop over all metadata keys, values found in DatasetMetadata.xml |
| 70 | + Metadata typeMetadata = productType.getTypeMetadata(); |
| 71 | + for (String key : datasetMetadata.getAllKeys()) { |
| 72 | + LOG.info("Updating key="+key); |
| 73 | + |
| 74 | + // remove this metadata key |
| 75 | + typeMetadata.removeMetadata(key); |
| 76 | + |
| 77 | + // insert all new (non-empty) values, overriding old values |
| 78 | + List<String> vals = new ArrayList<String>(); |
| 79 | + for (String value : datasetMetadata.getAllMetadata(key)) { |
| 80 | + if (value.trim().length()>0) { |
| 81 | + LOG.info("Adding value="+value+" to key="+key); |
| 82 | + typeMetadata.addMetadata(key, value); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + // write the updated product type object to XML |
| 89 | + final List<ProductType> producTypes = Arrays.asList( new ProductType[] { productType }); |
| 90 | + for (ProductType pt : producTypes) { |
| 91 | + LOG.info("Updating product type="+pt.getName()); |
| 92 | + } |
| 93 | + File productTypesXmlFile = new File(FileManagerUtils.getDatasetDir(dataset), "/policy/product-types.xml"); |
| 94 | + XmlStructFactory.writeProductTypeXmlDocument(producTypes, productTypesXmlFile.getAbsolutePath()); |
| 95 | + LOG.info("Written XML file="+ productTypesXmlFile.getAbsolutePath()); |
| 96 | + |
| 97 | + // send updated object to the File Manager |
| 98 | + String productTypeId = client.addProductType(productType); |
| 99 | + LOG.info("Updated product type="+productTypeId); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Utility method that reads the additional dataset metadata |
| 106 | + * from the file DatasetMetadata.xml located in the dataset staging directory. |
| 107 | + * |
| 108 | + * @param datasetName |
| 109 | + * @return |
| 110 | + * @throws IOException |
| 111 | + */ |
| 112 | + private static Metadata readDatasetMetadata(final String datasetName) { |
| 113 | + |
| 114 | + String stagingDir = System.getenv(Constants.ENV_LABCAS_STAGING) + "/" + datasetName; |
| 115 | + File datasetMetadataFile = new File(stagingDir, Constants.METADATA_FILE); |
| 116 | + |
| 117 | + // read input metadata |
| 118 | + Metadata metadata = new Metadata(); // empty metadata container |
| 119 | + if (datasetMetadataFile.exists()) { |
| 120 | + LOG.info("Updating metadata from file: "+datasetMetadataFile.getAbsolutePath()); |
| 121 | + |
| 122 | + try { |
| 123 | + SerializableMetadata sm = new SerializableMetadata("UTF-8", false); |
| 124 | + sm.loadMetadataFromXmlStream(datasetMetadataFile.toURI().toURL().openStream()); |
| 125 | + metadata = sm.getMetadata(); |
| 126 | + for (String key : metadata.getAllKeys()) { |
| 127 | + for (String val : metadata.getAllMetadata(key)) { |
| 128 | + LOG.fine("\t==> Read dataset metadata key=["+key+"] value=["+val+"]"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + } catch (Exception e) { |
| 133 | + LOG.warning(e.getMessage()); |
| 134 | + } |
| 135 | + |
| 136 | + } else { |
| 137 | + LOG.warning("Metadata file: "+datasetMetadataFile.getAbsolutePath()+" not found"); |
| 138 | + } |
| 139 | + |
| 140 | + return metadata; |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Retrieves the directory where an uploaded dataset will be archived. |
| 146 | + * |
| 147 | + * @param datasetName |
| 148 | + * @return |
| 149 | + */ |
| 150 | + private static File getDatasetDir(final String datasetName) { |
| 151 | + |
| 152 | + String archiveDir = System.getenv(Constants.ENV_LABCAS_ARCHIVE) + "/" + Constants.WORKFLOW_LABCAS_UPOLOAD; |
| 153 | + File datasetDir = new File(archiveDir, datasetName); |
| 154 | + return datasetDir; |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Constructs the product type name from a dataset identifier. |
| 160 | + * @param dataset |
| 161 | + * @return |
| 162 | + */ |
| 163 | + private static String getProductTypeName(String dataset) { |
| 164 | + |
| 165 | + String productType = WordUtils.capitalize(dataset).replaceAll("\\s+", "_"); |
| 166 | + return productType; |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments