/*
 * 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.netflix.eureka.server;

import java.util.List;

import lombok.extern.apachecommons.CommonsLog;

import org.springframework.beans.BeansException;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.discovery.shared.Application;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.lease.Lease;
import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
import com.netflix.eureka.resources.ServerCodecs;

/**
 * @author Spencer Gibb
 */
@CommonsLog
public class InstanceRegistry extends PeerAwareInstanceRegistryImpl implements ApplicationContextAware {

	private ApplicationContext ctxt;
	private int defaultOpenForTrafficCount;

	public InstanceRegistry(EurekaServerConfig serverConfig,
							EurekaClientConfig clientConfig, ServerCodecs serverCodecs,
							EurekaClient eurekaClient, int expectedNumberOfRenewsPerMin, int defaultOpenForTrafficCount) {
		super(serverConfig, clientConfig, serverCodecs, eurekaClient);

		this.expectedNumberOfRenewsPerMin = expectedNumberOfRenewsPerMin;
		this.defaultOpenForTrafficCount = defaultOpenForTrafficCount;
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.ctxt = context;
	}

	/**
	 * If {@link PeerAwareInstanceRegistryImpl#openForTraffic(ApplicationInfoManager, int)}
	 * is called with a zero * argument, it means that leases are not
	 * automatically * cancelled if the instance * hasn't sent any renewals
	 * recently. This happens for a standalone server. It seems like a bad
	 * default, so we set it to the smallest non-zero value we can, so that any
	 * instances that subsequently register can bump up the threshold.
	 */
	@Override
	public void openForTraffic(ApplicationInfoManager applicationInfoManager, int count) {
		super.openForTraffic(applicationInfoManager, count == 0 ? defaultOpenForTrafficCount : count);
	}


	@Override
	public void register(InstanceInfo info, boolean isReplication) {
		register(info, Lease.DEFAULT_DURATION_IN_SECS, isReplication);
	}

	@Override
	public void register(InstanceInfo info, int leaseDuration, boolean isReplication) {
		log.debug("register " + info.getAppName() + ", vip " + info.getVIPAddress()
				+ ", leaseDuration " + leaseDuration + ", isReplication " + isReplication);
		// TODO: what to publish from info (whole object?)
		this.ctxt.publishEvent(new EurekaInstanceRegisteredEvent(this, info,
				leaseDuration, isReplication));

		super.register(info, leaseDuration, isReplication);
	}

	@Override
	public boolean cancel(String appName, String serverId, boolean isReplication) {
		log.debug("cancel " + appName + " serverId " + serverId + ", isReplication {}"
				+ isReplication);
		this.ctxt.publishEvent(new EurekaInstanceCanceledEvent(this, appName, serverId,
				isReplication));

		return super.cancel(appName, serverId, isReplication);
	}

	@Override
	public boolean renew(final String appName, final String serverId,
						 boolean isReplication) {
		log.debug("renew " + appName + " serverId " + serverId + ", isReplication {}"
				+ isReplication);
		List<Application> applications = getSortedApplications();
		for (Application input : applications) {
			if (input.getName().equals(appName)) {
				InstanceInfo instance = null;
				for (InstanceInfo info : input.getInstances()) {
					if (info.getHostName().equals(serverId)) {
						instance = info;
						break;
					}
				}
				this.ctxt.publishEvent(new EurekaInstanceRenewedEvent(this, appName,
						serverId, instance, isReplication));
				break;
			}
		}
		return super.renew(appName, serverId, isReplication);
	}
}

@@ -3287,32 +3287,63 @@
 sReplication) %7B%0A
+%09%09if (log.isDebugEnabled()) %7B%0A%09
 %09%09log.debug(%22reg
@@ -3404,16 +3404,17 @@
 dress()%0A
+%09
 %09%09%09%09+ %22,
@@ -3485,16 +3485,20 @@
 ation);%0A
+%09%09%7D%0A
 %09%09// TOD
@@ -3792,32 +3792,63 @@
 sReplication) %7B%0A
+%09%09if (log.isDebugEnabled()) %7B%0A%09
 %09%09log.debug(%22can
@@ -3908,32 +3908,33 @@
 ication %7B%7D%22%0A%09%09%09%09
+%09
 + isReplication)
@@ -3935,16 +3935,20 @@
 ation);%0A
+%09%09%7D%0A
 %09%09this.c
@@ -4214,16 +4214,47 @@
 tion) %7B%0A
+%09%09if (log.isDebugEnabled()) %7B%0A%09
 %09%09log.de
@@ -4325,24 +4325,25 @@
 ion %7B%7D%22%0A%09%09%09%09
+%09
 + isReplicat
@@ -4348,16 +4348,20 @@
 ation);%0A
+%09%09%7D%0A
 %09%09List%3CA

Update InstanceRegistry.java