Skip to content

Commit 2b55ed6

Browse files
committed
Refactored code so that common property fallback resolution is in it's own utility class.
1 parent 8059404 commit 2b55ed6

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package edu.cornell.mannlib.vitro.webapp.searchengine.base;
2+
3+
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
4+
import org.apache.commons.logging.Log;
5+
import org.apache.commons.logging.LogFactory;
6+
7+
public class SearchEngineUtil {
8+
9+
private static final Log log = LogFactory.getLog(SearchEngineUtil.class);
10+
11+
public static String getSearchEngineURLProperty() {
12+
ConfigurationProperties config = ConfigurationProperties.getInstance();
13+
if (config.getProperty("vitro.local.searchengine.url", "").isEmpty()) {
14+
return tryFetchLegacySolrConfiguration(config);
15+
}
16+
17+
return config.getProperty("vitro.local.searchengine.url", "");
18+
}
19+
20+
private static String tryFetchLegacySolrConfiguration(ConfigurationProperties config) {
21+
String legacyConfigValue = config.getProperty("vitro.local.solr.url", "");
22+
if (!legacyConfigValue.isEmpty()) {
23+
log.warn(
24+
"vitro.local.solr.url is deprecated, switch to using" +
25+
" vitro.local.searchengine.url as soon as possible.");
26+
}
27+
28+
return legacyConfigValue;
29+
}
30+
}

api/src/main/java/edu/cornell/mannlib/vitro/webapp/searchengine/solr/SolrSearchEngine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import javax.servlet.ServletContext;
1212

13+
import edu.cornell.mannlib.vitro.webapp.searchengine.base.SearchEngineUtil;
1314
import edu.cornell.mannlib.vitro.webapp.servlet.setup.SearchEngineSmokeTest;
1415
import org.apache.http.impl.client.HttpClientBuilder;
1516
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
@@ -44,7 +45,7 @@ public class SolrSearchEngine implements SearchEngine {
4445
@Override
4546
public void startup(Application application, ComponentStartupStatus css) {
4647
ServletContext ctx = application.getServletContext();
47-
String solrServerUrlString = SearchEngineSmokeTest.getSearchEngineURLProperty();
48+
String solrServerUrlString = SearchEngineUtil.getSearchEngineURLProperty();
4849
if (solrServerUrlString == null) {
4950
css.fatal("Could not find vitro.local.searchengine.url in "
5051
+ "runtime.properties. Vitro application needs the URL of "

api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/SearchEngineSmokeTest.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import com.fasterxml.jackson.databind.JsonNode;
1212
import com.fasterxml.jackson.databind.ObjectMapper;
13-
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
13+
import edu.cornell.mannlib.vitro.webapp.searchengine.base.SearchEngineUtil;
1414
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
1515
import edu.cornell.mannlib.vitro.webapp.utils.http.ESHttpsBasicClientFactory;
1616
import org.apache.commons.logging.Log;
@@ -77,30 +77,11 @@ private static String getBaseServiceUrl(String url) {
7777
return url;
7878
}
7979

80-
public static String getSearchEngineURLProperty() {
81-
ConfigurationProperties config = ConfigurationProperties.getInstance();
82-
if (config.getProperty("vitro.local.searchengine.url", "").isEmpty()) {
83-
return tryFetchLegacySolrConfiguration(config);
84-
}
85-
86-
return config.getProperty("vitro.local.searchengine.url", "");
87-
}
88-
89-
private static String tryFetchLegacySolrConfiguration(ConfigurationProperties config) {
90-
String legacyConfigValue = config.getProperty("vitro.local.solr.url", "");
91-
if (!legacyConfigValue.isEmpty()) {
92-
log.warn(
93-
"vitro.local.solr.url is deprecated, switch to using vitro.local.searchengine.url as soon as possible.");
94-
}
95-
96-
return legacyConfigValue;
97-
}
98-
9980
@Override
10081
public void contextInitialized(ServletContextEvent sce) {
10182
final StartupStatus ss = StartupStatus.getBean(sce.getServletContext());
10283

103-
String searchEngineUrlString = getSearchEngineURLProperty();
84+
String searchEngineUrlString = SearchEngineUtil.getSearchEngineURLProperty();
10485

10586
if (searchEngineUrlString.isEmpty()) {
10687
ss.fatal(this, "No search engine is configured");

api/src/main/java/edu/cornell/mannlib/vitro/webapp/servlet/setup/SolrSmokeTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.servlet.ServletContextEvent;
1212
import javax.servlet.ServletContextListener;
1313

14+
import edu.cornell.mannlib.vitro.webapp.searchengine.base.SearchEngineUtil;
1415
import edu.cornell.mannlib.vitro.webapp.utils.http.HttpClientFactory;
1516
import org.apache.commons.logging.Log;
1617
import org.apache.commons.logging.LogFactory;
@@ -51,7 +52,7 @@ public SolrSmokeTest(ServletContextListener listener) {
5152
public void doTest(ServletContextEvent sce) {
5253
final StartupStatus ss = StartupStatus.getBean(sce.getServletContext());
5354

54-
String solrUrlString = SearchEngineSmokeTest.getSearchEngineURLProperty();
55+
String solrUrlString = SearchEngineUtil.getSearchEngineURLProperty();
5556
if (solrUrlString.isEmpty()) {
5657
ss.fatal(listener, "Can't connect to Solr search engine. "
5758
+ "runtime.properties must contain a value for "

0 commit comments

Comments
 (0)