001/* An actor that outputs a string read from a text file or URL. 002 003 Copyright (c) 2003-2009 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 */ 027package org.camera.service; 028 029import java.io.BufferedReader; 030 031import ptolemy.actor.TypedIOPort; 032import ptolemy.actor.lib.LimitedFiringSource; 033import ptolemy.data.BooleanToken; 034import ptolemy.data.StringToken; 035import ptolemy.data.expr.FileParameter; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.CompositeEntity; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040 041////////////////////////////////////////////////////////////////////////// 042//// CAMERAResourceMonitor 043 044/** 045 This actor reads a file or URL and outputs the entire file 046 as a single string. The file or URL is specified using any form 047 acceptable to FileParameter. 048 049 050 */ 051public class CAMERAResourceMonitor extends LimitedFiringSource { 052 /** Construct an actor with a name and a container. 053 * The container argument must not be null, or a 054 * NullPointerException will be thrown. 055 * @param container The container. 056 * @param name The name of this actor. 057 * @exception IllegalActionException If the container is incompatible 058 * with this actor. 059 * @exception NameDuplicationException If the name coincides with 060 * an actor already in the container. 061 */ 062 public CAMERAResourceMonitor(CompositeEntity container, String name) 063 throws IllegalActionException, NameDuplicationException { 064 super(container, name); 065 066 output.setTypeEquals(BaseType.BOOLEAN); 067 068 fileOrURL = new FileParameter(this, "fileOrURL"); 069 070 fileOrURLPort = new TypedIOPort(this, "fileOrURL", true, false); 071 fileOrURLPort.setTypeEquals(BaseType.STRING); 072 073 // newline = new Parameter(this, "newline"); 074 // newline.setExpression("property(\"line.separator\")"); 075 076 _attachText("_iconDescription", "<svg>\n" 077 + "<rect x=\"-25\" y=\"-20\" " + "width=\"50\" height=\"40\" " 078 + "style=\"fill:white\"/>\n" 079 + "<polygon points=\"-15,-10 -12,-10 -8,-14 -1,-14 3,-10" 080 + " 15,-10 15,10, -15,10\" " + "style=\"fill:red\"/>\n" 081 + "</svg>\n"); 082 } 083 084 /////////////////////////////////////////////////////////////////// 085 //// public variables //// 086 087 /** The file name or URL from which to read. This is a string with 088 * any form accepted by FileParameter. 089 * @see FileParameter 090 */ 091 public FileParameter fileOrURL; 092 093 /** An input port for optionally providing a file name. This has 094 * type string. 095 */ 096 public TypedIOPort fileOrURLPort; 097 098 /** The end of line character(s). The default value is the value 099 * of the line.separator property 100 */ 101 // public Parameter newline; 102 103 104 /////////////////////////////////////////////////////////////////// 105 //// public methods //// 106 107 /** Output the data read from the file or URL as a string. 108 * @exception IllegalActionException If there is no director or 109 * if reading the file triggers an exception. 110 */ 111 public void fire() throws IllegalActionException { 112 super.fire(); 113 boolean monitor = false; 114 115 // If the fileOrURL input port is connected and has data, then 116 // get the file name from there. 117 118 if (fileOrURLPort.isOutsideConnected()) { 119 if (fileOrURLPort.hasToken(0)) { 120 String name = ((StringToken) fileOrURLPort.get(0)) 121 .stringValue(); 122 123 // Using setExpression() rather than setToken() allows 124 // the string to refer to variables defined in the 125 // scope of this actor. 126 fileOrURL.setExpression(name); 127 } 128 } 129 130 BufferedReader reader = null; 131 132 try { 133 reader = fileOrURL.openForReading(); 134 135 StringBuilder lineBuilder = new StringBuilder(); 136 137 String line = null; 138 while ((line=reader.readLine()) != null) { 139 lineBuilder = lineBuilder.append(line); 140 } 141 142 monitor = true; 143 144 } catch (Throwable throwable) { 145 throwable.printStackTrace(); 146 } finally { 147 if (fileOrURL != null) { 148 fileOrURL.close(); 149 } 150 } 151 if(monitor){ 152 output.send(0,BooleanToken.TRUE); 153 }else{ 154 output.send(0,BooleanToken.FALSE); 155 } 156 } 157}