001/*
002 * Copyright (c) 2004-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.sdm.spa.actors.io;
031
032import java.io.File;
033
034import ptolemy.actor.TypedAtomicActor;
035import ptolemy.actor.TypedIOPort;
036import ptolemy.actor.parameters.PortParameter;
037import ptolemy.data.StringToken;
038import ptolemy.data.type.BaseType;
039import ptolemy.kernel.CompositeEntity;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NameDuplicationException;
042
043/**
044 * Creates the directory specified by the input port if it doesn't exist plus
045 * parent directories.
046 * 
047 * If the directory could not be created, perhaps because a file of the same
048 * name already exists, then a non-empty error message will be output at the
049 * errors port. Otherwise, an empty string will be output in that port after
050 * creation. No error will be reported if a directory of this name already
051 * exists.
052 * 
053 * @author xiaowen
054 * @version $Id: DirectoryCreate.java 24234 2010-05-06 05:21:26Z welker $
055 */
056public class DirectoryCreate extends TypedAtomicActor {
057        /**
058         * Create an instance of this actor.
059         * 
060         * @param container
061         *            The entity to contain this actor
062         * @param name
063         *            Name of the actor
064         * @throws IllegalActionException
065         *             If superclass throws it.
066         * @throws NameDuplicationException
067         *             If superclass throws it.
068         */
069        public DirectoryCreate(CompositeEntity container, String name)
070                        throws IllegalActionException, NameDuplicationException {
071                super(container, name);
072
073                // Initialize both ports.
074                this.name = new PortParameter(this, "name");
075                this.errors = new TypedIOPort(this, "errors", false, true);
076
077                // Set the type constraints.
078                this.name.setTypeEquals(BaseType.STRING);
079                this.errors.setTypeEquals(BaseType.STRING);
080        }
081
082        /**
083         * Create the directory with the name taken from the input port and output
084         * either the error or an empty string along the errors port.
085         * 
086         * @throws IllegalActionException
087         *             If superclass throws it.
088         */
089        public void fire() throws IllegalActionException {
090                super.fire();
091
092                // Get the input.
093                name.update();
094                String strName = ((StringToken) name.getToken()).stringValue();
095
096                // File object representing the target directory
097                File fileDir = new File(strName);
098
099                // If the directory already exists, then stop.
100                if (fileDir.isDirectory()) {
101                        errors.send(0, new StringToken());
102                        return;
103                }
104
105                boolean done = false;
106
107                try {
108                        done = fileDir.mkdirs();
109                } catch (Exception e) {
110                        e.printStackTrace();
111                        errors.send(0, new StringToken(
112                                        "Exception thrown while trying to create directory '"
113                                                        + strName + "' in actor '" + this.getFullName()
114                                                        + "': " + e.toString()));
115                        return;
116                }
117
118                if (!done) {
119                        errors.send(0, new StringToken("Could not create directory '"
120                                        + strName + "' in actor '" + this.getFullName() + "'."));
121                } else {
122                        errors.send(0, new StringToken());
123                }
124        }
125
126        /** Input port representing the name of the file to create. */
127        protected PortParameter name;
128
129        /** Port that outputs errors if there are any. */
130        protected TypedIOPort errors;
131}