View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Implementation of the {@link ResourceRepository} interface based on Maven.
39   */
40  public final class SessionedMavenResourceRepository implements
41      ResourceRepository
42  {
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The repository adapter to connect to the Maven infrastructure.
51     */
52    private final MavenRepository repository;
53  
54    /**
55     * The session associated with this instance to access the Maven
56     * infrastructure.
57     */
58    private final RepositorySystemSession session;
59  
60    // ****************************** Initializer *******************************
61  
62    // ****************************** Constructors ******************************
63  
64    /**
65     * Convenience constructor to consult the environment to create a connection
66     * to a Maven repository.
67     * <p>
68     * The connection properties are defined by
69     * {@link de.smartics.properties.resource.maven.repository.ResourceRepositoryProperties}.
70     * </p>
71     *
72     * @throws RepositoryException if the repository cannot be created from
73     *           environment information.
74     * @see #init(MavenRepository)
75     */
76    public SessionedMavenResourceRepository() throws RepositoryException
77    {
78      this(createRepository(null));
79    }
80  
81    /**
82     * Default constructor.
83     *
84     * @param repository the repository adapter to connect to the Maven
85     *          infrastructure.
86     * @throws NullPointerException if {@code repository} is <code>null</code>.
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    // ****************************** Inner Classes *****************************
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   // ********************************* Methods ********************************
169 
170   // --- init -----------------------------------------------------------------
171 
172   // --- get&set --------------------------------------------------------------
173 
174   // --- business -------------------------------------------------------------
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   // --- object basics --------------------------------------------------------
209 
210 }