Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.36 KB | None | 0 0
  1. // Sanity checks
  2. sanity = new SanityChecks();
  3. logger.info("Checking sanity...");
  4. try {
  5. sanity.doBasicChecks();
  6. logger.info("It all looks sane.");
  7. }
  8. catch( final Exception ex){
  9. logger.error("Something is wrong, we are not sane. Aborting...", ex);
  10. stop();
  11. }
  12.  
  13. package nz.co.great_ape.tempusFugit;
  14.  
  15. import com.almworks.sqlite4java.SQLite;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18.  
  19. import java.io.IOException;
  20. import java.nio.file.Files;
  21. import java.nio.file.Paths;
  22.  
  23. /**
  24. * Checks to see if all is ok for standard usage of tempusFugit.
  25. *
  26. * Checks for valid paths, existing database etc, if not assumes that
  27. * this is the first time tempusFugit has been run for this user and
  28. * tries to set up the basic files and data.
  29. *
  30. * @author Rob Brown-Bayliss
  31. * Created on 2/11/14
  32. */
  33. public class SanityChecks {
  34.  
  35.  
  36. public static final Logger logger = LoggerFactory.getLogger(AppConsts.LOGGER_NAME);
  37.  
  38. public SanityChecks() {
  39. // todo
  40. // todo
  41. SQLite.setLibraryPath(AppConsts.HOME_DIR); // TODO: Why is this required? can we not place the native libs somewhere else or in the jar?
  42. // todo
  43. // todo
  44. }
  45.  
  46. /**
  47. * Performs basic checks to see if it is ok to run the app. If not it will attempt to
  48. * set up for first time use.
  49. *
  50. * @return true if all is ok
  51. */
  52. public boolean doBasicChecks() {
  53. logger.info("Starting basic checks...");
  54. if (!HomeDirExists()) {
  55. return false;
  56. }
  57. if (!DatabaseExists()) {
  58. logger.info("Trying to create a new database.");
  59. DatabaseUpgrade dug = new DatabaseUpgrade();
  60. if (!dug.upGrade()) {
  61. return false;
  62. }
  63. logger.info("Created a new database.");
  64. // At this point a usable database should exist and it should be current
  65. }
  66. if (!DatabaseVersionCorrect()) {
  67. // // If the database is old we will upgrade it to the current version.
  68. // DatabaseUpgrade dug = new DatabaseUpgrade();
  69. // if (!dug.upGrade()) {
  70. // return false;
  71. // }
  72. logger.info("is this it?.");
  73. }
  74. // At this point all basic checks have passed and we are good to go...
  75. logger.info("Basic checks are complete. All is good in the universe...");
  76. return true;
  77. }
  78.  
  79. /**
  80. * Checks if the app home directory exists, if not it tries to create it.
  81. *
  82. * @return true if exists or was able to create the directory
  83. */
  84. private boolean HomeDirExists() {
  85. if (!Files.isDirectory(Paths.get(AppConsts.HOME_DIR))) {
  86. try {
  87. Files.createDirectory(Paths.get(AppConsts.HOME_DIR));
  88. }
  89. catch (IOException io) {
  90. logger.error("The directory " + AppConsts.HOME_DIR + " does not exist and could not be created. This is not the best but we can survive.", io);
  91. return false;
  92. }
  93. }
  94. logger.info("The directory " + AppConsts.HOME_DIR + " exists. This is good.");
  95. return true;
  96. }
  97.  
  98. /**
  99. * Checks if the SQLite database exists, if not it tries to create it.
  100. * or was able to create the database
  101. *
  102. * @return true if the database exists
  103. */
  104. private boolean DatabaseExists() {
  105. if (Files.notExists(Paths.get(AppConsts.TF_DATABASE))) {
  106. logger.error("The database " + AppConsts.TF_DATABASE + " does not exist. This is bad.");
  107. return false;
  108. }
  109. logger.info("The database " + AppConsts.TF_DATABASE + " exists. This is good.");
  110. return true;
  111. }
  112.  
  113. /**
  114. * Checks if the SQLite database is correct for this version.
  115. *
  116. * @return true if correct version
  117. */
  118. private boolean DatabaseVersionCorrect() {
  119. Integer expectedVersion = AppConsts.TF_DATABASE_VERSION;
  120. logger.info("Checking the database version is correct. Looking for version "+ expectedVersion + "." );
  121. DatabaseUpgrade dug = new DatabaseUpgrade();
  122. logger.info("Checking the database version is correct. Looking for version "+ expectedVersion + "." );
  123. Integer currentVersion = dug.getVersion();
  124. if (currentVersion < expectedVersion) {
  125. logger.info("Expected version " + expectedVersion + ", but database is version " + currentVersion + ". This is bad.");
  126. return false;
  127. }
  128. logger.info("The database version is correct. This is good.");
  129. return true;
  130. }
  131.  
  132. }
  133.  
  134. package nz.co.great_ape.tempusFugit;
  135.  
  136. import com.almworks.sqlite4java.*;
  137. import org.slf4j.Logger;
  138. import org.slf4j.LoggerFactory;
  139.  
  140. import java.io.File;
  141. import java.nio.file.Files;
  142. import java.nio.file.Paths;
  143.  
  144. /**
  145. * Setup the database for current version tempusFugit. This will upgrade an
  146. * existing database or create a new empty database if required.
  147. *
  148. * @author Rob Brown-Bayliss
  149. * Created on 4/11/14
  150. */
  151. public class
  152. DatabaseUpgrade {
  153.  
  154. public static final Logger logger = LoggerFactory.getLogger(AppConsts.LOGGER_NAME);
  155.  
  156. private SQLiteConnection dbConn = null;
  157. private SQLiteQueue sQueue = null;
  158. private int currentVersion;
  159.  
  160. public DatabaseUpgrade() {
  161.  
  162. }
  163.  
  164. /**
  165. * Attempts to upgrade the existing database to the current version, or if
  166. * there is no existing database then create one suitable for the current app version.
  167. *
  168. * @return true if there is a current and usable database
  169. */
  170. public boolean upGrade() {
  171. logger.info(" Started an upgrade on the database, if the database does not exist it will be created at the current version, ");
  172. logger.info(" if it exists but is an older version it will be upgraded to the current version.");
  173. if (openDatabase()) {
  174. currentVersion = getVersion();
  175. if (currentVersion == AppConsts.FAIL) {
  176. logger.info("Database version is unknown. The file will be deleted and a new database created. We can survive this.");
  177. // TODO: Ask user if we should delete the old one or make a backup?
  178. closeDatabase();
  179. deleteDatabase();
  180. openDatabase();
  181. }
  182. if (currentVersion != AppConsts.TF_DATABASE_VERSION) {
  183. logger.info("Current Database version is " + currentVersion);
  184. // TODO: Backup current database.
  185. if (currentVersion < 1) {
  186. if (!makeVersionOne()) {
  187. logger.error("Unable to upgrade the database. This is VERY bad.");
  188. return false;
  189. }
  190. }
  191. currentVersion = 1;
  192. }
  193. closeDatabase(); // good practice
  194. }
  195. logger.info("The database is the current version. This is good.");
  196. return true;
  197. }
  198.  
  199. /**
  200. * Turns a blank SQLite database into a tempusFugit version 1 database by
  201. * adding the required tables and data.
  202. */
  203. private boolean makeVersionOne() {
  204. logger.info("Attempting to upgrade to version 1.");
  205. String CT_SQL = "CREATE TABLE IF NOT EXISTS dbVersion (id INTEGER PRIMARY KEY AUTOINCREMENT, version INTEGER NOT NULL UNIQUE, ";
  206. CT_SQL = CT_SQL + "upgraded INTEGER(4) NOT NULL DEFAULT (strftime('%s','now'))); ";
  207. String IN_SQL = "INSERT INTO dbVersion(version) values (1); ";
  208. try {
  209. execSQL("BEGIN TRANSACTION; ");
  210. execSQL(CT_SQL); // create the table
  211. execSQL(IN_SQL); // insert the record
  212. execSQL("COMMIT; ");
  213. }
  214. catch (Exception ex) {
  215. logger.error("Attempted upgrade of " + AppConsts.TF_DATABASE + " to version 1 has failed. This is VERY bad.", ex);
  216. return false;
  217. }
  218. logger.info("The database has been upgraded to version 1. This is good.");
  219. return true;
  220. }
  221.  
  222. private Integer execSQL(String SQL) {
  223. return sQueue.execute(new SQLiteJob<Integer>() {
  224. protected Integer job(SQLiteConnection con) throws SQLiteException {
  225. SQLiteStatement st = null;
  226. try {
  227. st = con.prepare(SQL);
  228. st.step();
  229. return AppConsts.SUCCESS;
  230. }
  231. catch (Exception ex) {
  232. logger.error("Tried to execute SQL: " + SQL, ex);
  233. return AppConsts.FAIL;
  234. }
  235. finally {
  236. if (st != null) {
  237. st.dispose();
  238. }
  239. }
  240. }
  241. }).complete();
  242. }
  243.  
  244. /**
  245. * Gets the current database version
  246. *
  247. * @return version as an int
  248. */
  249. public int getVersion() {
  250. return sQueue.execute(new SQLiteJob<Integer>() {
  251. protected Integer job(SQLiteConnection con) throws SQLiteException {
  252. SQLiteStatement st = null;
  253. try {
  254. st = con.prepare("SELECT version, upgraded FROM dbVersion ORDER BY upgraded DESC LIMIT 1;");
  255. st.step();
  256. return st.columnInt(0);
  257. }
  258. catch (Exception ex) {
  259. logger.error("The database version of " + AppConsts.TF_DATABASE + " is unknown. This is bad.", ex);
  260. return AppConsts.FAIL;
  261. }
  262. finally {
  263. if (st != null) {
  264. st.dispose();
  265. }
  266. }
  267. }
  268. }).complete();
  269. }
  270.  
  271. /**
  272. * Opens an existing SQLite database or creates a new blank database
  273. * if none exists.
  274. *
  275. * @return false if there is a problem. // TODO: Is it possible to have a problem?
  276. */
  277. private boolean openDatabase() {
  278. try {
  279. dbConn = new SQLiteConnection(new File(AppConsts.TF_DATABASE));
  280. dbConn.open(true);
  281. sQueue = new SQLiteQueue(new File(AppConsts.TF_DATABASE));
  282. sQueue.start();
  283. return true;
  284. }
  285. catch (Exception ex) {
  286. logger.error("The database " + AppConsts.TF_DATABASE + " could not be opened or created. This is VERY bad.", ex);
  287. return false;
  288. }
  289. }
  290.  
  291. /**
  292. * Closes an open database.
  293. *
  294. * @return false if there is a problem. // TODO: Is it possible to have a problem?
  295. */
  296. private boolean closeDatabase() {
  297. try {
  298. if (dbConn != null) {
  299. dbConn.dispose();
  300. }
  301. return true;
  302. }
  303. catch (Exception ex) {
  304. logger.error("The database " + AppConsts.TF_DATABASE + " could not be closed.", ex);
  305. return false;
  306. }
  307. }
  308.  
  309. /**
  310. * Deletes an existing database.
  311. *
  312. * @return false if there is a problem. // TODO: Is it possible to have a problem?
  313. */
  314. private boolean deleteDatabase() {
  315. try {
  316. Files.delete(Paths.get(AppConsts.TF_DATABASE));
  317. logger.info("The database " + AppConsts.TF_DATABASE + " has been deleted.");
  318. return true;
  319. }
  320. catch (Exception ex) {
  321. logger.error("The database " + AppConsts.TF_DATABASE + " could not be deleted.", ex);
  322. return false;
  323. }
  324. }
  325. }
  326.  
  327.  
  328. Plus the constants:
  329.  
  330. package nz.co.great_ape.tempusFugit;
  331.  
  332. /**
  333. * Constants used by tempusFugit
  334. *
  335. * @author Rob Brown-Bayliss
  336. * Created on 31/10/14
  337. */
  338. public class AppConsts {
  339.  
  340. // Application
  341. public static final String APP_NAME = "Tempus Fugit";
  342. public static final String VERSION_NUMBER = "0.0.1";
  343.  
  344. // Debug Mode On-Off
  345. public static final boolean DEBUG_MODE = true;
  346.  
  347. // Data files and paths
  348. public static final String FS = System.getProperty("file.separator");
  349. public static final String HOME_DIR = System.getProperty("user.home") + FS + ".tempusFugit"; // This is the tempusFugit home, not the users home.
  350. public static final String USER_NAME = System.getProperty("user.name");
  351.  
  352. public static final String LOGGER_NAME = "nz.co.great_ape.tempusFugit";
  353. public static final String LOG_FILE = HOME_DIR + FS + "tempusFugit.log";
  354.  
  355. // Database
  356. public static final String TF_DATABASE = HOME_DIR + FS + "tfData.sql";
  357. public static final int TF_DATABASE_VERSION = 1; // This is the current version of the database
  358.  
  359. // Error codes
  360. public static final int UNKNOWN = -1;
  361. public static final int FAIL = 0;
  362. public static final int SUCCESS = 1;
  363.  
  364. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement