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.util;
020
021import java.util.Properties;
022import java.util.Set;
023import java.io.StringWriter;
024import java.io.IOException;
025import java.io.StringReader;
026import java.io.Reader;
027
028import com.google.common.collect.ImmutableSet;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.mapreduce.MRJobConfig;
031import org.apache.oozie.ErrorCode;
032import org.apache.oozie.action.hadoop.JavaActionExecutor;
033import org.apache.oozie.client.OozieClient;
034import org.apache.oozie.command.CommandException;
035
036public class PropertiesUtils {
037
038    public static final String HADOOP_USER = "user.name";
039    public static final String YEAR = "YEAR";
040    public static final String MONTH = "MONTH";
041    public static final String DAY = "DAY";
042    public static final String HOUR = "HOUR";
043    public static final String MINUTE = "MINUTE";
044    public static final String DAYS = "DAYS";
045    public static final String HOURS = "HOURS";
046    public static final String MINUTES = "MINUTES";
047    public static final String KB = "KB";
048    public static final String MB = "MB";
049    public static final String GB = "GB";
050    public static final String TB = "TB";
051    public static final String PB = "PB";
052    public static final String RECORDS = "RECORDS";
053    public static final String MAP_IN = "MAP_IN";
054    public static final String MAP_OUT = "MAP_OUT";
055    public static final String REDUCE_IN = "REDUCE_IN";
056    public static final String REDUCE_OUT = "REDUCE_OUT";
057    public static final String GROUPS = "GROUPS";
058    public static final Set<String> DEFAULT_DISALLOWED_PROPERTIES = ImmutableSet.of(
059            JavaActionExecutor.HADOOP_USER, JavaActionExecutor.MR_JOB_USER_NAME
060    );
061
062    public static String propertiesToString(Properties props) {
063        ParamChecker.notNull(props, "props");
064        try {
065            StringWriter sw = new StringWriter();
066            props.store(sw, "");
067            sw.close();
068            return sw.toString();
069        }
070        catch (IOException ex) {
071            throw new RuntimeException(ex);
072        }
073    }
074
075    public static Properties stringToProperties(String str) {
076        ParamChecker.notNull(str, "str");
077        try {
078            StringReader sr = new StringReader(str);
079            Properties props = new Properties();
080            props.load(sr);
081            sr.close();
082            return props;
083        }
084        catch (IOException ex) {
085            throw new RuntimeException(ex);
086        }
087    }
088
089    public static Properties readProperties(Reader reader, int maxDataLen) throws IOException {
090        String data = IOUtils.getReaderAsString(reader, maxDataLen);
091        return stringToProperties(data);
092    }
093
094    /**
095     * Create a set from an array
096     *
097     * @param properties String array
098     * @param set String set
099     */
100    public static void createPropertySet(String[] properties, Set<String> set) {
101        ParamChecker.notNull(set, "set");
102        for (String p : properties) {
103            set.add(p);
104        }
105    }
106
107    /**
108     * Validate against DISALLOWED Properties.
109     *
110     * @param conf : configuration to check.
111     * @throws CommandException
112     */
113    public static void checkDisallowedProperties(Configuration conf, Set<String> set) throws CommandException {
114        ParamChecker.notNull(conf, "conf");
115        for (String prop : set) {
116            if (conf.get(prop) != null) {
117                throw new CommandException(ErrorCode.E0808, prop);
118            }
119        }
120    }
121
122    public static void checkDefaultDisallowedProperties(final Configuration conf) throws CommandException {
123        checkDisallowedProperties(conf, DEFAULT_DISALLOWED_PROPERTIES);
124    }
125}