001    package org.jadira.dependencynavigator.implementations.zip;
002    /**********************************************************************
003    Licensed under the Apache License, Version 2.0 (the "License");
004    you may not use this file except in compliance with the License.
005    You may obtain a copy of the License at
006    
007        http://www.apache.org/licenses/LICENSE-2.0
008    
009    Unless required by applicable law or agreed to in writing, software
010    distributed under the License is distributed on an "AS IS" BASIS,
011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    See the License for the specific language governing permissions and
013    limitations under the License.
014    
015    Contributors:
016    
017    **********************************************************************/
018    import org.apache.maven.project.interpolation.ModelInterpolationException;
019    import org.codehaus.plexus.context.Context;
020    import org.codehaus.plexus.context.ContextException;
021    import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
022    import org.jadira.dependencynavigator.controller.DependencyResolutionException;
023    import org.jadira.dependencynavigator.implementations.Workspace;
024    import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
025    import org.jadira.dependencynavigator.model.PomFile;
026    import org.jadira.dependencynavigator.model.RootPom;
027    
028    import java.io.BufferedReader;
029    import java.io.FileNotFoundException;
030    import java.io.IOException;
031    import java.io.InputStreamReader;
032    import java.util.zip.ZipEntry;
033    import java.util.zip.ZipFile;
034    
035    public class ZipFileWorkspace extends Workspace implements Contextualizable
036    {
037    
038        public static final String KEY_ZIPFILE_PATH = "zip.filepath";
039        public static final String PREFIX_WORKSPACE = "workspace";
040    
041        private ZipFile zipFile;
042        private String selectedPom;
043    
044        ZipFile getZipFile()
045        {
046            return this.zipFile;
047        }
048    
049        @Override
050        public String getPath(String moduleName)
051        {
052            return PREFIX_WORKSPACE + selectedPom.replaceFirst("/pom.xml$", "/" + moduleName + "/pom.xml");
053        }
054    
055        @Override
056        public PomFile loadModulePom(String module)
057        {
058            try
059            {
060                String zipEntryPath = getPath(module);
061                ZipEntry zipEntry = zipFile.getEntry(zipEntryPath);
062                BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
063                return loadPom(zipEntryReader);
064            }
065            catch (IOException e)
066            {
067                throw new DependencyResolutionException(e);
068            }
069        }
070    
071        @Override
072        public void reloadPomFile() throws ArtifactInitialisationException
073        {
074            try
075            {
076                ZipEntry zipEntry = zipFile.getEntry(PREFIX_WORKSPACE + selectedPom);
077                BufferedReader zipEntryReader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
078                root = new RootPom(loadPom(zipEntryReader), this);
079            }
080            catch (FileNotFoundException e)
081            {
082                throw new DependencyResolutionException(e);
083            }
084            catch (IOException e)
085            {
086                throw new DependencyResolutionException(e);
087            }
088            catch (ModelInterpolationException e)
089            {
090                throw new DependencyResolutionException(e);
091            }
092        }
093    
094        public void loadRootPom(String selectedPom) throws ArtifactInitialisationException
095        {
096            this.selectedPom = selectedPom;
097            reloadPomFile();
098        }
099    
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    }