View Javadoc
1   package org.kuali.ole;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil;
5   import org.kuali.ole.docstore.util.DocStoreEnvUtil;
6   import org.kuali.ole.docstore.utility.DocumentStoreMaintenance;
7   import org.kuali.ole.utility.Constants;
8   import org.kuali.rice.core.api.config.module.RunMode;
9   import org.kuali.rice.core.api.config.property.ConfigContext;
10  import org.kuali.rice.core.framework.config.module.ModuleConfigurer;
11  
12  import java.io.File;
13  import java.io.FileOutputStream;
14  import java.io.IOException;
15  import java.util.Arrays;
16  import java.util.Properties;
17  
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: pvsubrah
22   * Date: 6/13/13
23   * Time: 1:31 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class OleDocstoreModuleConfigurer extends ModuleConfigurer {
27      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleDocstoreModuleConfigurer.class);
28  
29      DocStoreEnvUtil docStoreEnvUtil = new DocStoreEnvUtil();
30      DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
31      DocumentStoreMaintenance documentStoreMaintenance = new DocumentStoreMaintenance();
32  
33      public OleDocstoreModuleConfigurer() {
34          super("OLE-DOCSTORE");
35          LOG.info("OLE-Docstore Configurer instantiated");
36          setValidRunModes(Arrays.asList(RunMode.LOCAL));
37      }
38  
39      @Override
40      protected void doAdditionalModuleStartLogic() throws Exception {
41          LOG.info("*********************************************************");
42          LOG.info("OLE Docstore Starting Module");
43          LOG.info("*********************************************************");
44          super.doAdditionalModuleStartLogic();
45  
46          try {
47              docStoreEnvUtil.initEnvironment();
48              discoveryEnvUtil.initEnvironment();
49              String fileSeparator = File.separator;
50              File file = new File(discoveryEnvUtil.getSolrConfigDirectoryPath()+fileSeparator+"bib"+fileSeparator+"conf"+fileSeparator+"solrcore.properties");
51              file.deleteOnExit();
52              try{
53                  Properties props = new Properties();
54                  FileOutputStream out = new FileOutputStream(file);
55                  props.setProperty("db.driver", ConfigContext.getCurrentContextConfig().getProperty("jdbc.driver"));
56                  props.setProperty("db.url", ConfigContext.getCurrentContextConfig().getProperty("jdbc.url"));
57                  props.setProperty("db.username", ConfigContext.getCurrentContextConfig().getProperty("jdbc.username"));
58                  props.setProperty("db.password", ConfigContext.getCurrentContextConfig().getProperty("jdbc.password"));
59                  props.store(out, null);
60                  out.close();
61              }catch(Exception e){
62                  e.printStackTrace();
63              }
64              String docstoreHome = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
65              String discoveryHome = System.getProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY);
66              validate(docstoreHome, discoveryHome);
67              documentStoreMaintenance.docStoreMaintenance(docstoreHome, discoveryHome);
68          } catch (IOException e) {
69              // If the system can't be initialized properly there is no point in continuing
70              // Always throw an exception here
71              throw new IllegalStateException(e);
72          }
73  
74      }
75  
76      protected void validate(String docstoreHome, String discoveryHome) {
77          StringBuilder sb = new StringBuilder();
78          if (StringUtils.isBlank(docstoreHome)) {
79              sb.append(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY + " cannot be blank. ");
80          }
81          if (StringUtils.isBlank(docstoreHome)) {
82              sb.append(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + " cannot be blank.");
83          }
84          if (sb.length() != 0) {
85              throw new IllegalStateException(sb.toString());
86          }
87      }
88  
89      @Override
90      protected void doAdditionalModuleStopLogic() throws Exception {
91          LOG.info("*********************************************************");
92          LOG.info("OLE Stopping Module");
93          LOG.info("*********************************************************");
94          super.doAdditionalModuleStopLogic();
95      }
96  }