]> 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
Improve JCR integration
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.osgi / src / main / java / org / argeo / slc / osgi / build / AbstractOsgiModularDistribution.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.osgi.build;
18
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.BasicNameVersion;
29 import org.argeo.slc.NameVersion;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.UnsupportedException;
32 import org.argeo.slc.build.Distribution;
33 import org.argeo.slc.build.ModularDistribution;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.Constants;
36 import org.springframework.beans.factory.InitializingBean;
37 import org.springframework.osgi.context.BundleContextAware;
38
39 public abstract class AbstractOsgiModularDistribution implements
40 ModularDistribution, BundleContextAware, InitializingBean {
41 private final static Log log = LogFactory
42 .getLog(AbstractOsgiModularDistribution.class);
43
44 private BundleContext bundleContext;
45 private EclipseUpdateSite eclipseUpdateSite;
46
47 /** Initialized by the object itself. */
48 private SortedMap<NameVersion, Distribution> distributions = new TreeMap<NameVersion, Distribution>();
49
50 protected abstract void fillDistributions(
51 SortedMap<NameVersion, Distribution> distributions)
52 throws Exception;
53
54 public Distribution getModuleDistribution(String moduleName,
55 String moduleVersion) {
56 return distributions
57 .get(new BasicNameVersion(moduleName, moduleVersion));
58 }
59
60 public String getDistributionId() {
61 return bundleContext.getBundle().getSymbolicName()
62 + "-"
63 + bundleContext.getBundle().getHeaders().get(
64 Constants.BUNDLE_VERSION);
65 }
66
67 public Set<NameVersion> listModulesNameVersions() {
68 return distributions.keySet();
69 }
70
71 public void setBundleContext(BundleContext bundleContext) {
72 this.bundleContext = bundleContext;
73 }
74
75 public void afterPropertiesSet() throws Exception {
76 fillDistributions(distributions);
77 if (log.isDebugEnabled())
78 log.debug("Distribution " + getName() + ":" + getVersion()
79 + " loaded (" + distributions.size() + " modules)");
80 }
81
82 protected String findVersion(String name) {
83 Set<String> versions = new HashSet<String>();
84 for (NameVersion key : distributions.keySet()) {
85 if (key.getName().equals(name))
86 versions.add(key.getVersion());
87 }
88
89 if (versions.size() == 0)
90 throw new SlcException("Cannot find version for name " + name);
91 else if (versions.size() > 1)
92 throw new SlcException("Found more than one version for name "
93 + name + ": " + versions);
94 else
95 return versions.iterator().next();
96
97 }
98
99 public Object getModulesDescriptor(String descriptorType) {
100 if (descriptorType.equals("eclipse"))
101 return writeEclipseUpdateSite();
102 else
103 throw new UnsupportedException("descriptorType", descriptorType);
104 }
105
106 protected Set<NameVersion> writePlainUrlList() {
107 return distributions.keySet();
108 }
109
110 protected String writeEclipseUpdateSite() {
111 if (eclipseUpdateSite == null)
112 throw new SlcException("No eclipse update site declared.");
113
114 StringBuffer buf = new StringBuffer("");
115 buf.append("<site>");
116
117 List<EclipseUpdateSiteCategory> usedCategories = new ArrayList<EclipseUpdateSiteCategory>();
118 for (EclipseUpdateSiteFeature feature : eclipseUpdateSite.getFeatures()) {
119
120 String featureId = feature.getName();
121 String featureVersion = findVersion(featureId);
122 buf.append("<feature");
123 buf.append(" url=\"features/").append(featureId).append('_')
124 .append(featureVersion).append(".jar\"");
125 buf.append(" id=\"").append(featureId).append("\"");
126 buf.append(" version=\"").append(featureVersion).append("\"");
127 buf.append(">\n");
128
129 for (EclipseUpdateSiteCategory category : feature.getCategories()) {
130 usedCategories.add(category);
131 buf.append(" <category name=\"").append(category.getName())
132 .append("\"/>\n");
133 }
134 buf.append("</feature>\n\n");
135 }
136
137 for (EclipseUpdateSiteCategory category : usedCategories) {
138 buf.append("<category-def");
139 buf.append(" name=\"").append(category.getName()).append("\"");
140 buf.append(" label=\"").append(category.getLabel()).append("\"");
141 buf.append(">\n");
142 buf.append(" <description>").append(category.getDescription())
143 .append("</description>\n");
144 buf.append("</category-def>\n\n");
145 }
146
147 buf.append("</site>");
148 return buf.toString();
149 }
150
151 public String getName() {
152 return bundleContext.getBundle().getSymbolicName();
153 }
154
155 public String getVersion() {
156 return bundleContext.getBundle().getHeaders().get(
157 Constants.BUNDLE_VERSION).toString();
158 }
159
160 @Override
161 public String toString() {
162 return new BasicNameVersion(this).toString();
163 }
164
165 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite) {
166 this.eclipseUpdateSite = eclipseUpdateSite;
167 }
168
169 public BundleContext getBundleContext() {
170 return bundleContext;
171 }
172
173 }