Search in sources :

Example 1 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class ExtensionComponent method activate.

@Override
public void activate() throws ServiceRuntimeException {
    if (componentStatus != ComponentStatus.RESOLVED) {
        return;
    }
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ComponentName extensionPointComponentName = extension.getTargetComponentName();
    ComponentInfo extensionPointComponentInfo = componentManager.getComponentInfo(extensionPointComponentName);
    if (extensionPointComponentInfo == null || !extensionPointComponentInfo.isActivated()) {
        return;
    }
    loadContributions(((ExtensionPointComponent) extensionPointComponentInfo).getExtensionPoint(), extension);
    Object target = extensionPointComponentInfo.getImplementation().getTarget();
    try {
        if (target instanceof Extensible) {
            ((Extensible) target).registerExtension(extension);
        } else {
            Method method = ReflectionUtils.findMethod(target.getClass(), "registerExtension", Extension.class);
            if (method == null) {
                throw new RuntimeException(ErrorCode.convert("01-01001", target.getClass().getCanonicalName()));
            }
            ReflectionUtils.invokeMethod(method, target, extension);
        }
    } catch (Throwable t) {
        throw new ServiceRuntimeException(ErrorCode.convert("01-01000", extensionPointComponentInfo.getName()), t);
    }
    componentStatus = ComponentStatus.ACTIVATED;
}
Also used : Extensible(com.alipay.sofa.service.api.component.Extensible) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Method(java.lang.reflect.Method) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 2 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class ExtensionComponent method resolve.

@Override
public boolean resolve() {
    if (componentStatus != ComponentStatus.REGISTERED) {
        return false;
    }
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    ComponentName extensionPointComponentName = extension.getTargetComponentName();
    ComponentInfo extensionPointComponentInfo = componentManager.getComponentInfo(extensionPointComponentName);
    if (extensionPointComponentInfo != null && extensionPointComponentInfo.isActivated()) {
        componentStatus = ComponentStatus.RESOLVED;
        return true;
    }
    return false;
}
Also used : ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentName(com.alipay.sofa.runtime.api.component.ComponentName) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo)

Example 3 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class ExtensionPointComponent method activate.

@Override
public void activate() throws ServiceRuntimeException {
    super.activate();
    ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
    for (ComponentInfo componentInfo : componentManager.getComponents()) {
        if (componentInfo.getType().equals(ExtensionComponent.EXTENSION_COMPONENT_TYPE) && !componentInfo.isResolved()) {
            ExtensionComponent extensionComponent = (ExtensionComponent) componentInfo;
            if (extensionComponent.getExtension().getTargetComponentName().equals(componentName)) {
                componentManager.resolvePendingResolveComponent(componentInfo.getName());
            }
        }
    }
}
Also used : ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo)

Example 4 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

the class ComponentManagerShutdownTest method testSkipAllComponentShutdown.

@Test
public void testSkipAllComponentShutdown() {
    SofaRuntimeProperties.setSkipAllComponentShutdown(Thread.currentThread().getContextClassLoader(), true);
    ComponentManager componentManager = initComponentManager();
    ComponentInfo serviceComponentInfo = componentManager.getComponentInfosByType(SERVICE_COMPONENT_TYPE).stream().findFirst().get();
    ComponentInfo springComponentInfo = componentManager.getComponentInfosByType(SPRING_COMPONENT_TYPE).stream().findFirst().get();
    GenericApplicationContext applicationContext = (GenericApplicationContext) springComponentInfo.getImplementation().getTarget();
    Assert.assertEquals(2, componentManager.size());
    Assert.assertTrue(serviceComponentInfo.isActivated());
    Assert.assertTrue(springComponentInfo.isActivated());
    Assert.assertTrue(applicationContext.isActive());
    componentManager.shutdown();
    Assert.assertEquals(2, componentManager.size());
    Assert.assertTrue(serviceComponentInfo.isActivated());
    Assert.assertTrue(springComponentInfo.isActivated());
    Assert.assertTrue(applicationContext.isActive());
    SofaRuntimeProperties.setSkipAllComponentShutdown(Thread.currentThread().getContextClassLoader(), false);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) Test(org.junit.Test)

Example 5 with ComponentManager

use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by sofastack.

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());
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ComponentManager(com.alipay.sofa.runtime.spi.component.ComponentManager) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)31 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)26 Test (org.junit.Test)14 ComponentName (com.alipay.sofa.runtime.api.component.ComponentName)10 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)10 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)6 Binding (com.alipay.sofa.runtime.spi.binding.Binding)4 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)4 SofaRuntimeContext (com.alipay.sofa.runtime.spi.component.SofaRuntimeContext)4 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)3 ComponentLifeCycle (com.alipay.sofa.runtime.api.component.ComponentLifeCycle)2 ComponentType (com.alipay.sofa.runtime.model.ComponentType)2 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)2 SpringImplementationImpl (com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl)2 SpringContextComponent (com.alipay.sofa.runtime.spring.SpringContextComponent)2 SpringContextImplementation (com.alipay.sofa.runtime.spring.SpringContextImplementation)2 SampleService (com.alipay.sofa.runtime.test.beans.facade.SampleService)2 DefaultSampleService (com.alipay.sofa.runtime.test.beans.service.DefaultSampleService)2