org.rapidpm.proxybuilder.type.dymamic.virtual.VirtualDynamicProxyInvocationHandler Maven / Gradle / Ivy
/*
* Copyright [2014] [www.rapidpm.org / Sven Ruppert ([email protected])]
*
* 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.rapidpm.proxybuilder.type.dymamic.virtual;
import org.rapidpm.proxybuilder.type.dymamic.DynamicProxyBuilder;
import javax.annotation.Nonnull;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class VirtualDynamicProxyInvocationHandler implements InvocationHandler {
private ServiceFactory serviceFactory;
private ServiceStrategyFactory serviceStrategyFactory;
private VirtualDynamicProxyInvocationHandler() {
}
private VirtualDynamicProxyInvocationHandler(final Builder builder) {
serviceFactory = builder.serviceFactory;
serviceStrategyFactory = builder.serviceStrategyFactory;
}
public static Builder newBuilder() {
return new Builder<>();
}
public Object invoke(Object proxy, @Nonnull Method method, Object[] args) throws Throwable {
final C obj = serviceStrategyFactory.realSubject(serviceFactory);
return DynamicProxyBuilder.invoke(obj, method, args);
// invoke(obj, method, args);
}
/**
* Strategy of creating: ThreadSave, NotThreadSave, ...
*/
public interface ServiceStrategyFactory {
C realSubject(ServiceFactory factory);
}
public interface ServiceFactory {
C createInstance();
}
public static final class Builder {
private ServiceFactory serviceFactory;
private ServiceStrategyFactory serviceStrategyFactory;
private Builder() {
}
@Nonnull
public Builder withServiceFactory(@Nonnull final ServiceFactory serviceFactory) {
this.serviceFactory = serviceFactory;
return this;
}
@Nonnull
public Builder withServiceStrategyFactory(@Nonnull final ServiceStrategyFactory serviceStrategyFactory) {
this.serviceStrategyFactory = serviceStrategyFactory;
return this;
}
@Nonnull
public VirtualDynamicProxyInvocationHandler build() {
return new VirtualDynamicProxyInvocationHandler<>(this);
}
}
}