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 */
018package org.apache.oozie.action.hadoop;
019
020import java.io.IOException;
021import java.io.StringReader;
022import java.util.Iterator;
023import java.util.Map;
024import java.util.Map.Entry;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.oozie.action.ActionExecutor.Context;
028import org.apache.oozie.client.OozieClient;
029import org.apache.oozie.client.WorkflowAction;
030import org.apache.oozie.service.Services;
031import org.apache.oozie.util.XConfiguration;
032
033import com.google.common.annotations.VisibleForTesting;
034
035public class OozieJobInfo {
036
037    public static final String BUNDLE_ID = "bundle.id";
038    public static final String BUNDLE_NAME = "bundle.name";
039    public static final String COORD_NAME = "coord.name";
040    public static final String COORD_ID = "coord.id";
041    public static final String COORD_NOMINAL_TIME = "coord.nominal.time";
042    public static final String WORKFLOW_ID = "wf.id";
043    public static final String WORKFLOW_NAME = "wf.name";
044    public static final String ACTION_TYPE = "action.type";
045    public static final String ACTION_NAME = "action.name";
046    public static final String JOB_INFO_KEY = "oozie.job.info";
047    public static final String CONF_JOB_INFO = "oozie.action.jobinfo.enable";
048    public final static String SEPARATOR = ",";
049
050    private Context context;
051    XConfiguration contextConf;
052    private WorkflowAction action;
053    private Configuration actionConf;
054    private static boolean jobInfo = Services.get().getConf().getBoolean(OozieJobInfo.CONF_JOB_INFO, false);
055
056    /**
057     * Instantiates a new oozie job info.
058     *
059     * @param jobconf the jobconf
060     * @param actionConf the action conf
061     * @param context the context
062     * @param action the action
063     * @throws IOException
064     */
065    public OozieJobInfo(Configuration actionConf, Context context, WorkflowAction action) throws IOException {
066        this.context = context;
067        contextConf = new XConfiguration(new StringReader(context.getWorkflow().getConf()));
068        this.action = action;
069        this.actionConf = actionConf;
070    }
071
072    public static boolean isJobInfoEnabled() {
073        return jobInfo;
074    }
075
076    @VisibleForTesting
077    public static void setJobInfo(boolean jobInfo) {
078        OozieJobInfo.jobInfo = jobInfo;
079    }
080
081    /**
082     * Get the job info.
083     *
084     * @return job info
085     * @throws IOException Signals that an I/O exception has occurred.
086     */
087    public String getJobInfo() throws IOException {
088        StringBuffer sb = new StringBuffer();
089        addBundleInfo(sb);
090        addCoordInfo(sb);
091        addWorkflowInfo(sb);
092        addActionInfo(sb);
093        addCustomInfo(sb);
094        return sb.toString();
095
096    }
097
098    private void addBundleInfo(StringBuffer sb) throws IOException {
099        addJobInfo(sb, BUNDLE_ID, contextConf.get(OozieClient.BUNDLE_ID));
100        addJobInfo(sb, BUNDLE_NAME, contextConf.get(OozieJobInfo.BUNDLE_NAME));
101
102    }
103
104    private void addCoordInfo(StringBuffer sb) throws IOException {
105        addJobInfo(sb, COORD_NAME, contextConf.get(OozieJobInfo.COORD_NAME));
106        addJobInfo(sb, COORD_NOMINAL_TIME, contextConf.get(OozieJobInfo.COORD_NOMINAL_TIME));
107        addJobInfo(sb, COORD_ID, context.getWorkflow().getParentId());
108
109    }
110
111    private void addWorkflowInfo(StringBuffer sb) {
112        addJobInfo(sb, WORKFLOW_ID, context.getWorkflow().getId());
113        addJobInfo(sb, WORKFLOW_NAME, context.getWorkflow().getAppName());
114
115    }
116
117    private void addActionInfo(StringBuffer sb) {
118        addJobInfo(sb, ACTION_NAME, action.getName());
119        addJobInfo(sb, ACTION_TYPE, action.getType());
120    }
121
122    private void addCustomInfo(StringBuffer sb) throws IOException {
123        addfromConf(actionConf, sb);
124    }
125
126    public void addfromConf(Configuration conf, StringBuffer sb) {
127        Iterator<Map.Entry<String, String>> it = conf.iterator();
128        while (it.hasNext()) {
129            Entry<String, String> entry = it.next();
130            if (entry.getKey().startsWith("oozie.job.info.")) {
131                addJobInfo(sb, entry.getKey().substring("oozie.job.info.".length()), entry.getValue());
132            }
133        }
134    }
135
136    private void addJobInfo(StringBuffer sb, String key, String value) {
137        if (value != null) {
138            sb.append(key).append("=").append(value).append(OozieJobInfo.SEPARATOR);
139        }
140
141    }
142}