1 package org.kuali.ole.docstore.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.*;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.kuali.ole.utility.ConfigUtil;
9 import org.kuali.ole.utility.Constants;
10 import org.kuali.ole.utility.ResourceItem;
11 import org.kuali.ole.utility.ResourceUtil;
12 import org.kuali.rice.core.api.config.property.ConfigContext;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22
23
24
25 public class DocStoreEnvUtil {
26 private static final Logger LOG = LoggerFactory.getLogger(DocStoreEnvUtil.class);
27 public static final String DOCSTORE_ROOT_DIR = "docstore";
28 public static final String DOCSTORE_ROOT_DEFAULT = "/opt/" + DOCSTORE_ROOT_DIR;
29 public static final String DOCSTORE_ROOT_TEST_DEFAULT = "/opt/docstore-test";
30 public static final String DOCSTORE_PROPERTIES_FOLDER = "properties";
31 public static final String DOCSTORE_PROPERTIES_FILE = "documentstore.properties";
32
33
34
35 public static final String DOCSTORE_LOG4J_PROPERTIES_FILE = "log4j.properties";
36 public static final String DOCSTORE_DOCUMENTCONFIG_FILE = "DocumentConfig.xml";
37 public static final String DOC_STORE_PROPERTIES_FILE_SYS_PROP = "ole.docstore.properties";
38
39 protected String docStorePropertiesFilePath;
40 protected String rootFolderPath;
41 protected String docStorePropertiesFolderPath;
42
43
44
45 protected String docStoreDocumentConfigFilePath;
46
47 private boolean testEnv = false;
48 ResourceUtil resourceUtil = new ResourceUtil();
49 private String vendor;
50 private Properties properties;
51
52
53
54
55 public void initEnvironment() throws IOException {
56 initEnv(DOCSTORE_ROOT_DEFAULT);
57 }
58
59
60
61
62 public void initTestEnvironment() throws IOException {
63 testEnv = true;
64 initEnv(DOCSTORE_ROOT_TEST_DEFAULT);
65 }
66
67
68
69
70 protected void initEnv(String defaultRootValue) throws IOException {
71 String docStoreRoot = getDocStoreRoot(defaultRootValue);
72 resourceUtil.setSystemProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY, docStoreRoot);
73 initEnvironmentCommon(docStoreRoot);
74 }
75
76
77
78
79
80 public String getDocStoreRoot(String defaultValue) {
81 ConfigUtil configUtil = new ConfigUtil();
82 String suppliedValue = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
83 if (StringUtils.isBlank(suppliedValue)) {
84 return configUtil.getApplicationHome(Constants.KUALI_GROUP, DOCSTORE_ROOT_DIR);
85 } else {
86 return System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY, defaultValue);
87 }
88 }
89
90 public void initPathValues(String docStoreRoot) {
91 rootFolderPath = docStoreRoot;
92 docStorePropertiesFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_PROPERTIES_FOLDER);
93
94
95
96 docStorePropertiesFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_PROPERTIES_FILE);
97
98 docStoreDocumentConfigFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_DOCUMENTCONFIG_FILE);
99 }
100
101
102
103
104
105 public void initEnvironmentCommon(String docStoreRoot) {
106 initPathValues(docStoreRoot);
107 List<ResourceItem> resourceItems = getResourceItems();
108 for (ResourceItem item : resourceItems) {
109 resourceUtil.validate(item);
110 }
111 }
112
113
114
115
116 protected List<ResourceItem> getResourceItems() {
117 String docStorePath = "";
118 String path = "";
119 if (testEnv) {
120 String rootPath = getClass().getClassLoader().getResource(".").getPath();
121 docStorePath = "File:" + rootPath + "/../../../ole-docstore-webapp/src/main/resources/";
122 path = docStorePath;
123 } else {
124 path = "classpath:";
125 }
126 ResourceItem docStorePropsFolder = new ResourceItem();
127 docStorePropsFolder.setDestination(new File(docStorePropertiesFolderPath));
128 docStorePropsFolder.setSystemProperty("docstore.properties.home");
129 docStorePropsFolder.setLocalDirectory(true);
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 ResourceItem documentConfigFile = new ResourceItem();
162 documentConfigFile.setDestination(new File(docStoreDocumentConfigFilePath));
163 documentConfigFile.setSourceLocation(path + "DocumentConfig.xml");
164 documentConfigFile.setSystemProperty("document.config.file");
165
166 List<ResourceItem> items = new ArrayList<ResourceItem>();
167 items.add(docStorePropsFolder);
168
169
170
171
172 items.add(documentConfigFile);
173 return items;
174 }
175
176 private Properties getProperties() {
177 if (null == properties) {
178 return ConfigContext.getCurrentContextConfig().getProperties();
179 }
180 return properties;
181 }
182
183
184 public void setProperties(Properties properties) {
185 this.properties = properties;
186 }
187
188 private String getVendor() {
189 if (null == vendor) {
190 return ConfigContext.getCurrentContextConfig().getProperty("db.vendor");
191 }
192 return vendor;
193 }
194
195 public void setVendor(String vendor) {
196 this.vendor = vendor;
197 }
198
199 public void printEnvironment() {
200 String docStoreRoot = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
201 System.out.println("**********{{ Docstore Environment **********");
202 System.out.println(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY + "=" + docStoreRoot);
203 System.out.println("docStorePropertiesFilePath=" + docStorePropertiesFilePath);
204
205
206
207 System.out.println("docStoreDocumentConfigFilePath=" + docStoreDocumentConfigFilePath);
208 System.out.println("**********}} DOCSTORE Environment **********");
209 }
210
211 public void logEnvironment() {
212 String docStoreRoot = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
213 LOG.info("**********{{ Docstore Environment **********");
214 LOG.info(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY + "=" + docStoreRoot);
215 LOG.info("docStorePropertiesFilePath=" + docStorePropertiesFilePath);
216
217
218
219 LOG.info("docStoreDocumentConfigFilePath=" + docStoreDocumentConfigFilePath);
220 LOG.info("**********}} Docstore Environment **********");
221 }
222
223 public String getDocStorePropertiesFolderPath() {
224 return docStorePropertiesFolderPath;
225 }
226
227 public void setDocStorePropertiesFolderPath(String docStorePropertiesFolderPath) {
228 this.docStorePropertiesFolderPath = docStorePropertiesFolderPath;
229 }
230
231 public String getDocStorePropertiesFilePath() {
232 return docStorePropertiesFilePath;
233 }
234
235 public void setDocStorePropertiesFilePath(String docStorePropertiesFilePath) {
236 this.docStorePropertiesFilePath = docStorePropertiesFilePath;
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public String getRootFolderPath() {
256 return rootFolderPath;
257 }
258
259 public void setRootFolderPath(String rootFolderPath) {
260 this.rootFolderPath = rootFolderPath;
261 }
262
263
264
265
266
267
268
269
270 }