001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.wf;
016    
017    import org.apache.oozie.client.OozieClient;
018    import org.apache.oozie.WorkflowActionBean;
019    import org.apache.oozie.WorkflowJobBean;
020    import org.apache.oozie.command.Command;
021    import org.apache.oozie.store.WorkflowStore;
022    import org.apache.oozie.store.Store;
023    import org.apache.oozie.util.XLog;
024    
025    import java.io.IOException;
026    import java.net.HttpURLConnection;
027    import java.net.URL;
028    
029    public class NotificationCommand extends WorkflowCommand<Void> {
030    
031        private static final String STATUS_PATTERN = "\\$status";
032        private static final String JOB_ID_PATTERN = "\\$jobId";
033        private static final String NODE_NAME_PATTERN = "\\$nodeName";
034    
035        private String url;
036        private int retries = 0;
037    
038        public NotificationCommand(WorkflowJobBean workflow) {
039            super("job.notification", "job.notification", 0, XLog.STD, false);
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            }
045        }
046    
047        public NotificationCommand(WorkflowJobBean workflow, WorkflowActionBean action) {
048            super("action.notification", "job.notification", 0, XLog.STD);
049            url = workflow.getWorkflowInstance().getConf().get(OozieClient.ACTION_NOTIFICATION_URL);
050            if (url != null) {
051                url = url.replaceAll(JOB_ID_PATTERN, workflow.getId());
052                url = url.replaceAll(NODE_NAME_PATTERN, action.getName());
053                if (action.isComplete()) {
054                    url = url.replaceAll(STATUS_PATTERN, "T:" + action.getTransition());
055                }
056                else {
057                    url = url.replaceAll(STATUS_PATTERN, "S:" + action.getStatus().toString());
058                }
059            }
060        }
061    
062        public Void call(WorkflowStore store) {
063            if (url != null) {
064                try {
065                    URL url = new URL(this.url);
066                    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
067                    if (urlConn.getResponseCode() != HttpURLConnection.HTTP_OK) {
068                        handleRetry();
069                    }
070                }
071                catch (IOException ex) {
072                    handleRetry();
073                }
074            }
075            return null;
076        }
077    
078        private void handleRetry() {
079            if (retries < 3) {
080                retries++;
081                queueCallable(this, 60 * 1000);
082            }
083            else {
084                XLog.getLog(getClass()).warn(XLog.OPS, "could not send notification [{0}]", url);
085            }
086        }
087    
088    }