Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014 Avanza Bank AB
*
* 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 com.avanza.astrix.beans.service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import com.avanza.astrix.beans.core.AstrixBeanKey;
import com.avanza.astrix.beans.core.ReactiveTypeConverter;
import com.avanza.astrix.core.util.ReflectionUtil;
import com.avanza.astrix.provider.component.AstrixServiceComponentNames;
import com.avanza.astrix.versioning.core.AstrixObjectSerializer;
import com.avanza.astrix.versioning.core.ObjectSerializerDefinition;
import com.avanza.astrix.versioning.core.ObjectSerializerFactory;
import rx.Observable;
/**
*
* @author Elias Lindholm (elilin)
*
*/
public class DirectComponent implements ServiceComponent {
private final static AtomicLong idGen = new AtomicLong();
private final static Map> providerById = new ConcurrentHashMap<>();
private final ObjectSerializerFactory objectSerializerFactory;
private final List> nonReleasedInstances = new ArrayList<>();
private final ConcurrentMap, String> idByExportedBean = new ConcurrentHashMap<>();
private final ReactiveTypeConverter reactiveTypeConverter;
public DirectComponent(ObjectSerializerFactory objectSerializerFactory, ReactiveTypeConverter reactiveTypeConverter) {
this.objectSerializerFactory = objectSerializerFactory;
this.reactiveTypeConverter = reactiveTypeConverter;
}
public List extends BoundServiceBeanInstance>> getBoundServices() {
return this.nonReleasedInstances;
}
@Override
public BoundServiceBeanInstance bind(ServiceDefinition serviceDefinition, ServiceProperties serviceProperties) {
String providerId = serviceProperties.getProperty("providerId");
ServiceProvider> serviceProvider = providerById.get(providerId);
if (serviceProvider == null) {
throw new IllegalStateException("No service provider is registered in the DirectComponent with the given serviceUri. id=" + providerId + ", type=" + serviceDefinition.getServiceType());
}
Object targetProvider = serviceProvider.getProvider(objectSerializerFactory, serviceDefinition.getObjectSerializerDefinition());
T provider;
if (serviceDefinition.getBeanKey().getBeanType().isAssignableFrom(targetProvider.getClass())) {
provider = serviceDefinition.getServiceType().cast(targetProvider);
} else {
// Async return type
provider = createProxy(serviceDefinition.getBeanKey().getBeanType(), targetProvider);
}
DirectBoundServiceBeanInstance directServiceBeanInstance = new DirectBoundServiceBeanInstance(provider);
this.nonReleasedInstances.add(directServiceBeanInstance);
return directServiceBeanInstance;
}
private T createProxy(Class proxyApi, final Object targetProvider) {
return ReflectionUtil.newProxy(proxyApi, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Method targetMethod = targetProvider.getClass().getMethod(method.getName(), method.getParameterTypes());
Observable