2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.osgi
.build
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashSet
;
21 import java
.util
.List
;
23 import java
.util
.SortedMap
;
24 import java
.util
.TreeMap
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.UnsupportedException
;
30 import org
.argeo
.slc
.build
.BasicNameVersion
;
31 import org
.argeo
.slc
.build
.Distribution
;
32 import org
.argeo
.slc
.build
.ModularDistribution
;
33 import org
.argeo
.slc
.build
.NameVersion
;
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
;
39 public abstract class AbstractOsgiModularDistribution
implements
40 ModularDistribution
, BundleContextAware
, InitializingBean
{
41 private final static Log log
= LogFactory
42 .getLog(AbstractOsgiModularDistribution
.class);
44 private BundleContext bundleContext
;
45 private EclipseUpdateSite eclipseUpdateSite
;
47 /** Initialized by the object itself. */
48 private SortedMap
<NameVersion
, Distribution
> distributions
= new TreeMap
<NameVersion
, Distribution
>();
50 protected abstract void fillDistributions(
51 SortedMap
<NameVersion
, Distribution
> distributions
)
54 public Distribution
getModuleDistribution(String moduleName
,
55 String moduleVersion
) {
57 .get(new BasicNameVersion(moduleName
, moduleVersion
));
60 public String
getDistributionId() {
61 return bundleContext
.getBundle().getSymbolicName()
63 + bundleContext
.getBundle().getHeaders().get(
64 Constants
.BUNDLE_VERSION
);
67 public Set
<NameVersion
> listModulesNameVersions() {
68 return distributions
.keySet();
71 public void setBundleContext(BundleContext bundleContext
) {
72 this.bundleContext
= bundleContext
;
75 public void afterPropertiesSet() throws Exception
{
76 fillDistributions(distributions
);
77 if (log
.isDebugEnabled())
78 log
.debug("Distribution " + getName() + ":" + getVersion()
79 + " loaded (" + distributions
.size() + " modules)");
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());
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
);
95 return versions
.iterator().next();
99 public Object
getModulesDescriptor(String descriptorType
) {
100 if (descriptorType
.equals("eclipse"))
101 return writeEclipseUpdateSite();
103 throw new UnsupportedException("descriptorType", descriptorType
);
106 protected Set
<NameVersion
> writePlainUrlList() {
107 return distributions
.keySet();
110 protected String
writeEclipseUpdateSite() {
111 if (eclipseUpdateSite
== null)
112 throw new SlcException("No eclipse update site declared.");
114 StringBuffer buf
= new StringBuffer("");
115 buf
.append("<site>");
117 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
118 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
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("\"");
129 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
130 usedCategories
.add(category
);
131 buf
.append(" <category name=\"").append(category
.getName())
134 buf
.append("</feature>\n\n");
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("\"");
142 buf
.append(" <description>").append(category
.getDescription())
143 .append("</description>\n");
144 buf
.append("</category-def>\n\n");
147 buf
.append("</site>");
148 return buf
.toString();
151 public String
getName() {
152 return bundleContext
.getBundle().getSymbolicName();
155 public String
getVersion() {
156 return bundleContext
.getBundle().getHeaders().get(
157 Constants
.BUNDLE_VERSION
).toString();
161 public String
toString() {
162 return new BasicNameVersion(this).toString();
165 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
166 this.eclipseUpdateSite
= eclipseUpdateSite
;
169 public BundleContext
getBundleContext() {
170 return bundleContext
;