/*
 * Copyright 2008-2017 by Emeric Vernat
 *
 *     This file is part of Java Melody.
 *
 * 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 net.bull.javamelody.internal.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Publish metrics to Graphite and AWS CloudWatch.
 * @author Emeric Vernat
 */
abstract class MetricsPublisher {
	static List<MetricsPublisher> getMetricsPublishers(
			List<JavaInformations> javaInformationsList) {
		assert javaInformationsList != null && !javaInformationsList.isEmpty();
		final List<MetricsPublisher> metricsPublishers = new ArrayList<MetricsPublisher>();
		final StringBuilder sb = new StringBuilder();
		for (final JavaInformations javaInformations : javaInformationsList) {
			if (sb.length() != 0) {
				sb.append('_');
			}
			sb.append(javaInformations.getHost().replaceFirst("@.*", ""));
		}
		final String contextPath = javaInformationsList.get(0).getContextPath();
		final String hosts = sb.toString();
		final Graphite graphite = Graphite.getInstance(contextPath, hosts);
		final CloudWatch cloudWatch = CloudWatch.getInstance(contextPath, hosts);
		final InfluxDB influxDb = InfluxDB.getInstance(contextPath, hosts);
		final Datadog datadog = Datadog.getInstance(contextPath, hosts);
		if (graphite != null) {
			metricsPublishers.add(graphite);
		}
		if (cloudWatch != null) {
			metricsPublishers.add(cloudWatch);
		}
		if (influxDb != null) {
			metricsPublishers.add(influxDb);
		}
		if (datadog != null) {
			metricsPublishers.add(datadog);
		}
		if (metricsPublishers.isEmpty()) {
			return Collections.emptyList();
		}
		return metricsPublishers;
	}

	abstract void addValue(String metric, double value) throws IOException;

	abstract void send() throws IOException;

	abstract void stop();
}

@@ -1459,30 +1459,24 @@
 ));%0D%0A%09%09%7D%0D%0A%09%09
-final 
 String conte
@@ -1527,24 +1527,141 @@
 extPath();%0D%0A
+%09%09if (contextPath == null) %7B%0D%0A%09%09%09// for NodesCollector in Jenkins, contextPath is null%0D%0A%09%09%09contextPath = %22NA%22;%0D%0A%09%09%7D%0D%0A
 %09%09final Stri

fix NPE in Jenkins when using CloudWatch or Graphite