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