View Javadoc
1   package org.argeo.cms.spring.osgi;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.osgi.framework.Bundle;
6   import org.osgi.framework.BundleContext;
7   import org.osgi.framework.Constants;
8   
9   /**
10   * Logs the name and version of an OSGi bundle based on its
11   * {@link BundleContext}.
12   */
13  public class OsgiModuleLabel {
14  	private final static Log log = LogFactory.getLog(OsgiModuleLabel.class);
15  
16  	private Bundle bundle;
17  
18  	public OsgiModuleLabel() {
19  	}
20  
21  	/** Sets without logging. */
22  	public OsgiModuleLabel(Bundle bundle) {
23  		this.bundle = bundle;
24  	}
25  
26  	/**
27  	 * Retrieved bundle from a bundle context and logs it. Typically to be set
28  	 * as a Spring bean.
29  	 */
30  	public void setBundleContext(BundleContext bundleContext) {
31  		this.bundle = bundleContext.getBundle();
32  		log.info(msg());
33  	}
34  
35  	public String msg() {
36  		String name = bundle.getHeaders().get(Constants.BUNDLE_NAME).toString();
37  		String symbolicName = bundle.getSymbolicName();
38  		String version = bundle.getVersion().toString();
39  		return name + " v" + version + " (" + symbolicName + ")";
40  	}
41  }