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
.List
;
22 import java
.util
.SortedMap
;
23 import java
.util
.TreeMap
;
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
;
38 public abstract class AbstractOsgiModularDistribution
implements
39 ModularDistribution
, BundleContextAware
, InitializingBean
{
40 private final static Log log
= LogFactory
41 .getLog(AbstractOsgiModularDistribution
.class);
43 private BundleContext bundleContext
;
44 private EclipseUpdateSite eclipseUpdateSite
;
46 /** Initialized by the object itself. */
47 private SortedMap
<NameVersion
, Distribution
> distributions
= new TreeMap
<NameVersion
, Distribution
>();
49 protected abstract void fillDistributions(
50 SortedMap
<NameVersion
, Distribution
> distributions
)
53 public Distribution
getModuleDistribution(String moduleName
,
54 String moduleVersion
) {
56 .get(new BasicNameVersion(moduleName
, moduleVersion
));
59 public String
getDistributionId() {
60 return bundleContext
.getBundle().getSymbolicName()
62 + bundleContext
.getBundle().getHeaders().get(
63 Constants
.BUNDLE_VERSION
);
66 public Set
<NameVersion
> listModulesNameVersions() {
67 return distributions
.keySet();
70 public void setBundleContext(BundleContext bundleContext
) {
71 this.bundleContext
= bundleContext
;
74 public void afterPropertiesSet() throws Exception
{
75 fillDistributions(distributions
);
76 if (log
.isDebugEnabled())
77 log
.debug("Distribution " + getName() + ":" + getVersion()
78 + " loaded (" + distributions
.size() + " modules)");
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());
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
);
94 return versions
.iterator().next();
98 public Object
getModulesDescriptor(String descriptorType
) {
99 if (descriptorType
.equals("eclipse"))
100 return writeEclipseUpdateSite();
102 throw new UnsupportedException("descriptorType", descriptorType
);
105 protected Set
<NameVersion
> writePlainUrlList() {
106 return distributions
.keySet();
109 protected String
writeEclipseUpdateSite() {
110 if (eclipseUpdateSite
== null)
111 throw new SlcException("No eclipse update site declared.");
113 StringBuffer buf
= new StringBuffer("");
114 buf
.append("<site>");
116 List
<EclipseUpdateSiteCategory
> usedCategories
= new ArrayList
<EclipseUpdateSiteCategory
>();
117 for (EclipseUpdateSiteFeature feature
: eclipseUpdateSite
.getFeatures()) {
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("\"");
128 for (EclipseUpdateSiteCategory category
: feature
.getCategories()) {
129 usedCategories
.add(category
);
130 buf
.append(" <category name=\"").append(category
.getName())
133 buf
.append("</feature>\n\n");
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("\"");
141 buf
.append(" <description>").append(category
.getDescription())
142 .append("</description>\n");
143 buf
.append("</category-def>\n\n");
146 buf
.append("</site>");
147 return buf
.toString();
150 public String
getName() {
151 return bundleContext
.getBundle().getSymbolicName();
154 public String
getVersion() {
155 return bundleContext
.getBundle().getHeaders().get(
156 Constants
.BUNDLE_VERSION
).toString();
160 public String
toString() {
161 return new BasicNameVersion(this).toString();
164 public void setEclipseUpdateSite(EclipseUpdateSite eclipseUpdateSite
) {
165 this.eclipseUpdateSite
= eclipseUpdateSite
;
168 public BundleContext
getBundleContext() {
169 return bundleContext
;