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