Subversion Repositories XServices

Rev

Rev 150 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
150 brianR 1
/*
2
 *   Copyright 2014 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.emitter.util;
17
 
18
import java.io.File;
19
 
20
import org.apache.log4j.Logger;
21
 
22
/**
23
 * The Class EmitterUtil.
24
 *
25
 * @author Brian Rosenberger, bru(at)brutex.de
26
 */
27
public final class EmitterUtil {
28
 
29
 
30
	private static Logger logger = Logger.getLogger(EmitterUtil.class);
31
 
32
 
33
	/**
34
	 * Check if the file exists and is readable.
35
	 *
36
	 * @param file The file to check
37
	 * @param isWritable also check if file is writable
38
	 * @param isExecutable also check if file is executable
39
	 * @return
40
	 */
41
	public static boolean verifyFile(String file, boolean isWritable, boolean isExecutable) {
42
		logger.debug(String.format("Verifying file '%s'.", file));
43
		File f = new File(file);
44
		String fn = f.getAbsolutePath();
45
		if(f.exists() && f.isFile()) {
46
			if(isWritable && !f.canWrite()) {
47
				logger.error(String.format("Cannot write to file '%s'.", fn));
48
				return false;
49
			}
50
			if(isExecutable && !f.canExecute()) {
51
				logger.error(String.format("Cannot execute file '%s'.", fn));
52
				return false;
53
			}
54
		return true;
55
		}
56
		logger.error(String.format("Cannot read file '%s'.", fn));
57
		return false;
58
	}
59
 
60
}