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