// Assign auto scaling only when necessary.
// Muti-tenant cartridge may not need a policy.
Policy autoScalingPolicy = null;
CartridgeInfo cartridgeInfo;
try {
cartridgeInfo = getServiceClient().getCartridgeInfo(cartridgeType);
} catch (UnregisteredCartridgeException e) {
String message = cartridgeType
+ " is not a valid cartridge type. Please try again with a valid cartridge type.";
log.error(message);
throw e;
} catch (Exception e) {
String message = "Error getting info for " + cartridgeType;
log.error(message, e);
throw new ADCException(message, e);
}
validateCartridgeAlias(cartName, cartridgeType);
if (cartridgeType.equals(CartridgeConstants.MYSQL_CARTRIDGE_NAME)) {
autoScalingPolicy = PolicyHolder.getInstance().getDefaultPolicy();
if (autoScalingPolicy == null) {
throw new PolicyException("Could not load default auto-scaling policy.");
}
if (log.isDebugEnabled()) {
log.debug("Selected Policy: " + new Gson().toJson(autoScalingPolicy));
}
dataCartridge = new DataCartridge();
mysqlPassword = generatePassword();
dataCartridge.setPassword(mysqlPassword);
dataCartridge.setDataCartridgeType(cartridgeType);
dataCartridge.setUserName(CartridgeConstants.MYSQL_DEFAULT_USER);
clusterDomain = getDynamicClusterDomain(cartridgeType, cartName, cartridgeInfo);
registerService(cartridgeType,
clusterDomain,
clusterSubDomain,
createPayload(cartridgeInfo, cartName, autoScalingPolicy, repoURL, mysqlPassword, "localhost", payloadZipFileName, tenantId, tenantDomain),
"*",
cartName + "." + cartridgeInfo.getHostName(),
setRegisterServiceProperties(autoScalingPolicy, tenantId,cartName));
deletePayloadFile(payloadZipFileName);
} else {
if (!new Boolean(System.getProperty(CartridgeConstants.FEATURE_INTERNAL_REPO_ENABLED))) {
if (log.isDebugEnabled()) {
log.debug("Internal repo feature is not enabled.");
}
if (repoURL == null || repoURL.trim().length() == 0) {
throw new RepositoryRequiredException("External repository required for subscription");
}
}
if (repoURL != null && repoURL.trim().length() > 0) {
if (log.isDebugEnabled()) {
log.debug("Repo URL entered: " + repoURL);
}
// Validate Remote Repository.
validateRepository(repoURL, repoUsername, repoPassword, privateRepo,
new Boolean(System.getProperty(CartridgeConstants.FEATURE_EXTERNAL_REPO_VAIDATION_ENABLED)));
}
try {
repository = manageRepository(repoURL, repoUsername, repoPassword, cartName, cartridgeInfo, username,
tenantDomain);
} catch (Exception e) {
log.error(e.getMessage());
throw new ADCException(e);
}
// TODO: Check logic here
if (!cartridgeInfo.getMultiTenant()) {
if (policy != null && policy.trim().length() > 0) {
autoScalingPolicy = PolicyHolder.getInstance().getPolicy(policy);
} else {
autoScalingPolicy = PolicyHolder.getInstance().getDefaultPolicy();
}
if (autoScalingPolicy == null) {
throw new PolicyException("Could not load auto-scaling policy.");
}
if (log.isDebugEnabled()) {
log.debug("Selected Policy: " + new Gson().toJson(autoScalingPolicy));
}
if (cartridgeInfo.getProvider().equalsIgnoreCase(CartridgeConstants.PROVIDER_NAME_WSO2)) { //carbon cartridge private jet mode
clusterDomain = getDynamicClusterDomain(cartridgeType, cartName, cartridgeInfo);
registerService(cartridgeType,
clusterDomain,
clusterSubDomain,
createCarbonPayload(cartridgeInfo, cartName, alias, payloadZipFileName, tenantDomain, false),
Integer.toString(tenantId),
cartName + "." + cartridgeInfo.getHostName(),
setRegisterServiceProperties(autoScalingPolicy,tenantId,cartName));
deletePayloadFile(payloadZipFileName);
} else {
clusterDomain = getDynamicClusterDomain(cartridgeType, cartName, cartridgeInfo);
String mySQLPassword = null;
String mySQLHostName = null;
if (dataCartridgeType != null && dataCartridgeType.trim().length() > 0 && dataCartridgeAlias != null
&& dataCartridgeAlias.trim().length() > 0) {
if (log.isInfoEnabled()) {
log.info("Retrieving Data Cartridge info for connect ... Alias : " + dataCartridgeAlias
+ ", Type: " + dataCartridgeType);
}
//TODO: Optimize following logic. Same logic is used in MySQLPasswordConfigurer
int maxAttempts = Integer.parseInt(System.getProperty(CartridgeConstants.MAX_ATTEMPTS, "50"));
int i = 0;
while (i < maxAttempts) {
i++;
Cartridge c = null;
try {
c = getCartridgeInfo(dataCartridgeAlias, tenantDomain);
} catch (NotSubscribedException e) {
// This cannot happen here.
}
if (c != null) {
if (!c.getStatus().equals("ACTIVE")) {
try {
Thread.sleep(3000);
} catch (InterruptedException ignore) {
}
} else {
mySQLPassword = c.getPassword();
mySQLHostName = c.getIp();
break;
}
}
}
log.info(" MYSQL Cartridge info retrieved ");
}
registerService(cartridgeType,
clusterDomain,
clusterSubDomain,
createPayload(cartridgeInfo, cartName, autoScalingPolicy, repoURL, mySQLPassword,
mySQLHostName, payloadZipFileName, tenantId, tenantDomain), "*",
cartName + "." + cartridgeInfo.getHostName(),
setRegisterServiceProperties(autoScalingPolicy,tenantId,cartName));
deletePayloadFile(payloadZipFileName);
}
} else {