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
.argeo
.api
.cms
.CmsLog
;
12 import org
.argeo
.slc
.DefaultNameVersion
;
13 import org
.argeo
.slc
.NameVersion
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.UnsupportedException
;
16 import org
.argeo
.slc
.build
.Distribution
;
17 import org
.argeo
.slc
.build
.ModularDistribution
;
18 import org
.eclipse
.gemini
.blueprint
.context
.BundleContextAware
;
19 import org
.osgi
.framework
.BundleContext
;
20 import org
.osgi
.framework
.Constants
;
21 import org
.springframework
.beans
.factory
.InitializingBean
;
23 public abstract class AbstractOsgiModularDistribution
implements
24 ModularDistribution
, BundleContextAware
, InitializingBean
{
25 private final static CmsLog log
= CmsLog
26 .getLog(AbstractOsgiModularDistribution
.class);
28 private BundleContext bundleContext
;
29 private EclipseUpdateSite eclipseUpdateSite
;
31 /** Initialized by the object itself. */
32 private SortedMap
<NameVersion
, Distribution
> distributions
= new TreeMap
<NameVersion
, Distribution
>();
34 protected abstract void fillDistributions(
35 SortedMap
<NameVersion
, Distribution
> distributions
)
38 public Distribution
getModuleDistribution(String moduleName
,
39 String moduleVersion
) {
40 return distributions
.get(new DefaultNameVersion(moduleName
,
44 public String
getDistributionId() {
45 return bundleContext
.getBundle().getSymbolicName()
47 + bundleContext
.getBundle().getHeaders()
48 .get(Constants
.BUNDLE_VERSION
);
51 public Set
<NameVersion
> listModulesNameVersions() {
52 return distributions
.keySet();
55 public Iterator
<NameVersion
> nameVersions() {
56 return distributions
.keySet().iterator();
59 public void setBundleContext(BundleContext bundleContext
) {
60 this.bundleContext
= bundleContext
;
63 public void afterPropertiesSet() throws Exception
{
64 fillDistributions(distributions
);
65 if (log
.isDebugEnabled())
66 log
.debug("Distribution " + getName() + ":" + getVersion()
67 + " loaded (" + distributions
.size() + " modules)");
70 protected String
findVersion(String name
) {
71 Set
<String
> versions
= new HashSet
<String
>();
72 for (NameVersion key
: distributions
.keySet()) {
73 if (key
.getName().equals(name
))
74 versions
.add(key
.getVersion());
77 if (versions
.size() == 0)
78 throw new SlcException("Cannot find version for name " + name
);
79 else if (versions
.size() > 1)
80 throw new SlcException("Found more than one version for name "
81 + name
+ ": " + versions
);
83 return versions
.iterator().next();
87 public Object
getModulesDescriptor(String descriptorType
) {
88 if (descriptorType
.equals("eclipse"))
89 return writeEclipseUpdateSite();
91 throw new UnsupportedException("descriptorType", descriptorType
);
94 protected Set
<NameVersion
> writePlainUrlList() {
95 return distributions
.keySet();
98 protected String
writeEclipseUpdateSite() {
99 if (eclipseUpdateSite
== null)
100 throw new SlcException("No eclipse update site declared.");
102 StringBuffer buf
= new StringBuffer("");
103 buf
.append("<site>");
105 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
106 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
108 String featureId
= feature
.getName();
109 String featureVersion
= findVersion(featureId
);
110 buf
.append("<feature");
111 buf
.append(" url=\"features/").append(featureId
).append('_')
112 .append(featureVersion
).append(".jar\"");
113 buf
.append(" id=\"").append(featureId
).append("\"");
114 buf
.append(" version=\"").append(featureVersion
).append("\"");
117 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
118 usedCategories
.add(category
);
119 buf
.append(" <category name=\"").append(category
.getName())
122 buf
.append("</feature>\n\n");
125 for (EclipseUpdateSiteCategory category
: usedCategories
) {
126 buf
.append("<category-def");
127 buf
.append(" name=\"").append(category
.getName()).append("\"");
128 buf
.append(" label=\"").append(category
.getLabel()).append("\"");
130 buf
.append(" <description>").append(category
.getDescription())
131 .append("</description>\n");
132 buf
.append("</category-def>\n\n");
135 buf
.append("</site>");
136 return buf
.toString();
139 public String
getName() {
140 return bundleContext
.getBundle().getSymbolicName();
143 public String
getVersion() {
144 return bundleContext
.getBundle().getHeaders()
145 .get(Constants
.BUNDLE_VERSION
).toString();
149 public String
toString() {
150 return new DefaultNameVersion(this).toString();
153 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
154 this.eclipseUpdateSite
= eclipseUpdateSite
;
157 public BundleContext
getBundleContext() {
158 return bundleContext
;