View Javadoc

1   package org.jadira.dependencynavigator.implementations.zip;
2   /**********************************************************************
3   Licensed under the Apache License, Version 2.0 (the "License");
4   you may not use this file except in compliance with the License.
5   You may obtain a copy of the License at
6   
7       http://www.apache.org/licenses/LICENSE-2.0
8   
9   Unless required by applicable law or agreed to in writing, software
10  distributed under the License is distributed on an "AS IS" BASIS,
11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  See the License for the specific language governing permissions and
13  limitations under the License.
14  
15  Contributors:
16  
17  **********************************************************************/
18  import org.apache.maven.project.interpolation.ModelInterpolationException;
19  import org.codehaus.plexus.context.Context;
20  import org.codehaus.plexus.context.ContextException;
21  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
22  import org.jadira.dependencynavigator.controller.DependencyResolutionException;
23  import org.jadira.dependencynavigator.implementations.Workspace;
24  import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
25  import org.jadira.dependencynavigator.model.PomFile;
26  import org.jadira.dependencynavigator.model.RootPom;
27  
28  import java.io.BufferedReader;
29  import java.io.FileNotFoundException;
30  import java.io.IOException;
31  import java.io.InputStreamReader;
32  import java.util.zip.ZipEntry;
33  import java.util.zip.ZipFile;
34  
35  public class ZipFileWorkspace extends Workspace implements Contextualizable
36  {
37  
38      public static final String KEY_ZIPFILE_PATH = "zip.filepath";
39      public static final String PREFIX_WORKSPACE = "workspace";
40  
41      private ZipFile zipFile;
42      private String selectedPom;
43  
44      ZipFile getZipFile()
45      {
46          return this.zipFile;
47      }
48  
49      @Override
50      public String getPath(String moduleName)
51      {
52          return PREFIX_WORKSPACE + selectedPom.replaceFirst("/pom.xml$", "/" + moduleName + "/pom.xml");
53      }
54  
55      @Override
56      public PomFile loadModulePom(String module)
57      {
58          try
59          {
60              String zipEntryPath = getPath(module);
61              ZipEntry zipEntry = zipFile.getEntry(zipEntryPath);
62              BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
63              return loadPom(zipEntryReader);
64          }
65          catch (IOException e)
66          {
67              throw new DependencyResolutionException(e);
68          }
69      }
70  
71      @Override
72      public void reloadPomFile() throws ArtifactInitialisationException
73      {
74          try
75          {
76              ZipEntry zipEntry = zipFile.getEntry(PREFIX_WORKSPACE + selectedPom);
77              BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
78              root = new RootPom(loadPom(zipEntryReader), this);
79          }
80          catch (FileNotFoundException e)
81          {
82              throw new DependencyResolutionException(e);
83          }
84          catch (IOException e)
85          {
86              throw new DependencyResolutionException(e);
87          }
88          catch (ModelInterpolationException e)
89          {
90              throw new DependencyResolutionException(e);
91          }
92      }
93  
94      public void loadRootPom(String selectedPom) throws ArtifactInitialisationException
95      {
96          this.selectedPom = selectedPom;
97          reloadPomFile();
98      }
99  
100     @Override
101     public boolean canCreateSnapshot()
102     {
103         return false;
104     }
105 
106     public void contextualize(Context context) throws ContextException
107     {
108         String zipFilePath = (String) context.get(KEY_ZIPFILE_PATH);
109         if (zipFilePath == null || zipFilePath.trim().length() == 0)
110         {
111             throw new ContextException("Missing configuration parameter: " + KEY_ZIPFILE_PATH);
112         }
113         try
114         {
115             this.zipFile = new ZipFile(zipFilePath);
116         }
117         catch (IOException e)
118         {
119             throw new ContextException("Error instantiating zip file: " + e.getMessage());
120         }
121     }
122 
123 }