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.expr.Parameter;
041import ptolemy.data.type.BaseType;
042import ptolemy.kernel.CompositeEntity;
043import ptolemy.kernel.util.IllegalActionException;
044import ptolemy.kernel.util.NameDuplicationException;
045
046//////////////////////////////////////////////////////////////////////////
047//// JobRemover
048
049/**
050 * <p>
051 * Delete Job from queue whether it is running or waiting in queue.
052 * </p>
053 * 
054 * <p>
055 * This actor uses the Job class to delete a submitted job.
056 * </p>
057 * 
058 * <p>
059 * The input should be a previously submitted job.
060 * </p>
061 * i.e. the output from a JobSubmitter.
062 * 
063 * <p>
064 * The ouput is boolean indicating whether the removal was successful or not. If
065 * not such job exists, the result will be true.
066 * </p>
067 * 
068 * @author Norbert Podhorszki
069 * @version $Id: JobRemover.java 24234 2010-05-06 05:21:26Z welker $
070 * @since Ptolemy II 5.0.1
071 */
072public class JobRemover extends TypedAtomicActor {
073        /**
074         * Construct an actor with the given container and name.
075         * 
076         * @param container
077         *            The container.
078         * @param name
079         *            The name of this actor.
080         * @exception IllegalActionException
081         *                If the actor cannot be contained by the proposed
082         *                container.
083         * @exception NameDuplicationException
084         *                If the container already has an actor with this name.
085         */
086        public JobRemover(CompositeEntity container, String name)
087                        throws NameDuplicationException, IllegalActionException {
088                super(container, name);
089
090                // Uncomment the next line to see debugging statements
091                // addDebugListener(new ptolemy.kernel.util.StreamListener());
092                job = new TypedIOPort(this, "job", true, false);
093                job.setTypeEquals(BaseType.OBJECT);
094                new Parameter(job, "_showName", BooleanToken.FALSE);
095
096                // Output: status code
097                succ = new TypedIOPort(this, "succ", false, true);
098                succ.setTypeEquals(BaseType.BOOLEAN);
099                new Parameter(succ, "_showName", BooleanToken.TRUE);
100        }
101
102        /***********************************************************
103         * ports and parameters
104         */
105
106        /**
107         * A submitted job This port is an output port of type Object.
108         */
109        public TypedIOPort job;
110
111        /**
112         * Output: true if removal succeeded, false otherwise This port is an output
113         * port of type Integer;
114         */
115        public TypedIOPort succ;
116
117        /***********************************************************
118         * public methods
119         */
120
121        /**
122         * fire
123         * 
124         * @exception IllegalActionException
125         *                Not thrown.
126         */
127        public void fire() throws IllegalActionException {
128                super.fire();
129
130                ObjectToken jobToken = (ObjectToken) job.get(0);
131                Job _job = (Job) jobToken.getValue();
132                boolean _succ = false;
133
134                try {
135                        if (_job == null)
136                                throw new Exception("JobStatus: Job is null");
137
138                        _succ = _job.deleteFromQueue(); // successful query or exception
139
140                } catch (Exception ex) {
141                        log.error(ex);
142                        ex.printStackTrace();
143                        throw new IllegalActionException("JobRemover Error: " + ex.toString());
144                }
145
146                succ.send(0, new BooleanToken(_succ));
147        }
148
149        private static final Log log = LogFactory
150                        .getLog(JobRemover.class.getName());
151        private static final boolean isDebugging = log.isDebugEnabled();
152
153}