use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.
the class ServiceComponent method resolveBinding.
private void resolveBinding() {
Object target = service.getTarget();
if (target == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00000"));
}
if (service.hasBinding()) {
Set<Binding> bindings = service.getBindings();
boolean allPassed = true;
for (Binding binding : bindings) {
BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
if (bindingAdapter == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00001", binding.getBindingType(), service));
}
SofaLogger.info(" <<PreOut Binding [{}] Begins - {}.", binding.getBindingType(), service);
try {
bindingAdapter.preOutBinding(service, binding, target, getContext());
} catch (Throwable t) {
allPassed = false;
SofaLogger.error(ErrorCode.convert("01-00002", binding.getBindingType(), service), t);
continue;
}
SofaLogger.info(" <<PreOut Binding [{}] Ends - {}.", binding.getBindingType(), service);
}
if (!allPassed) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00003", service));
}
}
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.
the class ServiceComponent method activateBinding.
private void activateBinding() {
Object target = service.getTarget();
if (target == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00000"));
}
if (service.hasBinding()) {
boolean allPassed = true;
Set<Binding> bindings = service.getBindings();
for (Binding binding : bindings) {
BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
if (bindingAdapter == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00001", binding.getBindingType(), service));
}
Object outBindingResult;
SofaLogger.info(" <<Out Binding [{}] Begins - {}.", binding.getBindingType(), service);
try {
outBindingResult = bindingAdapter.outBinding(service, binding, target, getContext());
} catch (Throwable t) {
allPassed = false;
binding.setHealthy(false);
SofaLogger.error(ErrorCode.convert("01-00004", binding.getBindingType(), service), t);
continue;
}
if (!Boolean.FALSE.equals(outBindingResult)) {
SofaLogger.info(" <<Out Binding [{}] Ends - {}.", binding.getBindingType(), service);
} else {
binding.setHealthy(false);
SofaLogger.info(" <<Out Binding [{}] Fails, Don't publish service - {}.", binding.getBindingType(), service);
}
}
if (!allPassed) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00005", service));
}
}
SofaLogger.info("Register Service - {}", service);
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.
the class ReferenceRegisterHelper method generateBindingHashCode.
public static int generateBindingHashCode(Reference reference) {
Collection<Binding> bindings = reference.getBindings();
int result = 1;
for (Binding binding : bindings) {
result = result * 31 + binding.getBindingHashCode();
}
ClassLoader cl = reference.getInterfaceType().getClassLoader();
if (cl != null) {
result += reference.getInterfaceType().getClassLoader().hashCode();
}
return result;
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.
the class ReferenceRegisterHelper method registerReference.
public static Object registerReference(Reference reference, BindingAdapterFactory bindingAdapterFactory, SofaRuntimeContext sofaRuntimeContext, ApplicationContext applicationContext) {
Binding binding = (Binding) reference.getBindings().toArray()[0];
if (!binding.getBindingType().equals(JvmBinding.JVM_BINDING_TYPE) && !SofaRuntimeProperties.isDisableJvmFirst(sofaRuntimeContext) && reference.isJvmFirst()) {
// as rpc invocation would be serialized, so here would Not ignore serialized
reference.addBinding(new JvmBinding());
}
ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
ReferenceComponent referenceComponent = new ReferenceComponent(reference, new DefaultImplementation(), bindingAdapterFactory, sofaRuntimeContext);
if (componentManager.isRegistered(referenceComponent.getName())) {
return componentManager.getComponentInfo(referenceComponent.getName()).getImplementation().getTarget();
}
ComponentInfo componentInfo = componentManager.registerAndGet(referenceComponent);
componentInfo.setApplicationContext(applicationContext);
return componentInfo.getImplementation().getTarget();
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by alipay.
the class AbstractComponent method aggregateBindingHealth.
protected String aggregateBindingHealth(Collection<Binding> bindings) {
List<String> healthResult = new ArrayList<>();
for (Binding binding : bindings) {
HealthResult result = binding.healthCheck();
String report = "[" + result.getHealthName() + "," + (result.getHealthReport() == null ? (result.isHealthy() ? "passed" : "failed") : result.getHealthReport()) + "]";
healthResult.add(report);
}
return String.join(" ", healthResult);
}
Aggregations