/* * Copyright 2000-2010 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.scriptedProperties; import com.intellij.openapi.diagnostic.Logger; import java.util.Collection; import jetbrains.buildServer.ExtensionHolder; import jetbrains.buildServer.agent.BuildAgentConfiguration; import jetbrains.buildServer.agent.impl.AgentBuildSettingsEx; import jetbrains.buildServer.agent.parameters.ParameterResolverAgentProvider; import jetbrains.buildServer.parameters.*; import jetbrains.buildServer.util.positioning.PositionAware; import jetbrains.buildServer.util.positioning.PositionConstraint; import org.jetbrains.annotations.NotNull; /** * @author Yegor.Yarko * Date: 18.09.2010 */ /** * TODO: !!!!!!!!!!!!!!! * - need to add ChainResolver into ResolverFromExtensionsFactory ... with anchoring and usage_hint (composite, chain) * - need to use simple true/false property inside property resolver... */ public class ScriptedPropertiesAgentParameterResolverProvider implements ParameterResolverAgentProvider, PositionAware { public static final String REPORT_ERRORS_PROPERTY_NAME = "teamcity.scriptedPropertyResolver.reportErrors"; final Logger LOG = Logger.getInstance(ScriptedPropertiesAgentParameterResolverProvider.class.getName()); public static final String ORDER_ID = "scripted-properties-resolver"; private ExtensionHolder myExtensionHolder; private BuildAgentConfiguration myAgentConfiguration; public ScriptedPropertiesAgentParameterResolverProvider(ExtensionHolder extensionHolder, BuildAgentConfiguration agentConfiguration) { myExtensionHolder = extensionHolder; myAgentConfiguration = agentConfiguration; } public ParameterResolver getParameterResolver(final ContextVariables contextVariables) { final Collection processors = myExtensionHolder.getExtensions(ScriptedPropertyProcessor.class); final ScriptedPropertyRegistry registry = new ScriptedPropertyRegistry(processors); boolean reportErrors = false; try { if (myAgentConfiguration != null) { reportErrors = Boolean.valueOf(myAgentConfiguration.getConfigurationParameters().get(REPORT_ERRORS_PROPERTY_NAME)); } } catch (Exception e) { //ignore, use default LOG.debug("Cannot get report errors property from agent config", e); } try { final AgentBuildSettingsEx agentBuildSettings = (AgentBuildSettingsEx)contextVariables.get(ContextVariablesPredefinedNames.AGENT_BUILD_SETTINGS); if (agentBuildSettings != null) { final String reportErrorsValue = agentBuildSettings.getUnresolvedConfigParameters().get(REPORT_ERRORS_PROPERTY_NAME); reportErrors = Boolean.valueOf(reportErrorsValue); LOG.debug("Property '" + REPORT_ERRORS_PROPERTY_NAME + "' found. Setting report errors to " + reportErrors); } } catch (Exception e) { //ignore, use default LOG.debug("Cannot get report errors property", e); } final boolean finalReportErrors = reportErrors; return new ParameterResolver() { @NotNull public ProcessingResult resolve(@NotNull String key, @NotNull String value, @NotNull ParametersProvider parameters) { return registry.resolve(key, value, parameters, contextVariables, finalReportErrors); } }; } @NotNull public String getOrderId() { return ORDER_ID; } @NotNull public PositionConstraint getConstraint() { return PositionConstraint.after("reference-resolver"); } }