This project has retired. For details please refer to its
Attic page.
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.Date;
021 import java.util.List;
022 import java.util.TimeZone;
023
024 /**
025 * Utility class to check common parameter preconditions.
026 */
027 public class ParamChecker {
028
029 /**
030 * Check that a value is not null. If null throws an IllegalArgumentException.
031 *
032 * @param obj value.
033 * @param name parameter name for the exception message.
034 * @return the given value.
035 */
036 public static <T> T notNull(T obj, String name) {
037 if (obj == null) {
038 throw new IllegalArgumentException(name + " cannot be null");
039 }
040 return obj;
041 }
042
043 /**
044 * Check that a list is not null and that none of its elements is null. If null or if the list has emtpy elements
045 * throws an IllegalArgumentException.
046 *
047 * @param list the list of strings.
048 * @param name parameter name for the exception message.
049 * @return the given list.
050 */
051 public static <T> List<T> notNullElements(List<T> list, String name) {
052 notNull(list, name);
053 for (int i = 0; i < list.size(); i++) {
054 notNull(list.get(i), XLog.format("list [{0}] element [{1}]", name, i));
055 }
056 return list;
057 }
058
059 /**
060 * Check that a string is not null and not empty. If null or emtpy throws an IllegalArgumentException.
061 *
062 * @param str value.
063 * @param name parameter name for the exception message.
064 * @return the given value.
065 */
066 public static String notEmpty(String str, String name) {
067 return notEmpty(str, name, null);
068 }
069
070 /**
071 * Check that a string is not null and not empty. If null or emtpy throws an IllegalArgumentException.
072 *
073 * @param str value.
074 * @param name parameter name for the exception message.
075 * @param info additional information to be printed with the exception message
076 * @return the given value.
077 */
078 public static String notEmpty(String str, String name, String info) {
079 if (str == null) {
080 throw new IllegalArgumentException(name + " cannot be null" + (info == null ? "" : ", " + info));
081 }
082 if (str.length() == 0) {
083 throw new IllegalArgumentException(name + " cannot be empty" + (info == null ? "" : ", " + info));
084 }
085 return str;
086 }
087
088 /**
089 * Check that a list is not null and that none of its elements is null. If null or if the list has emtpy elements
090 * throws an IllegalArgumentException.
091 *
092 * @param list the list of strings.
093 * @param name parameter name for the exception message.
094 * @return the given list.
095 */
096 public static List<String> notEmptyElements(List<String> list, String name) {
097 notNull(list, name);
098 for (int i = 0; i < list.size(); i++) {
099 notEmpty(list.get(i), XLog.format("list [{0}] element [{1}]", name, i));
100 }
101 return list;
102 }
103
104 private static final int MAX_NODE_NAME_LEN = 50;
105
106 /**
107 * Check that the given string is a valid action name [a-zA-Z_][0-9a-zA-Z_\-]* and not longer than 50 chars.
108 *
109 * @param actionName string to validate is a token.
110 * @return the given string.
111 */
112 public static String validateActionName(String actionName) {
113 ParamChecker.notEmpty(actionName, "action name");
114 if (actionName.length() > MAX_NODE_NAME_LEN) {
115 throw new IllegalArgumentException(XLog.format("name [{0}] must be {1} chars or less", actionName,
116 MAX_NODE_NAME_LEN));
117 }
118
119 char c = actionName.charAt(0);
120 if (!(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') && !(c == '_')) {
121 throw new IllegalArgumentException(XLog.format("name [{0}], must start with [A-Za-z_]", actionName));
122 }
123 for (int i = 1; i < actionName.length(); i++) {
124 c = actionName.charAt(i);
125 if (!(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z')
126 && !(c == '_' || c == '-')) {
127 throw new IllegalArgumentException(XLog.format("name [{0}] must be [A-Za-z_][0-9A-Za-z_]*", actionName));
128 }
129 }
130 return actionName;
131 }
132
133 /**
134 * Return if the specified token is a valid Java identifier.
135 *
136 * @param token string to validate if it is a valid Java identifier.
137 * @return if the specified token is a valid Java identifier.
138 */
139 public static boolean isValidIdentifier(String token) {
140 ParamChecker.notEmpty(token, "identifier");
141 for (int i = 0; i < token.length(); i++) {
142 char c = token.charAt(i);
143 if (!(c >= '0' && c <= '9') && !(c >= 'A' && c <= 'Z') && !(c >= 'a' && c <= 'z') && !(c == '_')) {
144 return false;
145 }
146 if (i == 0 && (c >= '0' && c <= '9')) {
147 return false;
148 }
149 }
150 return true;
151 }
152
153 /**
154 * Check whether the value is greater than or equals 0.
155 *
156 * @param value : value to test
157 * @param name : Name of the parameter
158 * @return If the value is > 0, return the value. Otherwise throw IllegalArgumentException
159 */
160 public static int checkGTZero(int value, String name) {
161 if (value <= 0) {
162 throw new IllegalArgumentException(XLog.format("parameter [{0}] = [{1}] must be greater than zero", name,
163 value));
164 }
165 return value;
166 }
167
168 /**
169 * Check whether the value is greater than or equals to 0.
170 *
171 * @param value : value to test
172 * @param name : Name of the parameter
173 * @return If the value is >= 0, return the value. Otherwise throw IllegalArgumentException
174 */
175 public static int checkGEZero(int value, String name) {
176 if (value < 0) {
177 throw new IllegalArgumentException(XLog.format(
178 "parameter [{0}] = [{1}] must be greater than or equals zero", name, value));
179 }
180 return value;
181 }
182
183 /**
184 * Check whether the value is Integer.
185 *
186 * @param value : value to test
187 * @param name : Name of the parameter
188 * @return If the value is integer, return the value. Otherwise throw IllegalArgumentException
189 */
190 public static int checkInteger(String val, String name) {
191 int ret;
192 try {
193 ret = Integer.parseInt(val);
194 }
195 catch (NumberFormatException nex) {
196 throw new IllegalArgumentException(XLog.format(
197 "parameter [{0}] = [{1}] must be an integer. Parsing error {2}", name, val, nex.getMessage(), nex));
198 }
199 return ret;
200 }
201
202 /**
203 * Check whether the value is Oozie processing timezone data format.
204 *
205 * @param value : value to test
206 * @param name : Name of the parameter
207 * @return If the value is in Oozie processing timezone date format, return the value.
208 * Otherwise throw IllegalArgumentException
209 */
210 public static Date checkDateOozieTZ(String date, String name) {
211 Date ret;
212 try {
213 ret = DateUtils.parseDateOozieTZ(date);
214 }
215 catch (Exception ex) {
216 throw new IllegalArgumentException(XLog.format(
217 "parameter [{0}] = [{1}] must be Date in {2} format ({3})."
218 + " Parsing error {4}", name, date, DateUtils.getOozieProcessingTimeZone().getID(),
219 DateUtils.getOozieTimeMask(), ex));
220 }
221 return ret;
222 }
223
224 /**
225 * Check whether the value mention correct Timezone.
226 *
227 * @param value : value to test
228 * @param name : Name of the parameter
229 * @return If the value is correct TZ return the value. Otherwise throw IllegalArgumentException
230 */
231 public static TimeZone checkTimeZone(String tzStr, String name) {
232 TimeZone tz;
233 try {
234 tz = DateUtils.getTimeZone(tzStr);
235 }
236 catch (Exception ex) {
237 throw new IllegalArgumentException(XLog.format("parameter [{0}] = [{1}] must be a valid TZ."
238 + " Parsing error {2}", name, tzStr, ex.getMessage(), ex));
239 }
240 return tz;
241 }
242
243 /**
244 * Check whether an item is a member of an array of string
245 *
246 * @param item : item to test
247 * @param members : List of items in string
248 * @param name : Name of the parameter
249 * @return If the item is in the member return true. Otherwise throw IllegalArgumentException
250 */
251 public static boolean isMember(String item, String[] members, String name) {
252 for (int i = 0; i < members.length; i++) {
253 if (members[i].equals(item)) {
254 return true;
255 }
256 }
257 // Error case
258 StringBuilder buff = new StringBuilder();
259 for (int i = 0; i < members.length; i++) {
260 buff.append(members[i]).append(", ");
261 }
262 throw new IllegalArgumentException(XLog.format("parameter [{0}] = [{1}] " + "must be in the list {2}", name,
263 item, buff.toString()));
264 }
265 }