]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/build/AbstractOsgiModularDistribution.java
Start improving Ant
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / 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.List;
6 import java.util.Set;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
9
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;
22
23 public abstract class AbstractOsgiModularDistribution implements
24 ModularDistribution, BundleContextAware, InitializingBean {
25 private final static Log log = LogFactory
26 .getLog(AbstractOsgiModularDistribution.class);
27
28 private BundleContext bundleContext;
29 private EclipseUpdateSite eclipseUpdateSite;
30
31 /** Initialized by the object itself. */
32 private SortedMap<NameVersion, Distribution> distributions = new TreeMap<NameVersion, Distribution>();
33
34 protected abstract void fillDistributions(
35 SortedMap<NameVersion, Distribution> distributions)
36 throws Exception;
37
38 public Distribution getModuleDistribution(String moduleName,
39 String moduleVersion) {
40 return distributions
41 .get(new BasicNameVersion(moduleName, moduleVersion));
42 }
43
44 public String getDistributionId() {
45 return bundleContext.getBundle().getSymbolicName()
46 + "-"
47 + bundleContext.getBundle().getHeaders().get(
48 Constants.BUNDLE_VERSION);
49 }
50
51 public Set<NameVersion> listModulesNameVersions() {
52 return distributions.keySet();
53 }
54
55 public void setBundleContext(BundleContext bundleContext) {
56 this.bundleContext = bundleContext;
57 }
58
59 public void afterPropertiesSet() throws Exception {
60 fillDistributions(distributions);
61 if (log.isDebugEnabled())
62 log.debug("Distribution " + getName() + ":" + getVersion()
63 + " loaded (" + distributions.size() + " modules)");
64 }
65
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());
71 }
72
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);
78 else
79 return versions.iterator().next();
80
81 }
82
83 public Object getModulesDescriptor(String descriptorType) {
84 if (descriptorType.equals("eclipse"))
85 return writeEclipseUpdateSite();
86 else
87 throw new UnsupportedException("descriptorType", descriptorType);
88 }
89
90 protected Set<NameVersion> writePlainUrlList() {
91 return distributions.keySet();
92 }
93
94 protected String writeEclipseUpdateSite() {
95 if (eclipseUpdateSite == null)
96 throw new SlcException("No eclipse update site declared.");
97
98 StringBuffer buf = new StringBuffer("");
99 buf.append("<site>");
100
101 List<EclipseUpdateSiteCategory> usedCategories = new ArrayList<EclipseUpdateSiteCategory>();
102 for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) {
103
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("\"");
111 buf.append(">\n");
112
113 for (EclipseUpdateSiteCategory category : feature.getCategories()) {
114 usedCategories.add(category);
115 buf.append(" <category name=\"").append(category.getName())
116 .append("\"/>\n");
117 }
118 buf.append("</feature>\n\n");
119 }
120
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("\"");
125 buf.append(">\n");
126 buf.append(" <description>").append(category.getDescription())
127 .append("</description>\n");
128 buf.append("</category-def>\n\n");
129 }
130
131 buf.append("</site>");
132 return buf.toString();
133 }
134
135 public String getName() {
136 return bundleContext.getBundle().getSymbolicName();
137 }
138
139 public String getVersion() {
140 return bundleContext.getBundle().getHeaders().get(
141 Constants.BUNDLE_VERSION).toString();
142 }
143
144 @Override
145 public String toString() {
146 return new BasicNameVersion(this).toString();
147 }
148
149 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) {
150 this.eclipseUpdateSite = eclipseUpdateSite;
151 }
152
153 public BundleContext getBundleContext() {
154 return bundleContext;
155 }
156
157 }