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.extender.support.scanning;
18  
19  import java.util.Enumeration;
20  
21  import org.osgi.framework.Bundle;
22  import org.springframework.osgi.extender.support.internal.ConfigUtils;
23  import org.springframework.osgi.io.OsgiBundleResource;
24  import org.springframework.util.ObjectUtils;
25  
26  /**
27   * Default implementation of {@link ConfigurationScanner} interface.
28   * 
29   * <p/>Supports <tt>Spring-Context</tt> manifest header and
30   * <tt>META-INF/spring/*.xml</tt>.
31   * 
32   * @author Costin Leau
33   * 
34   */
35  public class DefaultConfigurationScanner implements ConfigurationScanner {
36  
37  	private static final String CONTEXT_DIR = "/META-INF/spring/";
38  
39  	private static final String CONTEXT_FILES = "*.xml";
40  
41  	/** Default configuration location */
42  	public static final String DEFAULT_CONFIG = OsgiBundleResource.BUNDLE_URL_PREFIX + CONTEXT_DIR + CONTEXT_FILES;
43  
44  
45  	public String[] getConfigurations(Bundle bundle) {
46  		String[] locations = ConfigUtils.getHeaderLocations(bundle.getHeaders());
47  
48  		// if no location is specified in the header, try the defaults
49  		if (ObjectUtils.isEmpty(locations)) {
50  			// check the default locations if the manifest doesn't provide any info
51  			Enumeration defaultConfig = bundle.findEntries(CONTEXT_DIR, CONTEXT_FILES, false);
52  			if (defaultConfig != null && defaultConfig.hasMoreElements()) {
53  				return new String[] { DEFAULT_CONFIG };
54  			}
55  			else {
56  				return new String[0];
57  			}
58  		}
59  		else {
60  			return locations;
61  		}
62  	}
63  }