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.io.IOException;
022import java.io.StringReader;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.oozie.CoordinatorActionBean;
026import org.apache.oozie.ErrorCode;
027import org.apache.oozie.client.OozieClient;
028import org.apache.oozie.command.CommandException;
029import org.apache.oozie.command.NotificationXCommand;
030import org.apache.oozie.service.Services;
031import org.apache.oozie.util.LogUtils;
032import org.apache.oozie.util.ParamChecker;
033import org.apache.oozie.util.XConfiguration;
034
035/**
036 * This class will send the notification for the coordinator action
037 */
038public class CoordActionNotificationXCommand extends NotificationXCommand {
039
040    private final CoordinatorActionBean actionBean;
041    private static final String STATUS_PATTERN = "\\$status";
042    private static final String ACTION_ID_PATTERN = "\\$actionId";
043
044    // this variable is package private only for test purposes
045    int retries = 0;
046
047    public CoordActionNotificationXCommand(CoordinatorActionBean actionBean) {
048        super("coord_action_notification", "coord_action_notification", 0);
049        ParamChecker.notNull(actionBean, "Action Bean");
050        this.actionBean = actionBean;
051        jobId = actionBean.getId();
052
053    }
054
055    @Override
056    protected void loadState() throws CommandException {
057        Configuration conf;
058        try {
059            conf = new XConfiguration(new StringReader(actionBean.getRunConf()));
060        }
061        catch (IOException e1) {
062            LOG.warn("Configuration parse error. read from DB :" + actionBean.getRunConf());
063            throw new CommandException(ErrorCode.E1005, e1.getMessage(), e1);
064        }
065        url = conf.get(OozieClient.COORD_ACTION_NOTIFICATION_URL);
066        if (url != null) {
067            url = url.replaceAll(ACTION_ID_PATTERN, actionBean.getId());
068            url = url.replaceAll(STATUS_PATTERN, actionBean.getStatus().toString());
069            proxyConf = conf.get(OozieClient.COORD_ACTION_NOTIFICATION_PROXY,
070                    Services.get().getConf().get(NOTIFICATION_PROXY_KEY));
071            LOG.debug("Proxy :" + proxyConf);
072
073        }
074        LOG.debug("Notification URL :" + url);
075        LogUtils.setLogInfo(actionBean);
076    }
077}