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