package org.argeo.slc.osgi.build; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.DefaultNameVersion; import org.argeo.slc.NameVersion; import org.argeo.slc.SlcException; import org.argeo.slc.UnsupportedException; import org.argeo.slc.build.Distribution; import org.argeo.slc.build.ModularDistribution; import org.eclipse.gemini.blueprint.context.BundleContextAware; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.springframework.beans.factory.InitializingBean; public abstract class AbstractOsgiModularDistribution implements ModularDistribution, BundleContextAware, InitializingBean { private final static Log log = LogFactory .getLog(AbstractOsgiModularDistribution.class); private BundleContext bundleContext; private EclipseUpdateSite eclipseUpdateSite; /** Initialized by the object itself. */ private SortedMap distributions = new TreeMap(); protected abstract void fillDistributions( SortedMap distributions) throws Exception; public Distribution getModuleDistribution(String moduleName, String moduleVersion) { return distributions.get(new DefaultNameVersion(moduleName, moduleVersion)); } public String getDistributionId() { return bundleContext.getBundle().getSymbolicName() + "-" + bundleContext.getBundle().getHeaders() .get(Constants.BUNDLE_VERSION); } public Set listModulesNameVersions() { return distributions.keySet(); } public Iterator nameVersions() { return distributions.keySet().iterator(); } public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } public void afterPropertiesSet() throws Exception { fillDistributions(distributions); if (log.isDebugEnabled()) log.debug("Distribution " + getName() + ":" + getVersion() + " loaded (" + distributions.size() + " modules)"); } protected String findVersion(String name) { Set versions = new HashSet(); for (NameVersion key : distributions.keySet()) { if (key.getName().equals(name)) versions.add(key.getVersion()); } if (versions.size() == 0) throw new SlcException("Cannot find version for name " + name); else if (versions.size() > 1) throw new SlcException("Found more than one version for name " + name + ": " + versions); else return versions.iterator().next(); } public Object getModulesDescriptor(String descriptorType) { if (descriptorType.equals("eclipse")) return writeEclipseUpdateSite(); else throw new UnsupportedException("descriptorType", descriptorType); } protected Set writePlainUrlList() { return distributions.keySet(); } protected String writeEclipseUpdateSite() { if (eclipseUpdateSite == null) throw new SlcException("No eclipse update site declared."); StringBuffer buf = new StringBuffer(""); buf.append(""); List usedCategories = new ArrayList(); for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) { String featureId = feature.getName(); String featureVersion = findVersion(featureId); buf.append("\n"); for (EclipseUpdateSiteCategory category : feature.getCategories()) { usedCategories.add(category); buf.append(" \n"); } buf.append("\n\n"); } for (EclipseUpdateSiteCategory category : usedCategories) { buf.append("\n"); buf.append(" ").append(category.getDescription()) .append("\n"); buf.append("\n\n"); } buf.append(""); return buf.toString(); } public String getName() { return bundleContext.getBundle().getSymbolicName(); } public String getVersion() { return bundleContext.getBundle().getHeaders() .get(Constants.BUNDLE_VERSION).toString(); } @Override public String toString() { return new DefaultNameVersion(this).toString(); } public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) { this.eclipseUpdateSite = eclipseUpdateSite; } public BundleContext getBundleContext() { return bundleContext; } }