use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.
the class ScheduledTasksBeanDefinitionParser method doParse.
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
// lazy scheduled tasks are a contradiction in terms -> force to false
builder.setLazyInit(false);
ManagedList<RuntimeBeanReference> cronTaskList = new ManagedList<>();
ManagedList<RuntimeBeanReference> fixedDelayTaskList = new ManagedList<>();
ManagedList<RuntimeBeanReference> fixedRateTaskList = new ManagedList<>();
ManagedList<RuntimeBeanReference> triggerTaskList = new ManagedList<>();
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (!isScheduledElement(child, parserContext)) {
continue;
}
Element taskElement = (Element) child;
String ref = taskElement.getAttribute("ref");
String method = taskElement.getAttribute("method");
// Check that 'ref' and 'method' are specified
if (!StringUtils.hasText(ref) || !StringUtils.hasText(method)) {
parserContext.getReaderContext().error("Both 'ref' and 'method' are required", taskElement);
// Continue with the possible next task element
continue;
}
String cronAttribute = taskElement.getAttribute("cron");
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
String triggerAttribute = taskElement.getAttribute("trigger");
String initialDelayAttribute = taskElement.getAttribute("initial-delay");
boolean hasCronAttribute = StringUtils.hasText(cronAttribute);
boolean hasFixedDelayAttribute = StringUtils.hasText(fixedDelayAttribute);
boolean hasFixedRateAttribute = StringUtils.hasText(fixedRateAttribute);
boolean hasTriggerAttribute = StringUtils.hasText(triggerAttribute);
boolean hasInitialDelayAttribute = StringUtils.hasText(initialDelayAttribute);
if (!(hasCronAttribute || hasFixedDelayAttribute || hasFixedRateAttribute || hasTriggerAttribute)) {
parserContext.getReaderContext().error("one of the 'cron', 'fixed-delay', 'fixed-rate', or 'trigger' attributes is required", taskElement);
// with the possible next task element
continue;
}
if (hasInitialDelayAttribute && (hasCronAttribute || hasTriggerAttribute)) {
parserContext.getReaderContext().error("the 'initial-delay' attribute may not be used with cron and trigger tasks", taskElement);
// with the possible next task element
continue;
}
String runnableName = runnableReference(ref, method, taskElement, parserContext).getBeanName();
if (hasFixedDelayAttribute) {
fixedDelayTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedDelayAttribute, taskElement, parserContext));
}
if (hasFixedRateAttribute) {
fixedRateTaskList.add(intervalTaskReference(runnableName, initialDelayAttribute, fixedRateAttribute, taskElement, parserContext));
}
if (hasCronAttribute) {
cronTaskList.add(cronTaskReference(runnableName, cronAttribute, taskElement, parserContext));
}
if (hasTriggerAttribute) {
String triggerName = new RuntimeBeanReference(triggerAttribute).getBeanName();
triggerTaskList.add(triggerTaskReference(runnableName, triggerName, taskElement, parserContext));
}
}
String schedulerRef = element.getAttribute("scheduler");
if (StringUtils.hasText(schedulerRef)) {
builder.addPropertyReference("taskScheduler", schedulerRef);
}
builder.addPropertyValue("cronTasksList", cronTaskList);
builder.addPropertyValue("fixedDelayTasksList", fixedDelayTaskList);
builder.addPropertyValue("fixedRateTasksList", fixedRateTaskList);
builder.addPropertyValue("triggerTasksList", triggerTaskList);
}
use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.
the class ViewResolversBeanDefinitionParser method parse.
public BeanDefinition parse(Element element, ParserContext context) {
Object source = context.extractSource(element);
context.pushContainingComponent(new CompositeComponentDefinition(element.getTagName(), source));
ManagedList<Object> resolvers = new ManagedList<>(4);
resolvers.setSource(context.extractSource(element));
String[] names = new String[] { "jsp", "tiles", "bean-name", "freemarker", "groovy", "script-template", "bean", "ref" };
for (Element resolverElement : DomUtils.getChildElementsByTagName(element, names)) {
String name = resolverElement.getLocalName();
if ("bean".equals(name) || "ref".equals(name)) {
resolvers.add(context.getDelegate().parsePropertySubElement(resolverElement, null));
continue;
}
RootBeanDefinition resolverBeanDef;
if ("jsp".equals(name)) {
resolverBeanDef = new RootBeanDefinition(InternalResourceViewResolver.class);
resolverBeanDef.getPropertyValues().add("prefix", "/WEB-INF/");
resolverBeanDef.getPropertyValues().add("suffix", ".jsp");
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
} else if ("tiles".equals(name)) {
resolverBeanDef = new RootBeanDefinition(TilesViewResolver.class);
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
} else if ("freemarker".equals(name)) {
resolverBeanDef = new RootBeanDefinition(FreeMarkerViewResolver.class);
resolverBeanDef.getPropertyValues().add("suffix", ".ftl");
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
} else if ("groovy".equals(name)) {
resolverBeanDef = new RootBeanDefinition(GroovyMarkupViewResolver.class);
resolverBeanDef.getPropertyValues().add("suffix", ".tpl");
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
} else if ("script-template".equals(name)) {
resolverBeanDef = new RootBeanDefinition(ScriptTemplateViewResolver.class);
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
} else if ("bean-name".equals(name)) {
resolverBeanDef = new RootBeanDefinition(BeanNameViewResolver.class);
} else {
// Should never happen
throw new IllegalStateException("Unexpected element name: " + name);
}
resolverBeanDef.setSource(source);
resolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
resolvers.add(resolverBeanDef);
}
String beanName = VIEW_RESOLVER_BEAN_NAME;
RootBeanDefinition compositeResolverBeanDef = new RootBeanDefinition(ViewResolverComposite.class);
compositeResolverBeanDef.setSource(source);
compositeResolverBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
names = new String[] { "content-negotiation" };
List<Element> contentnNegotiationElements = DomUtils.getChildElementsByTagName(element, names);
if (contentnNegotiationElements.isEmpty()) {
compositeResolverBeanDef.getPropertyValues().add("viewResolvers", resolvers);
} else if (contentnNegotiationElements.size() == 1) {
BeanDefinition beanDef = createContentNegotiatingViewResolver(contentnNegotiationElements.get(0), context);
beanDef.getPropertyValues().add("viewResolvers", resolvers);
ManagedList<Object> list = new ManagedList<>(1);
list.add(beanDef);
compositeResolverBeanDef.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
compositeResolverBeanDef.getPropertyValues().add("viewResolvers", list);
} else if (contentnNegotiationElements.size() > 1) {
throw new IllegalArgumentException("Only one <content-negotiation> element is allowed.");
}
if (element.hasAttribute("order")) {
compositeResolverBeanDef.getPropertyValues().add("order", element.getAttribute("order"));
}
context.getReaderContext().getRegistry().registerBeanDefinition(beanName, compositeResolverBeanDef);
context.registerComponent(new BeanComponentDefinition(compositeResolverBeanDef, beanName));
context.popAndRegisterContainingComponent();
return null;
}
use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.
the class InterceptorsBeanDefinitionParser method getIncludePatterns.
private ManagedList<String> getIncludePatterns(Element interceptor, String elementName) {
List<Element> paths = DomUtils.getChildElementsByTagName(interceptor, elementName);
ManagedList<String> patterns = new ManagedList<>(paths.size());
for (Element path : paths) {
patterns.add(path.getAttribute("path"));
}
return patterns;
}
use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.
the class ComplexWebApplicationContext method refresh.
@Override
public void refresh() throws BeansException {
registerSingleton(DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME, SessionLocaleResolver.class);
registerSingleton(DispatcherServlet.THEME_RESOLVER_BEAN_NAME, SessionThemeResolver.class);
LocaleChangeInterceptor interceptor1 = new LocaleChangeInterceptor();
LocaleChangeInterceptor interceptor2 = new LocaleChangeInterceptor();
interceptor2.setParamName("locale2");
ThemeChangeInterceptor interceptor3 = new ThemeChangeInterceptor();
ThemeChangeInterceptor interceptor4 = new ThemeChangeInterceptor();
interceptor4.setParamName("theme2");
UserRoleAuthorizationInterceptor interceptor5 = new UserRoleAuthorizationInterceptor();
interceptor5.setAuthorizedRoles("role1", "role2");
List<Object> interceptors = new ArrayList<>();
interceptors.add(interceptor5);
interceptors.add(interceptor1);
interceptors.add(interceptor2);
interceptors.add(interceptor3);
interceptors.add(interceptor4);
interceptors.add(new MyHandlerInterceptor1());
interceptors.add(new MyHandlerInterceptor2());
interceptors.add(new MyWebRequestInterceptor());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("mappings", "/view.do=viewHandler\n/locale.do=localeHandler\nloc.do=anotherLocaleHandler");
pvs.add("interceptors", interceptors);
registerSingleton("myUrlMapping1", SimpleUrlHandlerMapping.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("mappings", "/form.do=localeHandler\n/unknown.do=unknownHandler\nservlet.do=myServlet");
pvs.add("order", "2");
registerSingleton("myUrlMapping2", SimpleUrlHandlerMapping.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("mappings", "/head.do=headController\n" + "body.do=bodyController\n/noview*=noviewController\n/noview/simple*=noviewController");
pvs.add("order", "1");
registerSingleton("handlerMapping", SimpleUrlHandlerMapping.class, pvs);
registerSingleton("myDummyAdapter", MyDummyAdapter.class);
registerSingleton("myHandlerAdapter", MyHandlerAdapter.class);
registerSingleton("standardHandlerAdapter", SimpleControllerHandlerAdapter.class);
registerSingleton("noviewController", NoViewController.class);
pvs = new MutablePropertyValues();
pvs.add("order", 0);
pvs.add("basename", "org.springframework.web.servlet.complexviews");
registerSingleton("viewResolver", ResourceBundleViewResolver.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("suffix", ".jsp");
registerSingleton("viewResolver2", InternalResourceViewResolver.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("viewName", "form");
registerSingleton("viewHandler", ParameterizableViewController.class, pvs);
registerSingleton("localeHandler", ComplexLocaleChecker.class);
registerSingleton("anotherLocaleHandler", ComplexLocaleChecker.class);
registerSingleton("unknownHandler", Object.class);
registerSingleton("headController", HeadController.class);
registerSingleton("bodyController", BodyController.class);
registerSingleton("servletPostProcessor", SimpleServletPostProcessor.class);
registerSingleton("handlerAdapter", SimpleServletHandlerAdapter.class);
registerSingleton("myServlet", MyServlet.class);
pvs = new MutablePropertyValues();
pvs.add("order", "1");
pvs.add("exceptionMappings", "java.lang.IllegalAccessException=failed2\n" + "ServletRequestBindingException=failed3");
pvs.add("defaultErrorView", "failed0");
registerSingleton("exceptionResolver1", SimpleMappingExceptionResolver.class, pvs);
pvs = new MutablePropertyValues();
pvs.add("order", "0");
pvs.add("exceptionMappings", "java.lang.Exception=failed1");
List<RuntimeBeanReference> mappedHandlers = new ManagedList<>();
mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler"));
pvs.add("mappedHandlers", mappedHandlers);
pvs.add("defaultStatusCode", "500");
pvs.add("defaultErrorView", "failed2");
registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);
registerSingleton("multipartResolver", MockMultipartResolver.class);
registerSingleton("testListener", TestApplicationListener.class);
addMessage("test", Locale.ENGLISH, "test message");
addMessage("test", Locale.CANADA, "Canadian & test message");
super.refresh();
}
use of org.springframework.beans.factory.support.ManagedList in project spring-framework by spring-projects.
the class MessageBrokerBeanDefinitionParser method registerMessageConverter.
private RuntimeBeanReference registerMessageConverter(Element element, ParserContext context, Object source) {
Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
ManagedList<? super Object> converters = new ManagedList<>();
if (convertersElement != null) {
converters.setSource(source);
for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
converters.add(object);
}
}
if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
converters.setSource(source);
converters.add(new RootBeanDefinition(StringMessageConverter.class));
converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
if (jackson2Present) {
RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
// Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
jacksonFactoryDef.setSource(source);
jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
converters.add(jacksonConverterDef);
}
}
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
cavs.addIndexedArgumentValue(0, converters);
RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cavs, null);
String name = MESSAGE_CONVERTER_BEAN_NAME;
registerBeanDefByName(name, messageConverterDef, context, source);
return new RuntimeBeanReference(name);
}
Aggregations