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