| 1 | |
package us.daveread.utility.formatcheck.util; |
| 2 | |
|
| 3 | |
import java.util.*; |
| 4 | |
import java.io.*; |
| 5 | |
import org.apache.log4j.Logger; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public class Configuration extends Properties { |
| 27 | |
private static Configuration config; |
| 28 | 2 | private static final Logger logger = Logger.getLogger(Configuration.class); |
| 29 | |
|
| 30 | |
private final static String PROPERTIES_DIRECTORY = "FormatCheck"; |
| 31 | |
private final static String FILENAME_PROPERTIES = "FormatCheck.Properties"; |
| 32 | |
private final static String PROP_SYSTEM_USERHOMEDIR = "user.home"; |
| 33 | |
|
| 34 | |
private static String userHomeDirectory; |
| 35 | |
|
| 36 | 1 | private Configuration() { |
| 37 | 1 | userHomeDirectory = System.getProperty(PROP_SYSTEM_USERHOMEDIR); |
| 38 | |
|
| 39 | 1 | if (userHomeDirectory == null) { |
| 40 | 0 | userHomeDirectory = PROPERTIES_DIRECTORY; |
| 41 | |
} else { |
| 42 | 1 | userHomeDirectory += "/" + PROPERTIES_DIRECTORY; |
| 43 | 1 | new File(userHomeDirectory).mkdirs(); |
| 44 | |
} |
| 45 | 1 | } |
| 46 | |
|
| 47 | |
public static synchronized Configuration instance() { |
| 48 | 3 | if (config == null) { |
| 49 | 1 | config = new Configuration(); |
| 50 | |
} |
| 51 | |
|
| 52 | 3 | config.setupProperties(); |
| 53 | |
|
| 54 | 3 | return config; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public void store() { |
| 58 | |
File configFile; |
| 59 | |
|
| 60 | 1 | configFile = getFile(FILENAME_PROPERTIES); |
| 61 | |
|
| 62 | |
try { |
| 63 | 1 | store(new FileOutputStream(configFile, false), "FormatCheck Configuration"); |
| 64 | |
} |
| 65 | 0 | catch (Throwable any) { |
| 66 | 0 | logger.error("Unable to store properties file (" + |
| 67 | |
configFile.getAbsolutePath() + ")", any); |
| 68 | 1 | } |
| 69 | 1 | } |
| 70 | |
|
| 71 | |
private void setupProperties() { |
| 72 | |
File propFile; |
| 73 | |
|
| 74 | 3 | propFile = getFile(FILENAME_PROPERTIES); |
| 75 | |
|
| 76 | |
try { |
| 77 | 3 | config.load(new FileInputStream(propFile)); |
| 78 | |
} |
| 79 | 0 | catch (Throwable any) { |
| 80 | 0 | logger.error("Unable to load properties file (" + |
| 81 | |
propFile.getAbsolutePath() + ")", any); |
| 82 | 3 | } |
| 83 | 3 | } |
| 84 | |
|
| 85 | |
public File getFile(String fileName) { |
| 86 | |
File configFile; |
| 87 | |
|
| 88 | 8 | configFile = new File(getFilePath(fileName)); |
| 89 | 8 | if (!configFile.exists()) { |
| 90 | 3 | userDefaultFile(fileName); |
| 91 | |
} |
| 92 | |
|
| 93 | 8 | return configFile; |
| 94 | |
} |
| 95 | |
|
| 96 | |
private String getFilePath(String fileName) { |
| 97 | 8 | return userHomeDirectory + "/" + fileName; |
| 98 | |
} |
| 99 | |
|
| 100 | |
private void userDefaultFile(String fileName) { |
| 101 | |
File realFile, defaultFile; |
| 102 | |
|
| 103 | 3 | realFile = new File(userHomeDirectory + "/" + fileName); |
| 104 | 3 | if (!realFile.exists()) { |
| 105 | 3 | defaultFile = new File(fileName + ".txt"); |
| 106 | 3 | if (defaultFile.exists()) { |
| 107 | 1 | copyFile(defaultFile, realFile); |
| 108 | |
} |
| 109 | |
} |
| 110 | 3 | } |
| 111 | |
|
| 112 | |
private static void copyFile(File source, File dest) { |
| 113 | |
BufferedReader in; |
| 114 | |
PrintWriter out; |
| 115 | |
String data; |
| 116 | |
|
| 117 | 1 | in = null; |
| 118 | 1 | out = null; |
| 119 | |
|
| 120 | |
try { |
| 121 | 1 | in = new BufferedReader(new FileReader(source)); |
| 122 | 1 | out = new PrintWriter(new FileWriter(dest)); |
| 123 | |
|
| 124 | 3 | while ((data = in.readLine()) != null) { |
| 125 | 2 | out.println(data); |
| 126 | |
} |
| 127 | 1 | } |
| 128 | 0 | catch (Throwable any) { |
| 129 | 0 | logger.warn("Unable to copy default file (" + source.getAbsolutePath() + |
| 130 | |
") to configuration file (" + dest.getAbsolutePath() + ")", any); |
| 131 | 0 | } |
| 132 | |
finally { |
| 133 | 0 | if (in != null) { |
| 134 | |
try { |
| 135 | 1 | in.close(); |
| 136 | |
} |
| 137 | 0 | catch (Throwable any) { |
| 138 | |
|
| 139 | 1 | } |
| 140 | |
} |
| 141 | 1 | if (out != null) { |
| 142 | |
try { |
| 143 | 1 | out.close(); |
| 144 | |
} |
| 145 | 0 | catch (Throwable any) { |
| 146 | |
|
| 147 | 2 | } |
| 148 | |
} |
| 149 | 1 | } |
| 150 | 1 | } |
| 151 | |
} |