2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.osgi
.build
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashSet
;
20 import java
.util
.Iterator
;
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
.DefaultNameVersion
;
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
.eclipse
.gemini
.blueprint
.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
) {
56 return distributions
.get(new DefaultNameVersion(moduleName
,
60 public String
getDistributionId() {
61 return bundleContext
.getBundle().getSymbolicName()
63 + bundleContext
.getBundle().getHeaders()
64 .get(Constants
.BUNDLE_VERSION
);
67 public Set
<NameVersion
> listModulesNameVersions() {
68 return distributions
.keySet();
71 public Iterator
<NameVersion
> nameVersions() {
72 return distributions
.keySet().iterator();
75 public void setBundleContext(BundleContext bundleContext
) {
76 this.bundleContext
= bundleContext
;
79 public void afterPropertiesSet() throws Exception
{
80 fillDistributions(distributions
);
81 if (log
.isDebugEnabled())
82 log
.debug("Distribution " + getName() + ":" + getVersion()
83 + " loaded (" + distributions
.size() + " modules)");
86 protected String
findVersion(String name
) {
87 Set
<String
> versions
= new HashSet
<String
>();
88 for (NameVersion key
: distributions
.keySet()) {
89 if (key
.getName().equals(name
))
90 versions
.add(key
.getVersion());
93 if (versions
.size() == 0)
94 throw new SlcException("Cannot find version for name " + name
);
95 else if (versions
.size() > 1)
96 throw new SlcException("Found more than one version for name "
97 + name
+ ": " + versions
);
99 return versions
.iterator().next();
103 public Object
getModulesDescriptor(String descriptorType
) {
104 if (descriptorType
.equals("eclipse"))
105 return writeEclipseUpdateSite();
107 throw new UnsupportedException("descriptorType", descriptorType
);
110 protected Set
<NameVersion
> writePlainUrlList() {
111 return distributions
.keySet();
114 protected String
writeEclipseUpdateSite() {
115 if (eclipseUpdateSite
== null)
116 throw new SlcException("No eclipse update site declared.");
118 StringBuffer buf
= new StringBuffer("");
119 buf
.append("<site>");
121 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
122 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
124 String featureId
= feature
.getName();
125 String featureVersion
= findVersion(featureId
);
126 buf
.append("<feature");
127 buf
.append(" url=\"features/").append(featureId
).append('_')
128 .append(featureVersion
).append(".jar\"");
129 buf
.append(" id=\"").append(featureId
).append("\"");
130 buf
.append(" version=\"").append(featureVersion
).append("\"");
133 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
134 usedCategories
.add(category
);
135 buf
.append(" <category name=\"").append(category
.getName())
138 buf
.append("</feature>\n\n");
141 for (EclipseUpdateSiteCategory category
: usedCategories
) {
142 buf
.append("<category-def");
143 buf
.append(" name=\"").append(category
.getName()).append("\"");
144 buf
.append(" label=\"").append(category
.getLabel()).append("\"");
146 buf
.append(" <description>").append(category
.getDescription())
147 .append("</description>\n");
148 buf
.append("</category-def>\n\n");
151 buf
.append("</site>");
152 return buf
.toString();
155 public String
getName() {
156 return bundleContext
.getBundle().getSymbolicName();
159 public String
getVersion() {
160 return bundleContext
.getBundle().getHeaders()
161 .get(Constants
.BUNDLE_VERSION
).toString();
165 public String
toString() {
166 return new DefaultNameVersion(this).toString();
169 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
170 this.eclipseUpdateSite
= eclipseUpdateSite
;
173 public BundleContext
getBundleContext() {
174 return bundleContext
;