/*
 * Copyright 2013-2015 the original author or authors.
 *
 * 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 org.springframework.cloud.consul.discovery;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;

/**
 * @author Spencer Gibb
 */
@Slf4j
public class ConsulLifecycle extends AbstractDiscoveryLifecycle {

	@Autowired
	private ConsulClient client;

	@Autowired
	private ConsulDiscoveryProperties properties;

	@Autowired(required = false)
	private TtlScheduler ttlScheduler;

	@Autowired
	private HeartbeatProperties ttlConfig;

	@Override
	protected void register() {
		NewService service = new NewService();
		String appName = getAppName();
		// TODO: move id to properties with context ID as default
		service.setId(getContext().getId());
		service.setName(appName);
		// TODO: support port = 0 random assignment
		Integer port = new Integer(getEnvironment().getProperty("server.port", "8080"));
		service.setPort(port);
		service.setTags(properties.getTags());
		NewService.Check check = new NewService.Check();
		if (ttlConfig.isEnabled()) {
			check.setTtl(ttlConfig.getTtl());
		}
		if (properties.getHealthCheckUrl() != null) {
			check.setHttp(properties.getHealthCheckUrl());
		} else {
			check.setHttp(String.format("http://%s:%s%s", properties.getHostname(),
					port, properties.getHealthCheckPath()));
		}
		check.setInterval(properties.getHealthCheckInterval());
		//TODO support http check timeout
		service.setCheck(check);
		register(service);
	}

	@Override
	protected void registerManagement() {
		NewService management = new NewService();
		management.setId(getManagementServiceId());
		management.setName(getManagementServiceName());
		management.setPort(getManagementPort());
		management.setTags(properties.getManagementTags());

		register(management);
	}

	protected void register(NewService service) {
		log.info("Registering service with consul: {}", service.toString());
		client.agentServiceRegister(service);
		if (ttlConfig.isEnabled() && ttlScheduler != null) {
			ttlScheduler.add(service);
		}
	}

	@Override
	protected Object getConfiguration() {
		return properties;
	}

	@Override
	protected void deregister() {
		deregister(getContext().getId());
	}

	@Override
	protected void deregisterManagement() {
		deregister(getManagementServiceName());
	}

	private void deregister(String serviceId) {
		ttlScheduler.remove(serviceId);
		client.agentServiceDeregister(serviceId);
	}

	@Override
	protected boolean isEnabled() {
		return properties.isEnabled();
	}
}

@@ -3073,16 +3073,47 @@
 ceId) %7B%0A
+%09%09if (ttlScheduler != null) %7B%0A%09
 %09%09ttlSch
@@ -3138,16 +3138,20 @@
 iceId);%0A
+%09%09%7D%0A
 %09%09client

fix NPE