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.local;
019
020 import org.apache.maven.model.Model;
021 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
022 import org.codehaus.plexus.context.Context;
023 import org.codehaus.plexus.context.ContextException;
024 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
025 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
026 import org.jadira.dependencynavigator.controller.DependencyResolutionException;
027 import org.jadira.dependencynavigator.implementations.Repository;
028
029 import java.io.File;
030 import java.io.FileNotFoundException;
031 import java.io.FileReader;
032 import java.io.IOException;
033
034 public class LocalDiskRepository implements Repository, Contextualizable {
035
036 public static final String KEY_LOCAL_REPO_PATH = "local.repo";
037
038 private File repositoryRoot;
039
040 public Model resolveFile(String groupId, String artifactId, String version) {
041 String[] groups = groupId.split("\\.");
042 File artifactPomFile = new File(repositoryRoot.getPath());
043 for (int i = 0; i < groups.length; i++) {
044 artifactPomFile = new File(artifactPomFile, groups[i]);
045 }
046 artifactPomFile = new File(artifactPomFile, artifactId);
047 artifactPomFile = new File(artifactPomFile, version);
048 artifactPomFile = new File(artifactPomFile, artifactId + "-" + version + ".pom");
049
050 if (!artifactPomFile.exists()) {
051 return null;
052 }
053
054 MavenXpp3Reader pomReader = new MavenXpp3Reader();
055 try {
056 return pomReader.read(new FileReader(artifactPomFile));
057 } catch (FileNotFoundException e) {
058 throw new DependencyResolutionException(e);
059 } catch (IOException e) {
060 throw new DependencyResolutionException(e);
061 } catch (XmlPullParserException e) {
062 // throw new DependencyResolutionException(e);
063 }
064 return null;
065 }
066
067 public File getRepositoryRoot() {
068 return this.repositoryRoot;
069 }
070
071 public boolean canCreateSnapshot() {
072 return true;
073 }
074
075 public void contextualize(Context context) throws ContextException {
076 String repoPath = (String) context.get(KEY_LOCAL_REPO_PATH);
077 if (repoPath == null || repoPath.trim().length() == 0) {
078 throw new ContextException("Missing configuration parameter: " + KEY_LOCAL_REPO_PATH);
079 }
080 repositoryRoot = new File(repoPath);
081 }
082 }