001package org.kuali.ole.docstore.discovery.util;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.List;
007
008import org.apache.commons.io.FileUtils;
009import org.apache.commons.lang.StringUtils;
010import org.kuali.ole.utility.Constants;
011import org.kuali.ole.utility.ResourceItem;
012import org.kuali.ole.utility.ResourceUtil;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Created by IntelliJ IDEA. User: Sreekanth Date: 6/6/12 Time: 2:41 PM To change this template use File | Settings | File Templates.
018 */
019public class DiscoveryEnvUtil {
020    private static final Logger LOG = LoggerFactory.getLogger(DiscoveryEnvUtil.class);
021    public static final String DISCOVERY_ROOT_DIR = "discovery";
022    public static final String DISCOVERY_ROOT_DEFAULT = "/opt/docstore/" + DISCOVERY_ROOT_DIR;
023    public static final String DISCOVERY_ROOT_TEST_DEFAULT = System.getProperty("user.home")+"/kuali/main/local/docstore-test/discovery-test";
024    public static final String SOLR_CONFIG_FOLDER = "solr-config";
025    public static final String SOLR_DATA_FOLDER = "solr-indexes";
026    public static final String DISCOVERY_PROPERTIES_FOLDER = "properties";
027    public static final String DISCOVERY_PROPERTIES_FILE = "ole-discovery.properties";
028    public static final String UTF8 = "UTF-8";
029
030    protected String rootFolderPath;
031    protected String solrConfigFolderPath;
032    protected String solrDataFolderPath;
033    protected String discoveryPropertyFolderPath;
034    protected String discoveryPropertiesFilePath;
035
036    public String getSolrConfigDirectoryPath() {
037        return solrConfigDirectoryPath;
038    }
039
040    private String solrConfigDirectoryPath;
041    private boolean testEnv = false;
042    ResourceUtil resourceUtil = new ResourceUtil();
043
044    /**
045     * Initializes environment for normal use of discovery. This should be called during web app initialization.
046     */
047    public void initEnvironment() {
048        initEnv(DISCOVERY_ROOT_DEFAULT);
049    }
050
051    /**
052     * Initializes environment for testing. This should be called in all test cases.
053     */
054    public void initTestEnvironment() {
055        testEnv = true;
056        initEnv(DISCOVERY_ROOT_TEST_DEFAULT);
057    }
058
059    /**
060     * Configure the home directory for discovery. When this method return, the system property <code>ole.discover.home</code> is guaranteed to be set.
061     */
062    protected void initEnv(String defaultDiscoveryHome) {
063        String discoveryHome = getDiscoveryHome(defaultDiscoveryHome);
064        LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + discoveryHome);
065        System.setProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY, discoveryHome);
066        initEnvironmentCommon(discoveryHome);
067    }
068
069    /**
070     * Logic for determining the correct home directory for discovery. The directory to use can be configured by system property settings. If no system property is set, discovery
071     * home becomes a path relative to docstore home. If docstore home is not set, it falls through to the default value.
072     */
073    public String getDiscoveryHome(String defaultDiscoveryHome) {
074        String direct = System.getProperty(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY);
075        if (!StringUtils.isBlank(direct)) {
076            return direct;
077        }
078        String docstoreHome = System.getProperty(Constants.OLE_DOCSTORE_HOME_SYSTEM_PROPERTY);
079        if (StringUtils.isBlank(docstoreHome)) {
080            return defaultDiscoveryHome;
081        } else {
082            return docstoreHome + "/" + DISCOVERY_ROOT_DIR;
083        }
084    }
085
086    public void initPathValues(String discoveryRoot) {
087        rootFolderPath = discoveryRoot;
088        solrConfigFolderPath = discoveryRoot.concat("/").concat(SOLR_CONFIG_FOLDER);
089        solrConfigDirectoryPath = solrConfigFolderPath;
090        solrDataFolderPath = discoveryRoot.concat("/").concat(SOLR_DATA_FOLDER);
091        discoveryPropertyFolderPath = discoveryRoot.concat("/").concat(DISCOVERY_PROPERTIES_FOLDER);
092        discoveryPropertiesFilePath = discoveryPropertyFolderPath.concat("/").concat(DISCOVERY_PROPERTIES_FILE);
093    }
094
095    /**
096     * Initializes an environment for discovery at the given root folder. Looks for necessary folders and files under the root folder. Any resources that already exist are left
097     * untouched. Any that are missing are created using default values.
098     */
099    public void initEnvironmentCommon(String docStoreRoot) {
100        initPathValues(docStoreRoot);
101        handleSolrConfig();
102        List<ResourceItem> resourceItems = getResourceItems();
103        for (ResourceItem item : resourceItems) {
104            resourceUtil.validate(item);
105        }
106    }
107
108    protected void handleSolrConfig() {
109        String srcLoc = "";
110        try {
111            File solrConfigDir = new File(solrConfigFolderPath);
112            // The directory already exists, leave it alone
113            if (solrConfigDir.isDirectory()) {
114                return;
115            }
116            if (testEnv) {
117                String path = getClass().getClassLoader().getResource(".").getPath();
118                srcLoc = "File:" + path + "/../../../ole-docstore-webapp/src/main/resources/solrconfig";
119            } else {
120                srcLoc = "classpath:solrconfig";
121            }
122            File srcDir = resourceUtil.getFile(srcLoc);
123            LOG.info("***** Begin Solr Config *****");
124            LOG.info("Source Loc: " + srcLoc);
125            LOG.info("Source Dir: " + srcDir);
126            LOG.info("Dest Dir: " + solrConfigDir);
127            LOG.info("Copying source dir to dest dir...");
128
129            resourceUtil.copyDirectory(srcLoc, solrConfigDir);
130            File solrConfigXmlFile = new File(solrConfigFolderPath + "/bib/conf/solrconfig.xml");
131            LOG.info("Updating " + solrConfigXmlFile);
132            String oldContent = FileUtils.readFileToString(solrConfigXmlFile, UTF8);
133            int beginIndex = oldContent.indexOf("<dataDir>");
134            int endIndex = oldContent.indexOf("</dataDir>");
135            String oldValue = oldContent.substring(beginIndex, endIndex).concat("</dataDir>");
136            String newValue = "<dataDir>" + solrDataFolderPath + "</dataDir>";
137            LOG.info("Setting datadir=" + solrDataFolderPath);
138            String newContent = oldContent.replace(oldValue, newValue);
139            FileUtils.writeStringToFile(solrConfigXmlFile, newContent, UTF8);
140            LOG.info("***** End Solr Config *****");
141        } catch (IOException e) {
142            throw new IllegalArgumentException(e);
143        }
144
145    }
146
147    /**
148     * Return a list of <code>ResourceItem</code> objects representing resources required by discovery
149     */
150    protected List<ResourceItem> getResourceItems() {
151        ResourceItem solrConfigFolder = new ResourceItem();
152        solrConfigFolder.setDestination(new File(solrConfigFolderPath));
153        solrConfigFolder.setSystemProperty("solr.solr.home");
154        solrConfigFolder.setLocalDirectory(true);
155
156        // ResourceItem discoveryPropertiesFile = new ResourceItem();
157        // discoveryPropertiesFile.setDestination(new File(discoveryPropertiesFilePath));
158        // discoveryPropertiesFile.setSystemProperty("discovery.properties.file");
159        // discoveryPropertiesFile.setSourceLocation("classpath:ole-discovery.properties");
160
161        List<ResourceItem> items = new ArrayList<ResourceItem>();
162        items.add(solrConfigFolder);
163        // items.add(discoveryPropertiesFile);
164        return items;
165    }
166
167    public void printEnvironment() {
168
169        // initEnvironmentCommon(SOLR_CONFIG_FOLDER);
170        System.out.println("**********{{ Discovery Environment **********");
171        System.out.println(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
172        System.out.println("solrConfigFolderPath=" + solrConfigFolderPath);
173        System.out.println("solrDataFolderPath=" + solrDataFolderPath);
174        System.out.println("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
175        System.out.println("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
176        System.out.println("**********}} Discovery Environment **********");
177    }
178
179    public void logEnvironment() {
180        LOG.info("**********{{ Discovery Environment **********");
181        LOG.info(Constants.OLE_DISCOVERY_HOME_SYSTEM_PROPERTY + "=" + rootFolderPath);
182        LOG.info("solrConfigFolderPath=" + solrConfigFolderPath);
183        LOG.info("solrDataFolderPath=" + solrDataFolderPath);
184        LOG.info("discoveryPropertyFolderPath=" + discoveryPropertyFolderPath);
185        LOG.info("discoveryPropertiesFilePath=" + discoveryPropertiesFilePath);
186        LOG.info("**********}} Discovery Environment **********");
187
188    }
189
190    public String getRootFolderPath() {
191        return rootFolderPath;
192    }
193
194    public void setRootFolderPath(String rootFolderPath) {
195        this.rootFolderPath = rootFolderPath;
196    }
197
198    public String getSolrConfigFolderPath() {
199        return solrConfigFolderPath;
200    }
201
202    public void setSolrConfigFolderPath(String solrConfigFolderPath) {
203        this.solrConfigFolderPath = solrConfigFolderPath;
204    }
205
206    public String getSolrDataFolderPath() {
207        return solrDataFolderPath;
208    }
209
210    public void setSolrDataFolderPath(String solrDataFolderPath) {
211        this.solrDataFolderPath = solrDataFolderPath;
212    }
213
214    public String getDiscoveryPropertyFolderPath() {
215        return discoveryPropertyFolderPath;
216    }
217
218    public void setDiscoveryPropertyFolderPath(String discoveryPropertyFolderPath) {
219        this.discoveryPropertyFolderPath = discoveryPropertyFolderPath;
220    }
221
222    public String getDiscoveryPropertiesFilePath() {
223        return discoveryPropertiesFilePath;
224    }
225
226    public void setDiscoveryPropertiesFilePath(String discoveryPropertiesFilePath) {
227        this.discoveryPropertiesFilePath = discoveryPropertiesFilePath;
228    }
229}