From e5fa072d93ae84ea24c66acca330a033dd7d83e0 Mon Sep 17 00:00:00 2001 From: James William Dumay Date: Thu, 18 Dec 2008 11:50:11 +0000 Subject: [PATCH] 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 --- .../consumers/OBRRepositoryConsumer.java | 77 ++++++++++++++---- .../consumers/OBRRepositoryConsumerTest.java | 55 ++++++++----- .../src/test/resources/repository.zip | Bin 0 -> 634 bytes 3 files changed, 94 insertions(+), 38 deletions(-) create mode 100644 archiva-modules/archiva-base/archiva-consumers/archiva-obr-consumer/src/test/resources/repository.zip 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 0000000000000000000000000000000000000000..8be37126cf705681f2a60b8e8c7e5a5718bd151e GIT binary patch literal 634 zcmWIWW@Zs#-~htO8z)&ZAOSugyC}6Fzc{lbzo=5LA~)x5*ulKT1_FD(Yu9|%G@FpK zeYeuou)wXWw%nE5W&fbTc{!`}&1IRtzFH?_C}b|#vP!9pf1=vq*)zW%d)B%6#)+Wl zf?GKrdTX>xxU?s{oa5$LAn5kcRiY*EORw+Y4k1-8%jM?MpWLI|^WN7n-Mn`Bw6W8M z`B$fUsxZEp_@e3NrG`^AlQJh826jD(GpY&8NR`Zx+a$@Yp}8?kOI;@8sruiH1t|{~ zKKV6GXt(AxtM((zebNq|sS6mdcXIl@X+8TPF6olWjb?)>HlE27&&;W6+v+eS(hlxC|M_jRgrrw$eB$?WQ}0{GufHy9t#T@Q!qffh#KZT$U#BL>S9X3~uK5c= z^@trUGIy5Qwxuq;6mu!hB}Pakii1_#X3jKAuLF~p)NtI2Eor`W|EphU{b%2!6HnjI z{Cw)n*GJ3qj_O)Gi{9D(p|0wt&qm0{T(I-OnezP`mt~?wh(fIz} zZ9lP`VBGJ$1?%St*>-0-)nr3Z#>bFXd_Te;jIj{rlqK zdfthNd6%AidsF{w+u!s1&Nb=(epjEg{#*4gp51=;UX}b<=Hp+`c-*T~*OBwjtIGD| z-Y_pN$$N(ljZU|oS|#<>eof@by|VM&-0yFDz2kSv{y*FS-s~Jt3_ZNwFanbS4+BGh jHzSh>10tm$%Yjk~DnLy)0p6@^AbCb0v;oo@OduWrHgy%A literal 0 HcmV?d00001 -- 2.39.5