xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java (revision 0e4c55544f8ea0a68e8bae37b0e502097e008ec1)
1b5840353SAdam Hornáček /*
2b5840353SAdam Hornáček  * CDDL HEADER START
3b5840353SAdam Hornáček  *
4b5840353SAdam Hornáček  * The contents of this file are subject to the terms of the
5b5840353SAdam Hornáček  * Common Development and Distribution License (the "License").
6b5840353SAdam Hornáček  * You may not use this file except in compliance with the License.
7b5840353SAdam Hornáček  *
8b5840353SAdam Hornáček  * See LICENSE.txt included in this distribution for the specific
9b5840353SAdam Hornáček  * language governing permissions and limitations under the License.
10b5840353SAdam Hornáček  *
11b5840353SAdam Hornáček  * When distributing Covered Code, include this CDDL HEADER in each
12b5840353SAdam Hornáček  * file and include the License file at LICENSE.txt.
13b5840353SAdam Hornáček  * If applicable, add the following below this CDDL HEADER, with the
14b5840353SAdam Hornáček  * fields enclosed by brackets "[]" replaced with your own identifying
15b5840353SAdam Hornáček  * information: Portions Copyright [yyyy] [name of copyright owner]
16b5840353SAdam Hornáček  *
17b5840353SAdam Hornáček  * CDDL HEADER END
18b5840353SAdam Hornáček  */
19b5840353SAdam Hornáček 
20b5840353SAdam Hornáček  /*
21*5d9f3aa0SAdam Hornáček  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
22b5840353SAdam Hornáček  */
239805b761SAdam Hornáček package org.opengrok.indexer.configuration;
24b5840353SAdam Hornáček 
25b5840353SAdam Hornáček import java.io.File;
26b5840353SAdam Hornáček import java.io.IOException;
27b5840353SAdam Hornáček import java.util.Date;
28b5840353SAdam Hornáček import java.util.logging.Level;
29b5840353SAdam Hornáček import java.util.logging.Logger;
309805b761SAdam Hornáček import org.opengrok.indexer.logger.LoggerFactory;
31b5840353SAdam Hornáček 
32b5840353SAdam Hornáček public class IndexTimestamp {
33b5840353SAdam Hornáček     private transient Date lastModified;
34b5840353SAdam Hornáček 
35b5840353SAdam Hornáček     private static final Logger LOGGER = LoggerFactory.getLogger(IndexTimestamp.class);
36b5840353SAdam Hornáček 
37b5840353SAdam Hornáček     /**
38b5840353SAdam Hornáček      * Get the date of the last index update.
39b5840353SAdam Hornáček      *
40b5840353SAdam Hornáček      * @return the time of the last index update.
41b5840353SAdam Hornáček      */
getDateForLastIndexRun()42b5840353SAdam Hornáček     public Date getDateForLastIndexRun() {
43b5840353SAdam Hornáček         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
44b5840353SAdam Hornáček         if (lastModified == null) {
45b5840353SAdam Hornáček             File timestamp = new File(env.getDataRootFile(), "timestamp");
46b5840353SAdam Hornáček             if (timestamp.exists()) {
47b5840353SAdam Hornáček                 lastModified = new Date(timestamp.lastModified());
48b5840353SAdam Hornáček             }
49b5840353SAdam Hornáček         }
50b5840353SAdam Hornáček         return lastModified;
51b5840353SAdam Hornáček     }
52b5840353SAdam Hornáček 
refreshDateForLastIndexRun()53b5840353SAdam Hornáček     public void refreshDateForLastIndexRun() {
54b5840353SAdam Hornáček         lastModified = null;
55b5840353SAdam Hornáček     }
56b5840353SAdam Hornáček 
stamp()57b5840353SAdam Hornáček     public void stamp() throws IOException {
58b5840353SAdam Hornáček         RuntimeEnvironment env = RuntimeEnvironment.getInstance();
59b5840353SAdam Hornáček         File timestamp = new File(env.getDataRootFile(), "timestamp");
60b5840353SAdam Hornáček         String purpose = "used for timestamping the index database.";
61b5840353SAdam Hornáček         if (timestamp.exists()) {
62b5840353SAdam Hornáček             if (!timestamp.setLastModified(System.currentTimeMillis())) {
63b5840353SAdam Hornáček                 LOGGER.log(Level.WARNING, "Failed to set last modified time on ''{0}'', {1}",
64b5840353SAdam Hornáček                     new Object[]{timestamp.getAbsolutePath(), purpose});
65b5840353SAdam Hornáček             }
66b5840353SAdam Hornáček         } else {
67b5840353SAdam Hornáček             if (!timestamp.createNewFile()) {
68b5840353SAdam Hornáček                 LOGGER.log(Level.WARNING, "Failed to create file ''{0}'', {1}",
69b5840353SAdam Hornáček                     new Object[]{timestamp.getAbsolutePath(), purpose});
70b5840353SAdam Hornáček             }
71b5840353SAdam Hornáček         }
72b5840353SAdam Hornáček     }
73b5840353SAdam Hornáček }
74