]> 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
Execution modules now properly started when a flow is called.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / build / AbstractOsgiModularDistribution.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.osgi.build;
17
18 import java.util.ArrayList;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Set;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.argeo.slc.BasicNameVersion;
28 import org.argeo.slc.NameVersion;
29 import org.argeo.slc.SlcException;
30 import org.argeo.slc.UnsupportedException;
31 import org.argeo.slc.build.Distribution;
32 import org.argeo.slc.build.ModularDistribution;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.Constants;
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.osgi.context.BundleContextAware;
37
38 public abstract class AbstractOsgiModularDistribution implements
39 ModularDistribution, BundleContextAware, InitializingBean {
40 private final static Log log = LogFactory
41 .getLog(AbstractOsgiModularDistribution.class);
42
43 private BundleContext bundleContext;
44 private EclipseUpdateSite eclipseUpdateSite;
45
46 /** Initialized by the object itself. */
47 private SortedMap<NameVersion, Distribution> distributions = new TreeMap<NameVersion, Distribution>();
48
49 protected abstract void fillDistributions(
50 SortedMap<NameVersion, Distribution> distributions)
51 throws Exception;
52
53 public Distribution getModuleDistribution(String moduleName,
54 String moduleVersion) {
55 return distributions
56 .get(new BasicNameVersion(moduleName, moduleVersion));
57 }
58
59 public String getDistributionId() {
60 return bundleContext.getBundle().getSymbolicName()
61 + "-"
62 + bundleContext.getBundle().getHeaders().get(
63 Constants.BUNDLE_VERSION);
64 }
65
66 public Set<NameVersion> listModulesNameVersions() {
67 return distributions.keySet();
68 }
69
70 public void setBundleContext(BundleContext bundleContext) {
71 this.bundleContext = bundleContext;
72 }
73
74 public void afterPropertiesSet() throws Exception {
75 fillDistributions(distributions);
76 if (log.isDebugEnabled())
77 log.debug("Distribution " + getName() + ":" + getVersion()
78 + " loaded (" + distributions.size() + " modules)");
79 }
80
81 protected String findVersion(String name) {
82 Set<String> versions = new HashSet<String>();
83 for (NameVersion key : distributions.keySet()) {
84 if (key.getName().equals(name))
85 versions.add(key.getVersion());
86 }
87
88 if (versions.size() == 0)
89 throw new SlcException("Cannot find version for name " + name);
90 else if (versions.size() > 1)
91 throw new SlcException("Found more than one version for name "
92 + name + ": " + versions);
93 else
94 return versions.iterator().next();
95
96 }
97
98 public Object getModulesDescriptor(String descriptorType) {
99 if (descriptorType.equals("eclipse"))
100 return writeEclipseUpdateSite();
101 else
102 throw new UnsupportedException("descriptorType", descriptorType);
103 }
104
105 protected Set<NameVersion> writePlainUrlList() {
106 return distributions.keySet();
107 }
108
109 protected String writeEclipseUpdateSite() {
110 if (eclipseUpdateSite == null)
111 throw new SlcException("No eclipse update site declared.");
112
113 StringBuffer buf = new StringBuffer("");
114 buf.append("<site>");
115
116 List<EclipseUpdateSiteCategory> usedCategories = new ArrayList<EclipseUpdateSiteCategory>();
117 for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) {
118
119 String featureId = feature.getName();
120 String featureVersion = findVersion(featureId);
121 buf.append("<feature");
122 buf.append(" url=\"features/").append(featureId).append('_')
123 .append(featureVersion).append(".jar\"");
124 buf.append(" id=\"").append(featureId).append("\"");
125 buf.append(" version=\"").append(featureVersion).append("\"");
126 buf.append(">\n");
127
128 for (EclipseUpdateSiteCategory category : feature.getCategories()) {
129 usedCategories.add(category);
130 buf.append(" <category name=\"").append(category.getName())
131 .append("\"/>\n");
132 }
133 buf.append("</feature>\n\n");
134 }
135
136 for (EclipseUpdateSiteCategory category : usedCategories) {
137 buf.append("<category-def");
138 buf.append(" name=\"").append(category.getName()).append("\"");
139 buf.append(" label=\"").append(category.getLabel()).append("\"");
140 buf.append(">\n");
141 buf.append(" <description>").append(category.getDescription())
142 .append("</description>\n");
143 buf.append("</category-def>\n\n");
144 }
145
146 buf.append("</site>");
147 return buf.toString();
148 }
149
150 public String getName() {
151 return bundleContext.getBundle().getSymbolicName();
152 }
153
154 public String getVersion() {
155 return bundleContext.getBundle().getHeaders().get(
156 Constants.BUNDLE_VERSION).toString();
157 }
158
159 @Override
160 public String toString() {
161 return new BasicNameVersion(this).toString();
162 }
163
164 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) {
165 this.eclipseUpdateSite = eclipseUpdateSite;
166 }
167
168 public BundleContext getBundleContext() {
169 return bundleContext;
170 }
171
172 }