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