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.kepler.actor.job;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.kepler.job.Job;
035
036import ptolemy.actor.TypedAtomicActor;
037import ptolemy.actor.TypedIOPort;
038import ptolemy.data.BooleanToken;
039import ptolemy.data.ObjectToken;
040import ptolemy.data.StringToken;
041import ptolemy.data.expr.Parameter;
042import ptolemy.data.type.BaseType;
043import ptolemy.kernel.CompositeEntity;
044import ptolemy.kernel.util.IllegalActionException;
045import ptolemy.kernel.util.NameDuplicationException;
046
047//////////////////////////////////////////////////////////////////////////
048//// JobGetDirectory
049
050/**
051 * <p>
052 * Get the working directory of a submitted Job
053 * </p>
054 * 
055 * <p>
056 * This actor uses the Job class to get the working directory of a submitted
057 * job. Since the working directory is created by the JobSubmitter, this is the
058 * way to get to know the actual directory name. If you want to use the output
059 * files of a job before deleting it, you need to get the working directory
060 * first.
061 * </p>
062 * 
063 * <p>
064 * The input should be a previously submitted job which has not been removed
065 * yet, i.e. the output from a JobSubmitter, never given to a JobRemover.
066 * </p>
067 * 
068 * <p>
069 * The ouput is the working directory of type String. If the job is null object
070 * this actor throws an IllegalActionException
071 * </p>
072 * .
073 * 
074 * <p>
075 * Note that if the working directory is not set by the workflow, it gets a
076 * default value at job submission. Before submission, this actor returns an
077 * empty string. It is advisable to set a working directory at job creation
078 * (with JobCreator) or use this actor only after submission.
079 * </p>
080 * 
081 * @author Norbert Podhorszki
082 * @version $Id: JobGetDirectory.java 24234 2010-05-06 05:21:26Z welker $
083 * @since Ptolemy II 5.0.1
084 */
085public class JobGetDirectory extends TypedAtomicActor {
086        /**
087         * Construct an actor with the given container and name.
088         * 
089         * @param container
090         *            The container.
091         * @param name
092         *            The name of this actor.
093         * @exception IllegalActionException
094         *                If the actor cannot be contained by the proposed
095         *                container.
096         * @exception NameDuplicationException
097         *                If the container already has an actor with this name.
098         */
099        public JobGetDirectory(CompositeEntity container, String name)
100                        throws NameDuplicationException, IllegalActionException {
101                super(container, name);
102
103                // Uncomment the next line to see debugging statements
104                // addDebugListener(new ptolemy.kernel.util.StreamListener());
105                job = new TypedIOPort(this, "job", true, false);
106                job.setTypeEquals(BaseType.OBJECT);
107                new Parameter(job, "_showName", BooleanToken.FALSE);
108
109                // Output: real JobID as string
110                dir = new TypedIOPort(this, "dir", false, true);
111                dir.setTypeEquals(BaseType.STRING);
112                new Parameter(dir, "_showName", BooleanToken.TRUE);
113
114        }
115
116        /***********************************************************
117         * ports and parameters
118         */
119
120        /**
121         * A submitted job This port is an output port of type Object.
122         */
123        public TypedIOPort job;
124
125        /**
126         * Working directory of the job that was assigned and created by the
127         * JobSubmitter when the job was submitted. This port is an output port of
128         * type String.
129         */
130        public TypedIOPort dir;
131
132        /***********************************************************
133         * public methods
134         */
135
136        /**
137         * fire
138         * 
139         * @exception IllegalActionException
140         *                Not thrown.
141         */
142        public void fire() throws IllegalActionException {
143                super.fire();
144
145                ObjectToken jobToken = (ObjectToken) job.get(0);
146                Job _job = (Job) jobToken.getValue();
147                String _dir = "";
148
149                try {
150                        if (_job == null)
151                                throw new IllegalActionException("JobGetDirectory: Job is null");
152
153                        _dir = _job.getWorkdirPath();
154                        if (_dir == null) {
155                                log
156                                                .warn("Working directory of the job "
157                                                                + _job.getJobID()
158                                                                + " is unknown. "
159                                                                + "Probably, you have not set a working directory for it and it has not been "
160                                                                + "submitted yet.");
161                        }
162                        if (isDebugging)
163                                log.debug("Working directory of job " + _job.getJobID() + ": "
164                                                + _dir);
165
166                } catch (Exception ex) {
167                        log.error(ex);
168                        ex.printStackTrace();
169                        throw new IllegalActionException("JobGetDirectory Error: " + ex.toString());
170                }
171
172                dir.send(0, new StringToken(_dir));
173
174        }
175
176        private static final Log log = LogFactory.getLog(JobGetDirectory.class
177                        .getName());
178        private static final boolean isDebugging = log.isDebugEnabled();
179
180}