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 public class DistributionBundle
{
40 private final static String INDEX_FILE_NAME
= "modularDistribution.csv";
42 private final String url
;
44 private Manifest manifest
;
45 private String symbolicName
;
46 private String version
;
49 private String baseUrl
;
51 private String relativeUrl
;
53 private List
/* <OsgiArtifact> */artifacts
;
55 private String separator
= ",";
57 public DistributionBundle(String url
) {
61 public DistributionBundle(String baseUrl
, String relativeUrl
) {
62 if (baseUrl
== null || !baseUrl
.endsWith("/"))
63 throw new OsgiBootException("Base url " + baseUrl
64 + " badly formatted");
65 if (relativeUrl
.startsWith("http") || relativeUrl
.startsWith("file:"))
66 throw new OsgiBootException("Relative URL " + relativeUrl
67 + " badly formatted");
68 this.url
= baseUrl
+ relativeUrl
;
69 this.baseUrl
= baseUrl
;
70 this.relativeUrl
= relativeUrl
;
73 public void processUrl() {
74 JarInputStream jarIn
= null;
77 jarIn
= new JarInputStream(u
.openStream());
80 manifest
= jarIn
.getManifest();
81 symbolicName
= manifest
.getMainAttributes().getValue(
82 Constants
.BUNDLE_SYMBOLICNAME
);
83 version
= manifest
.getMainAttributes().getValue(
84 Constants
.BUNDLE_VERSION
);
87 while ((indexEntry
= jarIn
.getNextJarEntry()) != null) {
88 String entryName
= indexEntry
.getName();
89 if (entryName
.equals(INDEX_FILE_NAME
)) {
96 if (indexEntry
== null)
97 throw new OsgiBootException("No index " + INDEX_FILE_NAME
99 artifacts
= listArtifacts(jarIn
);
103 // won't work if distribution artifact is not listed
104 for (int i
= 0; i
< artifacts
.size(); i
++) {
105 OsgiArtifact osgiArtifact
= (OsgiArtifact
) artifacts
.get(i
);
106 if (osgiArtifact
.getSymbolicName().equals(symbolicName
)
107 && osgiArtifact
.getVersion().equals(version
)) {
108 String relativeUrl
= osgiArtifact
.getRelativeUrl();
109 if (url
.endsWith(relativeUrl
)) {
110 baseUrl
= url
.substring(0, url
.length()
111 - osgiArtifact
.getRelativeUrl().length());
116 } catch (Exception e
) {
117 throw new OsgiBootException("Cannot list URLs from " + url
, e
);
122 } catch (IOException e
) {
128 protected List
/* <OsgiArtifact> */listArtifacts(InputStream in
) {
129 List osgiArtifacts
= new ArrayList();
130 BufferedReader reader
= null;
132 reader
= new BufferedReader(new InputStreamReader(in
));
134 while ((line
= reader
.readLine()) != null) {
135 StringTokenizer st
= new StringTokenizer(line
, separator
);
136 String moduleName
= st
.nextToken();
137 String moduleVersion
= st
.nextToken();
138 String relativeUrl
= st
.nextToken();
139 osgiArtifacts
.add(new OsgiArtifact(moduleName
, moduleVersion
,
142 } catch (Exception e
) {
143 throw new OsgiBootException("Cannot list artifacts", e
);
145 return osgiArtifacts
;
148 /** Convenience method */
149 public static DistributionBundle
processUrl(String baseUrl
,
150 String realtiveUrl
) {
151 DistributionBundle distributionBundle
= new DistributionBundle(baseUrl
,
153 distributionBundle
.processUrl();
154 return distributionBundle
;
158 * List full URLs of the bunmdles, based on base URL, usable directly for
161 public List
/* <String> */listUrls() {
163 throw new OsgiBootException("Base URL is not set");
165 if (artifacts
== null)
166 throw new OsgiBootException("Artifact list not initialized");
168 List
/* <String> */urls
= new ArrayList();
169 for (int i
= 0; i
< artifacts
.size(); i
++) {
170 OsgiArtifact osgiArtifact
= (OsgiArtifact
) artifacts
.get(i
);
171 urls
.add(baseUrl
+ osgiArtifact
.getRelativeUrl());
176 public void setBaseUrl(String baseUrl
) {
177 this.baseUrl
= baseUrl
;
180 /** Separator used to parse the tabular file */
181 public void setSeparator(String modulesUrlSeparator
) {
182 this.separator
= modulesUrlSeparator
;
185 public String
getRelativeUrl() {
189 /** One of the listed artifact */
190 protected static class OsgiArtifact
{
191 private final String symbolicName
;
192 private final String version
;
193 private final String relativeUrl
;
195 public OsgiArtifact(String symbolicName
, String version
,
196 String relativeUrl
) {
198 this.symbolicName
= symbolicName
;
199 this.version
= version
;
200 this.relativeUrl
= relativeUrl
;
203 public String
getSymbolicName() {
207 public String
getVersion() {
211 public String
getRelativeUrl() {