1 16 package org.outerj.daisy.install; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.util.Properties ; 21 22 import org.apache.commons.cli.CommandLine; 23 import org.apache.commons.cli.CommandLineParser; 24 import org.apache.commons.cli.HelpFormatter; 25 import org.apache.commons.cli.Option; 26 import org.apache.commons.cli.Options; 27 import org.apache.commons.cli.ParseException; 28 import org.apache.commons.cli.PosixParser; 29 import org.jaxen.dom.DOMXPath; 30 import org.outerj.daisy.repository.Credentials; 31 import org.outerj.daisy.repository.Repository; 32 import org.outerj.daisy.repository.RepositoryManager; 33 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager; 34 import org.outerj.daisy.repository.user.Role; 35 import org.outerj.daisy.repository.user.User; 36 import org.outerj.daisy.repository.user.UserManager; 37 import org.outerj.daisy.repository.user.UserNotFoundException; 38 import org.outerj.daisy.util.VersionHelper; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 42 public class DaisyWikiInitDataDirectory { 43 44 private File dataDirectory; 45 46 private File xconfFile; 47 48 private boolean advanced; 49 50 53 public static void main(String [] args) throws Exception { 54 Options options = new Options(); 55 Option dataDirOption = new Option("d", "wikidata", true, "Where to create the data directory"); 56 dataDirOption.setArgName("path"); 57 options.addOption(dataDirOption); 59 Option confOption = new Option("c", "conf", true, "Configuration file for automated install"); 60 confOption.setArgName("conf-file"); 61 options.addOption(new Option("a", "advanced", false, "Advanced mode (asks more questions)")); 62 options.addOption(confOption); 63 options.addOption(new Option("v", "version", false, "Print version info")); 64 65 CommandLineParser parser = new PosixParser(); 66 CommandLine cmd; 67 try { 68 cmd = parser.parse(options, args); 69 70 if (cmd.hasOption('v')) { 71 System.out.println(VersionHelper.getVersionString(DaisyWikiInitDataDirectory.class.getClassLoader(), 72 "org/outerj/daisy/install/versioninfo.properties")); 73 System.exit(0); 74 } 75 76 System.out.println("Wikidata directory creation"); 77 System.out.println("---------------------------"); 78 79 File datadir; 80 if (cmd.hasOption("d")) { 81 datadir = new File (cmd.getOptionValue("d")); 82 } else { 83 System.out.println("The wikidata directory is a directory containing all configuration"); 84 System.out.println("and data for the wiki."); 85 System.out.println(); 86 System.out.println("The wikidata directory path you enter should be a non-existing or empty"); 87 System.out.println("directory. For example, enter c:\\daisywikidata or /home/<someuser>/daisywikidata"); 88 System.out.println(); 89 System.out.println("PLEASE NOTE: this directory is unrelated to the repository's daisydata directory."); 90 System.out.println(); 91 92 String defaultPath = null; 93 if (InstallHelper.isDevelopmentSetup()) { 94 defaultPath = InstallHelper.getDaisySourceHome() + "/applications/daisywiki/frontend/mywikidata"; 95 } 96 97 String message = "Please enter a path where the wikidata directory should be created."; 98 datadir = InstallHelper.promptForEmptyDir(message, defaultPath); 99 } 100 101 DaisyWikiInitDataDirectory install = new DaisyWikiInitDataDirectory(cmd.hasOption("a"), datadir); 102 InitSettings initSettings; 103 104 if (cmd.hasOption("c")) 105 initSettings = new InitSettings(new File (cmd.getOptionValue("c"))); 106 else 107 initSettings = new InitSettings(); 108 109 install.install(initSettings); 110 } catch (ParseException e) { 111 HelpFormatter help = new HelpFormatter(); 112 help.printHelp("daisy-wikidata-init", options, true); 113 System.exit(1); 114 } 115 } 116 117 public DaisyWikiInitDataDirectory(boolean advanced, File dataDirectory) throws Exception { 118 this.advanced = advanced; 119 this.dataDirectory = dataDirectory; 120 if (dataDirectory.exists()) { 121 throw new Exception ("The datadirectory " + dataDirectory.getAbsolutePath() + " already exists. Choose another location"); 122 } 123 xconfFile = new File (dataDirectory, "daisy.xconf"); 124 } 125 126 public void install(InitSettings settings) throws Exception { 127 createDirectory(); 128 createRegistrar(settings); 129 updateXconf(settings); 130 } 131 132 private void createDirectory() throws Exception { 133 InstallHelper.printTitle("Creating daisy wikidata directory."); 134 File dataDirTemplate; 135 if (InstallHelper.isDevelopmentSetup()) { 136 File daisySourceHome = InstallHelper.getDaisySourceHome(); 137 dataDirTemplate = new File (daisySourceHome, "applications/daisywiki/frontend/src/cocoon/wikidata"); 138 } else { 139 File daisyHome = InstallHelper.getDaisyHome(); 140 dataDirTemplate = new File (daisyHome, "daisywiki/wikidata-template"); 141 } 142 InstallHelper.copyFile(dataDirTemplate, dataDirectory); 143 } 144 145 private void createRegistrar(InitSettings settings) throws Exception { 146 InstallHelper.printTitle("Creating registrar user."); 150 151 String suggestPwd = InstallHelper.isDevelopmentSetup() ? "defaultpwd" : InstallHelper.generatePassword(); 152 153 if (settings.getRepository() == null) 154 settings.setRepository(InstallHelper.promptRepository()); 155 156 UserManager userManager = settings.getRepository().getUserManager(); 157 User user; 158 boolean isNewUser = true; 159 try { 160 user = userManager.getUser("registrar", false); 161 System.out.println("Existing registrar user found, id = " + user.getId()); 162 isNewUser = false; 163 } catch (UserNotFoundException e) { 164 165 if (advanced) { 166 System.out.println(); 167 System.out.println("A user called \"registrar\" will now be created. This user"); 168 System.out.println("will be used to create new user accounts for the self-registering"); 169 System.out.println("users. A password is needed for this user."); 170 System.out.println("The default password presented below is secure-random generated."); 171 System.out.println("This password will be written in a configuration file, you do not"); 172 System.out.println("need to remember it, so it does not matter it is complex."); 173 System.out.println(); 174 175 settings.setRegistrarPassword(InstallHelper.prompt("Enter password for user 'registrar' [ default = " + suggestPwd + " ] : ", suggestPwd)); 176 } 177 178 if (settings.getRegistrarPassword() == null) 179 settings.setRegistrarPassword(suggestPwd); 180 181 user = userManager.createUser("registrar"); 182 user.setPassword(settings.getRegistrarPassword()); 183 Role adminRole = userManager.getRole(Role.ADMINISTRATOR, false); 184 user.addToRole(adminRole); 185 user.setDefaultRole(adminRole); 186 user.save(); 187 System.out.println("Registrar user created, id = " + user.getId()); 188 System.out.println(); 189 } 190 191 if (!xconfFile.exists()) { 192 System.out.println("daisy.xconf file does not exist at " + xconfFile.getAbsolutePath() + ", skipping automatic update."); 193 } else { 194 if (advanced) { 195 System.out.println("The password of the registrar user needs to be specified in"); 196 System.out.println("the daisy.xconf file. I can do this for you."); 197 settings.setUpdateXconf(InstallHelper.promptYesNo("OK to update daisy.xconf now? [default = yes] : ", true)); 198 System.out.println(); 199 } 200 if (settings.isUpdateXconf()) { 201 if (!isNewUser && InstallHelper.isDevelopmentSetup()) { 202 settings.setRegistrarPassword("defaultpwd"); 203 } else if (!isNewUser) { 204 System.out.println("Please enter the password for the existing registrar user. This might be found in another daisy wiki data directory. Look in the 'daisy.xconf' file for the word 'registrar'"); 205 settings.setRegistrarPassword(InstallHelper.prompt("Registrar user password : ")); 206 } 207 System.out.println("Will now update the daisy.xconf with the registrar user password."); 208 Document xconfDocument = InstallHelper.parseFile(xconfFile); 209 DOMXPath registrarXPath = new DOMXPath( 210 "/cocoon/component[@role='org.outerj.daisy.frontend.components.userregistrar.UserRegistrar']/registrarUser"); 211 Element registrarEl = (Element ) registrarXPath.selectSingleNode(xconfDocument); 212 registrarEl.setAttribute("password", settings.getRegistrarPassword()); 213 InstallHelper.saveDocument(xconfFile, xconfDocument); 214 } 215 } 216 } 217 218 private void updateXconf(InitSettings settings) throws Exception { 219 if (InstallHelper.isDevelopmentSetup()) 221 return; 222 223 if (!xconfFile.exists()) { 225 System.out.println("Did not find daisy.xconf at " + xconfFile.getAbsolutePath() + ", will skip updating it."); 226 } else { 227 String internalUserPassword = null; 228 String jmsAdminPassword = null; 229 if (settings.getRepoDataDir() == null) { 230 String repoDataDirPath = InstallHelper.prompt( 231 "Please enter the repository data directory. If there is no repository installed on the current machine leave blank", null); 232 if (repoDataDirPath != null) { 233 settings.setRepoDataDir(new File (repoDataDirPath)); 234 } else { 235 System.out.println("The repository resides on another machine."); 236 } 237 } 238 239 File myconfig = new File (settings.getRepoDataDir(), "conf" + File.separator + "myconfig.xml"); 240 if (settings.getRepoDataDir() != null && settings.getRepoDataDir().exists() && myconfig.exists()) { 241 Document myconfigDoc = InstallHelper.parseFile(myconfig); 242 DOMXPath internalUserPwdXpath = new DOMXPath( 243 "/targets/target[@path='/daisy/repository/fulltextindexupdater']/configuration/repositoryUser/@password"); 244 internalUserPassword = internalUserPwdXpath.stringValueOf(myconfigDoc); 245 DOMXPath jmsAdminPwdXpath = new DOMXPath( 246 "/targets/target[@path='/daisy/jmsclient/jmsclient']/configuration/jmsConnection/credentials/@password"); 247 jmsAdminPassword = jmsAdminPwdXpath.stringValueOf(myconfigDoc); 248 } else { 249 System.out.println("The myconfig.xml file could not be found at this location : " + myconfig.getAbsolutePath() + "."); 250 } 251 252 if (internalUserPassword == null || jmsAdminPassword == null) { 253 System.out.println("Certain values necessary for the installation could not be obtained. You will be prompted for these values : "); 254 internalUserPassword = InstallHelper.prompt("InternalUser password( search for 'internal')"); 255 jmsAdminPassword = InstallHelper.prompt("JmsAdmin password ( search for 'jms' )"); 256 } 257 258 Document xconfDoc = InstallHelper.parseFile(xconfFile); 259 260 DOMXPath cacheUserXPath = new DOMXPath("/cocoon/component[@class='org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager']/cacheUser"); 262 Element credentialsEl = (Element ) cacheUserXPath.selectSingleNode(xconfDoc); 263 credentialsEl.setAttribute("password", internalUserPassword); 264 265 DOMXPath jmsCredentialsXPath = new DOMXPath("/cocoon/component[@class='org.outerj.daisy.jms.impl.JmsClientImpl']/jmsConnection/credentials"); 267 Element jmsCredentialsEl = (Element ) jmsCredentialsXPath.selectSingleNode(xconfDoc); 268 jmsCredentialsEl.setAttribute("password", jmsAdminPassword); 269 270 System.out.println("Will now save the updated daisy.xconf"); 271 InstallHelper.saveDocument(xconfFile, xconfDoc); 272 } 273 274 } 275 276 private static class InitSettings { 277 278 public static final String DAISY_URL = "daisyUrl"; 279 280 public static final String DAISY_LOGIN = "daisyLogin"; 281 282 public static final String DAISY_PASSWORD = "daisyPassword"; 283 284 public static final String REGISTRAR_USER_PASSWORD = "registrarUserPassword"; 285 286 public static final String UPDATE_XCONF = "updateXConf"; 287 288 public static final String REPO_DATA_DIR = "repoDataDir"; 289 290 private String daisyUrl; 291 292 private Repository repository; 293 294 private Credentials credentials; 295 296 private String registrarPassword; 297 298 private boolean updateXconf = true; 299 300 private File repoDataDir; 301 302 public InitSettings() { 303 304 } 305 306 public InitSettings(File configProperties) throws Exception { 307 FileInputStream is = null; 308 try { 309 is = new FileInputStream (configProperties); 310 Properties props = new Properties (); 311 props.load(is); 312 313 credentials = new Credentials(InstallHelper.getPropertyValue(props, DAISY_LOGIN), InstallHelper.getPropertyValue(props, DAISY_PASSWORD)); 314 315 RepositoryManager repositoryManager = new RemoteRepositoryManager(InstallHelper.getPropertyValue(props, DAISY_URL), credentials); 316 repository = repositoryManager.getRepository(credentials); 317 repository.switchRole(Role.ADMINISTRATOR); 318 319 registrarPassword = InstallHelper.getPropertyValue(props, REGISTRAR_USER_PASSWORD, null); 320 updateXconf = Boolean.valueOf(InstallHelper.getPropertyValue(props, UPDATE_XCONF, Boolean.toString(updateXconf))).booleanValue(); 321 322 repoDataDir = new File (InstallHelper.getPropertyValue(props, REPO_DATA_DIR)); 323 324 } finally { 325 if (is != null) 326 is.close(); 327 else 328 System.out.println("Could not open " + configProperties.getAbsolutePath() + " exiting ..."); 329 } 330 } 331 332 public Credentials getCredentials() { 333 return credentials; 334 } 335 336 public void setCredentials(Credentials credentials) { 337 this.credentials = credentials; 338 } 339 340 public String getDaisyUrl() { 341 return daisyUrl; 342 } 343 344 public void setDaisyUrl(String daisyUrl) { 345 this.daisyUrl = daisyUrl; 346 } 347 348 public String getRegistrarPassword() { 349 return registrarPassword; 350 } 351 352 public void setRegistrarPassword(String registrarPassword) { 353 this.registrarPassword = registrarPassword; 354 } 355 356 public boolean isUpdateXconf() { 357 return updateXconf; 358 } 359 360 public void setUpdateXconf(boolean updateXconf) { 361 this.updateXconf = updateXconf; 362 } 363 364 public Repository getRepository() { 365 return repository; 366 } 367 368 public void setRepository(Repository repository) { 369 this.repository = repository; 370 } 371 372 public File getRepoDataDir() { 373 return repoDataDir; 374 } 375 376 public void setRepoDataDir(File repoDataDir) { 377 this.repoDataDir = repoDataDir; 378 } 379 380 } 381 382 } 383 | Popular Tags |