use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.
the class ReferenceComponent method publishAsService.
private void publishAsService(Reference reference, Object target) {
if (!reference.jvmService()) {
return;
}
Implementation serviceImplementation = new DefaultImplementation();
serviceImplementation.setTarget(target);
Service service = new ServiceImpl("", reference.getInterfaceType(), InterfaceMode.api, target);
service.addBinding(new JvmBinding());
ComponentInfo serviceComponent = new ServiceComponent(implementation, service, sofaRuntimeContext);
sofaRuntimeContext.getComponentManager().register(serviceComponent);
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.
the class SofaBindingTest method testServiceBinding.
@Test
public void testServiceBinding() {
ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
ServiceComponent serializeFalseViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "serializeFalseViaAnnotation"));
ServiceComponent defaultSerializeTrueViaAnnotation = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "defaultSerializeTrueViaAnnotation"));
ServiceComponent defaultElement = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "default-element"));
ServiceComponent element = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, "element"));
ServiceComponent noneUniqueId = (ServiceComponent) componentManager.getComponentInfo(ComponentNameUtil.getServiceComponentName(SampleService.class, ""));
Assert.assertNotNull(serializeFalseViaAnnotation);
Assert.assertNotNull(defaultSerializeTrueViaAnnotation);
Assert.assertNotNull(defaultElement);
Assert.assertNotNull(element);
Assert.assertNotNull(noneUniqueId);
JvmBinding jvmBinding;
jvmBinding = (JvmBinding) serializeFalseViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
jvmBinding = (JvmBinding) defaultSerializeTrueViaAnnotation.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
jvmBinding = (JvmBinding) defaultElement.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
jvmBinding = (JvmBinding) element.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
Assert.assertFalse(jvmBinding.getJvmBindingParam().isSerialize());
jvmBinding = (JvmBinding) noneUniqueId.getService().getBinding(JvmBinding.JVM_BINDING_TYPE);
Assert.assertTrue(jvmBinding.getJvmBindingParam().isSerialize());
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.
the class SofaEventHandlerTest method testDynamicProxyFinder.
@Test
public void testDynamicProxyFinder() throws Exception {
SofaFramework.registerSofaRuntimeManager(sofaRuntimeManager);
new MockUp<DynamicJvmServiceProxyFinder>() {
@Mock
public Biz getBiz(SofaRuntimeManager sofaRuntimeManager) {
return biz;
}
};
new Expectations() {
{
biz.getIdentity();
result = "MockName:MockVersion";
biz.getBizState();
result = BizState.ACTIVATED;
sofaRuntimeManager.getAppClassLoader();
result = ctx.getClassLoader().getParent();
}
};
new Expectations() {
{
sofaRuntimeManager.getComponentManager();
result = ((SofaRuntimeContext) ctx.getBean("sofaRuntimeContext")).getComponentManager();
contract.getInterfaceType();
result = SampleService.class;
contract.getUniqueId();
result = "";
contract.getBinding(JvmBinding.JVM_BINDING_TYPE);
result = new JvmBinding();
invocation.getArguments();
result = new Object[] {};
invocation.getMethod();
result = SampleService.class.getMethod("service");
}
};
DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().setHasFinishStartup(true);
ServiceProxy serviceProxy = DynamicJvmServiceProxyFinder.getDynamicJvmServiceProxyFinder().findServiceProxy(ctx.getClassLoader(), contract);
try {
Assert.assertTrue(SofaEventHandlerTest.class.getName().equals(serviceProxy.invoke(invocation)));
} catch (Throwable throwable) {
throw new RuntimeException("testDynamicProxyFinder case failed.", throwable);
}
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.
the class ServiceFactoryBean method doAfterPropertiesSet.
@Override
protected void doAfterPropertiesSet() {
if (!apiType && hasSofaServiceAnnotation()) {
throw new ServiceRuntimeException(ErrorCode.convert("01-00103", beanId, ref.getClass()));
}
Implementation implementation = new DefaultImplementation();
implementation.setTarget(ref);
service = buildService();
// default add jvm binding and service jvm binding should set serialize as true
if (bindings.size() == 0) {
JvmBindingParam jvmBindingParam = new JvmBindingParam().setSerialize(true);
bindings.add(new JvmBinding().setJvmBindingParam(jvmBindingParam));
}
for (Binding binding : bindings) {
service.addBinding(binding);
}
ComponentInfo componentInfo = new ServiceComponent(implementation, service, bindingAdapterFactory, sofaRuntimeContext);
componentInfo.setApplicationContext(applicationContext);
sofaRuntimeContext.getComponentManager().register(componentInfo);
}
use of com.alipay.sofa.runtime.service.binding.JvmBinding in project sofa-boot by alipay.
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;
}
Aggregations