View Javadoc
1   package org.kuali.ole.docstore.discovery.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.apache.commons.io.FileUtils;
9   import org.apache.commons.lang.StringUtils;
10  import org.kuali.ole.utility.Constants;
11  import org.kuali.ole.utility.ResourceItem;
12  import org.kuali.ole.utility.ResourceUtil;
13  import org.slf4j.Logger;
14  import org.slf4j.LoggerFactory;
15  
16  /**
17   * Created by IntelliJ IDEA. User: Sreekanth Date: 6/6/12 Time: 2:41 PM To change this template use File | Settings | File Templates.
18   */
19  public class DiscoveryEnvUtil {
20      private static final Logger LOG = LoggerFactory.getLogger(DiscoveryEnvUtil.class);
21      public static final String DISCOVERY_ROOT_DIR = "discovery";
22      public static final String DISCOVERY_ROOT_DEFAULT = "/opt/docstore/" + DISCOVERY_ROOT_DIR;
23      public static final String DISCOVERY_ROOT_TEST_DEFAULT = System.getProperty("user.home")+"/kuali/main/local/docstore-test/discovery-test";
24      public static final String SOLR_CONFIG_FOLDER = "solr-config";
25      public static final String SOLR_DATA_FOLDER = "solr-indexes";
26      public static final String DISCOVERY_PROPERTIES_FOLDER = "properties";
27      public static final String DISCOVERY_PROPERTIES_FILE = "ole-discovery.properties";
28      public static final String UTF8 = "UTF-8";
29  
30      protected String rootFolderPath;
31      protected String solrConfigFolderPath;
32      protected String solrDataFolderPath;
33      protected String discoveryPropertyFolderPath;
34      protected String discoveryPropertiesFilePath;
35  
36      public String getSolrConfigDirectoryPath() {
37          return solrConfigDirectoryPath;
38      }
39  
40      private String solrConfigDirectoryPath;
41      private boolean testEnv = false;
42      ResourceUtil resourceUtil = new ResourceUtil();
43  
44      /**
45       * Initializes environment for normal use of discovery. This should be called during web app initialization.
46       */
47      public void initEnvironment() {
48          initEnv(DISCOVERY_ROOT_DEFAULT);
49      }
50  
51      /**
52       * Initializes environment for testing. This should be called in all test cases.
53       */
54      public void initTestEnvironment() {
55          testEnv = true;
56          initEnv(DISCOVERY_ROOT_TEST_DEFAULT);
57      }
58  
59      /**
60       * Configure the home directory for discovery. When this method return, the system property <code>ole.discover.home</code> is guaranteed to be set.
61       */
62      protected void initEnv(String defaultDiscoveryHome) {
63          String discoveryHome = getDiscoveryHome(defaultDiscoveryHome);
64          LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + discoveryHome);
65          System.setProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY, discoveryHome);
66          initEnvironmentCommon(discoveryHome);
67      }
68  
69      /**
70       * Logic for determining the correct home directory for discovery. The directory to use can be configured by system property settings. If no system property is set, discovery
71       * home becomes a path relative to docstore home. If docstore home is not set, it falls through to the default value.
72       */
73      public String getDiscoveryHome(String defaultDiscoveryHome) {
74          String direct = System.getProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY);
75          if (!StringUtils.isBlank(direct)) {
76              return direct;
77          }
78          String docstoreHome = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
79          if (StringUtils.isBlank(docstoreHome)) {
80              return defaultDiscoveryHome;
81          } else {
82              return docstoreHome + "/" + DISCOVERY_ROOT_DIR;
83          }
84      }
85  
86      public void initPathValues(String discoveryRoot) {
87          rootFolderPath = discoveryRoot;
88          solrConfigFolderPath = discoveryRoot.concat("/").concat(SOLR_CONFIG_FOLDER);
89          solrConfigDirectoryPath = solrConfigFolderPath;
90          solrDataFolderPath = discoveryRoot.concat("/").concat(SOLR_DATA_FOLDER);
91          discoveryPropertyFolderPath = discoveryRoot.concat("/").concat(DISCOVERY_PROPERTIES_FOLDER);
92          discoveryPropertiesFilePath = discoveryPropertyFolderPath.concat("/").concat(DISCOVERY_PROPERTIES_FILE);
93      }
94  
95      /**
96       * Initializes an environment for discovery at the given root folder. Looks for necessary folders and files under the root folder. Any resources that already exist are left
97       * untouched. Any that are missing are created using default values.
98       */
99      public void initEnvironmentCommon(String docStoreRoot) {
100         initPathValues(docStoreRoot);
101         handleSolrConfig();
102         List<ResourceItem> resourceItems = getResourceItems();
103         for (ResourceItem item : resourceItems) {
104             resourceUtil.validate(item);
105         }
106     }
107 
108     protected void handleSolrConfig() {
109         String srcLoc = "";
110         try {
111             File solrConfigDir = new File(solrConfigFolderPath);
112             // The directory already exists, leave it alone
113             if (solrConfigDir.isDirectory()) {
114                 return;
115             }
116             if (testEnv) {
117                 String path = getClass().getClassLoader().getResource(".").getPath();
118                 srcLoc = "File:" + path + "/../../../ole-docstore-webapp/src/main/resources/solrconfig";
119             } else {
120                 srcLoc = "classpath:solrconfig";
121             }
122             File srcDir = resourceUtil.getFile(srcLoc);
123             LOG.info("***** Begin Solr Config *****");
124             LOG.info("Source Loc: " + srcLoc);
125             LOG.info("Source Dir: " + srcDir);
126             LOG.info("Dest Dir: " + solrConfigDir);
127             LOG.info("Copying source dir to dest dir...");
128 
129             resourceUtil.copyDirectory(srcLoc, solrConfigDir);
130             File solrConfigXmlFile = new File(solrConfigFolderPath + "/bib/conf/solrconfig.xml");
131             LOG.info("Updating " + solrConfigXmlFile);
132             String oldContent = FileUtils.readFileToString(solrConfigXmlFile, UTF8);
133             int beginIndex = oldContent.indexOf("<dataDir>");
134             int endIndex = oldContent.indexOf("</dataDir>");
135             String oldValue = oldContent.substring(beginIndex, endIndex).concat("</dataDir>");
136             String newValue = "<dataDir>" + solrDataFolderPath + "</dataDir>";
137             LOG.info("Setting datadir=" + solrDataFolderPath);
138             String newContent = oldContent.replace(oldValue, newValue);
139             FileUtils.writeStringToFile(solrConfigXmlFile, newContent, UTF8);
140             LOG.info("***** End Solr Config *****");
141         } catch (IOException e) {
142             throw new IllegalArgumentException(e);
143         }
144 
145     }
146 
147     /**
148      * Return a list of <code>ResourceItem</code> objects representing resources required by discovery
149      */
150     protected List<ResourceItem> getResourceItems() {
151         ResourceItem solrConfigFolder = new ResourceItem();
152         solrConfigFolder.setDestination(new File(solrConfigFolderPath));
153         solrConfigFolder.setSystemProperty("solr.solr.home");
154         solrConfigFolder.setLocalDirectory(true);
155 
156         // ResourceItem discoveryPropertiesFile = new ResourceItem();
157         // discoveryPropertiesFile.setDestination(new File(discoveryPropertiesFilePath));
158         // discoveryPropertiesFile.setSystemProperty("discovery.properties.file");
159         // discoveryPropertiesFile.setSourceLocation("classpath:ole-discovery.properties");
160 
161         List<ResourceItem> items = new ArrayList<ResourceItem>();
162         items.add(solrConfigFolder);
163         // items.add(discoveryPropertiesFile);
164         return items;
165     }
166 
167     public void printEnvironment() {
168 
169         // initEnvironmentCommon(SOLR_CONFIG_FOLDER);
170         System.out.println("**********{{ Discovery Environment **********");
171         System.out.println(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
172         System.out.println("solrConfigFolderPath=" + solrConfigFolderPath);
173         System.out.println("solrDataFolderPath=" + solrDataFolderPath);
174         System.out.println("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
175         System.out.println("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
176         System.out.println("**********}} Discovery Environment **********");
177     }
178 
179     public void logEnvironment() {
180         LOG.info("**********{{ Discovery Environment **********");
181         LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
182         LOG.info("solrConfigFolderPath=" + solrConfigFolderPath);
183         LOG.info("solrDataFolderPath=" + solrDataFolderPath);
184         LOG.info("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
185         LOG.info("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
186         LOG.info("**********}} Discovery Environment **********");
187 
188     }
189 
190     public String getRootFolderPath() {
191         return rootFolderPath;
192     }
193 
194     public void setRootFolderPath(String rootFolderPath) {
195         this.rootFolderPath = rootFolderPath;
196     }
197 
198     public String getSolrConfigFolderPath() {
199         return solrConfigFolderPath;
200     }
201 
202     public void setSolrConfigFolderPath(String solrConfigFolderPath) {
203         this.solrConfigFolderPath = solrConfigFolderPath;
204     }
205 
206     public String getSolrDataFolderPath() {
207         return solrDataFolderPath;
208     }
209 
210     public void setSolrDataFolderPath(String solrDataFolderPath) {
211         this.solrDataFolderPath = solrDataFolderPath;
212     }
213 
214     public String getDiscoveryPropertyFolderPath() {
215         return discoveryPropertyFolderPath;
216     }
217 
218     public void setDiscoveryPropertyFolderPath(String discoveryPropertyFolderPath) {
219         this.discoveryPropertyFolderPath = discoveryPropertyFolderPath;
220     }
221 
222     public String getDiscoveryPropertiesFilePath() {
223         return discoveryPropertiesFilePath;
224     }
225 
226     public void setDiscoveryPropertiesFilePath(String discoveryPropertiesFilePath) {
227         this.discoveryPropertiesFilePath = discoveryPropertiesFilePath;
228     }
229 }