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   * Created on 25-Jan-2006 by Adrian Colyer
17   */
18  
19  package org.springframework.osgi.service.exporter.support;
20  
21  import java.util.Map;
22  
23  import org.osgi.framework.BundleContext;
24  import org.osgi.framework.Constants;
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.osgi.context.BundleContextAware;
27  import org.springframework.osgi.service.exporter.OsgiServicePropertiesResolver;
28  import org.springframework.osgi.util.internal.MapBasedDictionary;
29  import org.springframework.util.Assert;
30  import org.springframework.util.StringUtils;
31  
32  /**
33   * {@link OsgiServicePropertiesResolver} that creates a service property set
34   * with the following properties:
35   * <ul>
36   * <li>Bundle-SymbolicName=&lt;bundle symbolic name&gt;</li>
37   * <li>Bundle-Version=&lt;bundle version&gt;</li>
38   * <li>org.springframework.osgi.bean.name="&lt;bean name&gt;</li>
39   * </ul>
40   * 
41   * 
42   * @author Adrian Colyer
43   * @author Hal Hildebrand
44   * 
45   * @see OsgiServicePropertiesResolver
46   * @see OsgiServiceFactoryBean
47   * 
48   */
49  public class BeanNameServicePropertiesResolver implements OsgiServicePropertiesResolver, BundleContextAware,
50  		InitializingBean {
51  
52  	private BundleContext bundleContext;
53  
54  
55  	public Map getServiceProperties(String beanName) {
56  		Map p = new MapBasedDictionary();
57  		p.put(BEAN_NAME_PROPERTY_KEY, beanName);
58  		String name = getSymbolicName();
59  		if (StringUtils.hasLength(name)) {
60  			p.put(Constants.BUNDLE_SYMBOLICNAME, name);
61  		}
62  		String version = getBundleVersion();
63  		if (StringUtils.hasLength(version)) {
64  			p.put(Constants.BUNDLE_VERSION, version);
65  		}
66  		return p;
67  	}
68  
69  	private String getBundleVersion() {
70  		return (String) this.bundleContext.getBundle().getHeaders().get(Constants.BUNDLE_VERSION);
71  	}
72  
73  	private String getSymbolicName() {
74  		return this.bundleContext.getBundle().getSymbolicName();
75  	}
76  
77  	public void setBundleContext(BundleContext context) {
78  		this.bundleContext = context;
79  	}
80  
81  	public void afterPropertiesSet() throws Exception {
82  		Assert.notNull(bundleContext, "required property bundleContext has not been set");
83  	}
84  
85  }