001/* 002 * #%L 003 * Netarchivesuite - deploy 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023package dk.netarkivet.deploy; 024 025import java.util.ArrayList; 026import java.util.List; 027 028import org.dom4j.Element; 029 030import dk.netarkivet.common.exceptions.ArgumentNotValid; 031 032/** 033 * The Parameters class contains the machine parameters. These are the user name, the install directory and the 034 * parameters for running the Java applications. These are inherited from the parent entity (e.g. the machine inherits 035 * the Parameters from the PhysicalLocation), then overwrites its own specified variables. 036 */ 037public class Parameters { 038 /** The class paths. */ 039 private List<Element> classPaths; 040 /** The options for java. */ 041 private List<Element> javaOptions; 042 /** Install directory. */ 043 private Element installDir; 044 /** The machine user name. */ 045 private Element machineUserName; 046 /** The directory for the harvest definition database. */ 047 private Element hdDatabaseDir; 048 /** The directory for the archive database. */ 049 private Element arcDatabaseDir; 050 051 /** 052 * Constructor. Retrieves the parameters from the XML tree. 053 * 054 * @param root The root of the branch for the parent instance. This retrieves the variables in the branch. 055 * @throws ArgumentNotValid If the root is null. 056 */ 057 public Parameters(XmlStructure root) throws ArgumentNotValid { 058 ArgumentNotValid.checkNotNull(root, "XmlStructure root"); 059 // initialise variables 060 classPaths = root.getChildren(Constants.DEPLOY_CLASS_PATH); 061 javaOptions = root.getChildren(Constants.DEPLOY_JAVA_OPTIONS); 062 installDir = root.getChild(Constants.DEPLOY_INSTALL_DIR); 063 machineUserName = root.getChild(Constants.DEPLOY_MACHINE_USER_NAME); 064 hdDatabaseDir = root.getChild(Constants.DEPLOY_HARVEST_DATABASE_DIR); 065 arcDatabaseDir = root.getChild(Constants.DEPLOY_ARCHIVE_DATABASE_DIR); 066 } 067 068 /** 069 * Constructor. Inherits the parameters of the parent instance. 070 * 071 * @param parent The parameters of the parent instance. 072 * @throws ArgumentNotValid If the parent is null. 073 */ 074 public Parameters(Parameters parent) throws ArgumentNotValid { 075 ArgumentNotValid.checkNotNull(parent, "Parameter parent"); 076 // copy parent class paths 077 classPaths = new ArrayList<Element>(); 078 for (Element e : parent.classPaths) { 079 classPaths.add(e.createCopy()); 080 } 081 // copy parent java options 082 javaOptions = new ArrayList<Element>(); 083 for (Element e : parent.javaOptions) { 084 javaOptions.add(e.createCopy()); 085 } 086 // copy parent install dir (if any) 087 if (parent.installDir != null) { 088 installDir = parent.installDir.createCopy(); 089 } 090 // copy parent install dir (if any) 091 if (parent.machineUserName != null) { 092 machineUserName = parent.machineUserName.createCopy(); 093 } 094 // copy parent harvest database dir (if any) 095 if (parent.hdDatabaseDir != null) { 096 hdDatabaseDir = parent.hdDatabaseDir.createCopy(); 097 } 098 // copy the parent archive database dir (if any) 099 if (parent.arcDatabaseDir != null) { 100 arcDatabaseDir = parent.arcDatabaseDir.createCopy(); 101 } 102 } 103 104 /** 105 * Overwrites the inherited parameters, if the root has new specified. 106 * 107 * @param root The root of the current instance. 108 * @throws ArgumentNotValid If the root is null. 109 */ 110 @SuppressWarnings("unchecked") 111 public void newParameters(Element root) throws ArgumentNotValid { 112 ArgumentNotValid.checkNotNull(root, "Element root"); 113 List<Element> tmp; 114 // Add application classpath to inherited ones. 115 classPaths.addAll(root.elements(Constants.DEPLOY_CLASS_PATH)); 116 117 // check if root contains any java options to overwrite inherited ones. 118 tmp = root.elements(Constants.DEPLOY_JAVA_OPTIONS); 119 if (tmp.size() > 0) { 120 javaOptions = tmp; 121 } 122 // check if root contains an install dir to overwrite inherited one. 123 tmp = root.elements(Constants.DEPLOY_INSTALL_DIR); 124 if (tmp.size() > 0) { 125 installDir = tmp.get(0); 126 // log if more than one install directory. 127 if (tmp.size() > 1) { 128 System.out.println("Maximum 1 value expected at: " + Constants.DEPLOY_INSTALL_DIR + " but " 129 + tmp.size() + " received."); 130 } 131 } 132 // check if root contains machine user name to overwrite inherited ones. 133 tmp = root.elements(Constants.DEPLOY_MACHINE_USER_NAME); 134 if (tmp.size() > 0) { 135 machineUserName = tmp.get(0); 136 // log if more than one machine user name. 137 if (tmp.size() > 1) { 138 System.out.println("Maximum 1 value expected at: " + Constants.DEPLOY_MACHINE_USER_NAME + " but " 139 + tmp.size() + " received."); 140 } 141 } 142 // check if root contains a harvest database dir to overwrite 143 // inherited ones. 144 tmp = root.elements(Constants.DEPLOY_HARVEST_DATABASE_DIR); 145 if (tmp.size() > 0) { 146 hdDatabaseDir = tmp.get(0); 147 // log if more than one database directory. 148 if (tmp.size() > 1) { 149 System.out.println("Maximum 1 value expected at: " + Constants.DEPLOY_HARVEST_DATABASE_DIR + " but " 150 + tmp.size() + " received."); 151 } 152 } 153 // check if root contains a archive database dir to overwrite 154 // inherited ones. 155 tmp = root.elements(Constants.DEPLOY_ARCHIVE_DATABASE_DIR); 156 if (tmp.size() > 0) { 157 arcDatabaseDir = tmp.get(0); 158 // log if more than one database directory. 159 if (tmp.size() > 1) { 160 System.out.println("Maximum 1 value expected at: " + Constants.DEPLOY_ARCHIVE_DATABASE_DIR + " but " 161 + tmp.size() + " received."); 162 } 163 } 164 } 165 166 /** 167 * Makes all the java options into a single String. 168 * 169 * @return All the java options. 170 */ 171 public String writeJavaOptions() { 172 StringBuilder res = new StringBuilder(); 173 // apply the java options 174 for (Element e : javaOptions) { 175 res.append(e.getText().trim()); 176 res.append(Constants.SPACE); 177 } 178 return res.toString(); 179 } 180 181 /** 182 * For retrieving the install directory parameter. 183 * 184 * @return The install directory element, or empty string if install dir is null. 185 */ 186 public String getInstallDirValue() { 187 if (installDir != null) { 188 return installDir.getText().trim(); 189 } else { 190 return ""; 191 } 192 } 193 194 /** 195 * For retrieving the directory for the database. 196 * 197 * @return The database directory element, or empty string if install dir is null. 198 */ 199 public String getHarvestDatabaseDirValue() { 200 if (hdDatabaseDir != null) { 201 return hdDatabaseDir.getText().trim(); 202 } else { 203 return ""; 204 } 205 } 206 207 /** 208 * For retrieving the directory for the archive database. 209 * 210 * @return The archive database install directory element, or empty string if install dir is null. 211 */ 212 public String getArchiveDatabaseDirValue() { 213 if (arcDatabaseDir != null) { 214 return arcDatabaseDir.getText().trim(); 215 } else { 216 return ""; 217 } 218 } 219 220 /** 221 * For retrieving the machine user name parameter. 222 * 223 * @return The machine user name. 224 */ 225 public Element getMachineUserName() { 226 return machineUserName; 227 } 228 229 /** 230 * For retrieving the list of class paths. 231 * 232 * @return The list of class paths. 233 */ 234 public List<Element> getClassPaths() { 235 return classPaths; 236 } 237}