Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / tasks / ScheduledTasks.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.tasks;
22
23 import java.io.File;
24 import java.util.Arrays;
25 import java.util.Date;
26
27 import org.apache.commons.io.FilenameUtils;
28 import org.apache.commons.io.comparator.LastModifiedFileComparator;
29 import org.onap.aai.aailog.logs.AaiScheduledTaskAuditLog;
30 import org.onap.aai.util.AAIConfig;
31 import org.onap.aai.util.AAIConstants;
32 import org.onap.logging.filter.base.ONAPComponents;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.scheduling.annotation.Scheduled;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class ScheduledTasks {
41
42     @Autowired
43     private AaiScheduledTaskAuditLog auditLog;
44
45     private static Logger LOGGER = LoggerFactory.getLogger(ScheduledTasks.class);
46     private static final long PROPERTY_READ_INTERVAL = 60000; // every minute
47     private String GlobalPropFileName = AAIConstants.AAI_CONFIG_FILENAME;
48
49     // for read and possibly reloading aaiconfig.properties and other
50     /**
51      * Load AAI properties.
52      */
53     // configuration properties files
54     @Scheduled(fixedRate = PROPERTY_READ_INTERVAL)
55     public void loadAAIProperties() {
56         auditLog.logBefore("LoadAaiPropertiesTask", ONAPComponents.AAI.toString());
57         String dir = FilenameUtils.getFullPathNoEndSeparator(GlobalPropFileName);
58         if (dir == null || dir.length() < 3) {
59             dir = "/opt/aai/etc";
60         }
61
62         File pdir = new File(dir);
63         File[] files = pdir.listFiles();
64         Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
65         String fn;
66
67         // leave this loop here since we may want to check other configurable
68         // property files in the SAME directory
69         for (File file : files) {
70             fn = file.getName();
71             if (fn.equals("aaiconfig.properties")) {
72                 Date lastMod = new Date(file.lastModified());
73                 long lastModTm = lastMod.getTime();
74                 Date curTS = new Date();
75                 long curTSTm = curTS.getTime();
76                 if (curTSTm - lastModTm < PROPERTY_READ_INTERVAL + 1000) {
77                     AAIConfig.reloadConfig();
78                     LOGGER.debug("reloaded from aaiconfig.properties");
79                 }
80                 break;
81             }
82         }
83         auditLog.logAfter();
84     }
85 }