View Javadoc
1   /*
2    * Copyright 2009-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.admin.integration;
17  
18  import org.springframework.batch.core.configuration.DuplicateJobException;
19  import org.springframework.core.io.ByteArrayResource;
20  import org.springframework.core.io.Resource;
21  import org.springframework.integration.annotation.MessageEndpoint;
22  import org.springframework.integration.annotation.ServiceActivator;
23  import org.springframework.util.StringUtils;
24  
25  /**
26   * Adapt a {@link JobConfigurationRequest} to a Spring {@link Resource} so it
27   * can be handled by a generic consumer.
28   * 
29   * @author Dave Syer
30   * 
31   */
32  @MessageEndpoint
33  public class JobConfigurationRequestToResourceAdapter {
34  
35  	private static final String EMPTY_BEANS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
36  			+ "<beans xmlns=\"http://www.springframework.org/schema/beans\""
37  			+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
38  			+ " xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\"/>";
39  
40  	@ServiceActivator
41  	public Resource adapt(JobConfigurationRequest request) throws DuplicateJobException {
42  
43  		String filename = request.getFilename();
44  		if (!StringUtils.hasText(request.getXml())) {
45  			return new ByteArrayResource(EMPTY_BEANS.getBytes(), filename + ":empty-string");
46  		}
47  		return new ByteArrayResource(request.getXml().getBytes(), filename);
48  
49  	}
50  
51  }