View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
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.argeo.slc.osgi;
17  
18  import org.argeo.slc.DefaultNameVersion;
19  import org.argeo.slc.NameVersion;
20  import org.argeo.slc.build.Distribution;
21  import org.argeo.slc.core.build.ResourceDistribution;
22  import org.argeo.slc.deploy.DeploymentData;
23  import org.argeo.slc.deploy.Module;
24  import org.argeo.slc.deploy.ModuleDescriptor;
25  import org.argeo.slc.deploy.TargetData;
26  import org.argeo.slc.execution.RealizedFlow;
27  import org.osgi.framework.Bundle;
28  import org.osgi.framework.Constants;
29  import org.springframework.core.io.Resource;
30  
31  /** A deployed OSGi bundle. */
32  public class OsgiBundle extends DefaultNameVersion implements Module {
33  	private ResourceDistribution distribution;
34  
35  	private Long internalBundleId;
36  
37  	private String title;
38  	private String description;
39  
40  	public OsgiBundle() {
41  
42  	}
43  
44  	public OsgiBundle(String name, String version) {
45  		super(name, version);
46  	}
47  
48  	public OsgiBundle(NameVersion nameVersion) {
49  		super(nameVersion);
50  	}
51  
52  	public OsgiBundle(Bundle bundle) {
53  		super(bundle.getSymbolicName(), getVersionSafe(bundle));
54  		internalBundleId = bundle.getBundleId();
55  	}
56  
57  	/**
58  	 * Initialize from a {@link RealizedFlow}.
59  	 * 
60  	 * @deprecated introduce an unnecessary dependency. TODO: create a separate
61  	 *             helper.
62  	 */
63  	public OsgiBundle(RealizedFlow realizedFlow) {
64  		super(realizedFlow.getModuleName(), realizedFlow.getModuleVersion());
65  	}
66  
67  	/** Utility to avoid NPE. */
68  	private static String getVersionSafe(Bundle bundle) {
69  		Object versionObj = bundle.getHeaders().get(Constants.BUNDLE_VERSION);
70  		if (versionObj != null)
71  			return versionObj.toString();
72  		else
73  			return null;
74  	}
75  
76  	/** Unique deployed system id. TODO: use internal bundle id when available? */
77  	public String getDeployedSystemId() {
78  		return getName() + ":" + getVersion();
79  	}
80  
81  	/**
82  	 * OSGi bundle are self-contained and do not require additional deployment
83  	 * data.
84  	 * 
85  	 * @return always null
86  	 */
87  	public DeploymentData getDeploymentData() {
88  		return null;
89  	}
90  
91  	/** The related distribution. */
92  	public Distribution getDistribution() {
93  		return distribution;
94  	}
95  
96  	/**
97  	 * The related distribution, a jar file with OSGi metadata referenced by a
98  	 * {@link Resource}.
99  	 */
100 	public ResourceDistribution getResourceDistribution() {
101 		return distribution;
102 	}
103 
104 	/** TODO: reference the {@link OsgiRuntime} as target data? */
105 	public TargetData getTargetData() {
106 		throw new UnsupportedOperationException();
107 	}
108 
109 	public void setResourceDistribution(ResourceDistribution distribution) {
110 		this.distribution = distribution;
111 	}
112 
113 	/**
114 	 * Bundle ID used by the OSGi runtime. To be used for optimization when
115 	 * looking in the bundle context. Can therefore be null.
116 	 */
117 	public Long getInternalBundleId() {
118 		return internalBundleId;
119 	}
120 
121 	/** Only package access for the time being. e.g. from {@link BundlesManager} */
122 	void setInternalBundleId(Long internalBundleId) {
123 		this.internalBundleId = internalBundleId;
124 	}
125 
126 	/** Value of the <code>Bundle-Name</code> directive. */
127 	public String getTitle() {
128 		return title;
129 	}
130 
131 	public void setTitle(String label) {
132 		this.title = label;
133 	}
134 
135 	/** Value of the <code>Bundle-Description</code> directive. */
136 	public String getDescription() {
137 		return description;
138 	}
139 
140 	public void setDescription(String description) {
141 		this.description = description;
142 	}
143 
144 	public ModuleDescriptor getModuleDescriptor() {
145 		ModuleDescriptor moduleDescriptor = new ModuleDescriptor();
146 		moduleDescriptor.setName(getName());
147 		moduleDescriptor.setVersion(getVersion());
148 		moduleDescriptor.setDescription(description);
149 		moduleDescriptor.setTitle(title);
150 		return moduleDescriptor;
151 	}
152 }