use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.
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;
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.
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());
}
}
}
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.
the class ExtensionPointComponent method deactivate.
@Override
public void deactivate() throws ServiceRuntimeException {
ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
for (ComponentInfo componentInfo : componentManager.getComponents()) {
if (componentInfo.getType().equals(ExtensionComponent.EXTENSION_COMPONENT_TYPE)) {
ExtensionComponent extensionComponent = (ExtensionComponent) componentInfo;
if (extensionComponent.getExtension().getTargetComponentName().equals(componentName)) {
componentManager.unregister(componentInfo);
}
}
}
if (componentStatus != ComponentStatus.ACTIVATED) {
return;
}
// skip deactivate SpringImplementationImpl because it's already deactivated
if (this.implementation != null && !(implementation instanceof SpringImplementationImpl)) {
Object target = this.implementation.getTarget();
if (target instanceof ComponentLifeCycle) {
((ComponentLifeCycle) target).deactivate();
}
}
componentStatus = ComponentStatus.RESOLVED;
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.
the class SofaBootComponentsEndPoint method components.
@ReadOperation
public ApplicationComponents components() {
ComponentManager componentManager = sofaRuntimeContext.getComponentManager();
Map<String, Collection<ComponentDisplayInfo>> componentsInfoMap = new HashMap<>();
Collection<ComponentType> componentTypes = componentManager.getComponentTypes();
componentTypes.forEach(componentType -> {
Collection<ComponentInfo> componentInfos = componentManager.getComponentInfosByType(componentType);
Collection<ComponentDisplayInfo> componentDisplayInfos = componentInfos.stream().map(componentInfo -> new ComponentDisplayInfo(componentInfo.getName().getName(), componentInfo.getApplicationContext().getId())).collect(Collectors.toList());
componentsInfoMap.put(componentType.getName(), componentDisplayInfos);
});
return new ApplicationComponents(componentsInfoMap);
}
use of com.alipay.sofa.runtime.spi.component.ComponentManager in project sofa-boot by alipay.
the class ComponentManagerShutdownTest method testNormalShutdown.
@Test
public void testNormalShutdown() {
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(0, componentManager.size());
Assert.assertFalse(serviceComponentInfo.isActivated());
Assert.assertFalse(springComponentInfo.isActivated());
Assert.assertFalse(applicationContext.isActive());
}
Aggregations