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 PARENT_ID_PATTERN = "\\$parentId";
035    private static final String NODE_NAME_PATTERN = "\\$nodeName";
036
037    public WorkflowNotificationXCommand(WorkflowJobBean workflow) {
038        super("job.notification", "job.notification", 0);
039        ParamChecker.notNull(workflow, "workflow");
040        jobId = workflow.getId();
041        url = workflow.getWorkflowInstance().getConf().get(OozieClient.WORKFLOW_NOTIFICATION_URL);
042        if (url != null) {
043            url = url.replaceAll(JOB_ID_PATTERN, workflow.getId());
044            url = url.replaceAll(STATUS_PATTERN, workflow.getStatus().toString());
045            if (workflow.getParentId() == null) {
046                url = url.replaceAll(PARENT_ID_PATTERN, "");
047            } else {
048                url = url.replaceAll(PARENT_ID_PATTERN, workflow.getParentId());
049            }
050            proxyConf = workflow.getWorkflowInstance().getConf()
051                    .get(OozieClient.WORKFLOW_NOTIFICATION_PROXY, ConfigurationService.get(NOTIFICATION_PROXY_KEY));
052            LOG.debug("Proxy :" + proxyConf);
053        }
054    }
055
056    public WorkflowNotificationXCommand(WorkflowJobBean workflow, WorkflowActionBean action) {
057        super("action.notification", "job.notification", 0);
058        ParamChecker.notNull(workflow, "workflow");
059        ParamChecker.notNull(action, "action");
060        jobId = action.getId();
061        url = workflow.getWorkflowInstance().getConf().get(OozieClient.ACTION_NOTIFICATION_URL);
062        if (url != null) {
063            url = url.replaceAll(JOB_ID_PATTERN, workflow.getId());
064            url = url.replaceAll(NODE_NAME_PATTERN, action.getName());
065            if (action.isComplete()) {
066                url = url.replaceAll(STATUS_PATTERN, "T:" + action.getTransition());
067            }
068            else {
069                url = url.replaceAll(STATUS_PATTERN, "S:" + action.getStatus().toString());
070            }
071            proxyConf = workflow.getWorkflowInstance().getConf()
072                    .get(OozieClient.WORKFLOW_NOTIFICATION_PROXY, ConfigurationService.get(NOTIFICATION_PROXY_KEY));
073            LOG.debug("Proxy :" + proxyConf);
074        }
075    }
076
077    @Override
078    protected void loadState() throws CommandException {
079        LogUtils.setLogInfo(jobId);
080    }
081}