Advertisement
k-joseph

Untitled

Jun 3rd, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs.module.solr;
  15.  
  16. import java.io.File;
  17. import java.io.StringWriter;
  18. import java.net.URL;
  19. import java.util.Properties;
  20.  
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.transform.OutputKeys;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.dom.DOMSource;
  27. import javax.xml.transform.stream.StreamResult;
  28.  
  29. import org.apache.commons.io.FileUtils;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.openmrs.api.context.Context;
  33. import org.openmrs.module.Activator;
  34. import org.openmrs.util.OpenmrsClassLoader;
  35. import org.openmrs.util.OpenmrsUtil;
  36. import org.w3c.dom.Document;
  37. import org.w3c.dom.Element;
  38. import org.w3c.dom.Node;
  39.  
  40. /**
  41. * This class contains the logic that is run every time this module is either started or stopped.
  42. */
  43. public class SolrActivator implements Activator {
  44.  
  45. protected Log log = LogFactory.getLog(getClass());
  46.  
  47. /**
  48. * @see Activator#startup()
  49. */
  50. public void startup() {
  51. log.info("Starting Solr Module");
  52.  
  53. try {
  54. //Get the solr home folder
  55. String solrHome = Context.getAdministrationService().getGlobalProperty("solr.home",
  56. new File(OpenmrsUtil.getApplicationDataDirectory(), "solr").getAbsolutePath());
  57.  
  58. //Tell solr that this is our home folder
  59. System.setProperty("solr.solr.home", solrHome);
  60.  
  61. //If user has not setup solr config folder, set a default one
  62. String configFolder = solrHome + File.separatorChar + "conf";
  63. System.out.println(configFolder);
  64.  
  65. if (!new File(configFolder).exists()) {
  66. URL url = OpenmrsClassLoader.getInstance().getResource("conf");
  67. File file = new File(url.getFile());
  68. FileUtils.copyDirectoryToDirectory(file, new File(solrHome));
  69.  
  70. setDataImportConnectionInfo(configFolder);
  71. }
  72. }
  73. catch (Exception ex) {
  74. log.error("Failed to copy Solr config folder", ex);
  75. }
  76. }
  77.  
  78. private void setDataImportConnectionInfo(String configFolder) throws Exception {
  79. Properties properties = Context.getRuntimeProperties();
  80.  
  81. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  82. DocumentBuilder db = dbf.newDocumentBuilder();
  83. Document doc = db.parse(OpenmrsClassLoader.getInstance().getResourceAsStream("conf/data-config.xml"));
  84.  
  85. System.out.println(Context.getRuntimeProperties().toString());
  86. Element node = (Element) doc.getElementsByTagName("dataSource").item(0);
  87.  
  88. node.setAttribute("url", properties.getProperty("connection.url"));
  89. node.setAttribute("user", properties.getProperty("connection.username"));
  90. node.setAttribute("password", properties.getProperty("connection.password"));
  91.  
  92. String xml = doc2String(doc);
  93. File file = new File(configFolder + File.separatorChar + "data-config.xml");
  94. FileUtils.writeStringToFile(file, xml, "UTF-8");
  95. }
  96.  
  97. public static String doc2String(Node doc) throws Exception {
  98. TransformerFactory tFactory = TransformerFactory.newInstance();
  99. Transformer transformer = tFactory.newTransformer();
  100.  
  101. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  102.  
  103. StringWriter outStream = new StringWriter();
  104. DOMSource source = new DOMSource(doc);
  105. StreamResult result = new StreamResult(outStream);
  106. transformer.transform(source, result);
  107. return outStream.getBuffer().toString();
  108. }
  109.  
  110. /**
  111. * @see Activator#shutdown()
  112. */
  113. public void shutdown() {
  114. log.info("Shutting down Solr Module");
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement