001package com.identityworksllc.iiq.common.plugin;
002
003import com.identityworksllc.iiq.common.service.BaseCommonService;
004import org.apache.commons.logging.Log;
005import org.apache.commons.logging.LogFactory;
006import sailpoint.api.SailPointContext;
007import sailpoint.plugin.PluginBaseHelper;
008import sailpoint.plugin.PluginContext;
009import sailpoint.tools.GeneralException;
010
011import java.sql.Connection;
012
013/**
014 * Abstract class to easily implement a Service that will only run on the
015 * alphabetically lowest Server name. This allows background execution
016 * of a frequent task more reliably than the scheduler, but still prevents
017 * two instances from clashing when more than one server runs the behavior
018 * at once.
019 *
020 * If the alphabetically lowest Server becomes deactivated, the next lowest
021 * will be selected on subsequent runs.
022 *
023 * Extensions should implement {@link #implementation(SailPointContext)}.
024 */
025public abstract class SingleServerService extends BaseCommonService implements PluginContext {
026
027    /**
028     * Constructs a new service object, providing the single-service invoker
029     * as an execution model.
030     */
031    public SingleServerService() {
032        // This determines how our service is actually invoked by the
033        // superclass. Wraps the default call to this::implementation.
034        this.invoker = (context) -> {
035            CommonPluginUtils.singleServerExecute(context, getDefinition(), this::implementation);
036            context.commitTransaction();
037        };
038    }
039
040    /**
041     * Required by the PluginContext interface. Implementing this here so that
042     * we don't have to implement it in every sub-class of this class.
043     *
044     * @return The plugin database connection
045     * @throws GeneralException if a DB connection failure occurs
046     */
047    @Override
048    public Connection getConnection() throws GeneralException {
049        return PluginBaseHelper.getConnection();
050    }
051}