Skip to content

Commit 157fcfb

Browse files
author
jefffischer
committed
BroadleafCommerce#1309 - Provide solr index status maintenance
1 parent 1c20b75 commit 157fcfb

8 files changed

+411
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
import org.broadleafcommerce.common.exception.ExceptionHelper;
23+
import org.broadleafcommerce.core.search.service.SearchService;
24+
import org.w3c.dom.Document;
25+
import org.w3c.dom.Element;
26+
import org.w3c.dom.NodeList;
27+
import org.xml.sax.SAXException;
28+
29+
import java.io.BufferedWriter;
30+
import java.io.File;
31+
import java.io.FileOutputStream;
32+
import java.io.IOException;
33+
import java.io.OutputStreamWriter;
34+
import java.text.ParseException;
35+
import java.text.SimpleDateFormat;
36+
import java.util.Map;
37+
38+
import javax.annotation.Resource;
39+
import javax.xml.parsers.DocumentBuilder;
40+
import javax.xml.parsers.DocumentBuilderFactory;
41+
import javax.xml.parsers.ParserConfigurationException;
42+
import javax.xml.transform.OutputKeys;
43+
import javax.xml.transform.Transformer;
44+
import javax.xml.transform.TransformerException;
45+
import javax.xml.transform.TransformerFactory;
46+
import javax.xml.transform.dom.DOMSource;
47+
import javax.xml.transform.stream.StreamResult;
48+
import javax.xml.xpath.XPath;
49+
import javax.xml.xpath.XPathConstants;
50+
import javax.xml.xpath.XPathExpressionException;
51+
import javax.xml.xpath.XPathFactory;
52+
53+
/**
54+
* @author Jeff Fischer
55+
*/
56+
public class FileSystemSolrIndexStatusProviderImpl implements SolrIndexStatusProvider {
57+
58+
@Resource(name="blSearchService")
59+
protected SearchService searchService;
60+
61+
protected DocumentBuilder builder;
62+
63+
protected XPath xPath;
64+
65+
protected SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
66+
67+
public FileSystemSolrIndexStatusProviderImpl() {
68+
XPathFactory factory=XPathFactory.newInstance();
69+
xPath=factory.newXPath();
70+
}
71+
72+
@Override
73+
public synchronized void handleUpdateIndexStatus(IndexStatusInfo status) {
74+
try {
75+
if (searchService instanceof SolrSearchServiceImpl) {
76+
String solrHome = ((SolrSearchServiceImpl) searchService).getSolrHomePath();
77+
File statusFile = new File(new File(solrHome), "solr_status.xml");
78+
boolean exists = statusFile.exists();
79+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
80+
dbf.setIgnoringElementContentWhitespace(true);
81+
dbf.setNamespaceAware(true);
82+
if (builder == null) {
83+
builder = dbf.newDocumentBuilder();
84+
}
85+
Document document;
86+
Element indexElement;
87+
if (exists) {
88+
document = builder.parse(statusFile);
89+
NodeList temp1 = (NodeList) xPath.evaluate("/status/index", document, XPathConstants.NODESET);
90+
indexElement = (Element) temp1.item(0);
91+
} else {
92+
document = builder.newDocument();
93+
Element root = document.createElement("status");
94+
document.appendChild(root);
95+
indexElement = document.createElement("index");
96+
indexElement.setAttribute("dateProcessed", "");
97+
root.appendChild(indexElement);
98+
}
99+
String dateString = format.format(status.getLastIndexDate());
100+
if (!dateString.equals(indexElement.getAttribute("dateProcessed"))) {
101+
indexElement.setAttribute("dateProcessed", dateString);
102+
NodeList children = (NodeList) xPath.evaluate("info", indexElement, XPathConstants.NODESET);
103+
for (int j = 0; j < children.getLength(); j++) {
104+
indexElement.removeChild(children.item(j));
105+
}
106+
children = indexElement.getChildNodes();
107+
for (int j = 0; j < children.getLength(); j++) {
108+
if (children.item(j).getNodeName().equalsIgnoreCase("#text")) {
109+
indexElement.removeChild(children.item(j));
110+
}
111+
}
112+
}
113+
for (Map.Entry<String, String> entry : status.getAdditionalInfo().entrySet()) {
114+
NodeList infos = (NodeList) xPath.evaluate("info[@key='" + entry.getKey() + "']", indexElement, XPathConstants.NODESET);
115+
if (infos.getLength() == 0) {
116+
Element addlInfo = document.createElement("info");
117+
addlInfo.setAttribute("key", entry.getKey());
118+
addlInfo.setAttribute("val", entry.getValue());
119+
indexElement.appendChild(addlInfo);
120+
}
121+
}
122+
123+
TransformerFactory tFactory = TransformerFactory.newInstance();
124+
Transformer xmlTransformer = tFactory.newTransformer();
125+
xmlTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
126+
xmlTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
127+
xmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
128+
xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
129+
130+
DOMSource source = new DOMSource(document);
131+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(statusFile, false), "UTF-8"));
132+
StreamResult result = new StreamResult(writer);
133+
xmlTransformer.transform(source, result);
134+
}
135+
} catch (ParserConfigurationException e) {
136+
throw ExceptionHelper.refineException(e);
137+
} catch (SAXException e) {
138+
throw ExceptionHelper.refineException(e);
139+
} catch (IOException e) {
140+
throw ExceptionHelper.refineException(e);
141+
} catch (XPathExpressionException e) {
142+
throw ExceptionHelper.refineException(e);
143+
} catch (TransformerException e) {
144+
throw ExceptionHelper.refineException(e);
145+
}
146+
}
147+
148+
@Override
149+
public synchronized IndexStatusInfo readIndexStatus(IndexStatusInfo status) {
150+
try {
151+
if (searchService instanceof SolrSearchServiceImpl) {
152+
String solrHome = ((SolrSearchServiceImpl) searchService).getSolrHomePath();
153+
File statusFile = new File(new File(solrHome), "solr_status.xml");
154+
boolean exists = statusFile.exists();
155+
if (exists) {
156+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
157+
dbf.setNamespaceAware(true);
158+
if (builder == null) {
159+
builder = dbf.newDocumentBuilder();
160+
}
161+
Document document = builder.parse(statusFile);
162+
NodeList temp1 = (NodeList) xPath.evaluate("/status/index", document, XPathConstants.NODESET);
163+
Element indexElement = (Element) temp1.item(0);
164+
status.setLastIndexDate(format.parse(indexElement.getAttribute("dateProcessed")));
165+
NodeList infos = (NodeList) xPath.evaluate("info", indexElement, XPathConstants.NODESET);
166+
for (int j = 0; j < infos.getLength(); j++) {
167+
Element info = (Element) infos.item(j);
168+
status.getAdditionalInfo().put(info.getAttribute("key"), info.getAttribute("val"));
169+
}
170+
}
171+
}
172+
} catch (ParserConfigurationException e) {
173+
throw ExceptionHelper.refineException(e);
174+
} catch (SAXException e) {
175+
throw ExceptionHelper.refineException(e);
176+
} catch (IOException e) {
177+
throw ExceptionHelper.refineException(e);
178+
} catch (XPathExpressionException e) {
179+
throw ExceptionHelper.refineException(e);
180+
} catch (ParseException e) {
181+
throw ExceptionHelper.refineException(e);
182+
}
183+
return status;
184+
}
185+
186+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
import java.util.Date;
23+
import java.util.Map;
24+
25+
/**
26+
* @author Jeff Fischer
27+
*/
28+
public interface IndexStatusInfo {
29+
Date getLastIndexDate();
30+
31+
void setLastIndexDate(Date lastIndexDate);
32+
33+
Map<String, String> getAdditionalInfo();
34+
35+
void setAdditionalInfo(Map<String, String> additionalInfo);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
import java.util.Date;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
/**
27+
* @author Jeff Fischer
28+
*/
29+
public class IndexStatusInfoImpl implements IndexStatusInfo {
30+
31+
private Date lastIndexDate;
32+
private Map<String, String> additionalInfo = new HashMap<String, String>();
33+
34+
@Override
35+
public Date getLastIndexDate() {
36+
return lastIndexDate;
37+
}
38+
39+
@Override
40+
public void setLastIndexDate(Date lastIndexDate) {
41+
this.lastIndexDate = lastIndexDate;
42+
}
43+
44+
@Override
45+
public Map<String, String> getAdditionalInfo() {
46+
return additionalInfo;
47+
}
48+
49+
@Override
50+
public void setAdditionalInfo(Map<String, String> additionalInfo) {
51+
this.additionalInfo = additionalInfo;
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
/**
23+
* @author Jeff Fischer
24+
*/
25+
public interface SolrIndexStatusProvider {
26+
void handleUpdateIndexStatus(IndexStatusInfo status);
27+
28+
IndexStatusInfo readIndexStatus(IndexStatusInfo status);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
/**
23+
* @author Jeff Fischer
24+
*/
25+
public interface SolrIndexStatusService {
26+
27+
void setIndexStatus(IndexStatusInfo status);
28+
29+
IndexStatusInfo getIndexStatus();
30+
31+
IndexStatusInfo getSeedStatusInstance();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Framework
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.core.search.service.solr;
21+
22+
import org.springframework.stereotype.Service;
23+
24+
import java.util.List;
25+
26+
import javax.annotation.Resource;
27+
28+
/**
29+
* @author Jeff Fischer
30+
*/
31+
@Service("blSolrIndexStatusService")
32+
public class SolrIndexStatusServiceImpl implements SolrIndexStatusService {
33+
34+
@Resource(name="blSolrIndexStatusProviders")
35+
List<SolrIndexStatusProvider> providers;
36+
37+
@Override
38+
public synchronized void setIndexStatus(IndexStatusInfo status) {
39+
for (SolrIndexStatusProvider provider : providers) {
40+
provider.handleUpdateIndexStatus(status);
41+
}
42+
}
43+
44+
@Override
45+
public synchronized IndexStatusInfo getIndexStatus() {
46+
IndexStatusInfo status = getSeedStatusInstance();
47+
for (SolrIndexStatusProvider provider : providers) {
48+
provider.readIndexStatus(status);
49+
}
50+
return status;
51+
}
52+
53+
@Override
54+
public IndexStatusInfo getSeedStatusInstance() {
55+
return new IndexStatusInfoImpl();
56+
}
57+
}

0 commit comments

Comments
 (0)