]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/src/org/argeo/osgi/boot/DistributionBundle.java
Create a demo user at initialisation
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / src / org / argeo / osgi / boot / DistributionBundle.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.osgi.boot;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.net.URL;
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;
29
30 import org.osgi.framework.Constants;
31
32 /**
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.
38 */
39 @SuppressWarnings({ "rawtypes", "unchecked" })
40 public class DistributionBundle {
41 private final static String INDEX_FILE_NAME = "modularDistribution.csv";
42
43 private final String url;
44
45 private Manifest manifest;
46 private String symbolicName;
47 private String version;
48
49 /** can be null */
50 private String baseUrl;
51 /** can be null */
52 private String relativeUrl;
53
54 private List/* <OsgiArtifact> */artifacts;
55
56 private String separator = ",";
57
58 public DistributionBundle(String url) {
59 this.url = url;
60 }
61
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;
72 }
73
74 public void processUrl() {
75 JarInputStream jarIn = null;
76 try {
77 URL u = new URL(url);
78 jarIn = new JarInputStream(u.openStream());
79
80 // meta data
81 manifest = jarIn.getManifest();
82 symbolicName = manifest.getMainAttributes().getValue(
83 Constants.BUNDLE_SYMBOLICNAME);
84 version = manifest.getMainAttributes().getValue(
85 Constants.BUNDLE_VERSION);
86
87 JarEntry indexEntry;
88 while ((indexEntry = jarIn.getNextJarEntry()) != null) {
89 String entryName = indexEntry.getName();
90 if (entryName.equals(INDEX_FILE_NAME)) {
91 break;
92 }
93 jarIn.closeEntry();
94 }
95
96 // list artifacts
97 if (indexEntry == null)
98 throw new OsgiBootException("No index " + INDEX_FILE_NAME
99 + " in " + url);
100 artifacts = listArtifacts(jarIn);
101 jarIn.closeEntry();
102
103 // find base URL
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());
113 break;
114 }
115 }
116 }
117 } catch (Exception e) {
118 throw new OsgiBootException("Cannot list URLs from " + url, e);
119 } finally {
120 if (jarIn != null)
121 try {
122 jarIn.close();
123 } catch (IOException e) {
124 // silent
125 }
126 }
127 }
128
129 protected List/* <OsgiArtifact> */listArtifacts(InputStream in) {
130 List osgiArtifacts = new ArrayList();
131 BufferedReader reader = null;
132 try {
133 reader = new BufferedReader(new InputStreamReader(in));
134 String line = null;
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,
141 relativeUrl));
142 }
143 } catch (Exception e) {
144 throw new OsgiBootException("Cannot list artifacts", e);
145 }
146 return osgiArtifacts;
147 }
148
149 /** Convenience method */
150 public static DistributionBundle processUrl(String baseUrl,
151 String realtiveUrl) {
152 DistributionBundle distributionBundle = new DistributionBundle(baseUrl,
153 realtiveUrl);
154 distributionBundle.processUrl();
155 return distributionBundle;
156 }
157
158 /**
159 * List full URLs of the bunmdles, based on base URL, usable directly for
160 * download.
161 */
162 public List/* <String> */listUrls() {
163 if (baseUrl == null)
164 throw new OsgiBootException("Base URL is not set");
165
166 if (artifacts == null)
167 throw new OsgiBootException("Artifact list not initialized");
168
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());
173 }
174 return urls;
175 }
176
177 public void setBaseUrl(String baseUrl) {
178 this.baseUrl = baseUrl;
179 }
180
181 /** Separator used to parse the tabular file */
182 public void setSeparator(String modulesUrlSeparator) {
183 this.separator = modulesUrlSeparator;
184 }
185
186 public String getRelativeUrl() {
187 return relativeUrl;
188 }
189
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;
195
196 public OsgiArtifact(String symbolicName, String version,
197 String relativeUrl) {
198 super();
199 this.symbolicName = symbolicName;
200 this.version = version;
201 this.relativeUrl = relativeUrl;
202 }
203
204 public String getSymbolicName() {
205 return symbolicName;
206 }
207
208 public String getVersion() {
209 return version;
210 }
211
212 public String getRelativeUrl() {
213 return relativeUrl;
214 }
215
216 }
217 }