use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.
the class ReferenceClientImpl method getReferenceFromReferenceParam.
@SuppressWarnings("unchecked")
private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
BindingParam bindingParam = referenceParam.getBindingParam();
Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isJvmFirst(), null);
if (bindingParam == null) {
// default add jvm binding and reference jvm binding should set serialize as false
JvmBindingParam jvmBindingParam = new JvmBindingParam();
jvmBindingParam.setSerialize(false);
reference.addBinding(new JvmBinding().setJvmBindingParam(jvmBindingParam));
} else {
BindingConverter bindingConverter = bindingConverterFactory.getBindingConverter(bindingParam.getBindingType());
if (bindingConverter == null) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00200", bindingParam.getBindingType()));
}
BindingConverterContext bindingConverterContext = new BindingConverterContext();
bindingConverterContext.setInBinding(true);
bindingConverterContext.setAppName(sofaRuntimeContext.getAppName());
bindingConverterContext.setAppClassLoader(sofaRuntimeContext.getAppClassLoader());
Binding binding = bindingConverter.convert(bindingParam, bindingConverterContext);
reference.addBinding(binding);
}
return reference;
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.
the class ReferenceComponent method activate.
@Override
public void activate() throws ServiceRuntimeException {
if (reference.hasBinding()) {
Binding candidate = null;
Set<Binding> bindings = reference.getBindings();
if (bindings.size() == 1) {
candidate = bindings.iterator().next();
} else if (bindings.size() > 1) {
Object backupProxy = null;
for (Binding binding : bindings) {
if (JvmBinding.JVM_BINDING_TYPE.getType().equals(binding.getName())) {
candidate = binding;
} else {
// Under normal RPC reference (local-first/jvm-first is not set to false) binding,
// backup proxy is the RPC proxy, which will be invoked if Jvm service is not found
backupProxy = createProxy(reference, binding);
}
}
if (candidate != null) {
((JvmBinding) candidate).setBackupProxy(backupProxy);
}
}
Object proxy = null;
if (candidate != null) {
proxy = createProxy(reference, candidate);
}
this.implementation = new DefaultImplementation();
implementation.setTarget(proxy);
}
super.activate();
latch.countDown();
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.
the class ServiceComponent method deactivate.
@Override
public void deactivate() throws ServiceRuntimeException {
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));
}
SofaLogger.info(" <<Pre un-out Binding [{}] Begins - {}.", binding.getBindingType(), service);
try {
bindingAdapter.preUnoutBinding(service, binding, target, getContext());
} catch (Throwable t) {
allPassed = false;
SofaLogger.error(ErrorCode.convert("01-00006", binding.getBindingType(), service), t);
continue;
}
SofaLogger.info(" <<Pre un-out Binding [{}] Ends - {}.", binding.getBindingType(), service);
}
if (!allPassed) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00007", service));
}
}
super.deactivate();
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.
the class ServiceComponent method unregister.
@Override
public void unregister() throws ServiceRuntimeException {
super.unregister();
Property unregisterDelayMillisecondsProperty = properties.get(UNREGISTER_DELAY_MILLISECONDS);
if (unregisterDelayMillisecondsProperty != null) {
int unregisterDelayMilliseconds = unregisterDelayMillisecondsProperty.getInteger();
try {
TimeUnit.MILLISECONDS.sleep(unregisterDelayMilliseconds);
} catch (InterruptedException e) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00010", service), e);
}
}
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));
}
SofaLogger.info(" <<Post un-out Binding [{}] Begins - {}.", binding.getBindingType(), service);
try {
bindingAdapter.postUnoutBinding(service, binding, target, getContext());
} catch (Throwable t) {
allPassed = false;
SofaLogger.error(ErrorCode.convert("01-00008", binding.getBindingType(), service), t);
continue;
}
SofaLogger.info(" <<Post un-out Binding [{}] Ends - {}.", binding.getBindingType(), service);
}
if (!allPassed) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00009", service));
}
}
}
use of com.alipay.sofa.runtime.spi.binding.Binding in project sofa-boot by sofastack.
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));
}
}
}
Aggregations