001/* 002 * Extra Bnd Repository Plugins 003 * Copyright (C) 2019 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Affero General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package de.mnl.osgi.bnd.maven; 020 021import aQute.maven.api.Archive; 022import aQute.maven.api.Program; 023import aQute.maven.api.Revision; 024import aQute.maven.provider.MavenRepository; 025import aQute.service.reporter.Reporter; 026import java.io.File; 027import java.lang.reflect.InvocationTargetException; 028import org.apache.maven.building.FileSource; 029import org.apache.maven.model.Dependency; 030import org.apache.maven.model.Parent; 031import org.apache.maven.model.Repository; 032import org.apache.maven.model.building.ModelSource; 033import org.apache.maven.model.resolution.InvalidRepositoryException; 034import org.apache.maven.model.resolution.ModelResolver; 035import org.apache.maven.model.resolution.UnresolvableModelException; 036 037/** 038 * Resolves (raw) model requests using a bnd {@link MavenRepository} 039 * as backend. 040 */ 041@SuppressWarnings("deprecation") 042public class BndModelResolver implements ModelResolver, Cloneable { 043 044 private final MavenRepository bndRepository; 045 @SuppressWarnings({ "PMD.SingularField", "unused" }) 046 private final Reporter reporter; 047 048 /** 049 * Instantiates a new bnd model resolver. 050 * 051 * @param bndRepository the backing repository 052 * @param reporter the reporter 053 */ 054 public BndModelResolver(MavenRepository bndRepository, Reporter reporter) { 055 this.bndRepository = bndRepository; 056 this.reporter = reporter; 057 } 058 059 @Override 060 @SuppressWarnings({ "PMD.AvoidCatchingGenericException", 061 "PMD.PreserveStackTrace" }) 062 public ModelSource resolveModel(String groupId, String artifactId, 063 String version) throws UnresolvableModelException { 064 Revision revision 065 = Program.valueOf(groupId, artifactId).version(version); 066 Archive pomArchive = revision.getPomArchive(); 067 try { 068 File pomFile = bndRepository.get(pomArchive).getValue(); 069 if (pomFile == null) { 070 throw new UnresolvableModelException("Not found.", groupId, 071 artifactId, version); 072 } 073 return new FileModelSource(pomFile); 074 } catch (InvocationTargetException e) { 075 throw new UnresolvableModelException(e.getCause(), groupId, 076 artifactId, version); 077 } catch (Exception e) { 078 throw new UnresolvableModelException(e, groupId, artifactId, 079 version); 080 } 081 } 082 083 @Override 084 @SuppressWarnings({ "PMD.AvoidCatchingGenericException", 085 "PMD.PreserveStackTrace" }) 086 public ModelSource resolveModel(Parent parent) 087 throws UnresolvableModelException { 088 Revision revision 089 = Program.valueOf(parent.getGroupId(), parent.getArtifactId()) 090 .version(parent.getVersion()); 091 try { 092 Archive pomArchive 093 = bndRepository.getResolvedArchive(revision, "pom", ""); 094 parent.setVersion(pomArchive.getRevision().version.toString()); 095 File pomFile = bndRepository.get(pomArchive).getValue(); 096 if (pomFile == null) { 097 throw new UnresolvableModelException("Not found.", 098 parent.getGroupId(), parent.getArtifactId(), 099 parent.getVersion()); 100 } 101 return new FileModelSource(pomFile); 102 } catch (InvocationTargetException e) { 103 throw new UnresolvableModelException(e.getCause(), 104 parent.getGroupId(), parent.getArtifactId(), 105 parent.getVersion()); 106 } catch (Exception e) { 107 throw new UnresolvableModelException(e, parent.getGroupId(), 108 parent.getArtifactId(), parent.getVersion()); 109 } 110 } 111 112 @Override 113 @SuppressWarnings({ "PMD.AvoidCatchingGenericException", 114 "PMD.PreserveStackTrace" }) 115 public ModelSource resolveModel(Dependency dependency) 116 throws UnresolvableModelException { 117 Revision revision 118 = Program.valueOf(dependency.getGroupId(), 119 dependency.getArtifactId()).version(dependency.getVersion()); 120 try { 121 Archive pomArchive 122 = bndRepository.getResolvedArchive(revision, "pom", ""); 123 dependency.setVersion(pomArchive.getRevision().version.toString()); 124 File pomFile = bndRepository.get(pomArchive).getValue(); 125 if (pomFile == null) { 126 throw new UnresolvableModelException("Not found.", 127 dependency.getGroupId(), dependency.getArtifactId(), 128 dependency.getVersion()); 129 } 130 return new FileModelSource(pomFile); 131 } catch (InvocationTargetException e) { 132 throw new UnresolvableModelException(e.getCause(), 133 dependency.getGroupId(), dependency.getArtifactId(), 134 dependency.getVersion()); 135 } catch (Exception e) { 136 throw new UnresolvableModelException(e, dependency.getGroupId(), 137 dependency.getArtifactId(), dependency.getVersion()); 138 } 139 } 140 141 @Override 142 public void addRepository(Repository repository) 143 throws InvalidRepositoryException { 144 // Ignored, repository is defined by constructor. 145 } 146 147 @Override 148 public void addRepository(Repository repository, boolean replace) 149 throws InvalidRepositoryException { 150 // Ignored, repository is defined by constructor. 151 } 152 153 @Override 154 public ModelResolver newCopy() { 155 try { 156 return (BndModelResolver) clone(); 157 } catch (CloneNotSupportedException e) { 158 // Doesn't happen 159 return null; 160 } 161 } 162 163 /** 164 * The Class FileModelSource. 165 */ 166 public static class FileModelSource extends FileSource 167 implements ModelSource { 168 169 /** 170 * Instantiates a new file model source. 171 * 172 * @param file the file 173 */ 174 public FileModelSource(File file) { 175 super(file); 176 } 177 } 178}