View Javadoc

1   /*
2    * Copyright 2006-2008 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  
17  package org.springframework.osgi.test.platform;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Properties;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * Base class for OsgiPlatform classes. Provides common functionality such as
28   * creation a temporary folder on startup and deletion on shutdown. Uses system
29   * properties to allow easy configuration from the command line.
30   * 
31   * @author Costin Leau
32   */
33  abstract class AbstractOsgiPlatform implements OsgiPlatform {
34  
35  	private static final String TMP_DIR_FALLBACK = "./tmp-test";
36  
37  	private static final String DEFAULT_SUFFIX = "osgi";
38  
39  	private static final String TMP_PREFIX = "org.sfw.osgi";
40  
41  	final Log log = LogFactory.getLog(getClass());
42  
43  	/**
44  	 * Subclasses should override this field.
45  	 */
46  	String toString = getClass().getName();
47  
48  	private Properties configurationProperties = null;
49  
50  
51  	/**
52  	 * {@inheritDoc}
53  	 * 
54  	 * This implementation considers existing system properties as well as
55  	 * platform specific ones, defined in this class. The system properties are
56  	 * convenient for changing the configuration directly from the command line
57  	 * (useful for CI builds) leaving the programmer to ultimately decide the
58  	 * actual configuration used.
59  	 */
60  	public Properties getConfigurationProperties() {
61  		// check if defaults should apply
62  		if (configurationProperties == null) {
63  			configurationProperties = new Properties();
64  			// system properties
65  			configurationProperties.putAll(System.getProperties());
66  			// local properties
67  			configurationProperties.putAll(getPlatformProperties());
68  			return configurationProperties;
69  		}
70  		return configurationProperties;
71  	}
72  
73  	/**
74  	 * Subclasses can override this to provide special platform properties.
75  	 * 
76  	 * @return platform implementation specific properties.
77  	 */
78  	abstract Properties getPlatformProperties();
79  
80  	/**
81  	 * Returns the underlying OSGi platform name.
82  	 * 
83  	 * @return the platform name
84  	 */
85  	public String toString() {
86  		return toString;
87  	}
88  
89  	File createTempDir(String suffix) {
90  		if (suffix == null)
91  			suffix = DEFAULT_SUFFIX;
92  		File tempFileName;
93  
94  		try {
95  			tempFileName = File.createTempFile(TMP_PREFIX, suffix);
96  		}
97  		catch (IOException ex) {
98  			if (log.isWarnEnabled()) {
99  				log.warn("Could not create temporary directory, returning a temp folder inside the current folder", ex);
100 			}
101 			return new File(TMP_DIR_FALLBACK);
102 		}
103 
104 		tempFileName.delete(); // we want it to be a directory...
105 		File tempFolder = new File(tempFileName.getAbsolutePath());
106 		tempFolder.mkdir();
107 		return tempFolder;
108 	}
109 }