1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl.synccontext.named;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.Map;
27
28 import org.eclipse.aether.MultiRuntimeException;
29 import org.eclipse.aether.RepositorySystemSession;
30 import org.eclipse.aether.impl.RepositorySystemLifecycle;
31 import org.eclipse.aether.named.NamedLockFactory;
32 import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
33 import org.eclipse.aether.util.ConfigUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import static java.util.Objects.requireNonNull;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @Singleton
52 @Named
53 public class NamedLockFactoryAdapterFactoryImpl implements NamedLockFactoryAdapterFactory {
54 public static final String DEFAULT_FACTORY_NAME = FileLockNamedLockFactory.NAME;
55
56 public static final String DEFAULT_NAME_MAPPER_NAME = NameMappers.FILE_GAV_NAME;
57
58
59
60
61
62
63
64
65 public static final String CONFIG_PROP_FACTORY_KEY = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "factory";
66
67
68
69
70
71
72
73
74
75 public static final String CONFIG_PROP_NAME_MAPPER_KEY = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "nameMapper";
76
77 protected final Logger logger = LoggerFactory.getLogger(getClass());
78
79 protected final Map<String, NamedLockFactory> factories;
80
81 protected final String defaultFactoryName;
82
83 protected final Map<String, NameMapper> nameMappers;
84
85 protected final String defaultNameMapperName;
86
87 @Inject
88 public NamedLockFactoryAdapterFactoryImpl(
89 final Map<String, NamedLockFactory> factories,
90 final Map<String, NameMapper> nameMappers,
91 final RepositorySystemLifecycle lifecycle) {
92 this(factories, DEFAULT_FACTORY_NAME, nameMappers, DEFAULT_NAME_MAPPER_NAME, lifecycle);
93 }
94
95 public NamedLockFactoryAdapterFactoryImpl(
96 final Map<String, NamedLockFactory> factories,
97 final String defaultFactoryName,
98 final Map<String, NameMapper> nameMappers,
99 final String defaultNameMapperName,
100 final RepositorySystemLifecycle lifecycle) {
101 this.factories = requireNonNull(factories);
102 this.defaultFactoryName = requireNonNull(defaultFactoryName);
103 this.nameMappers = requireNonNull(nameMappers);
104 this.defaultNameMapperName = requireNonNull(defaultNameMapperName);
105 lifecycle.addOnSystemEndedHandler(this::shutdown);
106
107 logger.debug(
108 "Created adapter factory; available factories {}; available name mappers {}",
109 factories.keySet(),
110 nameMappers.keySet());
111 }
112
113
114
115
116 @Override
117 public NamedLockFactoryAdapter getAdapter(RepositorySystemSession session) {
118 return createAdapter(session);
119 }
120
121
122
123
124 protected NamedLockFactoryAdapter createAdapter(RepositorySystemSession session) {
125 final String nameMapperName = requireNonNull(getNameMapperName(session));
126 final String factoryName = requireNonNull(getFactoryName(session));
127 final NameMapper nameMapper = selectNameMapper(nameMapperName);
128 final NamedLockFactory factory = selectFactory(factoryName);
129 logger.debug("Creating adapter using nameMapper '{}' and factory '{}'", nameMapperName, factoryName);
130 return new NamedLockFactoryAdapter(nameMapper, factory);
131 }
132
133
134
135
136 protected String getFactoryName(RepositorySystemSession session) {
137 return ConfigUtils.getString(session, getDefaultFactoryName(), CONFIG_PROP_FACTORY_KEY);
138 }
139
140
141
142
143 protected String getDefaultFactoryName() {
144 return defaultFactoryName;
145 }
146
147
148
149
150 protected String getNameMapperName(RepositorySystemSession session) {
151 return ConfigUtils.getString(session, getDefaultNameMapperName(), CONFIG_PROP_NAME_MAPPER_KEY);
152 }
153
154
155
156
157 protected String getDefaultNameMapperName() {
158 return defaultNameMapperName;
159 }
160
161
162
163
164 protected NamedLockFactory selectFactory(final String factoryName) {
165 NamedLockFactory factory = factories.get(factoryName);
166 if (factory == null) {
167 throw new IllegalArgumentException(
168 "Unknown NamedLockFactory name: '" + factoryName + "', known ones: " + factories.keySet());
169 }
170 return factory;
171 }
172
173
174
175
176 protected NameMapper selectNameMapper(final String nameMapperName) {
177 NameMapper nameMapper = nameMappers.get(nameMapperName);
178 if (nameMapper == null) {
179 throw new IllegalArgumentException(
180 "Unknown NameMapper name: '" + nameMapperName + "', known ones: " + nameMappers.keySet());
181 }
182 return nameMapper;
183 }
184
185
186
187
188 protected void shutdown() {
189 logger.debug(
190 "Shutting down adapter factory; available factories {}; available name mappers {}",
191 factories.keySet(),
192 nameMappers.keySet());
193 ArrayList<Exception> exceptions = new ArrayList<>();
194 for (Map.Entry<String, NamedLockFactory> entry : factories.entrySet()) {
195 try {
196 logger.debug("Shutting down '{}' factory", entry.getKey());
197 entry.getValue().shutdown();
198 } catch (Exception e) {
199 exceptions.add(e);
200 }
201 }
202 MultiRuntimeException.mayThrow("Problem shutting down factories", exceptions);
203 }
204 }