1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.jadira.dependencynavigator.implementations.zip;
19
20 import java.io.BufferedInputStream;
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipFile;
27
28 import org.apache.maven.model.Model;
29 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
30 import org.codehaus.plexus.context.Context;
31 import org.codehaus.plexus.context.ContextException;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34 import org.jadira.dependencynavigator.controller.DependencyResolutionException;
35 import org.jadira.dependencynavigator.implementations.Repository;
36
37 public class ZipFileRepository implements Repository, Contextualizable {
38
39 public static final String KEY_ZIPFILE_PATH = "zip.filepath";
40 public static final String PREFIX_REPOSITORY = "repository";
41
42 private File repositoryZipFile;
43
44 public Model resolveFile(String groupId, String artifactId, String version) {
45 try {
46 String path = PREFIX_REPOSITORY + "/" + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".pom";
47 ZipFile zipFile = new ZipFile(repositoryZipFile);
48 ZipEntry entry = zipFile.getEntry(path);
49 if (entry == null) {
50 return null;
51 }
52
53 MavenXpp3Reader pomReader = new MavenXpp3Reader();
54
55 return pomReader.read(new InputStreamReader(new BufferedInputStream(zipFile.getInputStream(entry))));
56 } catch (FileNotFoundException e) {
57 throw new DependencyResolutionException(e);
58 } catch (IOException e) {
59 throw new DependencyResolutionException(e);
60 } catch (XmlPullParserException e) {
61 throw new DependencyResolutionException(e);
62 }
63 }
64
65 public boolean canCreateSnapshot() {
66 return false;
67 }
68
69 public void contextualize(Context context) throws ContextException {
70 String zipFilePath = (String) context.get(KEY_ZIPFILE_PATH);
71 if (zipFilePath == null || zipFilePath.trim().length() == 0) {
72 throw new ContextException("Missing configuration parameter: " + KEY_ZIPFILE_PATH);
73 }
74 this.repositoryZipFile = new File(zipFilePath);
75 }
76 }