Search in sources :

Example 6 with ServiceRuntimeException

use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.

the class AbstractContractDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    String id = element.getAttribute(BEAN_ID_ELEMENT);
    builder.addPropertyValue(BEAN_ID_PROPERTY, id);
    String interfaceType = element.getAttribute(INTERFACE_ELEMENT);
    builder.addPropertyValue(INTERFACE_PROPERTY, interfaceType);
    String uniqueId = element.getAttribute(UNIQUE_ID_ELEMENT);
    builder.addPropertyValue(UNIQUE_ID_PROPERTY, uniqueId);
    String repeatReferLimit = element.getAttribute(REPEAT_REFER_LIMIT_ELEMENT);
    builder.addPropertyValue(REPEAT_REFER_LIMIT_PROPERTY, repeatReferLimit);
    List<Element> childElements = DomUtils.getChildElements(element);
    List<TypedStringValue> elementAsTypedStringValueList = new ArrayList<TypedStringValue>();
    for (Element childElement : childElements) {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(childElement), new StreamResult(buffer));
            String str = buffer.toString();
            elementAsTypedStringValueList.add(new TypedStringValue(str, Element.class));
        } catch (Exception e) {
            throw new ServiceRuntimeException(e);
        }
    }
    builder.addPropertyValue("documentEncoding", element.getOwnerDocument().getXmlEncoding());
    builder.addPropertyValue(ELEMENTS, elementAsTypedStringValueList);
    doParseInternal(element, parserContext, builder);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) StringWriter(java.io.StringWriter) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue)

Example 7 with ServiceRuntimeException

use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.

the class ServiceAnnotationBeanPostProcessor method processSofaService.

private void processSofaService(Object bean, String beanName) {
    final Class<?> beanClass = AopProxyUtils.ultimateTargetClass(bean);
    SofaService sofaServiceAnnotation = beanClass.getAnnotation(SofaService.class);
    if (sofaServiceAnnotation == null) {
        return;
    }
    Class<?> interfaceType = sofaServiceAnnotation.interfaceType();
    if (interfaceType.equals(void.class)) {
        Class<?>[] interfaces = beanClass.getInterfaces();
        if (interfaces == null || interfaces.length == 0 || interfaces.length > 1) {
            throw new ServiceRuntimeException("Bean " + beanName + " does not has any interface or has more than one interface.");
        }
        interfaceType = interfaces[0];
    }
    Implementation implementation = new SpringImplementationImpl(beanName, applicationContext);
    implementation.setTarget(bean);
    Service service = new ServiceImpl(sofaServiceAnnotation.uniqueId(), interfaceType, InterfaceMode.annotation, bean);
    service.addBinding(new JvmBinding());
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) ServiceImpl(com.alipay.sofa.runtime.service.component.impl.ServiceImpl) Service(com.alipay.sofa.runtime.service.component.Service) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) SofaService(com.alipay.sofa.runtime.api.annotation.SofaService) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) SpringImplementationImpl(com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 8 with ServiceRuntimeException

use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.

the class ServiceFactoryBean method doAfterPropertiesSet.

@Override
protected void doAfterPropertiesSet() throws Exception {
    if (hasSofaServiceAnnotation()) {
        throw new ServiceRuntimeException("Bean " + beanId + " of type " + ref.getClass() + " has already annotated by @SofaService," + " can not be registered using xml. Please check it.");
    }
    Implementation implementation = new DefaultImplementation();
    implementation.setTarget(ref);
    service = buildService();
    if (bindings.size() == 0) {
        bindings.add(new JvmBinding());
    }
    for (Binding binding : bindings) {
        service.addBinding(binding);
    }
    ComponentInfo componentInfo = new ServiceComponent(implementation, service, sofaRuntimeContext);
    sofaRuntimeContext.getComponentManager().register(componentInfo);
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceComponent(com.alipay.sofa.runtime.service.component.ServiceComponent) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) ComponentInfo(com.alipay.sofa.runtime.spi.component.ComponentInfo) DefaultImplementation(com.alipay.sofa.runtime.spi.component.DefaultImplementation) Implementation(com.alipay.sofa.runtime.spi.component.Implementation) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding)

