You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2011/08/10 21:28:35 UTC

svn commit: r1156329 - in /karaf/sandbox/jbonofre/cave/trunk/server/backend/src: main/java/org/apache/karaf/cave/server/backend/ main/java/org/apache/karaf/cave/server/backend/impl/ test/java/org/apache/karaf/cave/server/backend/impl/

Author: jbonofre
Date: Wed Aug 10 19:28:35 2011
New Revision: 1156329

URL: http://svn.apache.org/viewvc?rev=1156329&view=rev
Log:
Add Cave Repository service.

Added:
    karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryServiceImpl.java
Modified:
    karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepository.java
    karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepositoryService.java
    karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImpl.java
    karaf/sandbox/jbonofre/cave/trunk/server/backend/src/test/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImplTest.java

Modified: karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepository.java
URL: http://svn.apache.org/viewvc/karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepository.java?rev=1156329&r1=1156328&r2=1156329&view=diff
==============================================================================
--- karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepository.java (original)
+++ karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepository.java Wed Aug 10 19:28:35 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.karaf.cave.server.backend;
 
+import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
 
@@ -33,38 +34,34 @@ public interface CaveRepository {
     String getName();
 
     /**
-     * Get the location (path) of this repository.
+     * Set the name of the repository.
      *
-     * @return the location of this repository.
+     * @param name the name of the repository
      */
-    String getLocation();
+    void setName(String name);
 
     /**
-     * Upload an artifact from the given URL into the repository.
-     * TODO use a Artifact object in place of raw artifact
+     * Get the location (filesystem) of this repository.
      *
-     * @param location  location of the artifact to upload in the repository.
-     * @throws Exception in case of upload failure.
+     * @return the location of this repository.
      */
-    void upload(String location) throws Exception;
+    File getLocation();
 
     /**
-     * Upload an artifact from the given URL into the repository.
-     * TODO use a Artifact object in place of raw artifact
+     * Set the location (filesystem) of this repository.
      *
-     * @param url the URL of the artifact.
-     * @throws Exception in case of upload failure.
+     * @param location the location of this repository
      */
-    void upload(URL url) throws Exception;
+    void setLocation(File location);
 
     /**
-     * Upload an artifact from a given stream into the repository.
+     * Upload an artifact from the given URL into the repository.
      * TODO use a Artifact object in place of raw artifact
      *
-     * @param stream the input stream of the artifact.
+     * @param url the URL of the artifact.
      * @throws Exception in case of upload failure.
      */
-    void upload(InputStream stream) throws Exception;
+    void upload(URL url) throws Exception;
 
     /**
      * Scan the whole repository, reading bundle MANIFEST, etc to update

Modified: karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepositoryService.java
URL: http://svn.apache.org/viewvc/karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepositoryService.java?rev=1156329&r1=1156328&r2=1156329&view=diff
==============================================================================
--- karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepositoryService.java (original)
+++ karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/CaveRepositoryService.java Wed Aug 10 19:28:35 2011
@@ -21,7 +21,7 @@ package org.apache.karaf.cave.server.bac
  */
 public interface CaveRepositoryService {
 
-    CaveRepository createRepository(String name, String location) throws Exception;
+    CaveRepository createRepository(String name) throws Exception;
 
     CaveRepository[] getRepositories();
 

Modified: karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImpl.java
URL: http://svn.apache.org/viewvc/karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImpl.java?rev=1156329&r1=1156328&r2=1156329&view=diff
==============================================================================
--- karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImpl.java (original)
+++ karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImpl.java Wed Aug 10 19:28:35 2011
@@ -1,9 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.karaf.cave.server.backend.impl;
 
+import org.apache.felix.bundlerepository.RepositoryAdmin;
 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.karaf.cave.server.backend.CaveRepository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -19,10 +35,12 @@ public class CaveRepositoryImpl implemen
     private final static Logger LOGGER = LoggerFactory.getLogger(CaveRepositoryImpl.class);
 
     private String name;
-    private String location;
+    private File location;
+
     private RepositoryImpl obrRepository;
+    private RepositoryAdmin repositoryAdmin;
 
-    public CaveRepositoryImpl(String name, String location) throws Exception {
+    public CaveRepositoryImpl(String name, File location) throws Exception {
         this.name = name;
         this.location = location;
 
@@ -34,11 +52,10 @@ public class CaveRepositoryImpl implemen
      */
     private void createRepositoryDirectory() throws Exception {
         LOGGER.debug("Create Karaf Cave repository {} folder.", name);
-        File directory = new File(this.location);
-        if (!directory.exists()) {
-            directory.mkdirs();
+        if (!location.exists()) {
+            location.mkdirs();
             LOGGER.debug("Karaf Cave repository {} location has been created.", name);
-            LOGGER.debug(location);
+            LOGGER.debug(location.getAbsolutePath());
         }
         File repositoryXml = new File(location, "repository.xml");
         if (repositoryXml.exists()) {
@@ -52,88 +69,42 @@ public class CaveRepositoryImpl implemen
         return this.name;
     }
 
-    public String getLocation() {
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public File getLocation() {
         return this.location;
     }
 
-    /**
-     * Update the repository.xml with the artifact at the given URL.
-     *
-     * @param resource the bundle resource
-     * @throws Exception in case of repository.xml update failure.
-     */
-    private void updateRepositoryXml(Resource resource) throws Exception {
-        if (resource != null) {
-            obrRepository.addResource(resource);
-            obrRepository.setLastModified(System.currentTimeMillis());
-            File repositoryXml = new File(location, "repository.xml");
-            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(repositoryXml));
-            new DataModelHelperImpl().writeRepository(obrRepository, writer);
-            writer.flush();
-            writer.close();
-        }
+    public void setLocation(File location) {
+        this.location = location;
     }
 
     /**
-     * Add an artifact into this repository
+     * Generate the repository.xml with the artifact at the given URL.
      *
-     * @param path the location of the raw artifact
-     * @throws Exception in case of upload failure
+     * @throws Exception in case of repository.xml update failure.
      */
-    public void upload(String path) throws Exception {
-        LOGGER.debug("Upload new artifact from {}", location);
-        // copy the file
-        LOGGER.debug("Copy the file from {}", location);
-        File source = new File(path);
-        if (!source.exists()) {
-            throw new IllegalArgumentException("The artifact location " + location + " doesn't exist.");
-        }
-        File destination = new File(location, source.getName());
-        FileInputStream fis = new FileInputStream(source);
-        FileOutputStream fos = new FileOutputStream(destination);
-        byte[] buffer = new byte[1024];
-        int len;
-        while ((len = fis.read(buffer)) > 0) {
-            fos.write(buffer, 0, len);
-        }
-        fis.close();
-        fos.flush();
-        fos.close();
-        // update the repository.xml
-        Resource resource = new DataModelHelperImpl().createResource(destination.toURI().toURL());
-        if (resource == null) {
-            destination.delete();
-            throw new IllegalArgumentException("The " + path + " source is not a valid OSGi bundle");
-        }
-        this.updateRepositoryXml(resource);
+    private void generateRepositoryXml() throws Exception {
+        File repositoryXml = new File(location, "repository.xml");
+        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(repositoryXml));
+        new DataModelHelperImpl().writeRepository(obrRepository, writer);
+        writer.flush();
+        writer.close();
     }
 
     /**
-     * Upload an artifact using a stream.
+     * Add a resource in the OBR repository.
      *
-     * @param stream the artifact stream.
-     * @throws Exception in case of upload failure.
+     * @param resource the resource to add.
+     * @throws Exception in case of failure.
      */
-    public void upload(InputStream stream) throws Exception {
-        LOGGER.debug("Upload new artifact from a stream");
-        String artifactName = "artifact-" + System.currentTimeMillis();
-        File destination = new File(location, artifactName);
-        FileOutputStream fos = new FileOutputStream(destination);
-        byte[] buffer = new byte[1024];
-        int len;
-        while ((len = stream.read(buffer)) > 0) {
-            fos.write(buffer, 0, len);
-        }
-        fos.flush();
-        fos.close();
-        // update the repository.xml
-        Resource resource = new DataModelHelperImpl().createResource(destination.toURI().toURL());
-        if (resource == null) {
-            destination.delete();
-            throw new IllegalArgumentException("The stream source is not a valid OSGi bundle");
+    private void addResource(Resource resource) throws Exception {
+        if (resource != null) {
+            obrRepository.addResource(resource);
+            obrRepository.setLastModified(System.currentTimeMillis());
         }
-        destination.renameTo(new File(location, resource.getSymbolicName() + "-" + resource.getVersion()));
-        this.updateRepositoryXml(resource);
     }
 
     /**
@@ -145,8 +116,8 @@ public class CaveRepositoryImpl implemen
     public void upload(URL url) throws Exception {
         LOGGER.debug("Upload new artifact from {}", url);
         String artifactName = "artifact-" + System.currentTimeMillis();
-        File destination = new File(location, artifactName);
-        FileOutputStream fos = new FileOutputStream(destination);
+        File temp = new File(location, artifactName);
+        FileOutputStream fos = new FileOutputStream(temp);
         InputStream stream = url.openStream();
         byte[] buffer = new byte[1024];
         int len;
@@ -157,13 +128,16 @@ public class CaveRepositoryImpl implemen
         fos.flush();
         fos.close();
         // update the repository.xml
-        Resource resource = new DataModelHelperImpl().createResource(destination.toURI().toURL());
+        Resource resource = new DataModelHelperImpl().createResource(temp.toURI().toURL());
         if (resource == null) {
-            destination.delete();
+            temp.delete();
             throw new IllegalArgumentException("The " + url + " artifact source is not a valid OSGi bundle");
         }
-        destination.renameTo(new File(location, resource.getSymbolicName() + "-" + resource.getVersion()));
-        this.updateRepositoryXml(resource);
+        File destination = new File(location, resource.getSymbolicName() + "-" + resource.getVersion() + ".jar");
+        temp.renameTo(destination);
+        resource = new DataModelHelperImpl().createResource(destination.toURI().toURL());
+        this.addResource(resource);
+        this.generateRepositoryXml();
     }
 
     /**
@@ -172,7 +146,27 @@ public class CaveRepositoryImpl implemen
      * @throws Exception in case of scan failure.
      */
     public void scan() throws Exception {
-        // TODO
+        this.scan(location);
+        this.generateRepositoryXml();
+    }
+
+    /**
+     * Recursive method to traverse all file in the repository.
+     *
+     * @param entry the
+     * @throws Exception
+     */
+    private void scan(File entry) throws Exception {
+        if (entry.isDirectory()) {
+            File[] children = entry.listFiles();
+            for (int i = 0; i < children.length; i++) {
+                scan(children[i]);
+            }
+        } else {
+            // populate the repository
+            Resource resource = new DataModelHelperImpl().createResource(entry.toURI().toURL());
+            this.addResource(resource);
+        }
     }
 
     /**
@@ -181,7 +175,8 @@ public class CaveRepositoryImpl implemen
      * @throws Exception in case of register failure.
      */
     public void register() throws Exception {
-        // TODO
+        File repositoryXml = new File(location, "repository.xml");
+        repositoryAdmin.addRepository(repositoryXml.toURI().toURL());
     }
 
     /**
@@ -190,8 +185,7 @@ public class CaveRepositoryImpl implemen
      * @throws Exception in case of destroy failure.
      */
     public void destroy() throws Exception {
-        File storage = new File(location);
-        storage.delete();
+        location.delete();
     }
 
 }

Added: karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryServiceImpl.java?rev=1156329&view=auto
==============================================================================
--- karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryServiceImpl.java (added)
+++ karaf/sandbox/jbonofre/cave/trunk/server/backend/src/main/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryServiceImpl.java Wed Aug 10 19:28:35 2011
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.karaf.cave.server.backend.impl;
+
+import org.apache.karaf.cave.server.backend.CaveRepository;
+import org.apache.karaf.cave.server.backend.CaveRepositoryService;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Default implementation of the Cave Repository Service.
+ */
+public class CaveRepositoryServiceImpl implements CaveRepositoryService {
+
+    private File storageLocation;
+
+    private Map<String, CaveRepository> repositories = new HashMap<String, CaveRepository>();
+
+    public File getStorageLocation() {
+        return this.storageLocation;
+    }
+
+    public void setStorageLocation(File storageLocation) {
+        this.storageLocation = storageLocation;
+    }
+
+    public synchronized CaveRepository createRepository(String name) throws Exception {
+        if (repositories.get(name) != null) {
+            throw new IllegalArgumentException("Cave repository " + name + " already exists.");
+        }
+        CaveRepository repository = new CaveRepositoryImpl(name, new File(storageLocation, name));
+        repositories.put(name, repository);
+        return repository;
+    }
+
+    public synchronized CaveRepository[] getRepositories() {
+        return repositories.values().toArray(new CaveRepository[0]);
+    }
+
+    public synchronized CaveRepository getRepository(String name) {
+        return repositories.get(name);
+    }
+
+}

Modified: karaf/sandbox/jbonofre/cave/trunk/server/backend/src/test/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImplTest.java
URL: http://svn.apache.org/viewvc/karaf/sandbox/jbonofre/cave/trunk/server/backend/src/test/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImplTest.java?rev=1156329&r1=1156328&r2=1156329&view=diff
==============================================================================
--- karaf/sandbox/jbonofre/cave/trunk/server/backend/src/test/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImplTest.java (original)
+++ karaf/sandbox/jbonofre/cave/trunk/server/backend/src/test/java/org/apache/karaf/cave/server/backend/impl/CaveRepositoryImplTest.java Wed Aug 10 19:28:35 2011
@@ -22,6 +22,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+import java.io.File;
 import java.net.URL;
 
 /**
@@ -34,7 +35,7 @@ public class CaveRepositoryImplTest {
 
     @Before
     public void setUp() throws Exception {
-        repository = new CaveRepositoryImpl("test", "target/test-repository");
+        repository = new CaveRepositoryImpl("test", new File("target/test-repository"));
     }
 
     @Test