001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.command.coord;
020
021import java.util.Date;
022
023import org.apache.oozie.CoordinatorActionBean;
024import org.apache.oozie.CoordinatorJobBean;
025import org.apache.oozie.AppType;
026import org.apache.oozie.command.XCommand;
027import org.apache.oozie.coord.CoordELFunctions;
028import org.apache.oozie.event.CoordinatorActionEvent;
029import org.apache.oozie.event.CoordinatorJobEvent;
030
031/**
032 * Abstract coordinator command class derived from XCommand
033 */
034public abstract class CoordinatorXCommand<T> extends XCommand<T> {
035
036    /**
037     * Base class constructor for coordinator commands.
038     *
039     * @param name command name
040     * @param type command type
041     * @param priority command priority
042     */
043    public CoordinatorXCommand(String name, String type, int priority) {
044        super(name, type, priority);
045    }
046
047    /**
048     * Base class constructor for coordinator commands.
049     *
050     * @param name command name
051     * @param type command type
052     * @param priority command priority
053     * @param dryrun true if rerun is enabled for command
054     */
055    public CoordinatorXCommand(String name, String type, int priority, boolean dryrun) {
056        super(name, type, priority, dryrun);
057    }
058
059    public static void generateEvent(CoordinatorActionBean coordAction, String user, String appName, Date startTime) {
060        if (eventService.isSupportedApptype(AppType.COORDINATOR_ACTION.name())) {
061            String missDep = coordAction.getMissingDependencies();
062            if (missDep != null && missDep.length() > 0) {
063                missDep = missDep.split(CoordELFunctions.INSTANCE_SEPARATOR)[0];
064            }
065            String pushMissDep = coordAction.getPushMissingDependencies();
066            if (pushMissDep != null && pushMissDep.length() > 0) {
067                pushMissDep = pushMissDep.split(CoordELFunctions.INSTANCE_SEPARATOR)[0];
068            }
069            String deps = missDep == null ? (pushMissDep == null ? null : pushMissDep) : (pushMissDep == null ? missDep
070                    : missDep + CoordELFunctions.INSTANCE_SEPARATOR + pushMissDep);
071            CoordinatorActionEvent event = new CoordinatorActionEvent(coordAction.getId(), coordAction.getJobId(),
072                    coordAction.getStatus(), user, appName, coordAction.getNominalTime(), startTime,
073                    deps);
074            event.setErrorCode(coordAction.getErrorCode());
075            event.setErrorMessage(coordAction.getErrorMessage());
076            eventService.queueEvent(event);
077        }
078    }
079
080    protected void generateEvent(CoordinatorJobBean coordJob) {
081        if (eventService.isSupportedApptype(AppType.COORDINATOR_JOB.name())) {
082            CoordinatorJobEvent event = new CoordinatorJobEvent(coordJob.getId(), coordJob.getBundleId(),
083                    coordJob.getStatus(), coordJob.getUser(), coordJob.getAppName(), coordJob.getStartTime(),
084                    coordJob.getEndTime());
085            eventService.queueEvent(event);
086        }
087    }
088
089}