Example 9 with ServiceRuntimeException

use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.

the class ReferenceClientImpl method getReferenceFromReferenceParam.

private <T> Reference getReferenceFromReferenceParam(ReferenceParam<T> referenceParam) {
    BindingParam bindingParam = referenceParam.getBindingParam();
    Reference reference = new ReferenceImpl(referenceParam.getUniqueId(), referenceParam.getInterfaceType(), InterfaceMode.api, referenceParam.isLocalFirst(), referenceParam.isJvmService(), null);
    if (bindingParam == null) {
        // add JVM Binding Default
        reference.addBinding(new JvmBinding());
    } else {
        BindingConverter bindingConverter = BindingFactoryContainer.getBindingConverterFactory().getBindingConverter(bindingParam.getBindingType());
        if (bindingConverter == null) {
            throw new ServiceRuntimeException("Can not found binding converter for binding type " + 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;
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) Reference(com.alipay.sofa.runtime.service.component.Reference) BindingConverter(com.alipay.sofa.runtime.spi.service.BindingConverter) BindingParam(com.alipay.sofa.runtime.api.client.param.BindingParam) ReferenceImpl(com.alipay.sofa.runtime.service.component.impl.ReferenceImpl) BindingConverterContext(com.alipay.sofa.runtime.spi.service.BindingConverterContext) JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 10 with ServiceRuntimeException

use of com.alipay.sofa.runtime.api.ServiceRuntimeException in project sofa-boot by alipay.

the class ReferenceComponent method unregister.

@Override
public void unregister() throws ServiceRuntimeException {
    super.unregister();
    if (reference.hasBinding()) {
        for (Binding binding : reference.getBindings()) {
            BindingAdapter<Binding> bindingAdapter = this.bindingAdapterFactory.getBindingAdapter(binding.getBindingType());
            if (bindingAdapter == null) {
                throw new ServiceRuntimeException("Can't find BindingAdapter of type " + binding.getBindingType() + " for reference " + reference + ".");
            }
            SofaLogger.info(" >>Un-in Binding [{0}] Begins - {1}.", binding.getBindingType(), reference);
            try {
                bindingAdapter.unInBinding(reference, binding, sofaRuntimeContext);
            } finally {
                SofaLogger.info(" >>Un-in Binding [{0}] Ends - {1}.", binding.getBindingType(), reference);
            }
        }
    }
    removeService();
}
Also used : JvmBinding(com.alipay.sofa.runtime.service.binding.JvmBinding) Binding(com.alipay.sofa.runtime.spi.binding.Binding) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Aggregations

ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)11 Binding (com.alipay.sofa.runtime.spi.binding.Binding)9 JvmBinding (com.alipay.sofa.runtime.service.binding.JvmBinding)6 ComponentInfo (com.alipay.sofa.runtime.spi.component.ComponentInfo)4 ServiceComponent (com.alipay.sofa.runtime.service.component.ServiceComponent)3 DefaultImplementation (com.alipay.sofa.runtime.spi.component.DefaultImplementation)3 Implementation (com.alipay.sofa.runtime.spi.component.Implementation)3 BindingParam (com.alipay.sofa.runtime.api.client.param.BindingParam)2 Service (com.alipay.sofa.runtime.service.component.Service)2 ServiceImpl (com.alipay.sofa.runtime.service.component.impl.ServiceImpl)2 BindingConverter (com.alipay.sofa.runtime.spi.service.BindingConverter)2 BindingConverterContext (com.alipay.sofa.runtime.spi.service.BindingConverterContext)2 SofaService (com.alipay.sofa.runtime.api.annotation.SofaService)1 Property (com.alipay.sofa.runtime.api.component.Property)1 Reference (com.alipay.sofa.runtime.service.component.Reference)1 ReferenceComponent (com.alipay.sofa.runtime.service.component.ReferenceComponent)1 ReferenceImpl (com.alipay.sofa.runtime.service.component.impl.ReferenceImpl)1 ComponentManager (com.alipay.sofa.runtime.spi.component.ComponentManager)1 SpringImplementationImpl (com.alipay.sofa.runtime.spi.spring.SpringImplementationImpl)1 StringWriter (java.io.StringWriter)1