View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
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  }