1 23 24 package com.sun.enterprise.cli.commands; 25 26 import com.sun.enterprise.cli.framework.*; 27 import com.sun.enterprise.deployment.backend.DeploymentStatus; 28 import com.sun.enterprise.deployment.client.DeploymentFacility; 29 import com.sun.enterprise.deployment.client.DeploymentFacilityFactory; 30 import com.sun.enterprise.deployment.client.JESProgressObject; 31 import com.sun.enterprise.deployment.client.JESTarget; 32 import com.sun.enterprise.deployment.client.ServerConnectionIdentifier; 33 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 34 import com.sun.enterprise.deployment.deploy.shared.Archive; 35 import com.sun.enterprise.deployment.deploy.shared.ArchiveFactory; 36 import com.sun.enterprise.deployment.util.DeploymentProperties; 37 import com.sun.enterprise.server.Constants; 38 import javax.enterprise.deploy.spi.Target ; 39 import javax.management.ObjectName ; 40 41 import java.io.*; 43 import java.net.URI ; 44 import java.net.URISyntaxException ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 import java.util.Properties ; 49 50 54 public class DeployCommand extends S1ASCommand 55 { 56 private static final String TARGET_OPTION = "target"; 57 private static final String CONTEXT_ROOT_OPTION = "contextroot"; 58 private static final String FORCE_OPTION = "force"; 59 private static final String PRECOMPILE_JSP_OPTION = "precompilejsp"; 60 private static final String VERIFY_OPTION = "verify"; 61 private static final String UPLOAD_OPTION = "upload"; 62 private static final String ENABLED_OPTION = "enabled"; 63 private static final String COMPONENT_NAME = "name"; 64 private static final String RETRIEVE_DIR = "retrieve"; 65 private static final String VIRTUALSERVERS_OPTION = "virtualservers"; 66 private static final String DB_VENDOR_NAME_OPTION = "dbvendorname"; 67 private static final String CREATE_TABLES_OPTION = "createtables"; 68 private static final String DROP_AND_CREATE_TABLES_OPTION = "dropandcreatetables"; 69 private static final String UNIQUE_TABLENAMES_OPTION = "uniquetablenames"; 70 private static final String DEPLOYMENTPLAN_OPTION = "deploymentplan"; 71 private static final String AVAILABILITY_ENABLED_OPTION= "availabilityenabled"; 72 private static final String GENERATE_RMI_STUBS_OPTION = "generatermistubs"; 73 private static final String LIBRARIES_OPTION = "libraries"; 74 75 private String filePath = null; 76 private String serverFilePath = null; 77 private boolean isUpload = false; 78 private String componentName = null; 79 80 84 public void runCommand() 85 throws CommandException, CommandValidationException 86 { 87 validateOptions(); 88 89 DeploymentFacility df = DeploymentFacilityFactory.getDeploymentFacility(); 90 ServerConnectionIdentifier conn = createServerConnectionIdentifier( 91 getHost(), getPort(), getUser(), getPassword()); 92 df.connect(conn); 93 94 final String targetName = getOption(TARGET_OPTION); 96 97 final String deploymentPlan = getOption(DEPLOYMENTPLAN_OPTION); 98 JESProgressObject progressObject = null; 99 try 100 { 101 AbstractArchive arch = (new ArchiveFactory()).openArchive(filespecToJarURI(filePath)); 102 AbstractArchive plan = null; 103 if (deploymentPlan != null) { 104 plan = (new ArchiveFactory()).openArchive(filespecToJarURI(deploymentPlan)); 105 } 106 Map deployOptions = createDeploymentProperties(); 108 109 if (df.isConnected()) 110 { 111 Target[] targets = df.createTargets(new String []{targetName}); 112 if (targets == null) 113 { 114 throw new CommandException(getLocalizedString("InvalidTarget", new Object [] {targetName})); 116 } 117 118 progressObject = df.deploy(targets, arch, plan, deployOptions); 119 } else 120 { 121 CLILogger.getInstance().printError( 122 getLocalizedString("CouldNotConnectToDAS")); 123 } 124 } 125 catch (Exception e) 126 { 127 if (e.getLocalizedMessage() != null) 129 CLILogger.getInstance().printDetailMessage( 130 e.getLocalizedMessage()); 131 132 throw new CommandException(getLocalizedString( 133 "CommandUnSuccessful", new Object [] {name} ), e); 134 } 135 136 137 DeploymentStatus status = df.waitFor(progressObject); 138 final String statusString = status.getStageStatusMessage(); 139 140 if (status != null && status.getStatus() == DeploymentStatus.FAILURE) { 141 checkDeployStatus(status, statusString); 142 } 143 144 final String retrievePath = getOption(RETRIEVE_DIR); 145 if (retrievePath != null){ 146 try { 147 CLILogger.getInstance().printDebugMessage("componentName = " + componentName + 148 " retrievePath = " + retrievePath); 149 final String fileName = df.downloadFile(new File(retrievePath), componentName, null); 150 CLILogger.getInstance().printDebugMessage("downloaded stubs to : " + fileName ); 151 } 152 catch(Exception e){ 153 throw new CommandException((getLocalizedString( 154 "InvalidValueInOption", new Object [] {RETRIEVE_DIR, 155 retrievePath})) + "\n"+ e.getLocalizedMessage()); 156 } 157 } 158 159 if (status != null && status.getStatus() == DeploymentStatus.WARNING) { 160 CLILogger.getInstance().printDetailMessage(getLocalizedString( 161 "CommandSuccessfulWithMsg", 162 new Object [] {name, statusString})); 163 } else { 164 CLILogger.getInstance().printDetailMessage(getLocalizedString( 165 "CommandSuccessful", 166 new Object [] {name} ) ); 167 } 168 } 169 170 175 private URI filespecToJarURI(String fileSpec) throws URISyntaxException { 176 File archiveFile = new File(fileSpec); 177 String prefix = (archiveFile.isDirectory()) ? "file" : "jar"; 178 URI archiveFileURI = archiveFile.toURI(); 179 URI archiveJarURI = new URI (prefix, "" , archiveFileURI.getSchemeSpecificPart(), null, null); 180 return archiveJarURI; 181 } 182 183 188 private Map createDeploymentProperties() 189 { 190 final String virtualServers = getOption(VIRTUALSERVERS_OPTION); 191 final String contextRoot = getContextRoot(); 192 final String dbVendorName = getOption(DB_VENDOR_NAME_OPTION); 193 final String createTable = getOption(CREATE_TABLES_OPTION); 194 final String dropCreateTable = getOption(DROP_AND_CREATE_TABLES_OPTION); 195 final String uniqueTableNames = getOption(UNIQUE_TABLENAMES_OPTION); 196 final String target = getOption(TARGET_OPTION); 197 final String libraries = getOption(LIBRARIES_OPTION); 198 199 Properties props = new Properties(); 200 201 if (props != null) 202 props.put(DeploymentProperties.TARGET, target); 203 204 if (serverFilePath != null && isUpload) 205 props.put(DeploymentProperties.ARCHIVE_NAME, serverFilePath); 206 else if(filePath != null) 207 props.put(DeploymentProperties.ARCHIVE_NAME, filePath); 208 209 210 if(contextRoot != null) 211 props.put(DeploymentProperties.CONTEXT_ROOT, contextRoot); 212 213 if(virtualServers != null) 214 props.put(DeploymentProperties.VIRTUAL_SERVERS, 215 virtualServers); 216 217 if(dbVendorName != null) 218 props.put(Constants.CMP_DB_VENDOR_NAME, dbVendorName); 219 220 if(createTable != null) 221 props.put(Constants.CMP_CREATE_TABLES, createTable); 222 223 if(dropCreateTable != null) 224 props.put(Constants.CMP_DROP_AND_CREATE_TABLES, dropCreateTable); 225 226 if(uniqueTableNames != null) 227 props.put(Constants.CMP_UNIQUE_TABLE_NAMES, uniqueTableNames); 228 229 if(libraries != null) 230 props.put(DeploymentProperties.DEPLOY_OPTION_LIBRARIES_KEY, libraries); 231 232 props.put(DeploymentProperties.NAME, componentName); 236 237 props.put(DeploymentProperties.VERIFY, 238 getOption(VERIFY_OPTION)); 239 240 props.put(DeploymentProperties.PRECOMPILE_JSP, 241 getOption(PRECOMPILE_JSP_OPTION)); 242 243 props.put(DeploymentProperties.ENABLE, 244 getOption(ENABLED_OPTION)); 245 246 props.put(DeploymentProperties.FORCE, 247 getOption(FORCE_OPTION)); 248 249 props.put(DeploymentProperties.AVAILABILITY_ENABLED, 250 getOption(AVAILABILITY_ENABLED_OPTION)); 251 252 props.put(DeploymentProperties.GENERATE_RMI_STUBS, 253 getOption(GENERATE_RMI_STUBS_OPTION)); 254 255 final String retrievePath = getOption(RETRIEVE_DIR); 256 if (retrievePath != null){ 257 props.put(DeploymentProperties.CLIENTJARREQUESTED, "true"); 258 }; 259 260 return props; 261 262 } 263 264 265 276 private void checkDeployStatus(DeploymentStatus status, 277 String statusString) throws CommandException 278 { 279 if (status != null && status.getStatus() == DeploymentStatus.FAILURE) { 280 throw new CommandException(getLocalizedString( 281 "CommandUnSuccessfulWithMsg", new Object [] {name, 282 statusString} )); 283 } 284 } 285 286 287 294 public boolean validateOptions() throws CommandValidationException 295 { 296 super.validateOptions(); 297 if (getOption(CREATE_TABLES_OPTION) !=null && 298 getOption(DROP_AND_CREATE_TABLES_OPTION) != null) 299 throw new CommandValidationException(getLocalizedString( 300 "MutuallyExclusiveOption", 301 new Object [] {CREATE_TABLES_OPTION, 302 DROP_AND_CREATE_TABLES_OPTION 303 })); 304 305 filePath = (String ) getOperands().get(0); 306 isUpload = getOption(UPLOAD_OPTION)==null?false:getBooleanOption(UPLOAD_OPTION); 307 componentName = getComponentName(); 308 return true; 309 } 310 311 312 317 private String getContextRoot() 318 { 319 String contextRoot = getOption(CONTEXT_ROOT_OPTION); 320 return contextRoot; 321 } 322 323 324 329 private String getComponentName() throws CommandValidationException 330 { 331 String name = getOption(COMPONENT_NAME); 332 if (name == null) 333 { 334 name = getNameFromFilePath(); 335 } 336 if ((name == null) || (name.equals(""))) 338 throw new CommandValidationException(getLocalizedString("ComponentNameNull")); 339 return name; 340 } 341 342 343 347 private String getNameFromFilePath() 348 { 349 String fileNameOperand = (String ) getOperands().get(0); 350 final String fileName = new File(fileNameOperand).getName(); 351 int toIndex = fileName.lastIndexOf('.'); 352 if (toIndex < 0) 353 { 354 toIndex = fileName.length(); 355 } 356 return fileName.substring(0, toIndex); 357 } 358 } 359 | Popular Tags |