001/* An atomic DDP pattern actor with a path parameter. 002 * 003 * Copyright (c) 2014 The Regents of the University of California. 004 * All rights reserved. 005 * 006 * '$Author: crawl $' 007 * '$Date: 2014-10-08 21:02:15 +0000 (Wed, 08 Oct 2014) $' 008 * '$Revision: 32992 $' 009 * 010 * Permission is hereby granted, without written agreement and without 011 * license or royalty fees, to use, copy, modify, and distribute this 012 * software and its documentation for any purpose, provided that the above 013 * copyright notice and the following two paragraphs appear in all copies 014 * of this software. 015 * 016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 020 * SUCH DAMAGE. 021 * 022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 027 * ENHANCEMENTS, OR MODIFICATIONS. 028 * 029 */ 030package org.kepler.ddp.actor.pattern; 031 032import java.io.File; 033import java.net.URI; 034 035import ptolemy.actor.parameters.FilePortParameter; 036import ptolemy.data.StringToken; 037import ptolemy.kernel.CompositeEntity; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.SingletonAttribute; 041 042/** An atomic DDP pattern actor with a path parameter. 043 * 044 * @author Daniel Crawl 045 * @version $Id: AtomicPathActor.java 32992 2014-10-08 21:02:15Z crawl $ 046 */ 047public class AtomicPathActor extends AtomicPatternActor { 048 049 /** Create a new AtomicPathActor in the speficied container with the 050 * specified name. 051 */ 052 public AtomicPathActor(CompositeEntity container, String name) 053 throws IllegalActionException, NameDuplicationException { 054 055 super(container, name); 056 057 path = new FilePortParameter(this, "path"); 058 new SingletonAttribute(path.getPort(), "_showName"); 059 060 } 061 062 /** Get the path as a URI string. */ 063 public URI getPathAsURI() throws IllegalActionException { 064 065 String pathStr = ((StringToken)path.getToken()).stringValue(); 066 if(pathStr.isEmpty()) { 067 throw new IllegalActionException(this, "Must specify data input file name."); 068 } 069 070 if(pathStr.startsWith("hdfs://") || pathStr.startsWith("file://")) { 071 return URI.create(pathStr); 072 } else { 073 File file = path.asFile(); 074 if(file == null) { 075 throw new IllegalActionException(this, "Must specify data input file name."); 076 } 077 return file.toURI(); 078 } 079 } 080 081 /** Update the path parameter. */ 082 @Override 083 public boolean prefire() throws IllegalActionException { 084 085 boolean rc = super.prefire(); 086 path.update(); 087 088 return rc; 089 } 090 091 /** The path to read/write the data. */ 092 public FilePortParameter path; 093 094}