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 org.apache.hadoop.conf.Configuration; 021 import org.jdom.Element; 022 import org.json.JSONObject; 023 024 import java.text.SimpleDateFormat; 025 import java.util.Map; 026 import java.util.Properties; 027 import java.util.TimeZone; 028 import java.util.Date; 029 import java.net.URLEncoder; 030 import java.io.UnsupportedEncodingException; 031 032 /** 033 * Base EL constants and functions. 034 */ 035 public class ELConstantsFunctions { 036 037 /** 038 * KiloByte constant (1024). Defined for EL as 'KB'. 039 */ 040 public static final long KB = 1024; 041 042 /** 043 * MegaByte constant (1024 KB). Defined for EL as 'MB'. 044 */ 045 public static final long MB = KB * 1024; 046 047 /** 048 * GigaByte constant (1024 MB). Defined for EL as 'GB'. 049 */ 050 public static final long GB = MB * 1024; 051 052 /** 053 * TeraByte constant (1024 GB). Defined for EL as 'TB'. 054 */ 055 public static final long TB = GB * 1024; 056 057 /** 058 * PetaByte constant (1024 TB). Defined for EL as 'PB'. 059 */ 060 public static final long PB = TB * 1024; 061 062 public static final int SUBMIT_MINUTES = 1; 063 public static final int SUBMIT_HOURS = 60; 064 public static final int SUBMIT_DAYS = 24 * 60; 065 066 /** 067 * Return the first not <code>null</code> value, or <code>null</code> if both are <code>null</code>. Defined for EL 068 * as 'Object firstNotNull(Object, Object)'. 069 * 070 * @param o1 first value. 071 * @param o2 second value. 072 * @return the first not <code>null</code> value, or or <code>null</code> if both are <code>null</code> 073 */ 074 public static Object firstNotNull(Object o1, Object o2) { 075 return (o1 != null) ? o1 : o2; 076 } 077 078 /** 079 * Return the concatenation of 2 strings. <p/> A string with <code>null</code> value is considered as an empty 080 * string. 081 * 082 * @param s1 first string. 083 * @param s2 second string. 084 * @return the concatenation of <code>s1</code> and <code>s2</code>. 085 */ 086 public static String concat(String s1, String s2) { 087 StringBuilder sb = new StringBuilder(); 088 if (s1 != null) { 089 sb.append(s1); 090 } 091 if (s2 != null) { 092 sb.append(s2); 093 } 094 return sb.toString(); 095 } 096 097 /** 098 * Return the trimmed version of the given string. 099 * 100 * @param input string to be trimmed 101 * @return the trimmed version of the given string or the empty string if the given string was <code>null</code> 102 */ 103 public static String trim(String input) { 104 return (input == null) ? "" : input.trim(); 105 } 106 107 /** 108 * Return the UTC current date and time in W3C format down to second (yyyy-MM-ddTHH:mm:ssZ). i.e.: 109 * 1997-07-16T19:20:30Z 110 * 111 * @return the formatted time string. 112 */ 113 public static String timestamp() { 114 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 115 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 116 return sdf.format(new Date()); 117 } 118 119 /** 120 * Translates a string into <code>application/x-www-form-urlencoded</code> format using UTF-8 encoding scheme. Bytes 121 * for unsafe characters are also obtained using UTF-8 scheme. 122 * 123 * @param input string to be encoded 124 * @return the encoded <code>String</code> 125 */ 126 public static String urlEncode(String input) { 127 try { 128 return (input == null) ? "" : URLEncoder.encode(input, "UTF-8"); 129 } 130 catch (UnsupportedEncodingException uee) { 131 throw new RuntimeException("It should never happen"); 132 } 133 } 134 135 public static String toJsonStr(Map<String, String> map) { 136 JSONObject json = new JSONObject(map); 137 return XmlUtils.escapeCharsForXML(json.toString()); 138 } 139 140 public static String toPropertiesStr(Map<String, String> map) { 141 Properties props = new Properties(); 142 for (Map.Entry<String, String> entry: map.entrySet()) { 143 props.setProperty(entry.getKey(), entry.getValue()); 144 } 145 return XmlUtils.escapeCharsForXML(PropertiesUtils.propertiesToString(props)); 146 } 147 148 public static String toConfigurationStr(Map<String, String> map) { 149 Configuration conf = new Configuration(false); 150 for (Map.Entry<String, String> entry: map.entrySet()) { 151 conf.set(entry.getKey(), entry.getValue()); 152 } 153 return XmlUtils.escapeCharsForXML(XmlUtils.prettyPrint(conf).toString()); 154 } 155 156 }