1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.aether.internal

File XWikiRepositorySystemSession.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
21
5
1
130
71
8
0.38
4.2
5
1.6

Classes

Class Line # Actions
XWikiRepositorySystemSession 50 21 0% 8 2
0.9333333493.3%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.repository.aether.internal;
21   
22    import java.io.File;
23    import java.io.IOException;
24    import java.util.Map;
25   
26    import org.apache.commons.io.FileUtils;
27    import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
28    import org.eclipse.aether.AbstractForwardingRepositorySystemSession;
29    import org.eclipse.aether.ConfigurationProperties;
30    import org.eclipse.aether.DefaultRepositorySystemSession;
31    import org.eclipse.aether.RepositorySystem;
32    import org.eclipse.aether.RepositorySystemSession;
33    import org.eclipse.aether.artifact.ArtifactTypeRegistry;
34    import org.eclipse.aether.artifact.DefaultArtifactType;
35    import org.eclipse.aether.repository.LocalRepository;
36    import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry;
37    import org.eclipse.aether.util.repository.JreProxySelector;
38    import org.eclipse.aether.util.repository.SimpleArtifactDescriptorPolicy;
39    import org.xwiki.extension.internal.maven.MavenUtils;
40   
41    import com.google.common.io.Files;
42   
43    /**
44    * Encapsulate {@link DefaultRepositorySystemSession} to generate and clean a temporary local repository for each
45    * sessions.
46    *
47    * @version $Id: 6b31c4d2347740b401222204c1a51a4b3f2fb3b0 $
48    * @since 6.0
49    */
 
50    public class XWikiRepositorySystemSession extends AbstractForwardingRepositorySystemSession implements AutoCloseable
51    {
52    static final JreProxySelector JREPROXYSELECTOR = new JreProxySelector();
53   
54    private final DefaultRepositorySystemSession session;
55   
56    /**
57    * @param repositorySystem the AETHER repository system component
58    */
 
59  126 toggle public XWikiRepositorySystemSession(RepositorySystem repositorySystem)
60    {
61  126 this.session = MavenRepositorySystemUtils.newSession();
62   
63    // Local repository
64   
65  126 File localDir = Files.createTempDir();
66  126 LocalRepository localRepository = new LocalRepository(localDir);
67  126 this.session
68    .setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(this.session, localRepository));
69   
70    // Proxy selector
71   
72  126 this.session.setProxySelector(JREPROXYSELECTOR);
73   
74    // Remove all system properties that could disrupt effective pom resolution
75  126 this.session.setSystemProperty("version", null);
76  126 this.session.setSystemProperty("groupId", null);
77   
78    // Add various type descriptors
79  126 ArtifactTypeRegistry artifactTypeRegistry = this.session.getArtifactTypeRegistry();
80  126 if (artifactTypeRegistry instanceof DefaultArtifactTypeRegistry) {
81  126 DefaultArtifactTypeRegistry defaultArtifactTypeRegistry =
82    (DefaultArtifactTypeRegistry) artifactTypeRegistry;
83  126 defaultArtifactTypeRegistry
84    .add(new DefaultArtifactType("bundle", MavenUtils.JAR_EXTENSION, "", MavenUtils.JAVA_LANGUAGE));
85  126 defaultArtifactTypeRegistry
86    .add(new DefaultArtifactType("eclipse-plugin", MavenUtils.JAR_EXTENSION, "", MavenUtils.JAVA_LANGUAGE));
87    }
88   
89    // Fail when the pom is missing or invalid
90  126 this.session.setArtifactDescriptorPolicy(new SimpleArtifactDescriptorPolicy(false, false));
91    }
92   
 
93  11296 toggle @Override
94    protected RepositorySystemSession getSession()
95    {
96  11297 return this.session;
97    }
98   
 
99  139 toggle @Override
100    public void close()
101    {
102  139 LocalRepository repository = this.session.getLocalRepository();
103   
104  139 if (repository.getBasedir().exists()) {
105  126 try {
106  126 FileUtils.deleteDirectory(repository.getBasedir());
107    } catch (IOException e) {
108    // TODO: Should probably log something even if it should be pretty rare
109    }
110    }
111    }
112   
113    /**
114    * @param userAgent the user agent
115    */
 
116  126 toggle public void setUserAgent(String userAgent)
117    {
118  126 this.session.setConfigProperty(ConfigurationProperties.USER_AGENT, userAgent);
119    }
120   
121    /**
122    * @param properties the custom properties
123    */
 
124  126 toggle public void addConfigurationProperties(Map<String, String> properties)
125    {
126  126 for (Map.Entry<String, String> entry : properties.entrySet()) {
127  0 this.session.setConfigProperty(entry.getKey(), entry.getValue());
128    }
129    }
130    }