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;
/**
*
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();
+ }
}
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;
/**
*
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());
}
}