From: James William Dumay Date: Thu, 18 Dec 2008 11:50:11 +0000 (+0000) Subject: Working obr consumer. Portions of this work have come from, http://www.osgi.org/svn... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e5fa072d93ae84ea24c66acca330a033dd7d83e0;p=archiva.git Working obr consumer. Portions of this work have come from, http://www.osgi.org/svn/public/trunk/org.osgi.impl.bundle.bindex/ and are licensed under the apache 2 licence git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/archiva-obr-repository@727702 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/main/java/org/apache/archiva/consumers/OBRRepositoryConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/main/java/org/apache/archiva/consumers/OBRRepositoryConsumer.java index e59187cc8..8994ae002 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/main/java/org/apache/archiva/consumers/OBRRepositoryConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/main/java/org/apache/archiva/consumers/OBRRepositoryConsumer.java @@ -17,22 +17,30 @@ package org.apache.archiva.consumers; +import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.IOException; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.URL; import java.util.Arrays; import java.util.Date; import java.util.List; -import org.apache.commons.io.FileUtils; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; import org.apache.maven.archiva.consumers.ConsumerException; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.maven.archiva.repository.ManagedRepositoryContent; import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.types.FileSet; -import org.apache.tools.ant.types.selectors.FilenameSelector; -import org.osgi.impl.bundle.bindex.ant.BindexTask; +import org.osgi.impl.bundle.obr.resource.BundleInfo; +import org.osgi.impl.bundle.obr.resource.RepositoryImpl; +import org.osgi.impl.bundle.obr.resource.ResourceImpl; +import org.osgi.impl.bundle.obr.resource.Tag; +import org.osgi.service.obr.Repository; +import org.osgi.service.obr.Resource; /** * @@ -76,24 +84,57 @@ public class OBRRepositoryConsumer public void processFile(String path) throws ConsumerException { - BindexTask task = new BindexTask(); - File repositoryIndexFile = new File(new File(path).getParentFile(), ".repository.xml"); - task.setRepositoryFile(repositoryIndexFile); - task.setName(content.getRepository().getName()); - task.setQuiet(false); - task.setRoot(new File(content.getRepoRoot())); - - FileSet fileSet = new FileSet(); - fileSet.setDir(new File(path).getParentFile()); - fileSet.setIncludes("**/*.jar"); try { - task.execute(); + final String name = content.getRepository().getName(); + final File repoRoot = new File(content.getRepository().getLocation()); + createRepositoryIndex(name, repoRoot, new File(path)); } - catch (BuildException e) + catch (Exception e) { throw new ConsumerException("Could not add jar " + path + " to obr repository.xml", e); } } + + private void createRepositoryIndex(String repositoryName, File repoRoot, File jarFile) throws Exception + { + File repositoryXml = new File(repoRoot, "repository.zip"); + RepositoryImpl repositoryImpl = new RepositoryImpl(repositoryXml.toURL()); + repositoryImpl.refresh(); + + BundleInfo info = new BundleInfo(repositoryImpl, jarFile); + + Tag tag = new Tag("repository"); + tag.addAttribute("lastmodified", new Date()); + tag.addAttribute("name", repositoryName); + tag.addContent(info.build().toXML()); + for (Resource resource : repositoryImpl.getResources()) + { + tag.addContent(((ResourceImpl)resource).toXML()); + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "UTF-8")); + tag.print(0, pw); + pw.close(); + byte buffer[] = out.toByteArray(); + String name = "repository.xml"; + FileOutputStream fout = new FileOutputStream(repositoryXml); + + if (repositoryXml.getAbsolutePath().endsWith(".zip")) + { + ZipOutputStream zip = new ZipOutputStream(fout); + CRC32 checksum = new CRC32(); + checksum.update(buffer); + ZipEntry ze = new ZipEntry(name); + ze.setSize(buffer.length); + ze.setCrc(checksum.getValue()); + zip.putNextEntry(ze); + zip.write(buffer, 0, buffer.length); + zip.closeEntry(); + zip.close(); + } + fout.close(); + } } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/java/org/apache/archiva/consumers/OBRRepositoryConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/java/org/apache/archiva/consumers/OBRRepositoryConsumerTest.java index deab25e76..f173cf917 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/java/org/apache/archiva/consumers/OBRRepositoryConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/java/org/apache/archiva/consumers/OBRRepositoryConsumerTest.java @@ -18,10 +18,14 @@ package org.apache.archiva.consumers; import java.io.File; +import java.io.IOException; import java.util.Date; import junit.framework.TestCase; import org.apache.commons.io.FileUtils; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.osgi.impl.bundle.obr.resource.RepositoryImpl; +import org.osgi.impl.bundle.obr.resource.ResourceImpl; /** * @@ -35,37 +39,48 @@ public class OBRRepositoryConsumerTest extends TestCase private ManagedRepositoryConfiguration configuration; - @Override - protected void setUp() throws Exception { - super.setUp(); - - File testRepoData = new File("src/test/resources/repository"); - testRepo = new File("target/obrtestrepo").getAbsoluteFile(); + private void createRepository(String name) throws ConsumerException, IOException + { + testRepo = new File("target/" + name + "_repo").getAbsoluteFile(); + if (testRepo.exists()) + { + FileUtils.deleteDirectory(testRepo); + } testRepo.mkdirs(); consumer = new OBRRepositoryConsumer(); - FileUtils.copyDirectory(testRepoData, testRepo); configuration = new ManagedRepositoryConfiguration(); configuration.setName("My Test OSGi repository"); configuration.setId("test-obr-osgi-repo"); configuration.setLocation(testRepo.getAbsolutePath()); + FileUtils.copyDirectory(new File("src/test/resources/repository"), testRepo); consumer.beginScan(configuration, new Date()); } - @Override - protected void tearDown() - throws Exception - { - super.tearDown(); - //FileUtils.deleteDirectory(testRepo); - } - public void testCreatesRepositoryXml() throws Exception { - File repositoryXml = new File(configuration.getLocation(), "repository.xml"); - assertFalse("repository.xml should not exist", repositoryXml.exists()); - consumer.processFile(new File(testRepo, "./commons-codec/commons-codec/1.3.0/commons-codec-1.3.0.jar").getAbsolutePath()); + createRepository("testCreatesRepositoryXml"); + File repositoryZip = new File(configuration.getLocation(), "repository.zip"); + assertFalse("repository.xml should not exist", repositoryZip.exists()); + consumer.processFile(new File(testRepo, "commons-codec/commons-codec/1.3.0/commons-codec-1.3.0.jar").getAbsolutePath()); + consumer.processFile(new File(testRepo, "commons-io/commons-io/1.4.0/commons-io-1.4.0.jar").getAbsolutePath()); + assertTrue("repository.xml should exist", repositoryZip.exists()); + + RepositoryImpl repository = new RepositoryImpl(repositoryZip.toURL()); + repository.refresh(); + + assertEquals(configuration.getName(), repository.getName()); + assertEquals(2, repository.getResources().length); + + assertEquals("com.springsource.org.apache.commons.io/1.4.0", repository.getResources()[0].getId()); + assertEquals("Apache Commons IO", repository.getResources()[0].getPresentationName()); + assertEquals("com.springsource.org.apache.commons.io", repository.getResources()[0].getSymbolicName()); + assertEquals(new File(testRepo, "commons-io/commons-io/1.4.0/commons-io-1.4.0.jar").toURL().toString(), repository.getResources()[0].getURL().toString()); + assertEquals("1.4.0", repository.getResources()[0].getVersion().toString()); - assertTrue(".repository.xml iterim file should exist", new File(testRepo, "commons-codec/commons-codec/1.3.0/.repository.xml").exists()); - assertTrue("repository.xml should exist", repositoryXml.exists()); + assertEquals("com.springsource.org.apache.commons.codec/1.3.0", repository.getResources()[1].getId()); + assertEquals("Apache Commons Codec", repository.getResources()[1].getPresentationName()); + assertEquals("com.springsource.org.apache.commons.codec", repository.getResources()[1].getSymbolicName()); + assertEquals(new File(testRepo, "commons-codec/commons-codec/1.3.0/commons-codec-1.3.0.jar").toURL().toString(), repository.getResources()[1].getURL().toString()); + assertEquals("1.3.0", repository.getResources()[1].getVersion().toString()); } } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/resources/repository.zip b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/resources/repository.zip new file mode 100644 index 000000000..8be37126c Binary files /dev/null and b/archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/resources/repository.zip differ