1 23 24 27 28 package com.sun.enterprise.cli.commands; 29 30 import com.sun.appserv.management.client.prefs.LoginInfo; 31 import com.sun.appserv.management.client.prefs.LoginInfoStore; 32 import com.sun.appserv.management.client.prefs.LoginInfoStoreFactory; 33 import com.sun.enterprise.cli.framework.CommandValidationException; 34 import com.sun.enterprise.cli.framework.CommandException; 35 import com.sun.enterprise.cli.framework.CLILogger; 36 37 import com.sun.enterprise.util.net.NetUtils; 38 import com.sun.enterprise.util.io.FileUtils; 39 40 import com.sun.enterprise.admin.servermgmt.DomainConfig; 41 import com.sun.enterprise.admin.servermgmt.DomainsManager; 42 43 import java.io.File ; 45 46 import java.util.Properties ; 47 import java.util.StringTokenizer ; 48 49 50 51 55 public class CreateDomainCommand extends BaseLifeCycleCommand 56 { 57 private static final String DOMAIN_PATH = "path"; 59 private static final String INSTANCE_PORT = "instanceport"; 60 private static final String DOCROOT = "docroot"; 61 private static final String TEMPLATE = "template"; 62 private static final String DOMAIN_PROPERTIES = "domainproperties"; 63 private static final String CHECKPORTS_OPTION = "checkports"; 64 private static final int DEFAULT_HTTPSSL_PORT = 8181; 65 private static final int DEFAULT_IIOPSSL_PORT = 3820; 66 private static final int DEFAULT_IIOPMUTUALAUTH_PORT = 3920; 67 private static final int DEFAULT_INSTANCE_PORT = 8080; 68 private static final int DEFAULT_JMS_PORT = 7676; 69 private static final String DEFAULT_JMS_USER = "admin"; 70 private static final String DEFAULT_JMS_PASSWORD = "admin"; 71 private static final int DEFAULT_IIOP_PORT = 3700; 72 private static final int DEFAULT_JMX_PORT = 8686; 73 74 public static String DOMAINDIR_OPTION = "domaindir"; 75 private static String SAVELOGIN_OPTION = "savelogin"; 76 77 private String domainName = null; 78 private String adminUser = null; 79 private String adminPassword = null; 80 private String masterPassword = null; 81 82 83 public CreateDomainCommand() 84 { 85 } 86 87 92 public boolean validateOptions() throws CommandValidationException 93 { 94 return super.validateOptions(); 95 } 96 97 98 99 public void validatePasswords() throws CommandValidationException 100 { 101 if (!isPasswordValid(adminPassword)) { 103 throw new CommandValidationException(getLocalizedString("PasswordLimit", 104 new Object []{ADMIN_PASSWORD})); 105 } 106 107 if (!isPasswordValid(masterPassword)) { 109 throw new CommandValidationException(getLocalizedString("PasswordLimit", 110 new Object []{MASTER_PASSWORD})); 111 } 112 113 CLILogger.getInstance().printDebugMessage("domainName = " + domainName); 115 } 116 117 118 131 protected String getAdminPassword() 132 throws CommandValidationException, CommandException 133 { 134 return getPassword(ADMIN_PASSWORD, "AdminPasswordPrompt", "AdminPasswordConfirmationPrompt", 137 true, true, false, false, null, null, 138 true, true, true, true); 139 } 140 141 142 146 public void runCommand() 147 throws CommandException, CommandValidationException 148 { 149 validateOptions(); 150 setLoggerLevel(); 151 152 String domainDirValue = getOption(DOMAINDIR_OPTION); 154 if ((domainDirValue != null) && !isWindows() && isSpaceInPath(domainDirValue)) 155 throw new CommandException(getLocalizedString("SpaceNotAllowedInPath", 156 new Object []{DOMAINDIR_OPTION})); 157 try { 159 domainName = (String )operands.firstElement(); 160 DomainsManager manager = getFeatureFactory().getDomainsManager(); 161 DomainConfig config = getDomainConfig(domainName); 162 manager.validateDomain(config, false); 163 } catch (Exception e) { 164 CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage()); 165 throw new CommandException(getLocalizedString("CouldNotCreateDomain", 166 new Object [] {domainName}), e); 167 } 168 169 adminUser = getAdminUser(); 170 171 adminPassword = getAdminPassword(); 172 masterPassword = getMasterPassword(true); 173 validatePasswords(); 174 175 try 176 { 177 verifyPortIsValid(getOption(ADMIN_PORT)); 179 if (getOption(INSTANCE_PORT) != null) 181 verifyPortIsValid(getOption(INSTANCE_PORT)); 182 183 Properties domainProperties = getDomainProperties(getOption(DOMAIN_PROPERTIES)); 185 domainProperties.remove(DomainConfig.K_ADMIN_PORT); 187 createTheDomain(getDomainsRoot(), domainProperties); 189 } 190 catch (Exception e) 191 { 192 CLILogger.getInstance().printDetailMessage(e.getLocalizedMessage()); 193 throw new CommandException(getLocalizedString("CouldNotCreateDomain", 194 new Object [] {domainName}), e); 195 } 196 } 197 198 199 208 private void verifyPortIsValid(String portNum) 209 throws CommandException, CommandValidationException 210 { 211 212 final int portToVerify = convertPortStr(portNum); 213 214 if (portToVerify <= 0 || portToVerify > 65535) 215 { 216 throw new CommandException(getLocalizedString("InvalidPortRange", 217 new Object [] {portNum})); 218 } 219 if (getBooleanOption(CHECKPORTS_OPTION) && !NetUtils.isPortFree(portToVerify)) 220 { 221 throw new CommandException(getLocalizedString("PortInUse", 222 new Object [] {(String ) operands.firstElement(), 223 portNum})); 224 } 225 CLILogger.getInstance().printDebugMessage("Port =" + portToVerify); 226 } 227 228 229 235 private void createTheDomain(final String domainPath, 236 Properties domainProperties) 237 throws Exception 238 { 239 Integer adminPort = new Integer (getOption(ADMIN_PORT)); 240 241 242 247 String domainFilePath = (domainPath + File.separator + domainName); 248 if (FileUtils.safeGetCanonicalFile(new File (domainFilePath)).exists()) { 249 throw new CommandValidationException( 250 getLocalizedString("DomainExists", new Object [] {domainName})); 251 } 252 253 final Integer instancePort = getPort(domainProperties, 254 DomainConfig.K_INSTANCE_PORT, 255 getOption(INSTANCE_PORT), 256 Integer.toString(DEFAULT_INSTANCE_PORT), 257 "HTTP Instance"); 258 259 final Integer jmsPort = getPort(domainProperties, 260 DomainConfig.K_JMS_PORT, null, 261 Integer.toString(DEFAULT_JMS_PORT), "JMS"); 262 263 272 final String jmsUser = DEFAULT_JMS_USER; 273 final String jmsPassword = DEFAULT_JMS_PASSWORD; 274 275 final Integer orbPort = getPort(domainProperties, 276 DomainConfig.K_ORB_LISTENER_PORT, 277 null, Integer.toString(DEFAULT_IIOP_PORT), 278 "IIOP"); 279 280 final Integer httpSSLPort = getPort(domainProperties, 281 DomainConfig.K_HTTP_SSL_PORT, null, 282 Integer.toString(DEFAULT_HTTPSSL_PORT), 283 "HTTP_SSL"); 284 285 final Integer iiopSSLPort = getPort(domainProperties, 286 DomainConfig.K_IIOP_SSL_PORT, null, 287 Integer.toString(DEFAULT_IIOPSSL_PORT), 288 "IIOP_SSL"); 289 290 final Integer iiopMutualAuthPort = getPort(domainProperties, 291 DomainConfig.K_IIOP_MUTUALAUTH_PORT, null, 292 Integer.toString(DEFAULT_IIOPMUTUALAUTH_PORT), 293 "IIOP_MUTUALAUTH"); 294 295 final Integer jmxPort = getPort(domainProperties, 302 DomainConfig.K_JMX_PORT, null, 303 Integer.toString(DEFAULT_JMX_PORT), 304 "JMX_ADMIN"); 305 306 Boolean saveMasterPassword = getSaveMasterPassword(masterPassword); 307 308 311 DomainConfig domainConfig = new DomainConfig(domainName, 312 adminPort, domainPath, adminUser, 313 adminPassword, 314 masterPassword, 315 saveMasterPassword, instancePort, jmsUser, 316 jmsPassword, jmsPort, orbPort, 317 httpSSLPort, iiopSSLPort, 318 iiopMutualAuthPort, jmxPort, 319 domainProperties); 320 if (getOption(TEMPLATE) != null) { 321 domainConfig.put(DomainConfig.K_TEMPLATE_NAME, getOption(TEMPLATE)); 322 } 323 324 domainConfig.put(DomainConfig.K_VALIDATE_PORTS, new Boolean (getBooleanOption(CHECKPORTS_OPTION))); 325 326 DomainsManager manager = getFeatureFactory().getDomainsManager(); 327 328 manager.createDomain(domainConfig); 329 CLILogger.getInstance().printDetailMessage(getLocalizedString("DomainCreated", 330 new Object [] {domainName})); 331 checkAsadminPrefsFile(); 332 if (getBooleanOption(SAVELOGIN_OPTION)) 333 saveLogin(adminPort, adminUser, adminPassword, domainName); 334 } 335 336 337 344 private void checkAsadminPrefsFile() 345 { 346 try { 347 checkForFileExistence(System.getProperty("user.home"), ASADMINPREFS); 348 CLILogger.getInstance().printMessage(getLocalizedString("AsadminPrefFileDetected", new Object [] {domainName})); 349 } 350 catch(CommandException ce) { 351 } 354 } 355 356 357 358 367 private Integer getPort(Properties properties, 368 String key, 369 String portStr, 370 String defaultPort, 371 String name) 372 throws CommandValidationException 373 { 374 int port = 0; 375 boolean portNotSpecified = false; 376 boolean invalidPortSpecified = false; 377 boolean defaultPortUsed = false; 378 if ((portStr != null) && !portStr.equals("")) 379 { 380 port = convertPortStr(portStr); 381 if ((port <= 0) || (port > 65535)) 382 invalidPortSpecified = true; 383 } 384 else if (properties != null) 385 { 386 String property = properties.getProperty(key); 387 if ((property != null) && !property.equals("")) { 388 port = convertPortStr(property); 389 } 390 else { 391 portNotSpecified = true; 392 } 393 } 394 else { 395 portNotSpecified = true; 396 } 397 if (portNotSpecified) { 398 port = convertPortStr(defaultPort); 399 defaultPortUsed = true; 400 } 401 if(getBooleanOption(CHECKPORTS_OPTION) && !NetUtils.isPortFree(port)) 402 { 403 port = NetUtils.getFreePort(); 404 if (portNotSpecified) { 407 if (defaultPortUsed) { 408 CLILogger.getInstance().printDetailMessage( 409 getLocalizedString("DefaultPortInUse", 410 new Object [] {name, defaultPort, Integer.toString(port)})); 411 } 412 else { 413 CLILogger.getInstance().printDetailMessage( 414 getLocalizedString("PortNotSpecified", 415 new Object [] {name, new Integer (port)})); 416 } 417 } 418 else if (invalidPortSpecified){ 419 CLILogger.getInstance().printDetailMessage( 420 getLocalizedString("InvalidPortRangeMsg", 421 new Object [] {name, new Integer (port)})); 422 } 423 else { 424 CLILogger.getInstance().printDetailMessage( 425 getLocalizedString("PortInUseMsg", 426 new Object [] {name, new Integer (port)})); 427 } 428 } 429 else if (defaultPortUsed) { 430 CLILogger.getInstance().printDetailMessage( 431 getLocalizedString("UsingDefaultPort", 432 new Object [] {name, Integer.toString(port)})); 433 } 434 435 if (properties != null) { 436 properties.remove(key); 437 } 438 return new Integer (port); 439 } 440 441 442 448 private Properties getDomainProperties(final String propertyValues) 449 throws CommandException, CommandValidationException 450 { 451 Properties propertyList = new Properties (); 452 453 if (propertyValues == null) return propertyList; 454 StringTokenizer st = new StringTokenizer (propertyValues, DELIMITER); 455 while (st.hasMoreTokens()) 456 { 457 String propertyString = st.nextToken(); 458 while (st.hasMoreTokens() && 459 propertyString.endsWith(Character.toString(ESCAPE_CHAR))) 460 { 461 propertyString = propertyString.concat(st.nextToken()); 462 } 463 final int index = propertyString.indexOf(Character.toString(EQUAL_SIGN)); 464 if (index == -1) 465 throw new CommandValidationException(getLocalizedString("InvalidPropertySyntax")); 466 final String propertyName = propertyString.substring(0, index); 467 final String propertyValue = propertyString.substring(index+1); 468 propertyList.put(propertyName, propertyValue); 469 } 470 CLILogger.getInstance().printDebugMessage("domain properties = " + propertyList); 471 return propertyList; 472 } 473 474 480 private int convertPortStr(final String port) 481 throws CommandValidationException 482 { 483 try 484 { 485 return Integer.parseInt(port); 486 } 487 catch(Exception e) 488 { 489 throw new CommandValidationException( 490 getLocalizedString("InvalidPortNumber", 491 new Object [] {port})); 492 } 493 } 494 495 498 private void saveLogin(final int port, final String user, final String password, final String dn) 499 { 500 final CLILogger logger = CLILogger.getInstance(); 501 try { 502 final LoginInfoStore store = LoginInfoStoreFactory.getStore(null); 504 final LoginInfo login = new LoginInfo("localhost", port, user, password); 505 if (store.exists(login.getHost(), login.getPort())) { 506 final Object [] params = new Object [] {login.getHost(), ""+login.getPort()}; 508 final String msg = getLocalizedString("OverwriteLoginMsgCreateDomain", params); 509 logger.printMessage(msg); 510 } 511 store.store(login, true); 512 final Object [] params = new String [] {user, dn, store.getName()}; 513 final String msg = getLocalizedString("LoginInfoStoredCreateDomain", params); 514 logger.printMessage(msg); 515 } 516 catch(final Exception e) { 517 final Object [] params = new String [] {user, dn}; 518 final String msg = getLocalizedString("LoginInfoStoredCreateDomain", params); 519 logger.printWarning(msg); 520 if (logger.isDebug()) 521 logger.printExceptionStackTrace(e); 522 } 523 } 524 } 525 | Popular Tags |