From f9fb23d472a4f9f5694358020e57472b30efd05a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 7 Aug 2014 11:32:19 -0700 Subject: [PATCH] Move piggy back stuff from cloudfoundry --- .../eureka/advice/LeaseManagerLite.java | 28 ++++++++ .../advice/PiggybackMethodInterceptor.java | 64 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java create mode 100644 spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/PiggybackMethodInterceptor.java diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java new file mode 100644 index 00000000..603319b3 --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/LeaseManagerLite.java @@ -0,0 +1,28 @@ +/* + * Copyright 2013-2014 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.platform.netflix.eureka.advice; + +import com.netflix.appinfo.InstanceInfo; + +/** + * @author Dave Syer + * + */ +public interface LeaseManagerLite { + + void register(final InstanceInfo info, final boolean isReplication); + +} diff --git a/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/PiggybackMethodInterceptor.java b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/PiggybackMethodInterceptor.java new file mode 100644 index 00000000..239c6168 --- /dev/null +++ b/spring-platform-netflix-core/src/main/java/org/springframework/platform/netflix/eureka/advice/PiggybackMethodInterceptor.java @@ -0,0 +1,64 @@ +/* + * Copyright 2013-2014 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.platform.netflix.eureka.advice; + +import java.lang.reflect.Method; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; +import org.springframework.util.ReflectionUtils; + +/** + * @author Dave Syer + * + */ +public class PiggybackMethodInterceptor implements MethodInterceptor { + + private Object delegate; + private Class[] types; + + public PiggybackMethodInterceptor(Object delegate, Class... types) { + this.delegate = delegate; + this.types = types; + } + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Object result = invocation.proceed(); + invokeAfter(invocation.getMethod(), invocation.getArguments()); + return result; + } + + private void invokeAfter(Method method, Object[] arguments) throws Exception { + for (Class type : types) { + Method target = getTarget(type, method); + if (target != null) { + target.invoke(delegate, arguments); + return; + } + } + } + + private Method getTarget(Class type, Method method) { + Method target = ReflectionUtils.findMethod(type, method.getName(), + method.getParameterTypes()); + if (target != null) { + return target; + } + return null; + } + +}