-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
package org.argeo.osgi.boot;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
private String baseUrl;
/** can be null */
private String relativeUrl;
+ private String localCache;
private List<OsgiArtifact> artifacts;
this.url = url;
}
- public DistributionBundle(String baseUrl, String relativeUrl) {
+ public DistributionBundle(String baseUrl, String relativeUrl, String localCache) {
if (baseUrl == null || !baseUrl.endsWith("/"))
throw new OsgiBootException("Base url " + baseUrl + " badly formatted");
if (relativeUrl.startsWith("http") || relativeUrl.startsWith("file:"))
this.url = constructUrl(baseUrl, relativeUrl);
this.baseUrl = baseUrl;
this.relativeUrl = relativeUrl;
+ this.localCache = localCache;
}
protected String constructUrl(String baseUrl, String relativeUrl) {
if (res.size() == 0)
throw new OsgiBootException("No file matching " + relativeUrl + " found in " + baseUrl);
return res.get(res.firstKey()).toUri().toString();
- // try (DirectoryStream<Path> ds =
- // Files.newDirectoryStream(basePath)) {
- // Path res = null;
- // for (Path path : ds) {
- // if (pm.matches(path)) {
- // if (res == null)
- // res = path;
- // else
- // throw new OsgiBootException(
- // "More than one file matching " + relativeUrl + " found in " +
- // baseUrl);
- // }
- // }
- // if (res == null)
- // throw new OsgiBootException("No file matching " + relativeUrl
- // + " found in " + baseUrl);
- // return res.toUri().toURL().toString();
- // // Iterator<Path> it = ds.iterator();
- // // if (!it.hasNext())
- // // throw new OsgiBootException("No file matching " +
- // // relativeUrl + " found in " + baseUrl);
- // // Path distributionBundlePath = it.next();
- // // if (it.hasNext())// TODO implement version ordered
- // // throw new OsgiBootException(
- // // "More than one file matching " + relativeUrl + " found in
- // // " + baseUrl);
- // // return distributionBundlePath.toUri().toURL().toString();
- // }
} else {
return baseUrl + relativeUrl;
}
}
}
- // public static void main(String[] args) {
- // try {
- // String baseUrl = "file:///home/mbaudier/.m2/repository/";
- // String relativeUrl =
- // "org/argeo/commons/org.argeo.dep.cms.node/2.1.*-SNAPSHOT/org.argeo.dep.cms.node-2.1.*-SNAPSHOT.jar";
- // Path basePath = Paths.get(new URI(baseUrl));
- // PathMatcher pm = basePath.getFileSystem().getPathMatcher("glob:" +
- // relativeUrl);
- //
- // try (DirectoryStream<Path> ds = Files.newDirectoryStream(basePath, "**"))
- // {
- // for (Path path : ds) {
- // System.out.println(path);
- // }
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
-
public void processUrl() {
JarInputStream jarIn = null;
try {
URL u = new URL(url);
+
+ // local cache
+ URI localUri = new URI(localCache + relativeUrl);
+ Path localPath = Paths.get(localUri);
+ if (Files.exists(localPath))
+ u = localUri.toURL();
jarIn = new JarInputStream(u.openStream());
// meta data
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = null;
- while ((line = reader.readLine()) != null) {
+ lines: while ((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, separator);
String moduleName = st.nextToken();
String moduleVersion = st.nextToken();
String relativeUrl = st.nextToken();
+ if (relativeUrl.endsWith(".pom"))
+ continue lines;
osgiArtifacts.add(new OsgiArtifact(moduleName, moduleVersion, relativeUrl));
}
} catch (Exception e) {
}
/** Convenience method */
- public static DistributionBundle processUrl(String baseUrl, String realtiveUrl) {
- DistributionBundle distributionBundle = new DistributionBundle(baseUrl, realtiveUrl);
+ public static DistributionBundle processUrl(String baseUrl, String relativeUrl, String localCache) {
+ DistributionBundle distributionBundle = new DistributionBundle(baseUrl, relativeUrl, localCache);
distributionBundle.processUrl();
return distributionBundle;
}
List<String> urls = new ArrayList<String>();
for (int i = 0; i < artifacts.size(); i++) {
OsgiArtifact osgiArtifact = (OsgiArtifact) artifacts.get(i);
- urls.add(baseUrl + osgiArtifact.getRelativeUrl());
+ // local cache
+ URI localUri;
+ try {
+ localUri = new URI(localCache + relativeUrl);
+ } catch (URISyntaxException e) {
+ OsgiBootUtils.warn(e.getMessage());
+ localUri = null;
+ }
+ Version version = new Version(osgiArtifact.getVersion());
+ if (localUri != null && Files.exists(Paths.get(localUri)) && version.getQualifier() != null
+ && version.getQualifier().startsWith("SNAPSHOT")) {
+ urls.add(localCache + osgiArtifact.getRelativeUrl());
+ } else {
+ urls.add(baseUrl + osgiArtifact.getRelativeUrl());
+ }
}
return urls;
}