001/*
002 * Copyright (c) 2007-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 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 */
029
030package org.nees;
031
032import java.io.File;
033import java.io.IOException;
034
035import ptolemy.actor.TypedAtomicActor;
036import ptolemy.actor.TypedIOPort;
037import ptolemy.actor.parameters.PortParameter;
038import ptolemy.data.BooleanToken;
039import ptolemy.data.StringToken;
040import ptolemy.data.expr.Parameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.Attribute;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046
047//////////////////////////////////////////////////////////////////////////
048//// CreateUniqueFile
049/**
050 * This actor creates a uniquely-named file in given a directory <i>dir</i>.
051 * 
052 * @author Daniel Crawl
053 * @version $Id: CreateUniqueFile.java 24234 2010-05-06 05:21:26Z welker $
054 */
055
056public class CreateUniqueFile extends TypedAtomicActor {
057
058        /**
059         * Construct a CreateUniqueFile source with the given container and name.
060         * 
061         * @param name
062         *            The name of this actor.
063         * @exception IllegalActionException
064         *                If the entity cannot be contained by the proposed
065         *                container.
066         * @exception NameDuplicationException
067         *                If the container already has an actor with this name.
068         */
069        public CreateUniqueFile(CompositeEntity container, String name)
070                        throws NameDuplicationException, IllegalActionException {
071                super(container, name);
072
073                prefix = new PortParameter(this, "prefix", new StringToken("pre"));
074                prefix.setTypeEquals(BaseType.STRING);
075                new Attribute(prefix, "_showName");
076
077                suffix = new PortParameter(this, "suffix", new StringToken(".tmp"));
078                suffix.setTypeEquals(BaseType.STRING);
079                new Attribute(suffix, "_showName");
080
081                dir = new PortParameter(this, "dir", new StringToken(System
082                                .getProperty("java.io.tmpdir")));
083                dir.setTypeEquals(BaseType.STRING);
084                new Attribute(dir, "_showName");
085
086                absFilename = new Parameter(this, "Absolute Path", new BooleanToken(
087                                true));
088                absFilename.setTypeEquals(BaseType.BOOLEAN);
089
090                filename = new TypedIOPort(this, "output", false, true);
091                filename.setTypeEquals(BaseType.STRING);
092
093                _attachText("_iconDescription", "<svg>\n" + "<rect x=\"0\" y=\"0\" "
094                                + "width=\"60\" height=\"20\" " + "style=\"fill:white\"/>\n"
095                                + "</svg>\n");
096        }
097
098        // /////////////////////////////////////////////////////////////////
099        // // ports and parameters ////
100
101        /**
102         * The character prefix to use in generating the file name.
103         */
104        public PortParameter prefix = null;
105
106        /**
107         * The character suffix to use in generating the file name.
108         */
109        public PortParameter suffix = null;
110
111        /**
112         * The directory in which to create the file.
113         */
114        public PortParameter dir = null;
115
116        /**
117         * If true, the output a full path instead of just the file name.
118         */
119        public Parameter absFilename = null;
120
121        /**
122         * The name of the created file.
123         */
124        public TypedIOPort filename = null;
125
126        // /////////////////////////////////////////////////////////////////
127        // // public methods ////
128
129        /**
130         * Create the file using java.lang.File.createTempFile().
131         * 
132         * @exception IllegalActionException
133         *                If it is thrown by the send() method sending out the
134         *                token.
135         */
136        public void fire() throws IllegalActionException {
137                super.fire();
138
139                StringToken token = null;
140
141                prefix.update();
142                String prefixStr = "";
143                if ((token = (StringToken) prefix.getToken()) != null) {
144                        prefixStr = token.stringValue();
145                }
146
147                suffix.update();
148                String suffixStr = "";
149                if ((token = (StringToken) suffix.getToken()) != null) {
150                        suffixStr = token.stringValue();
151                }
152
153                dir.update();
154                String dirStr = "";
155                if ((token = (StringToken) dir.getToken()) == null) {
156                        throw new IllegalActionException(this, "Must supply directory.");
157                } else {
158                        dirStr = token.stringValue();
159                }
160
161                File dirFile = new File(dirStr);
162
163                try {
164                        File tempFile = File.createTempFile(prefixStr, suffixStr, dirFile);
165
166                        boolean abs = ((BooleanToken) absFilename.getToken())
167                                        .booleanValue();
168
169                        String outStr = null;
170
171                        // see if we should output the entire path or just
172                        // the file name.
173                        if (abs) {
174                                outStr = tempFile.getAbsolutePath();
175                        } else {
176                                outStr = tempFile.getName();
177                        }
178
179                        filename.broadcast(new StringToken(outStr));
180                } catch (IOException e) {
181                        throw new IllegalActionException(this, "IOException: "
182                                        + e.getMessage());
183                }
184        }
185}