View Javadoc
1   /*
2    * Copyright 2012 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.docstore.utility;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.kuali.ole.docstore.DocStoreConstants;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.io.File;
24  import java.net.URL;
25  import java.util.Enumeration;
26  
27  /**
28   * Class to Doc Store Settings Util.
29   *
30   * @author Rajesh Chowdary K
31   * @created Apr 9, 2012
32   */
33  public class DocStoreSettingsUtil {
34  
35      private static final DocStoreSettingsUtil inst = new DocStoreSettingsUtil();
36      private static final Logger logger = LoggerFactory.getLogger(DocStoreSettingsUtil.class);
37  
38      /**
39       * Method to copyResources
40       */
41      public void copyResources() {
42          try {
43              File targetFolder = new File(DocStoreConstants.DOCSTORE_SETTINGS_DIR_PATH + File.separator + DocStoreConstants.RESOURCES_DIR_NAME);
44              if (!targetFolder.exists() || (targetFolder.exists() && targetFolder.list().length <= 0)) {
45                  Enumeration<URL> resources = this.getClass().getClassLoader().getResources(DocStoreConstants.RESOURCES_DIR_NAME);
46                  boolean canItBeTaken = false;
47                  while (resources.hasMoreElements()) {
48                      URL url = resources.nextElement();
49                      try {
50                          url.toURI();
51                          canItBeTaken = true;
52                      } catch (Exception e) {
53                          e.printStackTrace();
54                      }
55                      if (canItBeTaken) {
56                          logger.debug("Copying Settings from source @:" + url.getFile());
57                          File sourceDir = new File(url.toURI());
58                          FileUtils.copyDirectoryToDirectory(sourceDir, targetFolder.getParentFile());
59                      }
60                  }
61                  logger.debug("Copied Settings @: " + DocStoreConstants.DOCSTORE_SETTINGS_DIR_PATH);
62              } else
63                  logger.info("Using Existing Settings @: " + DocStoreConstants.DOCSTORE_SETTINGS_DIR_PATH);
64          } catch (Exception e) {
65              e.printStackTrace();
66          }
67      }
68  
69      private DocStoreSettingsUtil() {
70      }
71  
72      public static DocStoreSettingsUtil getInstance() {
73          return inst;
74      }
75  
76  }