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);
}
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);
}
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);
}
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;
}
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();
}
Aggregations