All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.subscription.InstanceLifecycleManagerBuilder Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
/*
 * *************************************************************************
 *  * (C) 2019-2021 SAP SE or an SAP affiliate company. All rights reserved. *
 *  *************************************************************************
 */

package com.sap.cloud.mt.subscription;

import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.xsa.core.instancemanager.client.InstanceManagerClient;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class InstanceLifecycleManagerBuilder {
    private InstanceManagerClient instanceManagerClient;
    private DbIdentifiers dbIdentifiers;
    private int timeout = -1;
    private List dbCredentialsList = new ArrayList<>();
    private DbIdentifiers.DB db = DbIdentifiers.DB.NONE;
    private Duration smCacheRefreshInterval;

    public InstanceLifecycleManager build() throws InternalError {
        if (instanceManagerClient != null) {
            if (dbIdentifiers != null && !(dbIdentifiers instanceof DbIdentifiersHana)) {
                throw new InternalError("Hana database specified with database identifiers of non-HANA DB");
            }
            // Use Hana DB as default
            if (dbIdentifiers == null) {
                dbIdentifiers = new DbIdentifiersHana(new HashSet<>());
            }
            return new InstanceLifecycleManagerImpl(instanceManagerClient, timeout, (DbIdentifiersHana) dbIdentifiers, smCacheRefreshInterval);
        } else if (dbIdentifiers != null) {
            if (dbIdentifiers instanceof DbIdentifiersSql) {
                return new InstanceLifecycleManagerSqlDb((DbIdentifiersSql) dbIdentifiers);
            } else if (dbIdentifiers instanceof DbIdentifiersSqLite) {
                return new InstanceLifecycleManagerSqLite(((DbIdentifiersSqLite) dbIdentifiers).getRoot());
            } else {
                throw new InternalError("No database credentials provided");
            }
        } else if (!dbCredentialsList.isEmpty()) {
            if (db == DbIdentifiers.DB.NONE) {
                throw new InternalError("No database type set");
            }
            return new InstanceLifecycleManagerSqlDb(new DbIdentifiersSql(dbCredentialsList));
        } else {
            throw new InternalError("No instance manager, service manager or databases specified");
        }
    }

    public static InstanceLifecycleManagerBuilder create() {
        return new InstanceLifecycleManagerBuilder();
    }

    private InstanceLifecycleManagerBuilder() {
    }

    public InstanceLifecycleManagerBuilder instanceManagerClient(InstanceManagerClient instanceManagerClient) {
        this.instanceManagerClient = instanceManagerClient;
        return this;
    }

    public InstanceLifecycleManagerBuilder dbIdentifiers(DbIdentifiers dbIdentifiers) {
        this.dbIdentifiers = dbIdentifiers;
        return this;
    }

    public InstanceLifecycleManagerBuilder timeout(int timeout) {
        this.timeout = timeout;
        return this;
    }

    public InstanceLifecycleManagerBuilder addDbCredentials(Map credentials) throws InternalError {
        return addDbCredentials(DbIdentifiers.DB.NONE, credentials);
    }

    public InstanceLifecycleManagerBuilder smCacheRefreshInterval(Duration smCacheRefreshInterval) {
        this.smCacheRefreshInterval = smCacheRefreshInterval;
        return this;
    }

    public InstanceLifecycleManagerBuilder addDbCredentials(DbIdentifiers.DB db,
                                                            Map credentials) throws InternalError {
        DbCredentialsBuilder dbCredentialsBuilder = DbCredentialsBuilder.create()
                .db(db)
                .credentials(credentials);
        DbCredentials dbCredentials = dbCredentialsBuilder.build();
        dbCredentialsList.add(dbCredentials);
        if (this.db != DbIdentifiers.DB.NONE && this.db != dbCredentials.getDB()) {
            throw new InternalError("It is not possible to combine different types of databases for multi tenancy");
        }
        if (this.db == DbIdentifiers.DB.NONE) {
            this.db = dbCredentials.getDB();
        }
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy