1 package org
.argeo
.slc
.osgi
.build
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashSet
;
5 import java
.util
.Iterator
;
8 import java
.util
.SortedMap
;
9 import java
.util
.TreeMap
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.slc
.DefaultNameVersion
;
14 import org
.argeo
.slc
.NameVersion
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.UnsupportedException
;
17 import org
.argeo
.slc
.build
.Distribution
;
18 import org
.argeo
.slc
.build
.ModularDistribution
;
19 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
20 import org
.osgi
.framework
.BundleContext
;
21 import org
.osgi
.framework
.Constants
;
22 import org
.springframework
.beans
.factory
.InitializingBean
;
24 public abstract class AbstractOsgiModularDistribution
implements
25 ModularDistribution
, BundleContextAware
, InitializingBean
{
26 private final static Log log
= LogFactory
27 .getLog(AbstractOsgiModularDistribution
.class);
29 private BundleContext bundleContext
;
30 private EclipseUpdateSite eclipseUpdateSite
;
32 /** Initialized by the object itself. */
33 private SortedMap
<NameVersion
, Distribution
> distributions
= new TreeMap
<NameVersion
, Distribution
>();
35 protected abstract void fillDistributions(
36 SortedMap
<NameVersion
, Distribution
> distributions
)
39 public Distribution
getModuleDistribution(String moduleName
,
40 String moduleVersion
) {
41 return distributions
.get(new DefaultNameVersion(moduleName
,
45 public String
getDistributionId() {
46 return bundleContext
.getBundle().getSymbolicName()
48 + bundleContext
.getBundle().getHeaders()
49 .get(Constants
.BUNDLE_VERSION
);
52 public Set
<NameVersion
> listModulesNameVersions() {
53 return distributions
.keySet();
56 public Iterator
<NameVersion
> nameVersions() {
57 return distributions
.keySet().iterator();
60 public void setBundleContext(BundleContext bundleContext
) {
61 this.bundleContext
= bundleContext
;
64 public void afterPropertiesSet() throws Exception
{
65 fillDistributions(distributions
);
66 if (log
.isDebugEnabled())
67 log
.debug("Distribution " + getName() + ":" + getVersion()
68 + " loaded (" + distributions
.size() + " modules)");
71 protected String
findVersion(String name
) {
72 Set
<String
> versions
= new HashSet
<String
>();
73 for (NameVersion key
: distributions
.keySet()) {
74 if (key
.getName().equals(name
))
75 versions
.add(key
.getVersion());
78 if (versions
.size() == 0)
79 throw new SlcException("Cannot find version for name " + name
);
80 else if (versions
.size() > 1)
81 throw new SlcException("Found more than one version for name "
82 + name
+ ": " + versions
);
84 return versions
.iterator().next();
88 public Object
getModulesDescriptor(String descriptorType
) {
89 if (descriptorType
.equals("eclipse"))
90 return writeEclipseUpdateSite();
92 throw new UnsupportedException("descriptorType", descriptorType
);
95 protected Set
<NameVersion
> writePlainUrlList() {
96 return distributions
.keySet();
99 protected String
writeEclipseUpdateSite() {
100 if (eclipseUpdateSite
== null)
101 throw new SlcException("No eclipse update site declared.");
103 StringBuffer buf
= new StringBuffer("");
104 buf
.append("<site>");
106 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
107 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
109 String featureId
= feature
.getName();
110 String featureVersion
= findVersion(featureId
);
111 buf
.append("<feature");
112 buf
.append(" url=\"features/").append(featureId
).append('_')
113 .append(featureVersion
).append(".jar\"");
114 buf
.append(" id=\"").append(featureId
).append("\"");
115 buf
.append(" version=\"").append(featureVersion
).append("\"");
118 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
119 usedCategories
.add(category
);
120 buf
.append(" <category name=\"").append(category
.getName())
123 buf
.append("</feature>\n\n");
126 for (EclipseUpdateSiteCategory category
: usedCategories
) {
127 buf
.append("<category-def");
128 buf
.append(" name=\"").append(category
.getName()).append("\"");
129 buf
.append(" label=\"").append(category
.getLabel()).append("\"");
131 buf
.append(" <description>").append(category
.getDescription())
132 .append("</description>\n");
133 buf
.append("</category-def>\n\n");
136 buf
.append("</site>");
137 return buf
.toString();
140 public String
getName() {
141 return bundleContext
.getBundle().getSymbolicName();
144 public String
getVersion() {
145 return bundleContext
.getBundle().getHeaders()
146 .get(Constants
.BUNDLE_VERSION
).toString();
150 public String
toString() {
151 return new DefaultNameVersion(this).toString();
154 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
155 this.eclipseUpdateSite
= eclipseUpdateSite
;
158 public BundleContext
getBundleContext() {
159 return bundleContext
;