001package com.identityworksllc.iiq.common.service;
002
003import com.identityworksllc.iiq.common.plugin.CommonPluginUtils;
004import sailpoint.api.SailPointContext;
005import sailpoint.object.Attributes;
006import sailpoint.object.ServiceDefinition;
007import sailpoint.tools.GeneralException;
008import sailpoint.tools.Util;
009
010/**
011 * Utilities for services
012 */
013public class ServiceUtils {
014    /**
015     * Stores the last start and stop timestamps on the given ServiceDefinition. This is
016     * most useful with a single-server service, implemented using either {@link com.identityworksllc.iiq.common.plugin.SingleServerService}
017     * or {@link com.identityworksllc.iiq.common.plugin.CommonPluginUtils#singleServerExecute(SailPointContext, ServiceDefinition, CommonPluginUtils.SingleServerExecute)}.
018     *
019     * The time of this method's invocation will be used as the 'last stop' timestamp.
020     *
021     * @param context The context of this service
022     * @param target The target of this service
023     * @param lastStart The last start timestamp of this service
024     * @throws GeneralException when something goes wrong persisting the ServiceDefinition change
025     */
026    public static void storeTimestamps(SailPointContext context, ServiceDefinition target, long lastStart) throws GeneralException {
027        ServiceDefinition reloaded = context.getObjectById(ServiceDefinition.class, target.getId());
028        Attributes<String, Object> attributes = reloaded.getAttributes();
029        if (attributes == null) {
030            attributes = new Attributes<>();
031            reloaded.setAttributes(attributes);
032        }
033        attributes.put("lastStart", lastStart);
034        attributes.put("lastStop", System.currentTimeMillis());
035        attributes.put("lastHost", Util.getHostName());
036        context.saveObject(reloaded);
037        context.commitTransaction();
038    }
039
040    /**
041     * Private utility constructor
042     */
043    private ServiceUtils() {
044
045    }
046}