]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.spring/src/org/argeo/slc/osgi/build/AbstractOsgiModularDistribution.java
Massive Argeo APIs refactoring
[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.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;
22
23 public abstract class AbstractOsgiModularDistribution implements
24 ModularDistribution, BundleContextAware, InitializingBean {
25 private final static CmsLog log = CmsLog
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.get(new DefaultNameVersion(moduleName,
41 moduleVersion));
42 }
43
44 public String getDistributionId() {
45 return bundleContext.getBundle().getSymbolicName()
46 + "-"
47 + bundleContext.getBundle().getHeaders()
48 .get(Constants.BUNDLE_VERSION);
49 }
50
51 public Set<NameVersion> listModulesNameVersions() {
52 return distributions.keySet();
53 }
54
55 public Iterator<NameVersion> nameVersions() {
56 return distributions.keySet().iterator();
57 }
58
59 public void setBundleContext(BundleContext bundleContext) {
60 this.bundleContext = bundleContext;
61 }
62
63 public void afterPropertiesSet() throws Exception {
64 fillDistributions(distributions);
65 if (log.isDebugEnabled())
66 log.debug("Distribution " + getName() + ":" + getVersion()
67 + " loaded (" + distributions.size() + " modules)");
68 }
69
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());
75 }
76
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);
82 else
83 return versions.iterator().next();
84
85 }
86
87 public Object getModulesDescriptor(String descriptorType) {
88 if (descriptorType.equals("eclipse"))
89 return writeEclipseUpdateSite();
90 else
91 throw new UnsupportedException("descriptorType", descriptorType);
92 }
93
94 protected Set<NameVersion> writePlainUrlList() {
95 return distributions.keySet();
96 }
97
98 protected String writeEclipseUpdateSite() {
99 if (eclipseUpdateSite == null)
100 throw new SlcException("No eclipse update site declared.");
101
102 StringBuffer buf = new StringBuffer("");
103 buf.append("<site>");
104
105 List<EclipseUpdateSiteCategory> usedCategories = new ArrayList<EclipseUpdateSiteCategory>();
106 for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) {
107
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("\"");
115 buf.append(">\n");
116
117 for (EclipseUpdateSiteCategory category : feature.getCategories()) {
118 usedCategories.add(category);
119 buf.append(" <category name=\"").append(category.getName())
120 .append("\"/>\n");
121 }
122 buf.append("</feature>\n\n");
123 }
124
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("\"");
129 buf.append(">\n");
130 buf.append(" <description>").append(category.getDescription())
131 .append("</description>\n");
132 buf.append("</category-def>\n\n");
133 }
134
135 buf.append("</site>");
136 return buf.toString();
137 }
138
139 public String getName() {
140 return bundleContext.getBundle().getSymbolicName();
141 }
142
143 public String getVersion() {
144 return bundleContext.getBundle().getHeaders()
145 .get(Constants.BUNDLE_VERSION).toString();
146 }
147
148 @Override
149 public String toString() {
150 return new DefaultNameVersion(this).toString();
151 }
152
153 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) {
154 this.eclipseUpdateSite = eclipseUpdateSite;
155 }
156
157 public BundleContext getBundleContext() {
158 return bundleContext;
159 }
160
161 }