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.support; |
17 | |
18 | import org.springframework.beans.factory.InitializingBean; |
19 | import org.springframework.util.Assert; |
20 | |
21 | /** |
22 | * Helper class that sets up a System property with a default value. A System |
23 | * property is created with the specified key name, and default value (i.e. if |
24 | * the property already exists it is not changed). |
25 | * |
26 | * @author Dave Syer |
27 | * |
28 | */ |
29 | public class SystemPropertyInitializer implements InitializingBean { |
30 | |
31 | /** |
32 | * Name of system property used by default. |
33 | */ |
34 | public static final String ENVIRONMENT = "org.springframework.batch.support.SystemPropertyInitializer.ENVIRONMENT"; |
35 | |
36 | private String keyName = ENVIRONMENT; |
37 | |
38 | private String defaultValue; |
39 | |
40 | /** |
41 | * Set the key name for the System property that is created. Defaults to |
42 | * {@link #ENVIRONMENT}. |
43 | * |
44 | * @param keyName the key name to set |
45 | */ |
46 | public void setKeyName(String keyName) { |
47 | this.keyName = keyName; |
48 | } |
49 | |
50 | /** |
51 | * Mandatory property specifying the default value of the System property. |
52 | * |
53 | * @param defaultValue the default value to set |
54 | */ |
55 | public void setDefaultValue(String defaultValue) { |
56 | this.defaultValue = defaultValue; |
57 | } |
58 | |
59 | /** |
60 | * Sets the System property with the provided name and default value. |
61 | * |
62 | * @see InitializingBean#afterPropertiesSet() |
63 | */ |
64 | public void afterPropertiesSet() throws Exception { |
65 | Assert.state(defaultValue != null || System.getProperty(keyName) != null, |
66 | "Either a default value must be specified or the value should already be set for System property: " |
67 | + keyName); |
68 | System.setProperty(keyName, System.getProperty(keyName, defaultValue)); |
69 | } |
70 | |
71 | } |