View Javadoc
1   package org.kuali.ole.docstore.utility;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.net.URISyntaxException;
6   import java.text.SimpleDateFormat;
7   import java.util.Date;
8   
9   import org.apache.commons.io.FileUtils;
10  import org.apache.commons.lang.StringUtils;
11  import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil;
12  import org.kuali.ole.docstore.indexer.solr.IndexerService;
13  import org.kuali.ole.docstore.util.DocStoreEnvUtil;
14  import org.kuali.ole.repository.DeleteManager;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  /**
19   *
20   */
21  public class DocumentStoreMaintenance {
22      DeleteManager deleteManager = new DeleteManager();
23      IndexerService indexerService = null;
24      DocStoreEnvUtil docStoreEnvUtil = new DocStoreEnvUtil();
25      DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
26      private static Logger logger = LoggerFactory.getLogger(DocumentStoreMaintenance.class);
27      private static final String PREPARE_FOR_NEW_BUILD = "prepareForNewBuild";
28      private static final String CLEANUP_DATA = "cleanupData";
29      private static final String RESET_DOCSTORE = "resetDocstore";
30  
31      /**
32       * Used to rename the folder / file with the current timestamp.
33       *
34       * @param filePath
35       * @param timeStamp
36       */
37      private void renameFolder(String filePath, String timeStamp) {
38          File srcFile = new File(filePath);
39          File destFile = new File(filePath + "-" + timeStamp);
40          boolean success = srcFile.renameTo(destFile);
41          if (success) {
42              logger.info("Renamed " + filePath + " as " + destFile.getAbsolutePath());
43          } else {
44              logger.info("Failed to rename " + filePath + " as " + destFile.getAbsolutePath());
45          }
46  
47      }
48  
49      /**
50       * Used to rename the %ole.docstore.home/repository folder with the current timestamp.
51       *
52       * @param timeStamp
53       */
54  //    private void deleteDocStoreData(String timeStamp) {
55  //        String docStoreDataFolder = docStoreEnvUtil.getJackrabbitFolderPath();
56  //        renameFolder(docStoreDataFolder, timeStamp);
57  //    }
58  
59      /**
60       * Used to rename the %ole.discovery.home%/solr-data folder with the current timestamp.
61       *
62       * @param timeStamp
63       */
64      private void deleteDiscoveryData(String timeStamp) {
65          String discoveryDateFilePath = discoveryEnvUtil.getSolrDataFolderPath();
66          renameFolder(discoveryDateFilePath, timeStamp);
67      }
68  
69      /**
70       * Used to rename the %ole.docstore.home/Properties folder with the current timestamp.
71       *
72       * @param timeStamp
73       */
74      private void deleteDocstoreProperties(String timeStamp) {
75          String docStorePropertiesFilePath = docStoreEnvUtil.getDocStorePropertiesFolderPath();
76          renameFolder(docStorePropertiesFilePath, timeStamp);
77      }
78  
79      /**
80       * Used to rename the %ole.discovery.home%/solr-config folder with the current timestamp.
81       *
82       * @param timeStamp
83       */
84      private void deleteDiscoveryConfig(String timeStamp) {
85          String discoceryConfigFilePath = discoveryEnvUtil.getSolrConfigFolderPath();
86          renameFolder(discoceryConfigFilePath, timeStamp);
87      }
88  
89      /**
90       * Used to rename the %ole.docstore.home folder with the current timestamp.
91       *
92       * @param timeStamp
93       */
94      private void deleteDocstore(String timeStamp) {
95          String docStoreRootFilePath = docStoreEnvUtil.getRootFolderPath();
96          renameFolder(docStoreRootFilePath, timeStamp);
97      }
98  
99      /**
100      * Used to rename the %ole.discovery.home folder with the current timestamp.
101      *
102      * @param timeStamp
103      */
104     private void deleteDiscovery(String timeStamp) {
105         String discoveryRootFilePath = discoveryEnvUtil.getRootFolderPath();
106         renameFolder(discoveryRootFilePath, timeStamp);
107     }
108 
109     /**
110      * This method will execute if the argument is prepareForNewBuild in the maintenance.command file. In this process
111      * %ole.docstore.home%/properties and %ole.discovery.home/solr-config will renamed with the current timestamp at the time of
112      * initialization.
113      *
114      * @param timeStamp
115      */
116 
117     private void prepareForNewBuild(String timeStamp) {
118         deleteDocstoreProperties(timeStamp);
119         deleteDiscoveryConfig(timeStamp);
120 
121     }
122 
123     /**
124      * This method will execute if the argument is resetDocStore in the maintenance.command file. In this process
125      * %ole.docstore.home%,%ole.docstore.home%/jackrabbit,%ole.discovery.home and %ole.discovery.home/solr-data will renamed with the
126      * current timestamp at the time of initialization.
127      *
128      * @param timeStamp
129      */
130     private void resetDocStore(String timeStamp) {
131 //        deleteDocStoreData(timeStamp);
132         deleteDiscoveryData(timeStamp);
133         deleteDiscovery(timeStamp);
134         deleteDocstore(timeStamp);
135 
136     }
137 
138     /**
139      * This method is called at the time of docstore initialization and operations are performed based on the command mentioned in
140      * maintenance.command file
141      *
142      * @param docStoreHome
143      * @param discoveryHome
144      * @throws IOException
145      * @throws URISyntaxException
146      */
147     public void docStoreMaintenance(String docStoreHome, String discoveryHome) throws IOException {
148         docStoreEnvUtil.initPathValues(docStoreHome);
149         discoveryEnvUtil.initPathValues(discoveryHome);
150         String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
151         String maintenanceFilePath = null;
152         String maintenanceFileName = null;
153         File maintenanceFile = null;
154         File folder = new File(docStoreHome);
155         File[] listOfFiles = folder.listFiles();
156         String maintenanceAction = null;
157         if (listOfFiles != null) {
158             for (int i = 0; i < listOfFiles.length; i++) {
159                 if (listOfFiles[i].isFile()) {
160                     maintenanceFileName = listOfFiles[i].getName();
161                     if (maintenanceFileName.equalsIgnoreCase("maintenance.command")) {
162                         maintenanceFilePath = listOfFiles[i].getAbsolutePath();
163                         maintenanceFile = new File(maintenanceFilePath);
164                         maintenanceAction = FileUtils.readFileToString(maintenanceFile);
165                         logger.info("maintenanceAction-->" + maintenanceAction);
166                         break;
167                     }
168                 }
169             }
170         }
171 
172         if (!StringUtils.isBlank(maintenanceAction)) {
173             if (maintenanceAction.equalsIgnoreCase(PREPARE_FOR_NEW_BUILD)) {
174                 prepareForNewBuild(timeStamp);
175 
176             } else if (maintenanceAction.equalsIgnoreCase(CLEANUP_DATA)) {
177                 // cleanupData();
178             } else if (maintenanceAction.equalsIgnoreCase(RESET_DOCSTORE)) {
179                 resetDocStore(timeStamp);
180             }
181             if (maintenanceFile != null) {
182                 maintenanceFile.delete();
183             }
184 
185         }
186     }
187 }