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
.osgi
.boot
;
18 import java
.io
.BufferedReader
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.io
.InputStreamReader
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
25 import java
.util
.StringTokenizer
;
26 import java
.util
.jar
.JarEntry
;
27 import java
.util
.jar
.JarInputStream
;
28 import java
.util
.jar
.Manifest
;
30 import org
.osgi
.framework
.Constants
;
33 * A distribution bundle is a bundle within a maven-like distribution
34 * groupId:Bundle-SymbolicName:Bundle-Version which references others OSGi
35 * bundle. It is not required to be OSGi complete also it will generally be
36 * expected that it is. The root of the repository is computed based on the file
37 * name of the URL and of the content of the index.
39 @SuppressWarnings({ "rawtypes", "unchecked" })
40 public class DistributionBundle
{
41 private final static String INDEX_FILE_NAME
= "modularDistribution.csv";
43 private final String url
;
45 private Manifest manifest
;
46 private String symbolicName
;
47 private String version
;
50 private String baseUrl
;
52 private String relativeUrl
;
54 private List
/* <OsgiArtifact> */artifacts
;
56 private String separator
= ",";
58 public DistributionBundle(String url
) {
62 public DistributionBundle(String baseUrl
, String relativeUrl
) {
63 if (baseUrl
== null || !baseUrl
.endsWith("/"))
64 throw new OsgiBootException("Base url " + baseUrl
65 + " badly formatted");
66 if (relativeUrl
.startsWith("http") || relativeUrl
.startsWith("file:"))
67 throw new OsgiBootException("Relative URL " + relativeUrl
68 + " badly formatted");
69 this.url
= baseUrl
+ relativeUrl
;
70 this.baseUrl
= baseUrl
;
71 this.relativeUrl
= relativeUrl
;
74 public void processUrl() {
75 JarInputStream jarIn
= null;
78 jarIn
= new JarInputStream(u
.openStream());
81 manifest
= jarIn
.getManifest();
82 symbolicName
= manifest
.getMainAttributes().getValue(
83 Constants
.BUNDLE_SYMBOLICNAME
);
84 version
= manifest
.getMainAttributes().getValue(
85 Constants
.BUNDLE_VERSION
);
88 while ((indexEntry
= jarIn
.getNextJarEntry()) != null) {
89 String entryName
= indexEntry
.getName();
90 if (entryName
.equals(INDEX_FILE_NAME
)) {
97 if (indexEntry
== null)
98 throw new OsgiBootException("No index " + INDEX_FILE_NAME
100 artifacts
= listArtifacts(jarIn
);
104 // won't work if distribution artifact is not listed
105 for (int i
= 0; i
< artifacts
.size(); i
++) {
106 OsgiArtifact osgiArtifact
= (OsgiArtifact
) artifacts
.get(i
);
107 if (osgiArtifact
.getSymbolicName().equals(symbolicName
)
108 && osgiArtifact
.getVersion().equals(version
)) {
109 String relativeUrl
= osgiArtifact
.getRelativeUrl();
110 if (url
.endsWith(relativeUrl
)) {
111 baseUrl
= url
.substring(0, url
.length()
112 - osgiArtifact
.getRelativeUrl().length());
117 } catch (Exception e
) {
118 throw new OsgiBootException("Cannot list URLs from " + url
, e
);
123 } catch (IOException e
) {
129 protected List
/* <OsgiArtifact> */listArtifacts(InputStream in
) {
130 List osgiArtifacts
= new ArrayList();
131 BufferedReader reader
= null;
133 reader
= new BufferedReader(new InputStreamReader(in
));
135 while ((line
= reader
.readLine()) != null) {
136 StringTokenizer st
= new StringTokenizer(line
, separator
);
137 String moduleName
= st
.nextToken();
138 String moduleVersion
= st
.nextToken();
139 String relativeUrl
= st
.nextToken();
140 osgiArtifacts
.add(new OsgiArtifact(moduleName
, moduleVersion
,
143 } catch (Exception e
) {
144 throw new OsgiBootException("Cannot list artifacts", e
);
146 return osgiArtifacts
;
149 /** Convenience method */
150 public static DistributionBundle
processUrl(String baseUrl
,
151 String realtiveUrl
) {
152 DistributionBundle distributionBundle
= new DistributionBundle(baseUrl
,
154 distributionBundle
.processUrl();
155 return distributionBundle
;
159 * List full URLs of the bunmdles, based on base URL, usable directly for
162 public List
/* <String> */listUrls() {
164 throw new OsgiBootException("Base URL is not set");
166 if (artifacts
== null)
167 throw new OsgiBootException("Artifact list not initialized");
169 List
/* <String> */urls
= new ArrayList();
170 for (int i
= 0; i
< artifacts
.size(); i
++) {
171 OsgiArtifact osgiArtifact
= (OsgiArtifact
) artifacts
.get(i
);
172 urls
.add(baseUrl
+ osgiArtifact
.getRelativeUrl());
177 public void setBaseUrl(String baseUrl
) {
178 this.baseUrl
= baseUrl
;
181 /** Separator used to parse the tabular file */
182 public void setSeparator(String modulesUrlSeparator
) {
183 this.separator
= modulesUrlSeparator
;
186 public String
getRelativeUrl() {
190 /** One of the listed artifact */
191 protected static class OsgiArtifact
{
192 private final String symbolicName
;
193 private final String version
;
194 private final String relativeUrl
;
196 public OsgiArtifact(String symbolicName
, String version
,
197 String relativeUrl
) {
199 this.symbolicName
= symbolicName
;
200 this.version
= version
;
201 this.relativeUrl
= relativeUrl
;
204 public String
getSymbolicName() {
208 public String
getVersion() {
212 public String
getRelativeUrl() {