1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.resource.maven.repository;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.sonatype.aether.RepositorySystemSession;
24 import org.sonatype.aether.artifact.Artifact;
25 import org.sonatype.aether.graph.DependencyFilter;
26 import org.sonatype.aether.repository.Authentication;
27 import org.sonatype.aether.resolution.DependencyResolutionException;
28 import org.sonatype.aether.util.artifact.DefaultArtifact;
29
30 import de.smartics.properties.resource.domain.ArtifactId;
31 import de.smartics.properties.resource.domain.ClassPathEnvironment;
32 import de.smartics.properties.resource.maven.repository.filter.GroupPrefixFilter;
33 import de.smartics.properties.resource.repository.RepositoryException;
34 import de.smartics.properties.resource.repository.ResourceRepository;
35 import de.smartics.util.lang.Arg;
36
37
38
39
40 public final class SessionedMavenResourceRepository implements
41 ResourceRepository
42 {
43
44
45
46
47
48
49
50
51
52 private final MavenRepository repository;
53
54
55
56
57
58 private final RepositorySystemSession session;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public SessionedMavenResourceRepository() throws RepositoryException
77 {
78 this(createRepository(null));
79 }
80
81
82
83
84
85
86
87
88 public SessionedMavenResourceRepository(final MavenRepository repository)
89 throws NullPointerException
90 {
91 Arg.checkNotNull("repository", repository);
92
93 this.repository = repository;
94 this.session = repository.createSession();
95 }
96
97
98
99 private static MavenRepository createRepository(final Authentication auth)
100 throws RepositoryException
101 {
102 final ResourceRepositoryProperties resourceRepository =
103 ResourceRepositoryPropertiesSingleton.instance();
104 final List<DependencyFilter> filters = createDependencyFilters();
105 final String localRepositoryPath = resourceRepository.localPath();
106
107 final MavenRepository repository;
108 if (StringUtils.isNotBlank(localRepositoryPath))
109 {
110 final String remoteRepositoryUrl = resourceRepository.remoteUrl();
111 repository =
112 new MavenRepository(remoteRepositoryUrl, auth, localRepositoryPath,
113 filters, false);
114 }
115 else
116 {
117 repository = constructFromSettings(filters);
118 }
119
120 return repository;
121 }
122
123 private static List<DependencyFilter> createDependencyFilters()
124 {
125 final List<DependencyFilter> filters = new ArrayList<DependencyFilter>();
126
127 final ResourceRepositoryProperties resourceRepository =
128 ResourceRepositoryPropertiesSingleton.instance();
129 final String prefix = resourceRepository.filterGroupIdPrefix();
130 if (StringUtils.isNotBlank(prefix))
131 {
132 final DependencyFilter filter = new GroupPrefixFilter(prefix);
133 filters.add(filter);
134 }
135
136 return filters;
137 }
138
139 private static MavenRepository constructFromSettings(
140 final List<DependencyFilter> filters)
141 {
142 final String defaultSettings = getDefaultSettings();
143 final ResourceRepositoryProperties resourceRepository =
144 ResourceRepositoryPropertiesSingleton.instance();
145 final String userSettings = resourceRepository.settingsFile();
146 final String settings =
147 StringUtils.isNotBlank(userSettings) ? userSettings : defaultSettings;
148
149 return createSettings(settings, filters);
150 }
151
152 private static MavenRepository createSettings(final String settings,
153 final List<DependencyFilter> filters) throws RepositoryException
154 {
155 final SettingsMavenRepoBuilder builder = new SettingsMavenRepoBuilder();
156 final MavenRepository repository = builder.build(settings, filters);
157 return repository;
158 }
159
160 private static String getDefaultSettings()
161 {
162 final String defaultSettings =
163 new File(System.getProperty("user.home"), ".m2/settings.xml")
164 .getAbsolutePath();
165 return defaultSettings;
166 }
167
168
169
170
171
172
173
174
175
176 @Override
177 public ClassPathEnvironment resolve(final ArtifactId artifactId)
178 throws RepositoryException
179 {
180 try
181 {
182 final Artifact artifact = createArtifact(artifactId);
183 final ClassPathEnvironment resources =
184 repository.resolve(session, artifact);
185 return resources;
186 }
187 catch (final DependencyResolutionException e)
188 {
189 throw new RepositoryException(new ArtifactIdMessageBean(e, artifactId));
190 }
191 }
192
193 private static Artifact createArtifact(final ArtifactId artifactId)
194 {
195 final DefaultArtifact artifact =
196 new DefaultArtifact(artifactId.getGroupId(), artifactId.getName(),
197 artifactId.getClassifier(), artifactId.getArchiveType(),
198 artifactId.getVersion());
199 return artifact;
200 }
201
202 @Override
203 public String getRemoteRepositoryUrl()
204 {
205 return repository.getRemoteRepositoryUrl();
206 }
207
208
209
210 }