001/* 002 * Copyright (c) 2004-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-08-24 22:47:39 +0000 (Mon, 24 Aug 2015) $' 007 * '$Revision: 33633 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.srb; 031 032import edu.sdsc.grid.io.GeneralFile; 033import edu.sdsc.grid.io.MetaDataCondition; 034import edu.sdsc.grid.io.MetaDataRecordList; 035import edu.sdsc.grid.io.MetaDataSelect; 036import edu.sdsc.grid.io.MetaDataSet; 037import edu.sdsc.grid.io.MetaDataTable; 038import edu.sdsc.grid.io.local.LocalFile; 039import edu.sdsc.grid.io.srb.SRBFile; 040import edu.sdsc.grid.io.srb.SRBFileSystem; 041import edu.sdsc.grid.io.srb.SRBMetaDataSet; 042import ptolemy.actor.NoTokenException; 043import ptolemy.actor.TypedAtomicActor; 044import ptolemy.actor.TypedIOPort; 045import ptolemy.data.BooleanToken; 046import ptolemy.data.ObjectToken; 047import ptolemy.data.StringToken; 048import ptolemy.data.expr.Parameter; 049import ptolemy.data.type.BaseType; 050import ptolemy.gui.GraphicalMessageHandler; 051import ptolemy.kernel.CompositeEntity; 052import ptolemy.kernel.util.Attribute; 053import ptolemy.kernel.util.IllegalActionException; 054import ptolemy.kernel.util.NameDuplicationException; 055 056////////////////////////////////////////////////////////////////////////// 057//// SRBGetMD 058/** 059 * <p> 060 * SRBGetMD is a Kepler Actor which gets user defined metadeta for an SRB 061 * dataset or collection. 062 * 063 * The following actor expects as input a reference to the SRB file system. This 064 * reference connection is obtained via the SRBConnect Actor in Kepler. <i>See 065 * SRBConnect and its documentation.</i> 066 * </p> 067 * <p> 068 * The file reference system is created with a unique SRB user account and with 069 * this connection reference as input the SRBGetMD actor is able to gain access 070 * to the SRB file space. Once an alive SRB file connection system has been 071 * established the actor gets the remode SRB file/directory and access the 072 * appropriate metadata via jargon API methods.If the recursive option is true, 073 * then recursively get the metadata for all sub directories of the files. 074 * </p> 075 * <p> 076 * <B>Actor Input:</B> Accepts a reference to the SRB files system, an SRB 077 * remote file/directory path 078 * </p> 079 * <p> 080 * <B>Actor Output:</B> Outputs the String representation of the SRB 081 * dataset/collection's metadata.h 082 * 083 * </p> 084 * <p> 085 * The following actor accesses SRB file reference system and SRB file space 086 * with the SRB Jargon API provided. The JARGON is a pure API for developing 087 * programs with a data grid interface and I/O for SRB file systems. 088 * </p> 089 * <A href="http://www.sdsc.edu/srb"><I>Further information on SRB</I> </A> 090 * 091 * @author Efrat Jaeger 092 * @version $Id: SRBGetMD.java 33633 2015-08-24 22:47:39Z crawl $ 093 * @category.name srb 094 * @category.name put 095 */ 096public class SRBGetMD extends TypedAtomicActor { 097 098 /** 099 * Construct a constant source with the given container and name. Create the 100 * <i>value</i> parameter, initialize its value to the default value of an 101 * IntToken with value 1. 102 * 103 * @param container 104 * The container. 105 * @param name 106 * The name of this actor. 107 * @exception IllegalActionException 108 * If the entity cannot be contained by the proposed 109 * container. 110 * @exception NameDuplicationException 111 * If the container already has an actor with this name. 112 */ 113 public SRBGetMD(CompositeEntity container, String name) 114 throws NameDuplicationException, IllegalActionException { 115 super(container, name); 116 117 SRBFileSystem = new TypedIOPort(this, "SRBFileSystem", true, false); 118 SRBFileSystem.setTypeEquals(BaseType.GENERAL); 119 new Attribute(SRBFileSystem, "_showName"); 120 121 srbFilePath = new TypedIOPort(this, "srbFilePath", true, false); 122 srbFilePath.setTypeEquals(BaseType.STRING); // or should it be an array 123 // of strings. 124 new Attribute(srbFilePath, "_showName"); 125 126 metadata = new TypedIOPort(this, "metadata", false, true); 127 metadata.setTypeEquals(BaseType.STRING); 128 new Attribute(metadata, "_showName"); 129 130 /* 131 * exitCode = new TypedIOPort(this, "exitCode", false, true); 132 * exitCode.setTypeEquals(BaseType.STRING); new Attribute(exitCode, 133 * "_showName"); 134 */ 135 recursive = new Parameter(this, "recursive", new BooleanToken(false)); 136 recursive.setTypeEquals(BaseType.BOOLEAN); 137 138 _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" " 139 + "width=\"150\" height=\"40\" " + "style=\"fill:white\"/>\n" 140 + "<text x=\"7\" y=\"30\"" 141 + "style=\"font-size:12; fill:black; font-family:SansSerif\">" 142 + "SRB$</text>\n" + "<text x=\"41\" y=\"31\"" 143 + "style=\"font-size:16; fill:blue; font-family:SansSerif\">" 144 + "Get MetaData</text>\n" + "</svg>\n"); 145 } 146 147 /** 148 * pointer to the SRB file system. 149 */ 150 public TypedIOPort SRBFileSystem; 151 /** 152 * Path to SRB file. 153 */ 154 public TypedIOPort srbFilePath; 155 156 /** 157 * srb file's metadata 158 */ 159 public TypedIOPort metadata; 160 // public TypedIOPort exitCode; 161 162 /** 163 * -r ; Returns metadata recursively for directories. 164 */ 165 public Parameter recursive; 166 167 // ///////////////////////////////////////////////////////////////// 168 // // public methods //// 169 /** 170 * Get the user defined metadata for SRB file paths. If the recursive flag 171 * is true, than recursively get the metadata for all sub directories of the 172 * files. 173 * 174 * @exception IllegalActionException 175 * If it is thrown if the SRB file cannot be accessed or the 176 * current directory cannot be broadcasted. 177 */ 178 public void fire() throws IllegalActionException { 179 180 LocalFile localFile; 181 String localFilePath; 182 String _exitCode = ""; 183 184 try { 185 // make sure there is an alive connection. 186 try { 187 srbFileSystem.getHost(); 188 } catch (Exception ex) { // connection was closed. 189 srbFileSystem = null; 190 ObjectToken SRBConOT = null; 191 try { // try to get a new connection in case the previous one 192 // has terminated. 193 SRBConOT = (ObjectToken) SRBFileSystem.get(0); 194 } catch (NoTokenException ntex) { 195 } 196 if (SRBConOT != null) { 197 srbFileSystem = (SRBFileSystem) SRBConOT.getValue(); 198 } 199 } 200 if (srbFileSystem == null) { 201 throw new IllegalActionException(this, 202 "No SRB connection available in actor " 203 + this.getName() + "."); 204 } 205 206 String srbFileStr = ((StringToken) srbFilePath.get(0)) 207 .stringValue(); 208 srbFile = new SRBFile(srbFileSystem, srbFileStr); 209 210 boolean r = ((BooleanToken) recursive.getToken()).booleanValue(); 211 212 MetaDataRecordList[] record = null; 213 if (srbFile.exists()) { 214 if (r || !srbFile.isDirectory()) { 215 MetaDataCondition conditions[] = { MetaDataSet 216 .newCondition(SRBMetaDataSet.USER_GROUP_NAME, 217 MetaDataCondition.LIKE, srbFileSystem 218 .getUserName()), }; 219 220 String[] selectFieldNames; 221 222 if (srbFile.isDirectory()) { 223 selectFieldNames = new String[2]; 224 selectFieldNames[0] = SRBMetaDataSet.DEFINABLE_METADATA_FOR_FILES; 225 selectFieldNames[1] = SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES; 226 } else { // if it's a file. 227 selectFieldNames = new String[1]; 228 selectFieldNames[0] = SRBMetaDataSet.DEFINABLE_METADATA_FOR_FILES; 229 } 230 MetaDataSelect selects[] = MetaDataSet 231 .newSelection(selectFieldNames); 232 233 record = ((GeneralFile) srbFile).query(conditions, selects); 234 } else { // get data for a directory non-recursively. 235 MetaDataCondition[] conditions = { MetaDataSet 236 .newCondition(SRBMetaDataSet.DIRECTORY_NAME, 237 MetaDataCondition.EQUAL, srbFile 238 .getAbsolutePath()) }; 239 String[] selectFieldNames = { SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES }; 240 241 MetaDataSelect selects[] = MetaDataSet 242 .newSelection(selectFieldNames); 243 244 record = srbFileSystem.query(conditions, selects); 245 if (record.length > 1) { 246 // looking for the record that holds the metadata. 247 for (int i = 0; i < record.length; i++) { 248 int mdInd = record[i] 249 .getFieldIndex(SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES); 250 if (mdInd > -1) { 251 MetaDataRecordList res = record[i]; 252 record = new MetaDataRecordList[1]; 253 record[0] = res; 254 break; 255 } 256 } 257 } 258 259 } 260 if (record == null) 261 System.out.println("no metadata"); 262 _sendResults(record, srbFile); 263 264 } else 265 GraphicalMessageHandler.error(srbFile.getAbsolutePath() 266 + "does not exist."); 267 268 // FIXME: what should be returned here.. 269 if (_exitCode.equals("")) { 270 _exitCode = "success"; 271 } 272 // exitCode.broadcast(new StringToken(_exitCode)); 273 } catch (Exception ex) { 274 srbFileSystem = SRBUtil.closeConnection(srbFileSystem); 275 ex.printStackTrace(); 276 throw new IllegalActionException(this, ex.getMessage() 277 + ". in actor " + this.getName()); 278 } 279 } 280 281 /** 282 * Initialize the srb file system to null. 283 */ 284 public void initialize() throws IllegalActionException { 285 super.initialize(); 286 srbFileSystem = null; 287 } 288 289 /** 290 * Disconnect from SRB. 291 */ 292 public void wrapup() { 293 srbFileSystem = SRBUtil.closeConnection(srbFileSystem); 294 } 295 296 // ///////////////////////////////////////////////////////////////// 297 // // private methods //// 298 299 /** 300 * Outputs the metadata 301 * 302 * @param rl 303 * metadata record list. 304 * @throws IllegalActionException 305 * broadcast exception 306 */ 307 private void _sendResults(MetaDataRecordList[] rl, SRBFile srbFile) 308 throws IllegalActionException { 309 if (srbFile != null) { 310 String results = ""; 311 // boolean r = ((BooleanToken)recursive.getToken()).booleanValue(); 312 if (rl != null) { // Nothing in the database matched the query 313 for (int i = 0; i < rl.length; i++) { 314 int collInd = rl[i] 315 .getFieldIndex(SRBMetaDataSet.DIRECTORY_NAME); 316 String dirName = rl[i].getStringValue(collInd); 317 int fileNameInd = rl[i] 318 .getFieldIndex(SRBMetaDataSet.FILE_NAME); 319 if (fileNameInd != -1) { // this is a file. 320 String fileName = rl[i].getStringValue(fileNameInd); 321 /* 322 * if (!r) { //non-recursive. if (srbFile.isDirectory() 323 * || // if the srbFile is a directory 324 * (!srbFile.isDirectory() && //or it's a file but has a 325 * differnet name or different dir, continue 326 * (!fileName.equals(srbFile.getName()) || 327 * !dirName.equals(srbFile.getParent())))) continue; } 328 */ 329 results += "file name: " + fileName + "\n"; 330 /* 331 * } else { // this is a directory if (!r) { if 332 * (!srbFile.isDirectory() || //if the srbFile is not a 333 * directory or it's a directory with a diff name, cont 334 * (srbFile.isDirectory() && 335 * dirName.equals(srbFile.getAbsolutePath()))) continue; 336 * } 337 */ 338 } 339 results += "directory name: " + dirName; 340 int ind = rl[i] 341 .getFieldIndex(SRBMetaDataSet.DEFINABLE_METADATA_FOR_FILES); 342 if (ind != -1) { // this is metadata for a file. 343 results += "\ndefinable metadata for files: \n"; 344 } else if (ind == -1) { // this is not metadata for a file. 345 ind = rl[i] 346 .getFieldIndex(SRBMetaDataSet.DEFINABLE_METADATA_FOR_DIRECTORIES); 347 if (ind != -1) { // this is metadata for a directory 348 results += "\ndefinable metadata for directories: \n"; 349 } else { 350 results += "\nno metadata\n"; 351 } 352 } 353 if (ind != -1) { // there is metadata for a file/dir 354 MetaDataTable mdt = rl[i].getTableValue(ind); 355 String tmp = mdt.toString(); 356 for (int j = 0; j < mdt.getRowCount(); j++) { 357 results += mdt.getStringValue(j, 0); 358 results += " = "; 359 results += mdt.getStringValue(j, 1) + "\n"; 360 System.out.println(mdt.getStringValue(j, 0)); 361 System.out.println(mdt.getStringValue(j, 1)); 362 } 363 } 364 } 365 } 366 metadata.broadcast(new StringToken(results)); 367 } 368 } 369 370 // ///////////////////////////////////////////////////////////////// 371 // // private members //// 372 373 /** 374 * An srb file variable. 375 */ 376 private SRBFile srbFile = null; 377 378 /** 379 * An srb file system variable. 380 */ 381 private SRBFileSystem srbFileSystem = null; 382}