001/* 002 * Copyright (c) 2009-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: tao $' 006 * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $' 007 * '$Revision: 24234 $' 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 */ 029package org.kepler.actor.io; 030 031import java.io.File; 032 033import ptolemy.actor.lib.LimitedFiringSource; 034import ptolemy.actor.parameters.PortParameter; 035import ptolemy.data.BooleanToken; 036import ptolemy.data.StringToken; 037import ptolemy.data.type.BaseType; 038import ptolemy.kernel.CompositeEntity; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041 042 043/** 044 * Delete a file or directory. If this actor deletes a directory, then the directory 045 * must be empty in order to be deleted. 046 * The output is a BooleanToken. true if and only if the file or directory is 047 * successfully deleted; false otherwise 048 * If the trigger port is connected, then this actor fires only if a token is 049 * provided on any channel of the trigger port. The value of that token 050 * does not matter. Specifically, if there is no such token, the prefire() 051 * method returns false. 052 * @author tao 053 * 054 */ 055public class FileDeleter extends LimitedFiringSource 056{ 057 058 /** Construct an actor with the given container and name. 059 * @param container The container. 060 * @param name The name of this actor. 061 * @exception IllegalActionException If the entity cannot be contained 062 * by the proposed container. 063 * @exception NameDuplicationException If the container already has an 064 * actor with this name. 065 */ 066 public FileDeleter(CompositeEntity container, String name) 067 throws NameDuplicationException, IllegalActionException 068 { 069 super(container, name); 070 filePathInputParameter = new PortParameter(this, "File Path"); 071 filePathInputParameter.setStringMode(true); 072 filePathInputParameter.setTypeEquals(BaseType.STRING); 073 filePathInputParameter.getPort().setTypeEquals(BaseType.STRING); 074 075 // Set the type constraint. 076 output.setTypeEquals(BaseType.BOOLEAN); 077 } 078 079 /////////////////////////////////////////////////////////////////// 080 //// ports and parameters //// 081 082 /** 083 * The file or directory path which will be deleted. 084 */ 085 public PortParameter filePathInputParameter; 086 087 088 /////////////////////////////////////////////////////////////////// 089 //// public methods //// 090 091 /** 092 * Delete the given file or directory and send out the status of the deleting. 093 * 094 * @exception IllegalActionException 095 * If it is thrown by the send() method sending out the 096 * token. 097 */ 098 @Override 099 public void fire() throws IllegalActionException 100 { 101 super.fire(); 102 filePathInputParameter.update(); 103 StringToken token = (StringToken) filePathInputParameter.getToken(); 104 String filePath = token.stringValue(); 105 106 if(filePath.trim().isEmpty()) { 107 throw new IllegalActionException(this, "File path is empty."); 108 } 109 110 File file = new File(filePath); 111 boolean result = file.delete(); 112 113 output.send(0, new BooleanToken(result)); 114 } 115 116}