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

File AetherExtensionFile.java

 

Coverage histogram

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

Code metrics

0
31
5
2
131
84
7
0.23
6.2
2.5
1.4

Classes

Class Line # Actions
AetherExtensionFile 46 27 0% 5 2
0.9333333493.3%
AetherExtensionFile.AetherExtensionFileInputStream 52 4 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 17 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.FileInputStream;
24    import java.io.FileNotFoundException;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.util.Arrays;
28    import java.util.List;
29   
30    import org.eclipse.aether.RepositorySystem;
31    import org.eclipse.aether.artifact.Artifact;
32    import org.eclipse.aether.impl.RepositoryConnectorProvider;
33    import org.eclipse.aether.repository.RemoteRepository;
34    import org.eclipse.aether.resolution.ArtifactRequest;
35    import org.eclipse.aether.resolution.ArtifactResolutionException;
36    import org.eclipse.aether.resolution.ArtifactResult;
37    import org.eclipse.aether.spi.connector.ArtifactDownload;
38    import org.eclipse.aether.spi.connector.RepositoryConnector;
39    import org.eclipse.aether.transfer.NoRepositoryConnectorException;
40    import org.xwiki.extension.ExtensionFile;
41   
42    /**
43    * @version $Id: c8763e6cd61ba04aa977dfee83721684a0e06a1e $
44    * @since 4.0M1
45    */
 
46    public class AetherExtensionFile implements ExtensionFile
47    {
48    private Artifact artifact;
49   
50    private AetherExtensionRepository repository;
51   
 
52    static class AetherExtensionFileInputStream extends FileInputStream
53    {
54    private XWikiRepositorySystemSession session;
55   
 
56  10 toggle public AetherExtensionFileInputStream(File file, XWikiRepositorySystemSession session)
57    throws FileNotFoundException
58    {
59  10 super(file);
60   
61  10 this.session = session;
62    }
63   
 
64  23 toggle @Override
65    public void close() throws IOException
66    {
67  23 super.close();
68   
69    // Cleanup AETHER session
70  23 this.session.close();
71    }
72    }
73   
 
74  51 toggle public AetherExtensionFile(Artifact artifact, AetherExtensionRepository repository)
75    {
76  51 this.repository = repository;
77  51 this.artifact = artifact;
78    }
79   
 
80  3 toggle @Override
81    public long getLength()
82    {
83    // TODO
84  3 return -1;
85    }
86   
 
87  10 toggle @Override
88    public InputStream openStream() throws IOException
89    {
90  10 XWikiRepositorySystemSession session = this.repository.createRepositorySystemSession();
91   
92  10 List<RemoteRepository> repositories = this.repository.newResolutionRepositories(session);
93  10 RemoteRepository repository = repositories.get(0);
94   
95  10 RepositoryConnector connector;
96  10 try {
97  10 RepositoryConnectorProvider repositoryConnectorProvider = this.repository.getRepositoryConnectorProvider();
98  10 connector = repositoryConnectorProvider.newRepositoryConnector(session, repository);
99    } catch (NoRepositoryConnectorException e) {
100  0 throw new IOException("Failed to download artifact [" + this.artifact + "]", e);
101    }
102   
103  10 ArtifactDownload download = new ArtifactDownload();
104  10 download.setArtifact(this.artifact);
105  10 download.setRepositories(repositories);
106   
107  10 try {
108  10 connector.get(Arrays.asList(download), null);
109    } finally {
110  10 connector.close();
111    }
112   
113    // /////////////////////////////////////////////////////////////////////////////:
114   
115  10 ArtifactRequest artifactRequest = new ArtifactRequest();
116  10 artifactRequest.setRepositories(repositories);
117  10 artifactRequest.setArtifact(this.artifact);
118   
119  10 ArtifactResult artifactResult;
120  10 try {
121  10 RepositorySystem repositorySystem = this.repository.getRepositorySystem();
122  10 artifactResult = repositorySystem.resolveArtifact(session, artifactRequest);
123    } catch (ArtifactResolutionException e) {
124  0 throw new IOException("Failed to resolve artifact", e);
125    }
126   
127  10 File aetherFile = artifactResult.getArtifact().getFile();
128   
129  10 return new AetherExtensionFileInputStream(aetherFile, session);
130    }
131    }