View Javadoc
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   * 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      private String vendor;
50      private Properties properties;
51  
52      /**
53       * Initializes environment for normal use of docStore. This should be called during web app initialization.
54       */
55      public void initEnvironment() throws IOException {
56          initEnv(DOCSTORE_ROOT_DEFAULT);
57      }
58  
59      /**
60       * Initializes environment for testing. This should be called in all test cases.
61       */
62      public void initTestEnvironment() throws IOException {
63          testEnv = true;
64          initEnv(DOCSTORE_ROOT_TEST_DEFAULT);
65      }
66  
67      /**
68       * Configure the home directory for docstore. When this method returns the system property <code>ole.docstore.home</code> is guaranteed to be set.
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       * 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
78       * default directory.
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  //        jackrabbitFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_FOLDER);
94  //        jackrabbitConfigFolderPath = rootFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_CONFIG_FOLDER);
95  //        jackrabbitPropertyFilePath = jackrabbitConfigFolderPath.concat("/").concat(DOCSTORE_JACKRABBIT_PROPERTIES_FILE);
96          docStorePropertiesFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_PROPERTIES_FILE);
97          // log4jPropertyFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_LOG4J_PROPERTIES_FILE);
98          docStoreDocumentConfigFilePath = docStorePropertiesFolderPath.concat("/").concat(DOCSTORE_DOCUMENTCONFIG_FILE);
99      }
100 
101     /**
102      * 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
103      * exist are left untouched. Any that are missing are created using default values.
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      * Return a list of <code>ResourceItem</code> objects representing resources required by the docstore application.
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         // ResourceItem docStorePropsFile = new ResourceItem();
132         // docStorePropsFile.setDestination(new File(docStorePropertiesFilePath));
133         // docStorePropsFile.setSourceLocation(path + "documentstore.properties");
134         // docStorePropsFile.setSystemProperty("ole.docstore.properties");
135 
136 //        ResourceItem jackrabbitFolder = new ResourceItem();
137 //        jackrabbitFolder.setDestination(new File(jackrabbitFolderPath));
138 //        jackrabbitFolder.setSystemProperty("org.apache.jackrabbit.repository.home");
139 //        jackrabbitFolder.setLocalDirectory(true);
140 
141 //        ResourceItem jackrabbitConfigFolder = new ResourceItem();
142 //        jackrabbitConfigFolder.setDestination(new File(jackrabbitConfigFolderPath));
143 //        jackrabbitConfigFolder.setLocalDirectory(true);
144 
145 //        String vendor = getVendor();
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(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     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 //        System.out.println("jackrabbitFolderPath=" + jackrabbitFolderPath);
205 //        System.out.println("jackrabbitPropertyFilePath=" + jackrabbitPropertyFilePath);
206         // System.out.println("log4jPropertyFilePath=" + log4jPropertyFilePath);
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 //        LOG.info("jackrabbitFolderPath=" + jackrabbitFolderPath);
217 //        LOG.info("jackrabbitPropertyFilePath=" + jackrabbitPropertyFilePath);
218         // LOG.info("log4jPropertyFilePath=" + log4jPropertyFilePath);
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 //    public String getJackrabbitFolderPath() {
240 //        return jackrabbitFolderPath;
241 //    }
242 //
243 //    public void setJackrabbitFolderPath(String jackrabbitFolderPath) {
244 //        this.jackrabbitFolderPath = jackrabbitFolderPath;
245 //    }
246 //
247 //    public String getJackrabbitPropertyFilePath() {
248 //        return jackrabbitPropertyFilePath;
249 //    }
250 //
251 //    public void setJackrabbitPropertyFilePath(String jackrabbitPropertyFilePath) {
252 //        this.jackrabbitPropertyFilePath = jackrabbitPropertyFilePath;
253 //    }
254 
255     public String getRootFolderPath() {
256         return rootFolderPath;
257     }
258 
259     public void setRootFolderPath(String rootFolderPath) {
260         this.rootFolderPath = rootFolderPath;
261     }
262 
263 //    public String getJackrabbitConfigFolderPath() {
264 //        return jackrabbitConfigFolderPath;
265 //    }
266 //
267 //    public void setJackrabbitConfigFolderPath(String jackrabbitConfigFolderPath) {
268 //        this.jackrabbitConfigFolderPath = jackrabbitConfigFolderPath;
269 //    }
270 }