1 package org
.argeo
.slc
.osgi
.build
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashSet
;
7 import java
.util
.SortedMap
;
8 import java
.util
.TreeMap
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.argeo
.slc
.UnsupportedException
;
14 import org
.argeo
.slc
.build
.BasicNameVersion
;
15 import org
.argeo
.slc
.build
.Distribution
;
16 import org
.argeo
.slc
.build
.ModularDistribution
;
17 import org
.argeo
.slc
.build
.NameVersion
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.Constants
;
20 import org
.springframework
.beans
.factory
.InitializingBean
;
21 import org
.springframework
.osgi
.context
.BundleContextAware
;
23 public abstract class AbstractOsgiModularDistribution
implements
24 ModularDistribution
, BundleContextAware
, InitializingBean
{
25 private final static Log log
= LogFactory
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
) {
41 .get(new BasicNameVersion(moduleName
, moduleVersion
));
44 public String
getDistributionId() {
45 return bundleContext
.getBundle().getSymbolicName()
47 + bundleContext
.getBundle().getHeaders().get(
48 Constants
.BUNDLE_VERSION
);
51 public Set
<NameVersion
> listModulesNameVersions() {
52 return distributions
.keySet();
55 public void setBundleContext(BundleContext bundleContext
) {
56 this.bundleContext
= bundleContext
;
59 public void afterPropertiesSet() throws Exception
{
60 fillDistributions(distributions
);
61 if (log
.isDebugEnabled())
62 log
.debug("Distribution " + getName() + ":" + getVersion()
63 + " loaded (" + distributions
.size() + " modules)");
66 protected String
findVersion(String name
) {
67 Set
<String
> versions
= new HashSet
<String
>();
68 for (NameVersion key
: distributions
.keySet()) {
69 if (key
.getName().equals(name
))
70 versions
.add(key
.getVersion());
73 if (versions
.size() == 0)
74 throw new SlcException("Cannot find version for name " + name
);
75 else if (versions
.size() > 1)
76 throw new SlcException("Found more than one version for name "
77 + name
+ ": " + versions
);
79 return versions
.iterator().next();
83 public Object
getModulesDescriptor(String descriptorType
) {
84 if (descriptorType
.equals("eclipse"))
85 return writeEclipseUpdateSite();
87 throw new UnsupportedException("descriptorType", descriptorType
);
90 protected Set
<NameVersion
> writePlainUrlList() {
91 return distributions
.keySet();
94 protected String
writeEclipseUpdateSite() {
95 if (eclipseUpdateSite
== null)
96 throw new SlcException("No eclipse update site declared.");
98 StringBuffer buf
= new StringBuffer("");
101 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
102 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
104 String featureId
= feature
.getName();
105 String featureVersion
= findVersion(featureId
);
106 buf
.append("<feature");
107 buf
.append(" url=\"features/").append(featureId
).append('_')
108 .append(featureVersion
).append(".jar\"");
109 buf
.append(" id=\"").append(featureId
).append("\"");
110 buf
.append(" version=\"").append(featureVersion
).append("\"");
113 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
114 usedCategories
.add(category
);
115 buf
.append(" <category name=\"").append(category
.getName())
118 buf
.append("</feature>\n\n");
121 for (EclipseUpdateSiteCategory category
: usedCategories
) {
122 buf
.append("<category-def");
123 buf
.append(" name=\"").append(category
.getName()).append("\"");
124 buf
.append(" label=\"").append(category
.getLabel()).append("\"");
126 buf
.append(" <description>").append(category
.getDescription())
127 .append("</description>\n");
128 buf
.append("</category-def>\n\n");
131 buf
.append("</site>");
132 return buf
.toString();
135 public String
getName() {
136 return bundleContext
.getBundle().getSymbolicName();
139 public String
getVersion() {
140 return bundleContext
.getBundle().getHeaders().get(
141 Constants
.BUNDLE_VERSION
).toString();
145 public String
toString() {
146 return new BasicNameVersion(this).toString();
149 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
150 this.eclipseUpdateSite
= eclipseUpdateSite
;
153 public BundleContext
getBundleContext() {
154 return bundleContext
;