1 package org.eclipse.aether.internal.impl.checksum;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.UncheckedIOException;
24 import java.nio.file.Path;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.aether.RepositorySystemSession;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.repository.ArtifactRepository;
31 import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
32 import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
33 import org.eclipse.aether.util.ConfigUtils;
34 import org.eclipse.aether.util.DirectoryUtils;
35
36 import static java.util.Objects.requireNonNull;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 abstract class FileTrustedChecksumsSourceSupport
58 implements TrustedChecksumsSource
59 {
60 private static final String CONFIG_PROP_PREFIX = "aether.trustedChecksumsSource.";
61
62 private static final String CONF_NAME_BASEDIR = "basedir";
63
64 private static final String CONF_NAME_ORIGIN_AWARE = "originAware";
65
66
67
68
69 static final String LOCAL_REPO_PREFIX_DIR = ".checksums";
70
71 private final String name;
72
73 FileTrustedChecksumsSourceSupport( String name )
74 {
75 this.name = requireNonNull( name );
76 }
77
78
79
80
81
82 @Override
83 public Map<String, String> getTrustedArtifactChecksums( RepositorySystemSession session,
84 Artifact artifact,
85 ArtifactRepository artifactRepository,
86 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories )
87 {
88 requireNonNull( session, "session is null" );
89 requireNonNull( artifact, "artifact is null" );
90 requireNonNull( artifactRepository, "artifactRepository is null" );
91 requireNonNull( checksumAlgorithmFactories, "checksumAlgorithmFactories is null" );
92 if ( isEnabled( session ) )
93 {
94 return requireNonNull(
95 doGetTrustedArtifactChecksums( session, artifact, artifactRepository, checksumAlgorithmFactories )
96 );
97 }
98 return null;
99 }
100
101
102
103
104
105 @Override
106 public Writer getTrustedArtifactChecksumsWriter( RepositorySystemSession session )
107 {
108 requireNonNull( session, "session is null" );
109 if ( isEnabled( session ) )
110 {
111 return doGetTrustedArtifactChecksumsWriter( session );
112 }
113 return null;
114 }
115
116
117
118
119 protected abstract Map<String, String> doGetTrustedArtifactChecksums(
120 RepositorySystemSession session, Artifact artifact, ArtifactRepository artifactRepository,
121 List<ChecksumAlgorithmFactory> checksumAlgorithmFactories );
122
123
124
125
126 protected Writer doGetTrustedArtifactChecksumsWriter( RepositorySystemSession session )
127 {
128 return null;
129 }
130
131
132
133
134 protected String configPropKey( String name )
135 {
136 requireNonNull( name );
137 return CONFIG_PROP_PREFIX + this.name + "." + name;
138 }
139
140
141
142
143
144
145 protected boolean isEnabled( RepositorySystemSession session )
146 {
147 return ConfigUtils.getBoolean( session, false, CONFIG_PROP_PREFIX + this.name );
148 }
149
150
151
152
153
154
155 protected boolean isOriginAware( RepositorySystemSession session )
156 {
157 return ConfigUtils.getBoolean( session, true, configPropKey( CONF_NAME_ORIGIN_AWARE ) );
158 }
159
160
161
162
163
164
165
166
167
168
169 protected Path getBasedir( RepositorySystemSession session, boolean mayCreate )
170 {
171 try
172 {
173 return DirectoryUtils.resolveDirectory(
174 session, LOCAL_REPO_PREFIX_DIR, configPropKey( CONF_NAME_BASEDIR ), mayCreate );
175 }
176 catch ( IOException e )
177 {
178 throw new UncheckedIOException( e );
179 }
180 }
181 }