|
| 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 | +} |
0 commit comments