package jetbrains.buildServer.buildTriggers.url; import jetbrains.buildServer.buildTriggers.*; import jetbrains.buildServer.log.Loggers; import jetbrains.buildServer.serverSide.InvalidProperty; import jetbrains.buildServer.serverSide.PropertiesProcessor; import jetbrains.buildServer.util.StringUtil; import jetbrains.buildServer.web.openapi.PluginDescriptor; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; import static jetbrains.buildServer.buildTriggers.url.UrlBuildTriggerUtil.POLL_INTERVAL_PARAM; import static jetbrains.buildServer.buildTriggers.url.UrlBuildTriggerUtil.URL_PARAM; /** * User: vbedrosova * Date: 06.12.10 * Time: 13:19 */ public class UrlBuildTrigger extends BuildTriggerService { @NotNull private static final Logger LOG = Logger.getLogger(Loggers.VCS_CATEGORY + UrlBuildTrigger.class); private static final Integer DEFAULT_POLL_INTERVAL = 30; @NotNull private final PluginDescriptor myPluginDescriptor; @NotNull private final UrlChangesDetector myChangesDetector; public UrlBuildTrigger(@NotNull final PluginDescriptor pluginDescriptor, @NotNull final UrlChangesDetector changesDetector) { myPluginDescriptor = pluginDescriptor; myChangesDetector = changesDetector; } @NotNull @Override public String getName() { return "urlBuildTrigger"; } @NotNull @Override public String getDisplayName() { return "URL build trigger"; } @NotNull @Override public String describeTrigger(@NotNull BuildTriggerDescriptor buildTriggerDescriptor) { return "Wait for a change at " + buildTriggerDescriptor.getProperties().get(URL_PARAM) + " URL"; } @NotNull @Override public BuildTriggeringPolicy getBuildTriggeringPolicy() { return new PolledBuildTrigger() { @Override public void triggerBuild(@NotNull PolledTriggerContext context) throws BuildTriggerException { final Map props = context.getTriggerDescriptor().getProperties(); final String url = props.get(URL_PARAM); if (StringUtil.isEmptyOrSpaces(url)) { LOG.warn(getDisplayName() + " settings are invalid in build configuration " + context.getBuildType()); return; } try { if (myChangesDetector.isChanged(url, context.getCustomDataStorage(), context.getBuildType().getConfigParameters())) { context.getBuildType().addToQueue(getDisplayName()); } } catch (Exception e) { throw new BuildTriggerException(getDisplayName() + " for " + url + " failed with error: " + e.getMessage(), e); } } @Override public int getPollInterval(@NotNull PolledTriggerContext context) { final Map props = context.getTriggerDescriptor().getProperties(); final String poll_interval = props.get(POLL_INTERVAL_PARAM); if (poll_interval == null) return DEFAULT_POLL_INTERVAL; try { return Integer.parseInt(poll_interval); } catch (NumberFormatException e) { return DEFAULT_POLL_INTERVAL; } } }; } @Override public PropertiesProcessor getTriggerPropertiesProcessor() { return new PropertiesProcessor() { public Collection process(Map properties) { final ArrayList invalidProps = new ArrayList(); if (StringUtil.isEmptyOrSpaces(properties.get(URL_PARAM))) { invalidProps.add(new InvalidProperty(URL_PARAM, "URL must be specified")); } //TODO: perform other checks return invalidProps; } }; } @Override public String getEditParametersUrl() { return myPluginDescriptor.getPluginResourcesPath("editUrlBuildTrigger.jsp"); } @Override public boolean isMultipleTriggersPerBuildTypeAllowed() { return true; } @Override public Map getDefaultTriggerProperties() { final Map defaultProps = new HashMap(1); defaultProps.put(POLL_INTERVAL_PARAM, DEFAULT_POLL_INTERVAL.toString()); return defaultProps; } }