package com.tyndalehouse.step.dataloader.loaders; import org.apache.log4j.Logger; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.install.InstallException; import org.crosswire.jsword.book.install.sword.HttpSwordInstaller; public class JSwordModuleInstaller { private final static String proxyHostProperty = "step.http.proxy"; private final static String proxyPortProperty = "step.http.port"; private final Logger log = Logger.getLogger(getClass()); /** default set of bibles */ String[] defaultModules = new String[] { "ESV", "KJV", "ASV", "StrongsHebrew", "StrongsGreek", "LXX", "BYZ", "WLC" }; /** * a default set of modules need to be installed since unit tests rely on * them to be present TODO: the whole project should probably be cleaned * up/rewritten in a neater fashion * * @throws InstallException * failed to install bible modules */ public void installDefaultModules() { try { log.info("about to create installer"); final HttpSwordInstaller installer = getInstaller(); log.info("got installer, going to reload books"); installer.reloadBookList(); log.info("reloaded books"); for (final String s : defaultModules) { installBible(installer, s); } } catch (final Exception ex) { log.error(ex.getMessage(), ex); } } private void installBible(final HttpSwordInstaller installer, final String initials) throws InstallException { Book b = null; try { final Books books = Books.installed(); if (books != null) { b = books.getBook(initials); if (b == null) { log.info("installing " + initials); installer.install(installer.getBook(initials)); } else { log.warn("Skipping " + initials); } } } catch (final Exception ex) { log.info("An exception occurred while reading the list of installed books (first time?)", ex); } } private HttpSwordInstaller getInstaller() { log.warn("Creating new installer for JSword"); final HttpSwordInstaller resourceInstaller = new HttpSwordInstaller(); System.out.println("Currently hardcoded installer host to:" + "www.crosswire.org"); System.out.println("Currently hardcoded property names for step"); final String host = "www.crosswire.org"; final String proxyHost = System.getProperty(proxyHostProperty); final String proxyPort = System.getProperty(proxyPortProperty); System.out.println(String.format("Setting to (%1$s via %2$s:%3$s)", "www.crosswire.org", proxyHost, proxyPort)); resourceInstaller.setHost(host); if (proxyHost != null && proxyHost.length() != 0) { resourceInstaller.setProxyHost(proxyHost); } if (proxyPort != null && proxyHost.length() != 0) { resourceInstaller.setProxyPort(Integer.parseInt(proxyPort)); } System.out.println("Setting package and catalog directories"); resourceInstaller.setPackageDirectory("/ftpmirror/pub/sword/packages/rawzip"); resourceInstaller.setCatalogDirectory("/ftpmirror/pub/sword/raw"); return resourceInstaller; } }