1 23 24 package com.sun.enterprise.cli.commands; 25 26 import java.io.*; 27 import java.util.*; 28 29 import com.sun.enterprise.cli.framework.CommandValidationException; 30 import com.sun.enterprise.cli.framework.CommandException; 31 import com.sun.enterprise.cli.framework.CLILogger; 32 import com.sun.enterprise.util.diagnostics.ObjectAnalyzer; 33 import com.sun.enterprise.util.SystemPropertyConstants; 35 import com.sun.enterprise.config.backup.BackupException; 36 import com.sun.enterprise.config.backup.BackupManager; 37 import com.sun.enterprise.config.backup.RestoreManager; 38 import com.sun.enterprise.config.backup.BackupRequest; 39 import com.sun.enterprise.config.backup.BackupWarningException; 40 import com.sun.enterprise.config.backup.ListManager; 41 42 import com.sun.enterprise.admin.servermgmt.DomainsManager; 43 import com.sun.enterprise.admin.servermgmt.DomainConfig; 44 import com.sun.enterprise.admin.servermgmt.InstancesManager; 45 import com.sun.enterprise.admin.common.Status; 46 import com.sun.enterprise.admin.pluggable.ClientPluggableFeatureFactory; 47 59 60 public class BackupCommands extends BaseLifeCycleCommand 61 { 62 public String toString() 63 { 64 return super.toString() + "\n" + ObjectAnalyzer.toString(this); 65 } 66 67 72 public boolean validateOptions() throws CommandValidationException 73 { 74 super.validateOptions(); 75 setOptions(); 76 checkOptions(); 77 prepareRequest(); 78 79 return true; 82 } 83 87 public void runCommand() throws CommandException, CommandValidationException 88 { 89 validateOptions(); 90 91 try 92 { 93 if(command == CmdType.BACKUP) 94 { 95 BackupManager mgr = new BackupManager(request); 96 CLILogger.getInstance().printMessage(mgr.backup()); 97 } 98 else if(command == CmdType.RESTORE) 99 { 100 RestoreManager mgr = new RestoreManager(request); 101 CLILogger.getInstance().printMessage(mgr.restore()); 102 } 103 else if(command == CmdType.LIST) 104 { 105 ListManager mgr = new ListManager(request); 106 CLILogger.getInstance().printMessage(mgr.list()); 107 } 108 else 109 { 110 throw new CommandException("Internal Error"); 112 } 113 } 114 catch(BackupWarningException bwe) 115 { 116 CLILogger.getInstance().printMessage(bwe.getMessage()); 117 } 118 catch(BackupException be) 119 { 120 throw new CommandException(be); 121 } 122 } 123 124 125 128 private void setOptions() throws CommandValidationException 129 { 130 setCommand(); 131 setDomainsDir(); 132 setDomainName(); 133 setBackupFilename(); 134 setDescription(); 135 setVerbosity(); 136 } 137 138 140 private void setCommand() throws CommandValidationException 141 { 142 String cmd = getName(); 143 command = CmdType.valueOf(cmd); 144 145 if(command == null) 146 { 147 throw new CommandValidationException( 149 getLocalizedString("NoUsageText", new String [] {cmd}) ); 150 } 151 } 152 153 155 private void setDomainName() throws CommandValidationException 156 { 157 try 158 { 159 domainName = getDomainName(); 160 } 161 catch(CommandException ce) 162 { 163 throw new CommandValidationException(ce); 164 } 165 } 167 168 170 private void setDomainsDir() throws CommandValidationException 171 { 172 domainsDir = getOption(DOMAINSDIR); 173 174 if(domainsDir == null || domainsDir.length() <= 0) 175 domainsDir = System.getProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY); 176 } 177 178 180 private void setBackupFilename() 181 { 182 backupFilename = getOption(FILENAME); 184 } 185 186 188 private void setVerbosity() throws CommandValidationException 189 { 190 if(getBooleanOption("terse")) 191 terse = true; 192 193 if(getBooleanOption("verbose")) 194 verbose = true; 195 196 198 if(verbose && terse) 199 throw new CommandValidationException(getLocalizedString("NoVerboseAndTerseAtTheSameTime")); 200 } 201 202 204 private void setDescription() 205 { 206 description = getOption(DESCRIPTION); 207 } 208 209 213 private void checkOptions() throws CommandValidationException 214 { 215 if(command == CmdType.BACKUP || command == CmdType.RESTORE) 217 { 218 if(!isNotRunning()) 219 { 220 throw new CommandValidationException(getLocalizedString("DomainIsNotStopped", 221 new String [] {command.name} )); 222 } 223 } 224 if(domainsDir == null || domainsDir.length() <= 0) 226 { 227 throw new CommandValidationException(getLocalizedString("InvalidDomainPath", 228 new String [] {domainsDir}) ); 229 } 230 231 File domainsDirFile = new File(domainsDir); 232 233 if(!domainsDirFile.isDirectory()) 235 { 236 throw new CommandValidationException(getLocalizedString("InvalidDomainPath", 237 new String [] {domainsDir}) ); 238 } 239 240 File domainFile = new File(domainsDirFile, domainName); 241 242 boolean domainDirDoesNotHaveToExist = 246 (command == CmdType.RESTORE) && backupFilename != null; 247 248 if(!domainDirDoesNotHaveToExist) 249 { 250 if(!domainFile.isDirectory() || !domainFile.canWrite()) 251 { 252 throw new CommandValidationException(getLocalizedString("InvalidDirectory", 253 new String [] {domainFile.getPath()}) ); 254 } 255 } 256 257 if(backupFilename != null) 258 { 259 File f = new File(backupFilename); 260 261 if(!f.exists() || !f.canRead()) 262 { 263 throw new CommandValidationException(getLocalizedString("FileDoesNotExist", 264 new String [] { backupFilename } )); 265 } 266 } 267 } 268 269 271 private void prepareRequest() throws CommandValidationException 272 { 273 if(backupFilename == null) 274 request = new BackupRequest(domainsDir, domainName, description); 275 else 276 request = new BackupRequest(domainsDir, domainName, description, backupFilename); 277 278 request.setTerse(terse); 279 request.setVerbose(verbose); 280 } 281 282 284 private boolean isNotRunning() throws CommandValidationException 285 { 286 try 287 { 288 ClientPluggableFeatureFactory cpff = getFeatureFactory(); 289 DomainsManager dm = cpff.getDomainsManager(); 290 DomainConfig dc = getDomainConfig(domainName); 291 InstancesManager im = dm.getInstancesManager(dc); 292 final int state = im.getInstanceStatus(); 293 294 return state == Status.kInstanceNotRunningCode; 295 } 296 catch(Exception e) 297 { 298 throw new CommandValidationException(e); 299 } 300 } 301 302 304 318 320 private static final String DOMAINSDIR = "domaindir"; 321 private static final String FILENAME = "filename"; 322 private static final String DESCRIPTION = "description"; 323 private BackupRequest request; 324 private String domainName; 325 private String domainsDir; 326 private String backupFilename; 327 private String description; 328 private CmdType command; 329 private boolean terse = false; 330 private boolean verbose = false; 331 332 private static class CmdType 333 { 334 private CmdType(String name) 335 { 336 this.name = name; 337 } 338 private static CmdType valueOf(String aName) 339 { 340 if(aName.equals(BACKUP.name)) 341 return BACKUP; 342 343 if(aName.equals(RESTORE.name)) 344 return RESTORE; 345 346 if(aName.equals(LIST.name)) 347 return LIST; 348 349 return null; 350 } 351 352 private static final CmdType BACKUP = new CmdType("backup-domain"); 353 private static final CmdType RESTORE = new CmdType("restore-domain"); 354 private static final CmdType LIST = new CmdType("list-backups"); 355 private final String name; 356 } 357 } 358 | Popular Tags |