001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.implementations.zip;
019    
020    import java.io.BufferedInputStream;
021    import java.io.File;
022    import java.io.FileNotFoundException;
023    import java.io.IOException;
024    import java.io.InputStreamReader;
025    import java.util.zip.ZipEntry;
026    import java.util.zip.ZipFile;
027    
028    import org.apache.maven.model.Model;
029    import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
030    import org.codehaus.plexus.context.Context;
031    import org.codehaus.plexus.context.ContextException;
032    import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
033    import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
034    import org.jadira.dependencynavigator.controller.DependencyResolutionException;
035    import org.jadira.dependencynavigator.implementations.Repository;
036    
037    public class ZipFileRepository implements Repository, Contextualizable {
038    
039        public static final String KEY_ZIPFILE_PATH = "zip.filepath";
040        public static final String PREFIX_REPOSITORY = "repository";
041    
042        private File repositoryZipFile;
043    
044        public Model resolveFile(String groupId, String artifactId, String version) {
045            try {
046                String path = PREFIX_REPOSITORY + "/" + groupId.replaceAll("\\.", "/") + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".pom";
047                ZipFile zipFile = new ZipFile(repositoryZipFile);
048                ZipEntry entry = zipFile.getEntry(path);
049                if (entry == null) {
050                    return null;
051                }
052    
053                MavenXpp3Reader pomReader = new MavenXpp3Reader();
054    
055                return pomReader.read(new InputStreamReader(new BufferedInputStream(zipFile.getInputStream(entry))));
056            } catch (FileNotFoundException e) {
057                throw new DependencyResolutionException(e);
058            } catch (IOException e) {
059                throw new DependencyResolutionException(e);
060            } catch (XmlPullParserException e) {
061                throw new DependencyResolutionException(e);
062            }
063        }
064    
065        public boolean canCreateSnapshot() {
066            return false;
067        }
068    
069        public void contextualize(Context context) throws ContextException {
070            String zipFilePath = (String) context.get(KEY_ZIPFILE_PATH);
071            if (zipFilePath == null || zipFilePath.trim().length() == 0) {
072                throw new ContextException("Missing configuration parameter: " + KEY_ZIPFILE_PATH);
073            }
074            this.repositoryZipFile = new File(zipFilePath);
075        }
076    }