View Javadoc

1   package org.kuali.ole.docstore.discovery.util;
2   
3   
4   import org.apache.commons.io.FileUtils;
5   
6   import java.io.*;
7   import java.net.*;
8   
9   /**
10   * Created by IntelliJ IDEA.
11   * User: SG7940
12   * Date: 3/15/12
13   * Time: 2:53 PM
14   * To change this template use File | Settings | File Templates.
15   */
16  public class EnvironmentUtil {
17      private static final org.apache.log4j.Logger LOG                         = org.apache.log4j.Logger
18              .getLogger(EnvironmentUtil.class);
19      public static final  String                  DISCOVERY_ROOT_DEFAULT      = "/opt/ole-discovery/local";
20      public static final  String                  DISCOVERY_ROOT_TEST_DEFAULT = "/opt/ole-discovery/test";
21      public static final  String                  SOLR_CONFIG_FOLDER          = "/solr-config";
22      public static final  String                  SOLR_DATA_FOLDER            = "/solr-data";
23      public static final  String                  SOLR_PROPERTIES_FOLDER      = "/properties";
24      public static final  String                  SOLR_PROPERTIES_FILE        = "/properties/ole-discovery.properties";
25  
26      public static String solrConfigFolderPath;
27      public static String solrDataFolderPath;
28      public static String solrPropertyFolderPath;
29      public static String discoveryPropertiesFilePath;
30  
31  
32      /**
33       * Initializes environment for normal use of discovery.
34       * This should be called during web app initialization.
35       */
36      public static void initEnvironment() {
37          String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
38          if (discoveryRoot == null || discoveryRoot.isEmpty()) {
39              System.setProperty("OLE_DISCOVERY_ROOT", DISCOVERY_ROOT_DEFAULT);
40          }
41          initEnvironmentCommon();
42      }
43  
44      /**
45       * Initializes environment for testing.
46       * This should be called in all test cases.
47       */
48      public static void initTestEnvironment() {
49          String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
50          if (discoveryRoot == null || discoveryRoot.isEmpty()) {
51              System.setProperty("OLE_DISCOVERY_ROOT", DISCOVERY_ROOT_TEST_DEFAULT);
52          }
53          initEnvironmentCommon();
54      }
55  
56      /**
57       * Initializes environment for Discovery app at the given root folder.
58       * Looks for necessary folders and property files in the root folder,
59       * and creates them if not available.
60       * <p/>
61       * Check for root folder.
62       * If found, exit. else proceed.
63       * Define the following system properties required by Discovery:
64       * solr.solr.home=%rootFolder%/solr-config
65       * discovery.properties.file=%rootFolder%/properties/ole-discovery.properties
66       * Create the root folder and subfolders (solr-config, solr-data, properties)
67       * Copy the contents of solrconfig (from discovery-core/resources) to %rootFolder%/solr-config
68       * Make sure the <dataDir> in %rootFolder%/solr-config/bib/conf/solrconfig.xml is set to %rootFolder%/solr-data
69       * Copy sample ole-discovery.properties file (from discovery-core/resources) to %rootFolder%/properties/ole-discovery.properties
70       *
71       * @throws java.io.IOException
72       * @throws java.net.URISyntaxException
73       */
74      public static void initEnvironmentCommon() {
75          try {
76              String rootFolder = System.getProperty("OLE_DISCOVERY_ROOT");
77              solrConfigFolderPath = rootFolder.concat(SOLR_CONFIG_FOLDER);
78              solrDataFolderPath = rootFolder.concat(SOLR_DATA_FOLDER);
79              solrPropertyFolderPath = rootFolder.concat(SOLR_PROPERTIES_FOLDER);
80              discoveryPropertiesFilePath = rootFolder.concat(SOLR_PROPERTIES_FILE);
81  
82              System.setProperty("solr.solr.home", solrConfigFolderPath);
83              System.setProperty("discovery.properties.file", discoveryPropertiesFilePath);
84  
85              File rootFolderPath = new File(rootFolder);
86              if (!rootFolderPath.isDirectory()) {
87                  rootFolderPath.mkdirs();
88                  //creating the Solr Root directory
89                  createDirectory(solrConfigFolderPath);
90                  //creating the Solr  Data directory
91                  createDirectory(solrDataFolderPath);
92                  //creating the Solr  Discovery directory
93                  createDirectory(solrPropertyFolderPath);
94                  URL solrConfigResource = EnvironmentUtil.class.getClassLoader().getResource("solrconfig");
95                  File ConfigResource = new File(solrConfigResource.toURI());
96                  File target = new File(solrConfigFolderPath);
97                  FileUtils.copyDirectory(ConfigResource, target);
98  
99                  //Make sure the <dataDir> in %rootFolder%/solr-config/bib/conf/solrconfig.xml is set to %rootFolder%/solr-data
100                 URL solrConfigFileResource = EnvironmentUtil.class.getClassLoader()
101                                                                   .getResource("./solrconfig/bib/conf/solrconfig.xml");
102                 File solrConfigFile = new File(solrConfigFileResource.toURI());
103                 String configFileContent = FileUtils.readFileToString(solrConfigFile, "UTF-8");
104                 int startingIndex = configFileContent.indexOf("<dataDir>");
105                 int endingIndex = configFileContent.indexOf("</dataDir>");
106                 String subString = configFileContent.substring(startingIndex, endingIndex).concat("</dataDir>");
107                 String modifiedConfigFileContent = configFileContent
108                         .replaceAll(subString, "<dataDir>" + solrDataFolderPath + "</dataDir>");
109                 File targetFile = new File(solrConfigFolderPath + "/bib/conf/solrconfig.xml");
110                 FileUtils.writeStringToFile(targetFile, modifiedConfigFileContent);
111 
112                 //Copy sample ole-discovery.properties file (from discovery-core/resources) to %rootFolder%/properties/ole-discovery.properties
113                 URL resource = EnvironmentUtil.class.getClassLoader().getResource("ole-discovery.properties");
114                 File sourceProperties = new File(resource.toURI());
115                 File targetProperties = new File(discoveryPropertiesFilePath);
116                 FileUtils.copyFile(sourceProperties, targetProperties);
117             }
118         }
119         catch (Exception e) {
120             e.printStackTrace();
121         }
122 
123     }
124 
125     public static void printEnvironment() {
126 
127         String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
128         //initEnvironmentCommon(SOLR_CONFIG_FOLDER);
129         System.out.println("**********{{ Discovery Environment **********");
130         System.out.println("OLE_DISCOVERY_ROOT=" + discoveryRoot);
131         System.out.println("solrConfigFolderPath=" + solrConfigFolderPath);
132         System.out.println("solrDataFolderPath=" + solrDataFolderPath);
133         System.out.println("solrPropertyFolderPath=" + solrPropertyFolderPath);
134         System.out.println("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
135 
136         System.out.println("solr.solr.home=" + System.getProperty("solr.solr.home"));
137         System.out.println("discovery.properties.file=" + System.getProperty("discovery.properties.file"));
138         System.out.println("**********}} Discovery Environment **********");
139     }
140 
141     //creates the Directory .
142     private static void createDirectory(String dirName) {
143         File solarTest = new File(dirName);
144         if (!solarTest.isDirectory()) {
145             solarTest.mkdirs();
146         }
147 
148     }
149 
150 
151 }