Repository: karaf-cave Updated Branches: refs/heads/master 8411c7447 -> 1fc37159f
[KARAF-3712] Make cave usable with Karaf 4 (first attempt, still in progress) Project: http://git-wip-us.apache.org/repos/asf/karaf-cave/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cave/commit/c610b479 Tree: http://git-wip-us.apache.org/repos/asf/karaf-cave/tree/c610b479 Diff: http://git-wip-us.apache.org/repos/asf/karaf-cave/diff/c610b479 Branch: refs/heads/master Commit: c610b47981cf834e58b20502ea1a7e7c567b1f98 Parents: 8411c74 Author: Guillaume Nodet <[email protected]> Authored: Thu Apr 30 09:56:47 2015 +0200 Committer: Guillaume Nodet <[email protected]> Committed: Thu Apr 30 09:56:47 2015 +0200 ---------------------------------------------------------------------- pom.xml | 15 +- .../karaf/cave/server/api/CaveRepository.java | 6 +- server/http/pom.xml | 4 - .../karaf/cave/server/http/WrapperServlet.java | 102 ++++++------ server/storage/pom.xml | 16 +- .../cave/server/storage/CaveRepositoryImpl.java | 154 +++++++++++++------ .../storage/CaveRepositoryServiceImpl.java | 14 +- .../cave/server/storage/OsgiRepository.java | 51 ++++++ .../OSGI-INF/blueprint/cave-storage.xml | 3 - 9 files changed, 226 insertions(+), 139 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 875f5a0..2dc9634 100644 --- a/pom.xml +++ b/pom.xml @@ -36,11 +36,10 @@ <properties> <commons-io.version>2.4</commons-io.version> - <cxf.version>3.0.0</cxf.version> - <felix.bundlerepository.version>1.6.6</felix.bundlerepository.version> + <cxf.version>3.1.0-SNAPSHOT</cxf.version> <httpclient.version>4.3.4</httpclient.version> <jsoup.version>1.7.3</jsoup.version> - <karaf.version>3.0.2</karaf.version> + <karaf.version>4.0.0-SNAPSHOT</karaf.version> <osgi.version>5.0.0</osgi.version> <wagon.version>1.0</wagon.version> </properties> @@ -85,9 +84,9 @@ <version>${karaf.version}</version> </dependency> <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.bundlerepository</artifactId> - <version>${felix.bundlerepository.version}</version> + <groupId>org.apache.karaf.features</groupId> + <artifactId>org.apache.karaf.features.core</artifactId> + <version>${karaf.version}</version> </dependency> <dependency> <groupId>org.jsoup</groupId> @@ -155,8 +154,8 @@ <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> - <source>1.6</source> - <target>1.6</target> + <source>1.7</source> + <target>1.7</target> </configuration> </plugin> <plugin> http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java ---------------------------------------------------------------------- diff --git a/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java b/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java index 025e530..d7bff20 100644 --- a/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java +++ b/server/api/src/main/java/org/apache/karaf/cave/server/api/CaveRepository.java @@ -118,13 +118,13 @@ public abstract class CaveRepository { public abstract void populate(URL url, String filter, boolean update) throws Exception; /** - * Return the output stream of the resource at the given URI. + * Return an URL for the resource at the given URI. * * @param uri the resource URI. - * @return the output stream of the resource. + * @return the URL for the resource. * @throws Exception in case of read failure. */ - //public abstract OutputStream getResourceByUri(String uri) throws Exception; + public abstract URL getResourceByUri(String uri) throws Exception; /** * Return the output stream of the resource identified by the given ID. http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/http/pom.xml ---------------------------------------------------------------------- diff --git a/server/http/pom.xml b/server/http/pom.xml index 2ec34d6..b248c56 100644 --- a/server/http/pom.xml +++ b/server/http/pom.xml @@ -41,10 +41,6 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.bundlerepository</artifactId> - </dependency> - <dependency> <groupId>org.apache.karaf.cave.server</groupId> <artifactId>org.apache.karaf.cave.server.api</artifactId> <version>${project.version}</version> http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/http/src/main/java/org/apache/karaf/cave/server/http/WrapperServlet.java ---------------------------------------------------------------------- diff --git a/server/http/src/main/java/org/apache/karaf/cave/server/http/WrapperServlet.java b/server/http/src/main/java/org/apache/karaf/cave/server/http/WrapperServlet.java index e24fcbf..2fb8914 100644 --- a/server/http/src/main/java/org/apache/karaf/cave/server/http/WrapperServlet.java +++ b/server/http/src/main/java/org/apache/karaf/cave/server/http/WrapperServlet.java @@ -16,8 +16,6 @@ */ package org.apache.karaf.cave.server.http; -import org.apache.felix.bundlerepository.RepositoryAdmin; -import org.apache.felix.bundlerepository.Resource; import org.apache.karaf.cave.server.api.CaveRepository; import org.apache.karaf.cave.server.api.CaveRepositoryService; import org.osgi.framework.BundleContext; @@ -57,17 +55,24 @@ public class WrapperServlet extends HttpServlet { public void doIt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // lookup on the OBR RepositoryAdmin service - ServiceReference repositoryAdminReference = bundleContext.getServiceReference(RepositoryAdmin.class.getName()); - if (repositoryAdminReference == null) { - throw new ServletException("OBR repository admin service is not available"); + ServiceReference caveRepositoryServiceReference = bundleContext.getServiceReference(CaveRepositoryService.class.getName()); + if (caveRepositoryServiceReference == null) { + throw new ServletException("CaveRepositoryService is not available"); } - RepositoryAdmin repositoryAdmin = (RepositoryAdmin) bundleContext.getService(repositoryAdminReference); - if (repositoryAdmin == null) { - bundleContext.ungetService(repositoryAdminReference); - throw new ServletException("OBR repository admin service is not available"); + CaveRepositoryService caveRepositoryService = (CaveRepositoryService) bundleContext.getService(caveRepositoryServiceReference); + if (caveRepositoryService == null) { + throw new ServletException("CaveRepositoryService is not available"); } + try { + doIt2(caveRepositoryService, request, response); + } finally { + bundleContext.ungetService(caveRepositoryServiceReference); + } + } + + private void doIt2(CaveRepositoryService caveRepositoryService, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String uri = request.getPathInfo(); // remove the starting / @@ -75,21 +80,14 @@ public class WrapperServlet extends HttpServlet { // listing the repositories if (request.getParameter("repositories") != null) { - ServiceReference caveRepositoryServiceReference = bundleContext.getServiceReference(CaveRepositoryService.class.getName()); - if (caveRepositoryServiceReference != null) { - CaveRepositoryService caveRepositoryService = (CaveRepositoryService) bundleContext.getService(caveRepositoryServiceReference); - if (caveRepositoryService != null) { - CaveRepository[] caveRepositories = caveRepositoryService.getRepositories(); - response.setContentType("text/plain"); - PrintWriter writer = response.getWriter(); - for (CaveRepository caveRepository : caveRepositories) { - writer.println(caveRepository.getName()); - } - writer.flush(); - writer.close(); - } - bundleContext.ungetService(caveRepositoryServiceReference); + CaveRepository[] caveRepositories = caveRepositoryService.getRepositories(); + response.setContentType("text/plain"); + PrintWriter writer = response.getWriter(); + for (CaveRepository caveRepository : caveRepositories) { + writer.println(caveRepository.getName()); } + writer.flush(); + writer.close(); return; } @@ -103,52 +101,44 @@ public class WrapperServlet extends HttpServlet { int index = uri.indexOf("-repository.xml"); String caveRepositoryName = uri.substring(0, index); - ServiceReference caveRepositoryServiceReference = bundleContext.getServiceReference(CaveRepositoryService.class.getName()); - if (caveRepositoryServiceReference != null) { - CaveRepositoryService caveRepositoryService = (CaveRepositoryService) bundleContext.getService(caveRepositoryServiceReference); - if (caveRepositoryService != null) { - CaveRepository caveRepository = caveRepositoryService.getRepository(caveRepositoryName); - if (caveRepository != null) { - url = caveRepository.getRepositoryXml(); - response.setContentType("text/xml"); + CaveRepository caveRepository = caveRepositoryService.getRepository(caveRepositoryName); + if (caveRepository == null) { + throw new ServletException("No repository found for name " + caveRepositoryName); + } + url = caveRepository.getRepositoryXml(); + response.setContentType("text/xml"); + } else { + for (CaveRepository repository : caveRepositoryService.getRepositories()) { + URL resourceUrl = repository.getResourceByUri(uri); + if (resourceUrl != null) { + if (url != null) { + throw new ServletException("Multiple resources found with URI " + uri); + } else { + url = resourceUrl; } } - bundleContext.ungetService(caveRepositoryServiceReference); } - } else { - Resource[] resources = repositoryAdmin.discoverResources("(uri=*" + uri + ")"); - if (resources.length == 0) { + if (url == null) { throw new ServletException("No resource found with URI " + uri); } - if (resources.length > 1) { - throw new ServletException("Multiple resources found with URI " + uri); - } - // I have exactly one resource associated to the URI - url = new URL(resources[0].getURI()); response.setContentType("application/java-archive"); } - if (url != null) { - // send the resource content to the HTTP response - InputStream inputStream = url.openStream(); - OutputStream outputStream = response.getOutputStream(); - int c; - while ((c = inputStream.read()) >= 0) { - outputStream.write(c); - } - inputStream.close(); - outputStream.flush(); - outputStream.close(); - } else { - throw new ServletException("No resource look-up provided"); + // send the resource content to the HTTP response + InputStream inputStream = url.openStream(); + OutputStream outputStream = response.getOutputStream(); + int c; + while ((c = inputStream.read()) >= 0) { + outputStream.write(c); } + inputStream.close(); + outputStream.flush(); + outputStream.close(); } catch (ServletException servletException) { throw servletException; } catch (Exception e) { throw new ServletException(e); - } finally { - bundleContext.ungetService(repositoryAdminReference); } } http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/storage/pom.xml ---------------------------------------------------------------------- diff --git a/server/storage/pom.xml b/server/storage/pom.xml index b1fb9ed..005f97e 100644 --- a/server/storage/pom.xml +++ b/server/storage/pom.xml @@ -44,8 +44,8 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>org.apache.felix</groupId> - <artifactId>org.apache.felix.bundlerepository</artifactId> + <groupId>org.apache.karaf.features</groupId> + <artifactId>org.apache.karaf.features.core</artifactId> </dependency> <dependency> <groupId>org.jsoup</groupId> @@ -59,6 +59,10 @@ <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> </dependency> + <dependency> + <groupId>org.osgi</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> </dependencies> <build> @@ -83,13 +87,17 @@ !org.apache.felix.shell, !org.apache.felix.bundlerepository.impl.wrapper, !org.osgi.service.obr, - !javax.xml.stream + org.osgi.resource*, + org.osgi.service.repository*, + javax.xml.stream*, </Import-Package> <Private-Package> org.kxml2.io, org.xmlpull.v1, org.apache.felix.utils*, - org.apache.felix.bundlerepository.impl* + org.apache.felix.bundlerepository.impl*, + org.apache.karaf.features.internal.repository, + org.apache.karaf.features.internal.resolver </Private-Package> </instructions> </configuration> http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java ---------------------------------------------------------------------- diff --git a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java index 1be4e0a..f0c4e06 100644 --- a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java +++ b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryImpl.java @@ -16,29 +16,44 @@ */ package org.apache.karaf.cave.server.storage; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.jar.Attributes; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + import org.apache.commons.io.FileUtils; -import org.apache.felix.bundlerepository.Resource; -import org.apache.felix.bundlerepository.impl.DataModelHelperImpl; -import org.apache.felix.bundlerepository.impl.RepositoryImpl; -import org.apache.felix.bundlerepository.impl.ResourceImpl; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.karaf.cave.server.api.CaveRepository; +import org.apache.karaf.features.internal.resolver.ResolverUtil; +import org.apache.karaf.features.internal.resolver.ResourceBuilder; +import org.apache.karaf.features.internal.resolver.ResourceImpl; import org.jsoup.Jsoup; import org.jsoup.UnsupportedMimeTypeException; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; +import org.osgi.resource.Capability; +import org.osgi.resource.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.net.URL; +import static java.util.jar.JarFile.MANIFEST_NAME; +import static org.osgi.service.repository.ContentNamespace.CAPABILITY_URL_ATTRIBUTE; +import static org.osgi.service.repository.ContentNamespace.CONTENT_NAMESPACE; /** * Default implementation of a Cave repository. @@ -47,7 +62,7 @@ public class CaveRepositoryImpl extends CaveRepository { private final static Logger LOGGER = LoggerFactory.getLogger(CaveRepositoryImpl.class); - private RepositoryImpl obrRepository; + private OsgiRepository repository; public CaveRepositoryImpl(String name, String location, boolean scan) throws Exception { super(); @@ -72,8 +87,8 @@ public class CaveRepositoryImpl extends CaveRepository { LOGGER.debug("Cave repository {} location has been created.", this.getName()); LOGGER.debug(locationFile.getAbsolutePath()); } - obrRepository = new RepositoryImpl(); - obrRepository.setName(this.getName()); + repository = new OsgiRepository(); + repository.setName(this.getName()); } /** @@ -84,7 +99,7 @@ public class CaveRepositoryImpl extends CaveRepository { private void generateRepositoryXml() throws Exception { File repositoryXml = this.getRepositoryXmlFile(); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(repositoryXml)); - new DataModelHelperImpl().writeRepository(obrRepository, writer); + repository.writeRepository(writer); writer.flush(); writer.close(); } @@ -97,9 +112,9 @@ public class CaveRepositoryImpl extends CaveRepository { */ private void addResource(ResourceImpl resource) throws Exception { if (resource != null) { - this.useResourceRelativeUri(resource); - obrRepository.addResource(resource); - obrRepository.setLastModified(System.currentTimeMillis()); + useResourceRelativeUri(resource); + repository.addResource(resource); + repository.setIncrement(System.currentTimeMillis()); } } @@ -111,26 +126,27 @@ public class CaveRepositoryImpl extends CaveRepository { */ public void upload(URL url) throws Exception { LOGGER.debug("Upload new artifact from {}", url); + // TODO: this is problematic if receiving multiple requests at the same time String artifactName = "artifact-" + System.currentTimeMillis(); File temp = new File(new File(this.getLocation()), artifactName); FileUtils.copyURLToFile(url, temp); // update the repository.xml - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(temp.toURI().toURL()); + ResourceImpl resource = createResource(temp.toURI().toURL()); if (resource == null) { temp.delete(); LOGGER.warn("The {} artifact source is not a valid OSGi bundle", url); throw new IllegalArgumentException("The " + url.toString() + " artifact source is not a valid OSGi bundle"); } - File destination = new File(new File(this.getLocation()), resource.getSymbolicName() + "-" + resource.getVersion() + ".jar"); + File destination = new File(new File(getLocation()), ResolverUtil.getSymbolicName(resource) + "-" + ResolverUtil.getVersion(resource) + ".jar"); if (destination.exists()) { temp.delete(); LOGGER.warn("The {} artifact is already present in the Cave repository", url); throw new IllegalArgumentException("The " + url.toString() + " artifact is already present in the Cave repository"); } FileUtils.moveFile(temp, destination); - resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); - this.addResource(resource); - this.generateRepositoryXml(); + resource = createResource(destination.toURI().toURL()); + addResource(resource); + generateRepositoryXml(); } /** @@ -139,8 +155,8 @@ public class CaveRepositoryImpl extends CaveRepository { * @throws Exception in case of scan failure. */ public void scan() throws Exception { - obrRepository = new RepositoryImpl(); - obrRepository.setName(this.getName()); + repository = new OsgiRepository(); + repository.setName(this.getName()); this.scan(new File(this.getLocation())); this.generateRepositoryXml(); } @@ -162,8 +178,8 @@ public class CaveRepositoryImpl extends CaveRepository { try { URL bundleUrl = entry.toURI().toURL(); if (isPotentialBundle(bundleUrl.toString())) { - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(bundleUrl); - this.addResource(resource); + ResourceImpl resource = createResource(bundleUrl); + addResource(resource); } } catch (IllegalArgumentException e) { LOGGER.warn(e.getMessage()); @@ -230,10 +246,10 @@ public class CaveRepositoryImpl extends CaveRepository { } else { try { if ((filter == null) || (entry.toURI().toURL().toString().matches(filter))) { - Resource resource = new DataModelHelperImpl().createResource(entry.toURI().toURL()); + Resource resource = createResource(entry.toURI().toURL()); if (resource != null) { - obrRepository.addResource(resource); - obrRepository.setLastModified(System.currentTimeMillis()); + repository.addResource(resource); + repository.setIncrement(System.currentTimeMillis()); } } } catch (IllegalArgumentException e) { @@ -242,6 +258,40 @@ public class CaveRepositoryImpl extends CaveRepository { } } + private ResourceImpl createResource(URL url) throws BundleException, IOException { + return createResource(url, url.toExternalForm()); + } + + private ResourceImpl createResource(URL url, String uri) throws BundleException, IOException { + Map<String, String> headers = getHeaders(url); + if (headers.get(Constants.BUNDLE_MANIFESTVERSION) == null) { + LOGGER.warn("The {} artifact source is not a valid OSGi bundle", url); + throw new IllegalArgumentException("The " + url.toString() + " artifact source is not a valid OSGi bundle"); + } + return ResourceBuilder.build(uri, headers); + } + + Map<String, String> getHeaders(URL url) throws IOException { + InputStream is = url.openStream(); + try { + ZipInputStream zis = new ZipInputStream(is); + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + if (MANIFEST_NAME.equals(entry.getName())) { + Attributes attributes = new Manifest(zis).getMainAttributes(); + Map<String, String> headers = new HashMap<String, String>(); + for (Map.Entry attr : attributes.entrySet()) { + headers.put(attr.getKey().toString(), attr.getValue().toString()); + } + return headers; + } + } + } finally { + is.close(); + } + throw new IllegalArgumentException("Resource " + url + " does not contain a manifest"); + } + /** * Proxy a HTTP URL locally. * @@ -263,10 +313,10 @@ public class CaveRepositoryImpl extends CaveRepository { // I have a jar/binary, potentially a resource try { if ((filter == null) || (url.matches(filter))) { - Resource resource = new DataModelHelperImpl().createResource(new URL(url)); + Resource resource = createResource(new URL(url)); if (resource != null) { - obrRepository.addResource(resource); - obrRepository.setLastModified(System.currentTimeMillis()); + repository.addResource(resource); + repository.setIncrement(System.currentTimeMillis()); } } } catch (IllegalArgumentException e) { @@ -344,16 +394,16 @@ public class CaveRepositoryImpl extends CaveRepository { } else { try { if ((filter == null) || (filesystem.toURI().toURL().toString().matches(filter))) { - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(filesystem.toURI().toURL()); + ResourceImpl resource = createResource(filesystem.toURI().toURL()); if (resource != null) { // copy the resource File destination = new File(new File(this.getLocation()), filesystem.getName()); LOGGER.debug("Copy from {} to {}", filesystem.getAbsolutePath(), destination.getAbsolutePath()); FileUtils.copyFile(filesystem, destination); if (update) { - resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); - LOGGER.debug("Update the OBR metadata with {}", resource.getId()); - this.addResource(resource); + resource = createResource(destination.toURI().toURL()); + LOGGER.debug("Update the OBR metadata with {}-{}", ResolverUtil.getSymbolicName(resource), ResolverUtil.getVersion(resource)); + addResource(resource); } } } @@ -385,21 +435,21 @@ public class CaveRepositoryImpl extends CaveRepository { // I have a jar/binary, potentially a resource try { if ((filter == null) || (url.matches(filter))) { - ResourceImpl resource = (ResourceImpl) new DataModelHelperImpl().createResource(new URL(url)); + ResourceImpl resource = createResource(new URL(url)); if (resource != null) { LOGGER.debug("Copy {} into the Cave repository storage", url); int index = url.lastIndexOf("/"); if (index > 0) { url = url.substring(index); } - File destination = new File(new File(this.getLocation()), url); + File destination = new File(new File(getLocation()), url); FileOutputStream outputStream = new FileOutputStream(destination); entity.writeTo(outputStream); outputStream.flush(); outputStream.close(); if (update) { - resource = (ResourceImpl) new DataModelHelperImpl().createResource(destination.toURI().toURL()); - LOGGER.debug("Update OBR metadata with {}", resource.getId()); + resource = createResource(destination.toURI().toURL()); + LOGGER.debug("Update OBR metadata with {}-{}", ResolverUtil.getSymbolicName(resource), ResolverUtil.getVersion(resource)); this.addResource(resource); } } @@ -416,7 +466,7 @@ public class CaveRepositoryImpl extends CaveRepository { for (int i = 1; i < links.size(); i++) { Element link = links.get(i); String absoluteHref = link.attr("abs:href"); - this.populateFromHttp(absoluteHref, filter, update); + populateFromHttp(absoluteHref, filter, update); } } } @@ -430,15 +480,20 @@ public class CaveRepositoryImpl extends CaveRepository { * @throws Exception in cave of URI conversion failure. */ private void useResourceRelativeUri(ResourceImpl resource) throws Exception { - String resourceURI = resource.getURI(); - String locationURI = "file:" + this.getLocation(); - LOGGER.debug("Converting resource URI {} relatively to repository URI {}", resourceURI, locationURI); - if (resourceURI.startsWith(locationURI)) { - resourceURI = resourceURI.substring(locationURI.length() + 1); - LOGGER.debug("Resource URI converted to " + resourceURI); - resource.put(Resource.URI, resourceURI); + for (Capability cap : resource.getCapabilities(null)) { + if (cap.getNamespace().equals(CONTENT_NAMESPACE)) { + String resourceURI = cap.getAttributes().get(CAPABILITY_URL_ATTRIBUTE).toString(); + String locationURI = "file:" + getLocation(); + LOGGER.debug("Converting resource URI {} relatively to repository URI {}", resourceURI, locationURI); + if (resourceURI.startsWith(locationURI)) { + resourceURI = resourceURI.substring(locationURI.length() + 1); + LOGGER.debug("Resource URI converted to " + resourceURI); + // This is a bit hacky, but the map is not read only + cap.getAttributes().put(CAPABILITY_URL_ATTRIBUTE, resourceURI); + } + break; + } } - } /** @@ -451,9 +506,10 @@ public class CaveRepositoryImpl extends CaveRepository { return new File(new File(this.getLocation()), "repository.xml"); } - public void getResourceByUri(String uri) { + public URL getResourceByUri(String uri) { // construct the file starting from the repository URI - + // TODO + throw new RuntimeException("Not implemented yet"); } /** http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryServiceImpl.java ---------------------------------------------------------------------- diff --git a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryServiceImpl.java b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryServiceImpl.java index eb1ec8a..557b51f 100644 --- a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryServiceImpl.java +++ b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/CaveRepositoryServiceImpl.java @@ -16,7 +16,6 @@ */ package org.apache.karaf.cave.server.storage; -import org.apache.felix.bundlerepository.RepositoryAdmin; import org.apache.karaf.cave.server.api.CaveRepository; import org.apache.karaf.cave.server.api.CaveRepositoryService; import org.slf4j.Logger; @@ -37,7 +36,6 @@ public class CaveRepositoryServiceImpl implements CaveRepositoryService { public static final String STORAGE_FILE = "repositories.properties"; private File storageLocation; - private RepositoryAdmin repositoryAdmin; private Map<String, CaveRepository> repositories = new HashMap<String, CaveRepository>(); @@ -49,14 +47,6 @@ public class CaveRepositoryServiceImpl implements CaveRepositoryService { this.storageLocation = storageLocation; } - public RepositoryAdmin getRepositoryAdmin() { - return this.repositoryAdmin; - } - - public void setRepositoryAdmin(RepositoryAdmin repositoryAdmin) { - this.repositoryAdmin = repositoryAdmin; - } - /** * Create a Cave repository. * @@ -100,7 +90,7 @@ public class CaveRepositoryServiceImpl implements CaveRepositoryService { if (repository == null) { throw new IllegalArgumentException("Cave repository " + name + " doesn't exist"); } - repositoryAdmin.removeRepository(repository.getRepositoryXml().toString()); + // TODO: unregister Repository service save(); } @@ -145,7 +135,7 @@ public class CaveRepositoryServiceImpl implements CaveRepositoryService { if (caveRepository == null) { throw new IllegalArgumentException("Cave repository " + name + " doesn't exist"); } - repositoryAdmin.addRepository(caveRepository.getRepositoryXml()); + // TODO: register the Repository service } /** http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/storage/src/main/java/org/apache/karaf/cave/server/storage/OsgiRepository.java ---------------------------------------------------------------------- diff --git a/server/storage/src/main/java/org/apache/karaf/cave/server/storage/OsgiRepository.java b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/OsgiRepository.java new file mode 100644 index 0000000..4407086 --- /dev/null +++ b/server/storage/src/main/java/org/apache/karaf/cave/server/storage/OsgiRepository.java @@ -0,0 +1,51 @@ +package org.apache.karaf.cave.server.storage; + +import java.io.OutputStreamWriter; +import java.util.UUID; + +import javax.xml.stream.XMLStreamException; + +import org.apache.karaf.features.internal.repository.StaxParser; +import org.apache.karaf.features.internal.repository.XmlRepository; +import org.osgi.resource.Resource; + +public class OsgiRepository extends XmlRepository { + + StaxParser.XmlRepository repository; + + public OsgiRepository() { + this(UUID.randomUUID().toString()); + repository = new StaxParser.XmlRepository(); + String url = getUrl(); + getLoaders().put(url, new XmlLoader(url, repository)); + } + + public OsgiRepository(String url) { + super(url); + } + + public void addResource(Resource resource) { + repository.resources.add(resource); + super.addResource(resource); + } + + public String getName() { + return repository.name; + } + + public void setName(String name) { + repository.name = name; + } + + public long getIncrement() { + return repository.increment; + } + + public void setIncrement(long increment) { + repository.increment = increment; + } + + public void writeRepository(OutputStreamWriter writer) throws XMLStreamException { + StaxParser.write(repository, writer); + } +} http://git-wip-us.apache.org/repos/asf/karaf-cave/blob/c610b479/server/storage/src/main/resources/OSGI-INF/blueprint/cave-storage.xml ---------------------------------------------------------------------- diff --git a/server/storage/src/main/resources/OSGI-INF/blueprint/cave-storage.xml b/server/storage/src/main/resources/OSGI-INF/blueprint/cave-storage.xml index d626174..1abcf71 100644 --- a/server/storage/src/main/resources/OSGI-INF/blueprint/cave-storage.xml +++ b/server/storage/src/main/resources/OSGI-INF/blueprint/cave-storage.xml @@ -31,11 +31,8 @@ <bean id="caveRepositoryService" class="org.apache.karaf.cave.server.storage.CaveRepositoryServiceImpl" init-method="init"> <property name="storageLocation" value="${storage.location}"/> - <property name="repositoryAdmin" ref="repositoryAdmin"/> </bean> - <reference id="repositoryAdmin" interface="org.apache.felix.bundlerepository.RepositoryAdmin"/> - <service ref="caveRepositoryService" interface="org.apache.karaf.cave.server.api.CaveRepositoryService"/> <!-- use the cm of Cave filesystem backend -->
