]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/build/AbstractOsgiModularDistribution.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / legacy / org.argeo.slc.spring / src / org / argeo / slc / osgi / build / AbstractOsgiModularDistribution.java
1 package org.argeo.slc.osgi.build;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Set;
8 import java.util.SortedMap;
9 import java.util.TreeMap;
10
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;
23
24 public abstract class AbstractOsgiModularDistribution implements
25 ModularDistribution, BundleContextAware, InitializingBean {
26 private final static Log log = LogFactory
27 .getLog(AbstractOsgiModularDistribution.class);
28
29 private BundleContext bundleContext;
30 private EclipseUpdateSite eclipseUpdateSite;
31
32 /** Initialized by the object itself. */
33 private SortedMap<NameVersion, Distribution> distributions = new TreeMap<NameVersion, Distribution>();
34
35 protected abstract void fillDistributions(
36 SortedMap<NameVersion, Distribution> distributions)
37 throws Exception;
38
39 public Distribution getModuleDistribution(String moduleName,
40 String moduleVersion) {
41 return distributions.get(new DefaultNameVersion(moduleName,
42 moduleVersion));
43 }
44
45 public String getDistributionId() {
46 return bundleContext.getBundle().getSymbolicName()
47 + "-"
48 + bundleContext.getBundle().getHeaders()
49 .get(Constants.BUNDLE_VERSION);
50 }
51
52 public Set<NameVersion> listModulesNameVersions() {
53 return distributions.keySet();
54 }
55
56 public Iterator<NameVersion> nameVersions() {
57 return distributions.keySet().iterator();
58 }
59
60 public void setBundleContext(BundleContext bundleContext) {
61 this.bundleContext = bundleContext;
62 }
63
64 public void afterPropertiesSet() throws Exception {
65 fillDistributions(distributions);
66 if (log.isDebugEnabled())
67 log.debug("Distribution " + getName() + ":" + getVersion()
68 + " loaded (" + distributions.size() + " modules)");
69 }
70
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());
76 }
77
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);
83 else
84 return versions.iterator().next();
85
86 }
87
88 public Object getModulesDescriptor(String descriptorType) {
89 if (descriptorType.equals("eclipse"))
90 return writeEclipseUpdateSite();
91 else
92 throw new UnsupportedException("descriptorType", descriptorType);
93 }
94
95 protected Set<NameVersion> writePlainUrlList() {
96 return distributions.keySet();
97 }
98
99 protected String writeEclipseUpdateSite() {
100 if (eclipseUpdateSite == null)
101 throw new SlcException("No eclipse update site declared.");
102
103 StringBuffer buf = new StringBuffer("");
104 buf.append("<site>");
105
106 List<EclipseUpdateSiteCategory> usedCategories = new ArrayList<EclipseUpdateSiteCategory>();
107 for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) {
108
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("\"");
116 buf.append(">\n");
117
118 for (EclipseUpdateSiteCategory category : feature.getCategories()) {
119 usedCategories.add(category);
120 buf.append(" <category name=\"").append(category.getName())
121 .append("\"/>\n");
122 }
123 buf.append("</feature>\n\n");
124 }
125
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("\"");
130 buf.append(">\n");
131 buf.append(" <description>").append(category.getDescription())
132 .append("</description>\n");
133 buf.append("</category-def>\n\n");
134 }
135
136 buf.append("</site>");
137 return buf.toString();
138 }
139
140 public String getName() {
141 return bundleContext.getBundle().getSymbolicName();
142 }
143
144 public String getVersion() {
145 return bundleContext.getBundle().getHeaders()
146 .get(Constants.BUNDLE_VERSION).toString();
147 }
148
149 @Override
150 public String toString() {
151 return new DefaultNameVersion(this).toString();
152 }
153
154 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) {
155 this.eclipseUpdateSite = eclipseUpdateSite;
156 }
157
158 public BundleContext getBundleContext() {
159 return bundleContext;
160 }
161
162 }