1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.common.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  public class ModeUtils {
25  
26  	private static final Logger logger = LoggerFactory.getLogger(ModeUtils.class);
27  
28  	public static final void validate(Mode mode, String msg) {
29  		validate(mode, msg, msg);
30  	}
31  
32  	public static final void validate(Mode mode, String msg, String errMsg) {
33  		validate(mode, msg, null, errMsg);
34  	}
35  
36  	public static final void validate(Mode mode, String msg, Object arg, String errMsg) {
37  		validate(mode, msg, arg, null, errMsg);
38  	}
39  
40  	public static final void validate(Mode mode, String msg, Object arg1, Object arg2, String errMsg) {
41  		validate(mode, msg, getArgs(arg1, arg2), errMsg);
42  	}
43  
44  	public static final void validate(Mode mode, String msg, Object[] args, String errMsg) {
45  		switch (mode) {
46  		case IGNORE:
47  			return;
48  		case DEBUG:
49  			logger.debug(msg, args);
50  			return;
51  		case INFORM:
52  			logger.info(msg, args);
53  			return;
54  		case WARN:
55  			logger.warn(msg, args);
56  			return;
57  		case ERROR:
58  			logger.error(msg, args);
59  			throw new IllegalStateException(errMsg);
60  		default:
61  			throw new IllegalArgumentException("Mode '" + mode + "' is unknown");
62  		}
63  	}
64  
65  	protected static final Object[] getArgs(Object arg1, Object arg2) {
66  		if (arg1 == null && arg2 == null) {
67  			return null;
68  		}
69  		List<Object> args = new ArrayList<Object>();
70  		if (arg1 != null) {
71  			args.add(arg1);
72  		}
73  		if (arg2 != null) {
74  			args.add(arg2);
75  		}
76  		return CollectionUtils.toObjectArray(args);
77  	}
78  
79  }