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.wf;
020
021import org.apache.oozie.client.OozieClient;
022import org.apache.oozie.WorkflowActionBean;
023import org.apache.oozie.WorkflowJobBean;
024import org.apache.oozie.command.CommandException;
025import org.apache.oozie.command.NotificationXCommand;
026import org.apache.oozie.service.ConfigurationService;
027import org.apache.oozie.util.LogUtils;
028import org.apache.oozie.util.ParamChecker;
029
030public class WorkflowNotificationXCommand extends NotificationXCommand {
031
032    private static final String STATUS_PATTERN = "\\$status";
033    private static final String JOB_ID_PATTERN = "\\$jobId";
034    private static final String NODE_NAME_PATTERN = "\\$nodeName";
035
036    public WorkflowNotificationXCommand(WorkflowJobBean workflow) {
037        super("job.notification", "job.notification", 0);
038        ParamChecker.notNull(workflow, "workflow");
039        jobId = workflow.getId();
040        url = workflow.getWorkflowInstance().getConf().get(OozieClient.WORKFLOW_NOTIFICATION_URL);
041        if (url != null) {
042            url = url.replaceAll(JOB_ID_PATTERN, workflow.getId());
043            url = url.replaceAll(STATUS_PATTERN, workflow.getStatus().toString());
044            proxyConf = workflow.getWorkflowInstance().getConf()
045                    .get(OozieClient.WORKFLOW_NOTIFICATION_PROXY, ConfigurationService.get(NOTIFICATION_PROXY_KEY));
046            LOG.debug("Proxy :" + proxyConf);
047        }
048    }
049
050    public WorkflowNotificationXCommand(WorkflowJobBean workflow, WorkflowActionBean action) {
051        super("action.notification", "job.notification", 0);
052        ParamChecker.notNull(workflow, "workflow");
053        ParamChecker.notNull(action, "action");
054        jobId = action.getId();
055        url = workflow.getWorkflowInstance().getConf().get(OozieClient.ACTION_NOTIFICATION_URL);
056        if (url != null) {
057            url = url.replaceAll(JOB_ID_PATTERN, workflow.getId());
058            url = url.replaceAll(NODE_NAME_PATTERN, action.getName());
059            if (action.isComplete()) {
060                url = url.replaceAll(STATUS_PATTERN, "T:" + action.getTransition());
061            }
062            else {
063                url = url.replaceAll(STATUS_PATTERN, "S:" + action.getStatus().toString());
064            }
065            proxyConf = workflow.getWorkflowInstance().getConf()
066                    .get(OozieClient.WORKFLOW_NOTIFICATION_PROXY, ConfigurationService.get(NOTIFICATION_PROXY_KEY));
067            LOG.debug("Proxy :" + proxyConf);
068        }
069    }
070
071    @Override
072    protected void loadState() throws CommandException {
073        LogUtils.setLogInfo(jobId);
074    }
075}