View Javadoc

1   /*
2    * Copyright 2006-2007 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.item.util;
17  
18  import org.springframework.batch.item.ExecutionContext;
19  import org.springframework.util.Assert;
20  
21  /**
22   * Facilitates assigning names to objects persisting data in {@link ExecutionContext} and generating keys for
23   * {@link ExecutionContext} based on the name.
24   * 
25   * @author Robert Kasanicky
26   */
27  public class ExecutionContextUserSupport {
28  
29  	private String name;
30  
31  	public ExecutionContextUserSupport() {
32  		super();
33  	}
34  
35  	public ExecutionContextUserSupport(String name) {
36  		super();
37  		this.name = name;
38  	}
39  
40  	/**
41  	 * @return name used to uniquely identify this instance's entries in shared context.
42  	 */
43  	protected String getName() {
44  		return this.name;
45  	}
46  
47  	/**
48  	 * @param name unique name used to create execution context keys.
49  	 */
50  	public void setName(String name) {
51  		this.name = name;
52  	}
53  
54  	/**
55  	 * Prefix the argument with {@link #getName()} to create a unique key that can be safely used to identify data
56  	 * stored in {@link ExecutionContext}.
57  	 */
58  	public String getKey(String s) {
59  		Assert.hasText(name, "Name must be assigned for the sake of defining the execution context keys prefix.");
60  		return name + "." + s;
61  	}
62  
63  }