1 package org.kuali.ole.docstore.discovery.util;
2
3
4 import org.apache.commons.io.FileUtils;
5
6 import java.io.*;
7 import java.net.*;
8
9
10
11
12
13
14
15
16 public class EnvironmentUtil {
17 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
18 .getLogger(EnvironmentUtil.class);
19 public static final String DISCOVERY_ROOT_DEFAULT = "/opt/ole-discovery/local";
20 public static final String DISCOVERY_ROOT_TEST_DEFAULT = "/opt/ole-discovery/test";
21 public static final String SOLR_CONFIG_FOLDER = "/solr-config";
22 public static final String SOLR_DATA_FOLDER = "/solr-data";
23 public static final String SOLR_PROPERTIES_FOLDER = "/properties";
24 public static final String SOLR_PROPERTIES_FILE = "/properties/ole-discovery.properties";
25
26 public static String solrConfigFolderPath;
27 public static String solrDataFolderPath;
28 public static String solrPropertyFolderPath;
29 public static String discoveryPropertiesFilePath;
30
31
32
33
34
35
36 public static void initEnvironment() {
37 String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
38 if (discoveryRoot == null || discoveryRoot.isEmpty()) {
39 System.setProperty("OLE_DISCOVERY_ROOT", DISCOVERY_ROOT_DEFAULT);
40 }
41 initEnvironmentCommon();
42 }
43
44
45
46
47
48 public static void initTestEnvironment() {
49 String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
50 if (discoveryRoot == null || discoveryRoot.isEmpty()) {
51 System.setProperty("OLE_DISCOVERY_ROOT", DISCOVERY_ROOT_TEST_DEFAULT);
52 }
53 initEnvironmentCommon();
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public static void initEnvironmentCommon() {
75 try {
76 String rootFolder = System.getProperty("OLE_DISCOVERY_ROOT");
77 solrConfigFolderPath = rootFolder.concat(SOLR_CONFIG_FOLDER);
78 solrDataFolderPath = rootFolder.concat(SOLR_DATA_FOLDER);
79 solrPropertyFolderPath = rootFolder.concat(SOLR_PROPERTIES_FOLDER);
80 discoveryPropertiesFilePath = rootFolder.concat(SOLR_PROPERTIES_FILE);
81
82 System.setProperty("solr.solr.home", solrConfigFolderPath);
83 System.setProperty("discovery.properties.file", discoveryPropertiesFilePath);
84
85 File rootFolderPath = new File(rootFolder);
86 if (!rootFolderPath.isDirectory()) {
87 rootFolderPath.mkdirs();
88
89 createDirectory(solrConfigFolderPath);
90
91 createDirectory(solrDataFolderPath);
92
93 createDirectory(solrPropertyFolderPath);
94 URL solrConfigResource = EnvironmentUtil.class.getClassLoader().getResource("solrconfig");
95 File ConfigResource = new File(solrConfigResource.toURI());
96 File target = new File(solrConfigFolderPath);
97 FileUtils.copyDirectory(ConfigResource, target);
98
99
100 URL solrConfigFileResource = EnvironmentUtil.class.getClassLoader()
101 .getResource("./solrconfig/bib/conf/solrconfig.xml");
102 File solrConfigFile = new File(solrConfigFileResource.toURI());
103 String configFileContent = FileUtils.readFileToString(solrConfigFile, "UTF-8");
104 int startingIndex = configFileContent.indexOf("<dataDir>");
105 int endingIndex = configFileContent.indexOf("</dataDir>");
106 String subString = configFileContent.substring(startingIndex, endingIndex).concat("</dataDir>");
107 String modifiedConfigFileContent = configFileContent
108 .replaceAll(subString, "<dataDir>" + solrDataFolderPath + "</dataDir>");
109 File targetFile = new File(solrConfigFolderPath + "/bib/conf/solrconfig.xml");
110 FileUtils.writeStringToFile(targetFile, modifiedConfigFileContent);
111
112
113 URL resource = EnvironmentUtil.class.getClassLoader().getResource("ole-discovery.properties");
114 File sourceProperties = new File(resource.toURI());
115 File targetProperties = new File(discoveryPropertiesFilePath);
116 FileUtils.copyFile(sourceProperties, targetProperties);
117 }
118 }
119 catch (Exception e) {
120 e.printStackTrace();
121 }
122
123 }
124
125 public static void printEnvironment() {
126
127 String discoveryRoot = System.getProperty("OLE_DISCOVERY_ROOT");
128
129 System.out.println("**********{{ Discovery Environment **********");
130 System.out.println("OLE_DISCOVERY_ROOT=" + discoveryRoot);
131 System.out.println("solrConfigFolderPath=" + solrConfigFolderPath);
132 System.out.println("solrDataFolderPath=" + solrDataFolderPath);
133 System.out.println("solrPropertyFolderPath=" + solrPropertyFolderPath);
134 System.out.println("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
135
136 System.out.println("solr.solr.home=" + System.getProperty("solr.solr.home"));
137 System.out.println("discovery.properties.file=" + System.getProperty("discovery.properties.file"));
138 System.out.println("**********}} Discovery Environment **********");
139 }
140
141
142 private static void createDirectory(String dirName) {
143 File solarTest = new File(dirName);
144 if (!solarTest.isDirectory()) {
145 solarTest.mkdirs();
146 }
147
148 }
149
150
151 }