gt:     if (createTablesCmd.isAvailable(hms)) {
context: package com.cloudera.cmf.service.hive;
  
  import com.cloudera.cmf.Constants;
  import com.cloudera.cmf.command.BasicCmdArgs;
  import com.cloudera.cmf.command.CmdWorkCreationException;
  import com.cloudera.cmf.command.RoleCommandHandler;
  import com.cloudera.cmf.command.ServiceCommandHandler;
  import com.cloudera.cmf.command.SvcCmdArgs;
  import com.cloudera.cmf.command.flow.CmdStep;
  import com.cloudera.cmf.command.flow.CmdStepOrderInfo;
  import com.cloudera.cmf.command.flow.CmdStepOrderInfo.CmdStepLocator;
  import com.cloudera.cmf.command.flow.work.ExecRoleCmdWork;
  import com.cloudera.cmf.command.flow.work.ExecSvcCmdWork;
  import com.cloudera.cmf.model.DbRole;
  import com.cloudera.cmf.model.DbService;
  import com.cloudera.cmf.persist.CmfEntityManager;
  import com.cloudera.cmf.service.AbstractBringUpBringDownCommands.GenericBringDownServiceCommand;
  import com.cloudera.cmf.service.AbstractBringUpBringDownCommands.GenericBringUpServiceCommand;
  import com.cloudera.cmf.service.AbstractOneOffRoleCommand;
  import com.cloudera.cmf.service.AbstractRestartCommands.GenericRestartServiceCommand;
  import com.cloudera.cmf.service.AbstractServiceHandler;
  import com.cloudera.cmf.service.ClientConfigHandler;
  import com.cloudera.cmf.service.ConditionallyRequiredConfigsValidator;
  import com.cloudera.cmf.service.ConditionallyRequiredConfigsValidator.Builder;
  import com.cloudera.cmf.service.DaemonRoleHandler.ProcessSupplierException;
  import com.cloudera.cmf.service.DependencyUtils;
  import com.cloudera.cmf.service.DeployServiceClientConfigsCommand;
  import com.cloudera.cmf.service.HandlerUtil;
  import com.cloudera.cmf.service.HdfsDependentDirValidators.DependentDirFederationValidator;
  import com.cloudera.cmf.service.HdfsDependentDirValidators.SharedDependentDirValidator;
  import com.cloudera.cmf.service.RoleCommandServiceWrapper;
  import com.cloudera.cmf.service.RoleHandler;
  import com.cloudera.cmf.service.SSLParams.I18nKeys;
  import com.cloudera.cmf.service.ServiceDataProvider;
  import com.cloudera.cmf.service.ServiceHandler;
  import com.cloudera.cmf.service.ServiceSparkValidator;
  import com.cloudera.cmf.service.ServiceType;
  import com.cloudera.cmf.service.Validator;
  import com.cloudera.cmf.service.config.BooleanParamSpec;
  import com.cloudera.cmf.service.config.ConditionalEvaluator;
  import com.cloudera.cmf.service.config.ConfigEvaluationContext;
  import com.cloudera.cmf.service.config.ConfigEvaluationPredicate;
  import com.cloudera.cmf.service.config.ConfigGenException;
  import com.cloudera.cmf.service.config.ConfigLocator;
  import com.cloudera.cmf.service.config.ConfigLocator.HasConfigLocator;
  import com.cloudera.cmf.service.config.DatabaseParamSpecs.DBType;
  import com.cloudera.cmf.service.config.DependencySecurityConfigUpdateListener;
  import com.cloudera.cmf.service.config.EnumParamSpec;
  import com.cloudera.cmf.service.config.LoadBalancerKerberosConfigUpdateListener;
  import com.cloudera.cmf.service.config.ParamParseException;
  import com.cloudera.cmf.service.config.ParamSpec;
  import com.cloudera.cmf.service.config.PathParamSpec;
  import com.cloudera.cmf.service.config.ServiceParamSpec;
  import com.cloudera.cmf.service.config.ServiceTypeParamSpec;
  import com.cloudera.cmf.version.CdhReleases;
  import com.cloudera.cmf.version.Release;
  import com.cloudera.enterprise.I18nKey;
  import com.cloudera.enterprise.MessageWithArgs;
  import com.cloudera.server.cmf.OperationsManager;
  import com.cloudera.server.cmf.components.EmbeddedDbManager;
  import com.cloudera.server.web.cmf.DbTestConnUtil;
  import com.cloudera.server.web.common.Humanize;
  import com.cloudera.server.web.common.I18n;
  import com.google.common.annotations.VisibleForTesting;
  import com.google.common.collect.ImmutableList;
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.ImmutableMap.Builder;
  import com.google.common.collect.ImmutableSet;
  import com.google.common.collect.Range;
  import com.google.common.collect.Sets;
  import java.io.File;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  public class HiveServiceHandler extends AbstractServiceHandler
  {
    public static final String SERVICE_TYPE = "HIVE";
    public static final String HUMANIZED_SERVICE_NAME = Humanize.humanizeServiceType("HIVE");
    public static final String SERVICE_DESCRIPTION_RESOURCE_ID = "message.hive.desc";
    public static final String CONF_DIR_PREFIX = "hive-conf";
    public static final String KERBEROS_PRINCIPAL_NAME = "hive";
    private static final List<String> NONE_LIST = ImmutableList.of("none");
  
    private static final ConfigLocator HIVE_CONFIG_LOC = ConfigLocator.getConfigLocator("HIVE");
  
    private static final CmdStepOrderInfo.CmdStepLocator CREATE_DB_STEP_LOC = new CmdStepOrderInfo.CmdStepLocator(HIVE_CONFIG_LOC, "CreateHiveDatabase");
  
    private static final CmdStepOrderInfo.CmdStepLocator CREATE_METASTORE_TABLES_STEP_LOC = new CmdStepOrderInfo.CmdStepLocator(HIVE_CONFIG_LOC, "HiveCreateMetastoreTables");
  
    private static final CmdStepOrderInfo.CmdStepLocator CREATE_USER_DIR_STEP_LOC = new CmdStepOrderInfo.CmdStepLocator(HIVE_CONFIG_LOC, "CreateHiveUserDir");
  
    private static final CmdStepOrderInfo.CmdStepLocator CREATE_WAREHOUSE_DIR_STEP_LOC = new CmdStepOrderInfo.CmdStepLocator(HIVE_CONFIG_LOC, "CreateHiveWarehouseDir");
    private final ImmutableMap<String, RoleHandler> roleHandlers;
    private final GatewayRoleHandler gatewayRoleHandler;
    private final HiveMetastoreRoleHandler hiveMetastoreRoleHandler;
    private final HiveServer2RoleHandler hiveserver2RoleHandler;
    private final WebHCatServerRoleHandler webHCatServer;
    private final ClientConfigHandler clientConfigHandler;
    public static final ServiceType TYPE = new ServiceType(null)
    {
      public ServiceHandler createHandlerImpl(Release release, ServiceDataProvider sdp) {
        return new HiveServiceHandler(sdp, release);
      }
    };
  
    public HiveServiceHandler(ServiceDataProvider serviceProvider, Release serviceVersion)
    {
      super(serviceProvider, serviceVersion, "HIVE", "message.hive.desc");
  
      this.gatewayRoleHandler = new GatewayRoleHandler(this, serviceProvider);
      this.hiveMetastoreRoleHandler = new HiveMetastoreRoleHandler(this, serviceProvider);
  
      if (serviceVersion.atLeast(CdhReleases.CDH4_2_0)) {
        this.hiveserver2RoleHandler = new HiveServer2RoleHandler(this, serviceProvider);
        this.webHCatServer = new WebHCatServerRoleHandler(this, serviceProvider);
  
        this.roleHandlers = HandlerUtil.makeRoleHandlerMap(new RoleHandler[] { this.gatewayRoleHandler, this.hiveMetastoreRoleHandler, this.webHCatServer, this.hiveserver2RoleHandler });
      }
      else
      {
        this.hiveserver2RoleHandler = null;
        this.webHCatServer = null;
        this.roleHandlers = HandlerUtil.makeRoleHandlerMap(new RoleHandler[] { this.gatewayRoleHandler, this.hiveMetastoreRoleHandler });
      }
  
      addServiceCommands(new ServiceCommandHandler[] { new AbstractBringUpBringDownCommands.GenericBringUpServiceCommand(this, serviceProvider), new AbstractBringUpBringDownCommands.GenericBringDownServiceCommand(this, serviceProvider), new AbstractRestartCommands.GenericRestartServiceCommand(this, serviceProvider), new DeployServiceClientConfigsCommand(serviceProvider), new HiveCreateUserDirCommand(this, serviceProvider), new HiveCreateWarehouseDirCommand(this, serviceProvider), new HiveReplicationCommand(serviceProvider), new HiveCreateDatabaseCommand(this, serviceProvider), new RoleCommandServiceWrapper(serviceProvider, (AbstractOneOffRoleCommand)this.hiveMetastoreRoleHandler.getRoleCommand("HiveCreateMetastoreTables"), RoleNames.HIVEMETASTORE) });
  
      if (serviceVersion.atLeast(CdhReleases.CDH4_0_0)) {
        addServiceCommands(new ServiceCommandHandler[] { new RoleCommandServiceWrapper(serviceProvider, (AbstractOneOffRoleCommand)this.hiveMetastoreRoleHandler.getRoleCommand("HiveUpdateLocation"), RoleNames.HIVEMETASTORE) });
      }
  
      if (serviceVersion.atLeast(CdhReleases.CDH5_0_0)) {
        addServiceCommands(new ServiceCommandHandler[] { new RoleCommandServiceWrapper(serviceProvider, (AbstractOneOffRoleCommand)this.hiveMetastoreRoleHandler.getRoleCommand("HiveUpgradeMetastore"), RoleNames.HIVEMETASTORE) });
      }
  
      this.clientConfigHandler = new HiveClientConfigHandler(this, serviceProvider);
  
      initialize();
  
      serviceProvider.getOperationsManager().addConfigUpdateListener(new DependencySecurityConfigUpdateListener(serviceProvider, this));
  
      serviceProvider.getOperationsManager().addConfigUpdateListener(new LoadBalancerKerberosConfigUpdateListener(serviceProvider, HiveParams.HS2_LOAD_BALANCER));
    }
  
    protected List<Validator> getAdditionalValidators()
    {
      Release version = getVersion();
      ServiceDataProvider serviceDataProvider = getServiceDataProvider();
      List validators = super.getAdditionalValidators();
      validators.add(new HdfsDependentDirValidators.SharedDependentDirValidator(HiveParams.HIVE_WAREHOUSE_DIRECTORY));
  
      validators.add(new HiveDerbyWarningValidator());
      validators.add(new HiveConcurrencyConfigValidator());
      for (ParamSpec ps : HiveParams.PARAMS_REQUIRED_DERBY) {
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, ps, ps.getTemplateName() + "_required_for_derby_validator", "message.conditionallyRequiredConfigsValidator.hive.isDerby.configRequiredError").ifOtherParamEquals(HiveParams.HIVE_METASTORE_DATABASE_TYPE, DatabaseParamSpecs.DBType.DERBY)).build());
      }
  
      ConfigEvaluationPredicate isNonDerbyDb = ConditionalEvaluator.not(ConditionalEvaluator.paramEvaluatesToValue(HiveParams.HIVE_METASTORE_DATABASE_TYPE, DatabaseParamSpecs.DBType.DERBY));
  
      for (ParamSpec ps : HiveParams.PARAMS_REQUIRED_NON_DERBY) {
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, ps, ps.getTemplateName() + "_required_for_non_derby_validator", "message.conditionallyRequiredConfigsValidator.hive.isNonDerby.configRequiredError").condition(isNonDerbyDb)).build());
      }
  
      validators.add(new HiveBypassMetastoreServerValidator());
      validators.add(new HiveEmbeddedDbValidator(serviceDataProvider.getEmbeddedDbManager()));
  
      if (version.atLeast(CdhReleases.CDH4_0_0)) {
        validators.add(new HdfsDependentDirValidators.DependentDirFederationValidator(HiveParams.HIVE_WAREHOUSE_DIRECTORY));
  
        validators.add(new HiveSentryValidator());
        validators.add(new HiveWebHcatHMSConfigValidator());
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_KEYSTORE_PATH, "keystore_file_required_for_ssl_validator", SSLParams.I18nKeys.KEYSTORE_FILE_REQUIRED.getKey()).ifOtherParamEquals(HiveParams.HS2_ENABLE_SSL, Boolean.valueOf(true))).build());
  
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_KEYSTORE_PASSWORD, "keystore_password_required_for_ssl_validator", SSLParams.I18nKeys.KEYSTORE_PASS_REQUIRED.getKey()).ifOtherParamEquals(HiveParams.HS2_ENABLE_SSL, Boolean.valueOf(true))).build());
      }
  
      if (version.atLeast(CdhReleases.CDH4_6_0)) {
        validators.add(new HiveProxyGroupsValidator());
      }
      if (HiveParams.HS2_LDAP_SINCE.contains(version)) {
        if (Constants.SERVICE_VERSIONS_CDH5_0_0_TO_CDH5_7_0.contains(version))
        {
          validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_ENABLE_LDAP_AUTH, "hive_kerberos_no_ldap_validator", I18nKeys.HS2_LDAP_NO_KERB.getKey()).condition(ConditionalEvaluator.kerberos())).failIfTargetParamPositive().build());
        }
  
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_LDAP_URI, "hive_ldap_uri_required_for_ldap_validator", I18nKeys.HS2_LDAP_NO_URI.getKey()).ifOtherParamEquals(HiveParams.HS2_ENABLE_LDAP_AUTH, Boolean.valueOf(true))).build());
  
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_LDAP_BASEDN, "hive_ldap_domain_or_basedn_validator", I18nKeys.HS2_LDAP_DOMAIN_AND_BASEDN.getKey()).ifOtherStringParamNotEmpty(HiveParams.HS2_LDAP_DOMAIN)).failIfTargetParamPositive().build());
  
        validators.add(((ConditionallyRequiredConfigsValidator.Builder)((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.HS2_ENABLE_LDAP_AUTH, "hive_client_ssl_recommended_with_ldap_auth_validator", I18nKeys.HS2_LDAP_NO_CLIENT_SSL.getKey()).ifOtherParamEquals(HiveParams.HS2_ENABLE_SSL, Boolean.valueOf(false))).failIfTargetParamPositive().warnOnly()).build());
      }
  
      if (HiveParams.SPARK_ON_YARN.supportsVersion(version)) {
        validators.add(makeSparkYarnValidator(serviceDataProvider));
        validators.add(new ServiceSparkValidator(HiveParams.SPARK_ON_YARN, RoleNames.HIVESERVER2));
  
        if (!HiveParams.ENABLE_HIVE_ON_SPARK.supportsVersion(version)) {
          validators.add(makeSparkDependencyMissingValidator(serviceDataProvider));
        }
      }
      if (version.atLeast(CdhReleases.CDH5_9_0)) {
        validators.add(new HiveS3SentryValidator());
      }
      return validators;
    }
  
    @VisibleForTesting
    static ConditionallyRequiredConfigsValidator<DbService> makeSparkYarnValidator(ServiceDataProvider serviceProvider) {
      return ((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceProvider, HiveParams.SPARK_ON_YARN, "hive_on_spark_requires_yarn_validator", I18nKeys.SPARK_REQUIRES_YARN.getKey()).failIfTargetParamPositive().condition(new ConfigEvaluationPredicate()
      {
        public boolean checkCondition(ServiceDataProvider sdp, DbService service, DbRole role, RoleHandler rh, Map<String, Object> configs)
          throws ConfigGenException, DaemonRoleHandler.ProcessSupplierException
        {
          DbService mrOrYarn = (DbService)HiveParams.MAPREDUCE_YARN.extract(configs);
          return (mrOrYarn == null) || (!"YARN".equals(mrOrYarn.getServiceType()));
        }
      })).build();
    }
  
    @VisibleForTesting
    static ConditionallyRequiredConfigsValidator<DbService> makeSparkDependencyMissingValidator(ServiceDataProvider serviceDataProvider)
    {
      return ((ConditionallyRequiredConfigsValidator.Builder)((ConditionallyRequiredConfigsValidator.Builder)ConditionallyRequiredConfigsValidator.builder(serviceDataProvider, HiveParams.SPARK_ON_YARN, "hive_on_spark_missing_dependency", I18nKeys.SPARK_DEPENDENCY_MISSING.getKey()).warnOnly()).condition(new ConfigEvaluationPredicate()
      {
        public boolean checkCondition(ServiceDataProvider sdp, DbService service, DbRole role, RoleHandler rh, Map<String, Object> configs)
          throws ConfigGenException, DaemonRoleHandler.ProcessSupplierException
        {
          List validValues = HiveParams.SPARK_ON_YARN.getValidValues(service, sdp.getServiceHandlerRegistry(), CmfEntityManager.currentCmfEntityManager());
  
          return !HiveServiceHandler.NONE_LIST.equals(validValues);
        }
      })).build();
    }
  
    GatewayRoleHandler getGatewayRoleHandler()
    {
      return this.gatewayRoleHandler;
    }
  
    protected ImmutableMap<String, RoleHandler> getRoleHandlerMap()
    {
      return this.roleHandlers;
    }
  
    protected Set<DbRole> getRolesWithPrimaryStatusLinks(DbService service)
    {
      Set roles = Sets.newLinkedHashSet();
      roles.addAll(service.getRolesWithType(RoleNames.HIVESERVER2.name()));
      return roles;
    }
  
    public boolean requiresCredentials(CmfEntityManager cmfEM, DbService service)
    {
      ServiceParamSpec sps = service.getServiceVersion().sameMajor(CdhReleases.CDH3_0_0) ? HiveParams.MAPREDUCE : HiveParams.MAPREDUCE_YARN;
  
      return DependencyUtils.dependencyRequiresCredentials(service, this, sps, cmfEM);
    }
  
    public ClientConfigHandler getClientConfigHandler()
    {
      return this.clientConfigHandler;
    }
  
    public String getRootLoggerEnvName()
    {
      return "HIVE_ROOT_LOGGER";
    }
  
    protected Set<ParamSpec<?>> getParamSpecs(ImmutableSet<ParamSpec<?>> proposed)
    {
      return Sets.union(HiveParams.SERVICE_PARAMS, proposed);
    }
    @VisibleForTesting
    public static String getInsufficientMetastoreServersMessage(DbService service) {
      return I18n.t("message.hive.insufficient_metastore_servers", new String[] { service.getDisplayName(), HiveParams.HUMANIZED_METASTORE_NAME });
    }
  
    public String getDerbyMetastoreDirIfRelevant(DbService service, DbRole roleToEmitFor)
      throws ParamParseException
    {
      checkArgument(service);
      Map hiveProps = service.getServiceConfigsMap();
      if (null == hiveProps) {
        return null;
      }
  
      Release serviceVersion = service.getServiceVersion();
      if ((!RoleNames.HIVEMETASTORE.name().equals(roleToEmitFor.getRoleType())) && (!((Boolean)HiveParams.HIVE_BYPASS_METASTORE_SERVER.extractFromStringMap(hiveProps, serviceVersion)).booleanValue()))
      {
        return null;
      }
  
      DatabaseParamSpecs.DBType dbType = (DatabaseParamSpecs.DBType)HiveParams.HIVE_METASTORE_DATABASE_TYPE.extractFromStringMap(hiveProps, serviceVersion);
  
      if (DatabaseParamSpecs.DBType.DERBY.equals(dbType)) {
        String derbyPath = (String)HiveParams.HIVE_METASTORE_DERBY_PATH.extractFromStringMap(hiveProps, serviceVersion);
  
        if ((derbyPath != null) && (!derbyPath.isEmpty())) {
          return new File(derbyPath).getParent();
        }
      }
      return null;
    }
  
    public List<String> getOptionalParcelTagsImpl()
    {
      return ImmutableList.of("cdh-plugin", "hive-plugin");
    }
  
    public ImmutableMap<CmdStepOrderInfo, CmdStep> getStepsBeforeStart(DbService service)
      throws CmdWorkCreationException
    {
      ImmutableMap.Builder ret = ImmutableMap.builder();
  
      ConfigEvaluationContext ctx = ConfigEvaluationContext.of(getServiceDataProvider(), service, null);
  
      CmdStepOrderInfo.CmdStepLocator createDbLocator = null;
      String externalHost = DbTestConnUtil.getEmbeddedDbExternalHost(getServiceDataProvider().getEmbeddedDbManager());
  
      if (DbTestConnUtil.isDbCreationNecessary(getServiceDataProvider().getEmbeddedDbManager().getDbAdminProperties(), externalHost, ConfigLocator.getConfigLocator("HIVE"), service.getServiceConfigsMap(), service.getServiceVersion()))
      {
        createDbLocator = CREATE_DB_STEP_LOC;
        ret.put(new CmdStepOrderInfo(ctx, createDbLocator, null), CmdStep.of(ExecSvcCmdWork.of(service, "CreateHiveDatabase", SvcCmdArgs.of(new String[0])), MessageWithArgs.of("message.first_run.step.create_hive_db", new String[0])));
      }
  
      Set hmsRoles = service.getRolesWithType(RoleNames.HIVEMETASTORE.name());
      if ((hmsRoles == null) || (hmsRoles.isEmpty())) {
        throw new CmdWorkCreationException(MessageWithArgs.of("message.missingRoleForService", new String[] { Humanize.humanizeRoleType(RoleNames.HIVEMETASTORE.name()), service.getName() }));
      }
  
      RoleHandler hmsRh = getRoleHandler(RoleNames.HIVEMETASTORE.name());
      RoleCommandHandler createTablesCmd = hmsRh.getRoleCommand("HiveCreateMetastoreTables");
  
      DbRole hms = (DbRole)hmsRoles.iterator().next();
  