From 55320b2c11506dcb114d72fbf3bc90e2be296107 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 8 May 2009 18:32:07 +0000 Subject: [PATCH] SPR-4359 Initial commit of scheduling namespace support. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1129 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../ScheduledTasksBeanDefinitionParser.java | 101 ++++++++++++++++++ .../config/SchedulingNamespaceHandler.java | 33 ++++++ .../main/resources/META-INF/spring.handlers | 1 + .../main/resources/META-INF/spring.schemas | 2 + .../config/spring-scheduling-3.0.xsd | 93 ++++++++++++++++ 5 files changed, 230 insertions(+) create mode 100644 org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java create mode 100644 org.springframework.context/src/main/java/org/springframework/scheduling/config/SchedulingNamespaceHandler.java create mode 100644 org.springframework.context/src/main/resources/org/springframework/scheduling/config/spring-scheduling-3.0.xsd diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java b/org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java new file mode 100644 index 00000000000..c23d501ed46 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/config/ScheduledTasksBeanDefinitionParser.java @@ -0,0 +1,101 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.config; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedMap; +import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; + +/** + * Parser for the 'scheduled-tasks' element of the scheduling namespace. + * + * @author Mark Fisher + * @since 3.0 + */ +public class ScheduledTasksBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { + + @Override + protected boolean shouldGenerateId() { + return true; + } + + @Override + protected String getBeanClassName(Element element) { + return "org.springframework.scheduling.config.ScheduledTaskRegistrar"; + } + + @Override + protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + ManagedMap cronTaskMap = new ManagedMap(); + ManagedMap fixedDelayTaskMap = new ManagedMap(); + ManagedMap fixedRateTaskMap = new ManagedMap(); + NodeList childNodes = element.getChildNodes(); + for (int i = 0; i < childNodes.getLength(); i++) { + Node child = childNodes.item(i); + if (!(child instanceof Element) || !child.getLocalName().equals("task")) { + continue; + } + Element taskElement = (Element) child; + String ref = taskElement.getAttribute("ref"); + String method = taskElement.getAttribute("method"); + RuntimeBeanReference runnableBeanRef = new RuntimeBeanReference( + this.createRunnableBean(ref, method, parserContext)); + String cronAttribute = taskElement.getAttribute("cron"); + if (StringUtils.hasText(cronAttribute)) { + cronTaskMap.put(runnableBeanRef, cronAttribute); + } + else { + String fixedDelayAttribute = taskElement.getAttribute("fixed-delay"); + if (StringUtils.hasText(fixedDelayAttribute)) { + fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute); + } + else { + String fixedRateAttribute = taskElement.getAttribute("fixed-rate"); + if (!StringUtils.hasText(fixedRateAttribute)) { + parserContext.getReaderContext().error( + "one of 'cron', 'fixed-delay', or 'fixed-rate' is required", taskElement); + return; + } + fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute); + } + } + } + String schedulerRef = element.getAttribute("scheduler"); + if (StringUtils.hasText(schedulerRef)) { + builder.addPropertyReference("taskScheduler", schedulerRef); + } + builder.addPropertyValue("cronTasks", cronTaskMap); + builder.addPropertyValue("fixedDelayTasks", fixedDelayTaskMap); + builder.addPropertyValue("fixedRateTasks", fixedRateTaskMap); + } + + private String createRunnableBean(String ref, String method, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.scheduling.support.MethodInvokingRunnable"); + builder.addPropertyReference("targetObject", ref); + builder.addPropertyValue("targetMethod", method); + return parserContext.getReaderContext().registerWithGeneratedName(builder.getBeanDefinition()); + } + +} diff --git a/org.springframework.context/src/main/java/org/springframework/scheduling/config/SchedulingNamespaceHandler.java b/org.springframework.context/src/main/java/org/springframework/scheduling/config/SchedulingNamespaceHandler.java new file mode 100644 index 00000000000..4bedd7841c2 --- /dev/null +++ b/org.springframework.context/src/main/java/org/springframework/scheduling/config/SchedulingNamespaceHandler.java @@ -0,0 +1,33 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.config; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +/** + * NamespaceHandler for the scheduling namespace. + * + * @author Mark Fisher + * @since 3.0 + */ +public class SchedulingNamespaceHandler extends NamespaceHandlerSupport { + + public void init() { + this.registerBeanDefinitionParser("scheduled-tasks", new ScheduledTasksBeanDefinitionParser()); + } + +} diff --git a/org.springframework.context/src/main/resources/META-INF/spring.handlers b/org.springframework.context/src/main/resources/META-INF/spring.handlers index 0d98a114a78..3fbbb2e3674 100644 --- a/org.springframework.context/src/main/resources/META-INF/spring.handlers +++ b/org.springframework.context/src/main/resources/META-INF/spring.handlers @@ -1,3 +1,4 @@ http\://www.springframework.org/schema/context=org.springframework.context.config.ContextNamespaceHandler http\://www.springframework.org/schema/jee=org.springframework.ejb.config.JeeNamespaceHandler http\://www.springframework.org/schema/lang=org.springframework.scripting.config.LangNamespaceHandler +http\://www.springframework.org/schema/scheduling=org.springframework.scheduling.config.SchedulingNamespaceHandler diff --git a/org.springframework.context/src/main/resources/META-INF/spring.schemas b/org.springframework.context/src/main/resources/META-INF/spring.schemas index 4b32bfb8485..10f3eec9e1f 100644 --- a/org.springframework.context/src/main/resources/META-INF/spring.schemas +++ b/org.springframework.context/src/main/resources/META-INF/spring.schemas @@ -6,3 +6,5 @@ http\://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ej http\://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd http\://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd http\://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd +http\://www.springframework.org/schema/scheduling/spring-scheduling-3.0.xsd=org/springframework/scheduling/config/spring-scheduling-3.0.xsd +http\://www.springframework.org/schema/scheduling/spring-scheduling.xsd=org/springframework/scheduling/config/spring-scheduling-3.0.xsd diff --git a/org.springframework.context/src/main/resources/org/springframework/scheduling/config/spring-scheduling-3.0.xsd b/org.springframework.context/src/main/resources/org/springframework/scheduling/config/spring-scheduling-3.0.xsd new file mode 100644 index 00000000000..10f8dd99c63 --- /dev/null +++ b/org.springframework.context/src/main/resources/org/springframework/scheduling/config/spring-scheduling-3.0.xsd @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +