View Javadoc

1   package org.kuali.ole.docstore.util;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.kuali.ole.utility.ConfigUtil;
10  import org.kuali.ole.utility.Constants;
11  import org.kuali.ole.utility.ResourceItem;
12  import org.kuali.ole.utility.ResourceUtil;
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   * Created by IntelliJ IDEA. User: Sreekanth Date: 6/6/12 Time: 3:19 PM To change this template use File | Settings | File Templates.
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  	public static final String DOCSTORE_JACKRABBIT_FOLDER = "jackrabbit";
33  	public static final String DOCSTORE_JACKRABBIT_CONFIG_FOLDER = "jackrabbit-config";
34  	public static final String DOCSTORE_JACKRABBIT_PROPERTIES_FILE = "repository.xml";
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  	protected String jackrabbitFolderPath;
43  	protected String jackrabbitPropertyFilePath;
44  	// protected String log4jPropertyFilePath;
45  	protected String docStoreDocumentConfigFilePath;
46  	protected String jackrabbitConfigFolderPath;
47  	private boolean testEnv = false;
48  	ResourceUtil resourceUtil = new ResourceUtil();
49  
50  	/**
51  	 * Initializes environment for normal use of docStore. This should be called during web app initialization.
52  	 */
53  	public void initEnvironment() throws IOException {
54  		initEnv(DOCSTORE_ROOT_DEFAULT);
55  	}
56  
57  	/**
58  	 * Initializes environment for testing. This should be called in all test cases.
59  	 */
60  	public void initTestEnvironment() throws IOException {
61  		testEnv = true;
62  		initEnv(DOCSTORE_ROOT_TEST_DEFAULT);
63  	}
64  
65  	/**
66  	 * Configure the home directory for docstore. When this method returns the system property <code>ole.docstore.home</code> is guaranteed to be set.
67  	 */
68  	protected void initEnv(String defaultRootValue) throws IOException {
69  		String docStoreRoot = getDocStoreRoot(defaultRootValue);
70  		resourceUtil.setSystemProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY, docStoreRoot);
71  		initEnvironmentCommon(docStoreRoot);
72  	}
73  
74  	/**
75  	 * Calculate the correct home directory for docstore based on system properties and environment. If no system properties or environment variables are set, fall through to the
76  	 * default directory.
77  	 */
78  	public String getDocStoreRoot(String defaultValue) {
79  		ConfigUtil configUtil = new ConfigUtil();
80  		String suppliedValue = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
81  		if (StringUtils.isBlank(suppliedValue)) {
82  			return configUtil.getApplicationHome(Constants.KUALI_GROUP, DOCSTORE_ROOT_DIR);
83  		} else {
84  			return System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY, defaultValue);
85  		}
86  	}
87  
88  	public void initPathValues(String docStoreRoot) {
89  		rootFolderPath = docStoreRoot;
90  		docStorePropertiesFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_PROPERTIES_FOLDER);
91  		jackrabbitFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_FOLDER);
92  		jackrabbitConfigFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_CONFIG_FOLDER);
93  		jackrabbitPropertyFilePath = jackrabbitConfigFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_PROPERTIES_FILE);
94  		docStorePropertiesFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_PROPERTIES_FILE);
95  		// log4jPropertyFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_LOG4J_PROPERTIES_FILE);
96  		docStoreDocumentConfigFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_DOCUMENTCONFIG_FILE);
97  	}
98  
99  	/**
100 	 * Initializes an environment for the DocStore application at the given root folder. Looks for necessary folders and files under the root folder. Any resources that already
101 	 * exist are left untouched. Any that are missing are created using default values.
102 	 */
103 	public void initEnvironmentCommon(String docStoreRoot) {
104 		initPathValues(docStoreRoot);
105 		List<ResourceItem> resourceItems = getResourceItems();
106 		for (ResourceItem item : resourceItems) {
107 			resourceUtil.validate(item);
108 		}
109 	}
110 
111 	/**
112 	 * Return a list of <code>ResourceItem</code> objects representing resources required by the docstore application.
113 	 */
114 	protected List<ResourceItem> getResourceItems() {
115 		String docStorePath = "";
116 		String path = "";
117 		if (testEnv) {
118 			String rootPath = getClass().getClassLoader().getResource(".").getPath();
119 			docStorePath = "File:" + rootPath + "/../../../ole-docstore-webapp/src/main/resources/";
120 			path = docStorePath;
121 		} else {
122 			path = "classpath:";
123 		}
124 		ResourceItem docStorePropsFolder = new ResourceItem();
125 		docStorePropsFolder.setDestination(new File(docStorePropertiesFolderPath));
126 		docStorePropsFolder.setSystemProperty("docstore.properties.home");
127 		docStorePropsFolder.setLocalDirectory(true);
128 
129 		// ResourceItem docStorePropsFile = new ResourceItem();
130 		// docStorePropsFile.setDestination(new File(docStorePropertiesFilePath));
131 		// docStorePropsFile.setSourceLocation(path + "documentstore.properties");
132 		// docStorePropsFile.setSystemProperty("ole.docstore.properties");
133 
134 		ResourceItem jackrabbitFolder = new ResourceItem();
135 		jackrabbitFolder.setDestination(new File(jackrabbitFolderPath));
136 		jackrabbitFolder.setSystemProperty("org.apache.jackrabbit.repository.home");
137 		jackrabbitFolder.setLocalDirectory(true);
138 
139 		ResourceItem jackrabbitConfigFolder = new ResourceItem();
140 		jackrabbitConfigFolder.setDestination(new File(jackrabbitConfigFolderPath));
141 		jackrabbitConfigFolder.setLocalDirectory(true);
142 
143 		PropertyUtil util = PropertyUtil.getPropertyUtil();
144 
145 		String vendor = util.getProperty("db.vendor");
146 		if (StringUtils.isBlank(vendor)) {
147 			throw new IllegalArgumentException("The property 'db.vendor' cannot be blank");
148 		}
149 		ResourceItem jackrabbitRepositoryFile = new ResourceItem();
150 		jackrabbitRepositoryFile.setDestination(new File(jackrabbitPropertyFilePath));
151 		jackrabbitRepositoryFile.setSourceLocation(path + "repository-" + vendor + ".xml");
152 		jackrabbitRepositoryFile.setSystemProperty("org.apache.jackrabbit.repository.conf");
153 		jackrabbitRepositoryFile.setFilter(true);
154 		jackrabbitRepositoryFile.setProperties(util.getProperties());
155 
156 		// ResourceItem log4jConfigFile = new ResourceItem();
157 		// log4jConfigFile.setDestination(new File(log4jPropertyFilePath));
158 		// log4jConfigFile.setSourceLocation(path + "log4j.properties");
159 		// log4jConfigFile.setSystemProperty("log4j.configuration");
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 		// items.add(docStorePropsFile);
169 		items.add(jackrabbitFolder);
170 		items.add(jackrabbitRepositoryFile);
171 		// items.add(log4jConfigFile);
172 		items.add(documentConfigFile);
173 		return items;
174 	}
175 
176 	public void printEnvironment() {
177 		String docStoreRoot = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
178 		System.out.println("**********{{ Docstore Environment **********");
179 		System.out.println(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY + "=" + docStoreRoot);
180 		System.out.println("docStorePropertiesFilePath=" + docStorePropertiesFilePath);
181 		System.out.println("jackrabbitFolderPath=" + jackrabbitFolderPath);
182 		System.out.println("jackrabbitPropertyFilePath=" + jackrabbitPropertyFilePath);
183 		// System.out.println("log4jPropertyFilePath=" + log4jPropertyFilePath);
184 		System.out.println("docStoreDocumentConfigFilePath=" + docStoreDocumentConfigFilePath);
185 		System.out.println("**********}} DOCSTORE Environment **********");
186 	}
187 
188 	public void logEnvironment() {
189 		String docStoreRoot = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
190 		LOG.info("**********{{ Docstore Environment **********");
191 		LOG.info(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY + "=" + docStoreRoot);
192 		LOG.info("docStorePropertiesFilePath=" + docStorePropertiesFilePath);
193 		LOG.info("jackrabbitFolderPath=" + jackrabbitFolderPath);
194 		LOG.info("jackrabbitPropertyFilePath=" + jackrabbitPropertyFilePath);
195 		// LOG.info("log4jPropertyFilePath=" + log4jPropertyFilePath);
196 		LOG.info("docStoreDocumentConfigFilePath=" + docStoreDocumentConfigFilePath);
197 		LOG.info("**********}} Docstore Environment **********");
198 	}
199 
200 	public String getDocStorePropertiesFolderPath() {
201 		return docStorePropertiesFolderPath;
202 	}
203 
204 	public void setDocStorePropertiesFolderPath(String docStorePropertiesFolderPath) {
205 		this.docStorePropertiesFolderPath = docStorePropertiesFolderPath;
206 	}
207 
208 	public String getDocStorePropertiesFilePath() {
209 		return docStorePropertiesFilePath;
210 	}
211 
212 	public void setDocStorePropertiesFilePath(String docStorePropertiesFilePath) {
213 		this.docStorePropertiesFilePath = docStorePropertiesFilePath;
214 	}
215 
216 	public String getJackrabbitFolderPath() {
217 		return jackrabbitFolderPath;
218 	}
219 
220 	public void setJackrabbitFolderPath(String jackrabbitFolderPath) {
221 		this.jackrabbitFolderPath = jackrabbitFolderPath;
222 	}
223 
224 	public String getJackrabbitPropertyFilePath() {
225 		return jackrabbitPropertyFilePath;
226 	}
227 
228 	public void setJackrabbitPropertyFilePath(String jackrabbitPropertyFilePath) {
229 		this.jackrabbitPropertyFilePath = jackrabbitPropertyFilePath;
230 	}
231 
232 	public String getRootFolderPath() {
233 		return rootFolderPath;
234 	}
235 
236 	public void setRootFolderPath(String rootFolderPath) {
237 		this.rootFolderPath = rootFolderPath;
238 	}
239 
240 	public String getJackrabbitConfigFolderPath() {
241 		return jackrabbitConfigFolderPath;
242 	}
243 
244 	public void setJackrabbitConfigFolderPath(String jackrabbitConfigFolderPath) {
245 		this.jackrabbitConfigFolderPath = jackrabbitConfigFolderPath;
246 	}
247 }