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
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 private boolean testEnv = false;
36 ResourceUtil resourceUtil = new ResourceUtil();
37
38
39
40
41 public void initEnvironment() {
42 initEnv(DISCOVERY_ROOT_DEFAULT);
43 }
44
45
46
47
48 public void initTestEnvironment() {
49 testEnv = true;
50 initEnv(DISCOVERY_ROOT_TEST_DEFAULT);
51 }
52
53
54
55
56 protected void initEnv(String defaultDiscoveryHome) {
57 String discoveryHome = getDiscoveryHome(defaultDiscoveryHome);
58 LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + discoveryHome);
59 System.setProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY, discoveryHome);
60 initEnvironmentCommon(discoveryHome);
61 }
62
63
64
65
66
67 public String getDiscoveryHome(String defaultDiscoveryHome) {
68 String direct = System.getProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY);
69 if (!StringUtils.isBlank(direct)) {
70 return direct;
71 }
72 String docstoreHome = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
73 if (StringUtils.isBlank(docstoreHome)) {
74 return defaultDiscoveryHome;
75 } else {
76 return docstoreHome + "/" + DISCOVERY_ROOT_DIR;
77 }
78 }
79
80 public void initPathValues(String discoveryRoot) {
81 rootFolderPath = discoveryRoot;
82 solrConfigFolderPath = discoveryRoot.concat("/").concat(SOLR_CONFIG_FOLDER);
83 solrDataFolderPath = discoveryRoot.concat("/").concat(SOLR_DATA_FOLDER);
84 discoveryPropertyFolderPath = discoveryRoot.concat("/").concat(DISCOVERY_PROPERTIES_FOLDER);
85 discoveryPropertiesFilePath = discoveryPropertyFolderPath.concat("/").concat(DISCOVERY_PROPERTIES_FILE);
86 }
87
88
89
90
91
92 public void initEnvironmentCommon(String docStoreRoot) {
93 initPathValues(docStoreRoot);
94 handleSolrConfig();
95 List<ResourceItem> resourceItems = getResourceItems();
96 for (ResourceItem item : resourceItems) {
97 resourceUtil.validate(item);
98 }
99 }
100
101 protected void handleSolrConfig() {
102 String srcLoc = "";
103 try {
104 File solrConfigDir = new File(solrConfigFolderPath);
105
106 if (solrConfigDir.isDirectory()) {
107 return;
108 }
109 if (testEnv) {
110 String path = getClass().getClassLoader().getResource(".").getPath();
111 srcLoc = "File:" + path + "/../../../ole-docstore-webapp/src/main/resources/solrconfig";
112 } else {
113 srcLoc = "classpath:solrconfig";
114 }
115 File srcDir = resourceUtil.getFile(srcLoc);
116 LOG.info("***** Begin Solr Config *****");
117 LOG.info("Source Loc: " + srcLoc);
118 LOG.info("Source Dir: " + srcDir);
119 LOG.info("Dest Dir: " + solrConfigDir);
120 LOG.info("Copying source dir to dest dir...");
121
122 resourceUtil.copyDirectory(srcLoc, solrConfigDir);
123 File solrConfigXmlFile = new File(solrConfigFolderPath + "/bib/conf/solrconfig.xml");
124 LOG.info("Updating " + solrConfigXmlFile);
125 String oldContent = FileUtils.readFileToString(solrConfigXmlFile, UTF8);
126 int beginIndex = oldContent.indexOf("<dataDir>");
127 int endIndex = oldContent.indexOf("</dataDir>");
128 String oldValue = oldContent.substring(beginIndex, endIndex).concat("</dataDir>");
129 String newValue = "<dataDir>" + solrDataFolderPath + "</dataDir>";
130 LOG.info("Setting datadir=" + solrDataFolderPath);
131 String newContent = oldContent.replace(oldValue, newValue);
132 FileUtils.writeStringToFile(solrConfigXmlFile, newContent, UTF8);
133 LOG.info("***** End Solr Config *****");
134 } catch (IOException e) {
135 throw new IllegalArgumentException(e);
136 }
137
138 }
139
140
141
142
143 protected List<ResourceItem> getResourceItems() {
144 ResourceItem solrConfigFolder = new ResourceItem();
145 solrConfigFolder.setDestination(new File(solrConfigFolderPath));
146 solrConfigFolder.setSystemProperty("solr.solr.home");
147 solrConfigFolder.setLocalDirectory(true);
148
149
150
151
152
153
154 List<ResourceItem> items = new ArrayList<ResourceItem>();
155 items.add(solrConfigFolder);
156
157 return items;
158 }
159
160 public void printEnvironment() {
161
162
163 System.out.println("**********{{ Discovery Environment **********");
164 System.out.println(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
165 System.out.println("solrConfigFolderPath=" + solrConfigFolderPath);
166 System.out.println("solrDataFolderPath=" + solrDataFolderPath);
167 System.out.println("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
168 System.out.println("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
169 System.out.println("**********}} Discovery Environment **********");
170 }
171
172 public void logEnvironment() {
173 LOG.info("**********{{ Discovery Environment **********");
174 LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
175 LOG.info("solrConfigFolderPath=" + solrConfigFolderPath);
176 LOG.info("solrDataFolderPath=" + solrDataFolderPath);
177 LOG.info("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
178 LOG.info("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
179 LOG.info("**********}} Discovery Environment **********");
180
181 }
182
183 public String getRootFolderPath() {
184 return rootFolderPath;
185 }
186
187 public void setRootFolderPath(String rootFolderPath) {
188 this.rootFolderPath = rootFolderPath;
189 }
190
191 public String getSolrConfigFolderPath() {
192 return solrConfigFolderPath;
193 }
194
195 public void setSolrConfigFolderPath(String solrConfigFolderPath) {
196 this.solrConfigFolderPath = solrConfigFolderPath;
197 }
198
199 public String getSolrDataFolderPath() {
200 return solrDataFolderPath;
201 }
202
203 public void setSolrDataFolderPath(String solrDataFolderPath) {
204 this.solrDataFolderPath = solrDataFolderPath;
205 }
206
207 public String getDiscoveryPropertyFolderPath() {
208 return discoveryPropertyFolderPath;
209 }
210
211 public void setDiscoveryPropertyFolderPath(String discoveryPropertyFolderPath) {
212 this.discoveryPropertyFolderPath = discoveryPropertyFolderPath;
213 }
214
215 public String getDiscoveryPropertiesFilePath() {
216 return discoveryPropertiesFilePath;
217 }
218
219 public void setDiscoveryPropertiesFilePath(String discoveryPropertiesFilePath) {
220 this.discoveryPropertiesFilePath = discoveryPropertiesFilePath;
221 }
222 }