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