001package org.eclipse.aether.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.lang.reflect.Constructor; 023import java.lang.reflect.Modifier; 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.Collections; 027import java.util.HashMap; 028import java.util.LinkedHashSet; 029import java.util.List; 030import java.util.Map; 031import static java.util.Objects.requireNonNull; 032 033import org.eclipse.aether.RepositorySystem; 034import org.eclipse.aether.internal.impl.DefaultArtifactResolver; 035import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider; 036import org.eclipse.aether.internal.impl.DefaultTrackingFileManager; 037import org.eclipse.aether.internal.impl.TrackingFileManager; 038import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector; 039import org.eclipse.aether.internal.impl.DefaultDeployer; 040import org.eclipse.aether.internal.impl.DefaultFileProcessor; 041import org.eclipse.aether.internal.impl.DefaultInstaller; 042import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider; 043import org.eclipse.aether.internal.impl.DefaultMetadataResolver; 044import org.eclipse.aether.internal.impl.DefaultOfflineController; 045import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager; 046import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider; 047import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher; 048import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider; 049import org.eclipse.aether.internal.impl.DefaultRepositorySystem; 050import org.eclipse.aether.internal.impl.DefaultTransporterProvider; 051import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager; 052import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer; 053import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; 054import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory; 055import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory; 056import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory; 057import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory; 058import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelector; 059import org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector; 060import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider; 061import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory; 062import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider; 063import org.eclipse.aether.spi.connector.transport.TransporterProvider; 064import org.eclipse.aether.spi.io.FileProcessor; 065import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; 066import org.eclipse.aether.spi.locator.Service; 067import org.eclipse.aether.spi.locator.ServiceLocator; 068import org.eclipse.aether.spi.log.LoggerFactory; 069import org.eclipse.aether.spi.synccontext.SyncContextFactory; 070 071/** 072 * A simple service locator that is already setup with all components from this library. To acquire a complete 073 * repository system, clients need to add an artifact descriptor reader, a version resolver, a version range resolver 074 * and optionally some repository connector and transporter factories to access remote repositories. Once the locator is 075 * fully populated, the repository system can be created like this: 076 * 077 * <pre> 078 * RepositorySystem repoSystem = serviceLocator.getService( RepositorySystem.class ); 079 * </pre> 080 * 081 * <em>Note:</em> This class is not thread-safe. Clients are expected to create the service locator and the repository 082 * system on a single thread. 083 * 084 * @deprecated Use some out-of-the-box DI implementation instead. 085 */ 086@Deprecated 087public final class DefaultServiceLocator 088 implements ServiceLocator 089{ 090 091 private class Entry<T> 092 { 093 094 private final Class<T> type; 095 096 private final Collection<Object> providers; 097 098 private List<T> instances; 099 100 Entry( Class<T> type ) 101 { 102 this.type = requireNonNull( type, "service type cannot be null" ); 103 providers = new LinkedHashSet<>( 8 ); 104 } 105 106 public synchronized void setServices( T... services ) 107 { 108 providers.clear(); 109 if ( services != null ) 110 { 111 for ( T service : services ) 112 { 113 providers.add( requireNonNull( service, "service instance cannot be null" ) ); 114 } 115 } 116 instances = null; 117 } 118 119 public synchronized void setService( Class<? extends T> impl ) 120 { 121 providers.clear(); 122 addService( impl ); 123 } 124 125 public synchronized void addService( Class<? extends T> impl ) 126 { 127 providers.add( requireNonNull( impl, "implementation class cannot be null" ) ); 128 instances = null; 129 } 130 131 public T getInstance() 132 { 133 List<T> instances = getInstances(); 134 return instances.isEmpty() ? null : instances.get( 0 ); 135 } 136 137 public synchronized List<T> getInstances() 138 { 139 if ( instances == null ) 140 { 141 instances = new ArrayList<>( providers.size() ); 142 for ( Object provider : providers ) 143 { 144 T instance; 145 if ( provider instanceof Class ) 146 { 147 instance = newInstance( (Class<?>) provider ); 148 } 149 else 150 { 151 instance = type.cast( provider ); 152 } 153 if ( instance != null ) 154 { 155 instances.add( instance ); 156 } 157 } 158 instances = Collections.unmodifiableList( instances ); 159 } 160 return instances; 161 } 162 163 private T newInstance( Class<?> impl ) 164 { 165 try 166 { 167 Constructor<?> constr = impl.getDeclaredConstructor(); 168 if ( !Modifier.isPublic( constr.getModifiers() ) ) 169 { 170 constr.setAccessible( true ); 171 } 172 Object obj = constr.newInstance(); 173 174 T instance = type.cast( obj ); 175 if ( instance instanceof Service ) 176 { 177 ( (Service) instance ).initService( DefaultServiceLocator.this ); 178 } 179 return instance; 180 } 181 catch ( Exception | LinkageError e ) 182 { 183 serviceCreationFailed( type, impl, e ); 184 } 185 return null; 186 } 187 188 } 189 190 private final Map<Class<?>, Entry<?>> entries; 191 192 private ErrorHandler errorHandler; 193 194 /** 195 * Creates a new service locator that already knows about all service implementations included this library. 196 */ 197 public DefaultServiceLocator() 198 { 199 entries = new HashMap<>(); 200 201 addService( RepositorySystem.class, DefaultRepositorySystem.class ); 202 addService( ArtifactResolver.class, DefaultArtifactResolver.class ); 203 addService( DependencyCollector.class, DefaultDependencyCollector.class ); 204 addService( Deployer.class, DefaultDeployer.class ); 205 addService( Installer.class, DefaultInstaller.class ); 206 addService( MetadataResolver.class, DefaultMetadataResolver.class ); 207 addService( RepositoryLayoutProvider.class, DefaultRepositoryLayoutProvider.class ); 208 addService( RepositoryLayoutFactory.class, Maven2RepositoryLayoutFactory.class ); 209 addService( TransporterProvider.class, DefaultTransporterProvider.class ); 210 addService( ChecksumPolicyProvider.class, DefaultChecksumPolicyProvider.class ); 211 addService( RepositoryConnectorProvider.class, DefaultRepositoryConnectorProvider.class ); 212 addService( RemoteRepositoryManager.class, DefaultRemoteRepositoryManager.class ); 213 addService( UpdateCheckManager.class, DefaultUpdateCheckManager.class ); 214 addService( UpdatePolicyAnalyzer.class, DefaultUpdatePolicyAnalyzer.class ); 215 addService( FileProcessor.class, DefaultFileProcessor.class ); 216 addService( org.eclipse.aether.impl.SyncContextFactory.class, 217 org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class ); 218 addService( SyncContextFactory.class, DefaultSyncContextFactory.class ); 219 addService( RepositoryEventDispatcher.class, DefaultRepositoryEventDispatcher.class ); 220 addService( OfflineController.class, DefaultOfflineController.class ); 221 addService( LocalRepositoryProvider.class, DefaultLocalRepositoryProvider.class ); 222 addService( LocalRepositoryManagerFactory.class, SimpleLocalRepositoryManagerFactory.class ); 223 addService( LocalRepositoryManagerFactory.class, EnhancedLocalRepositoryManagerFactory.class ); 224 addService( LoggerFactory.class, Slf4jLoggerFactory.class ); 225 addService( TrackingFileManager.class, DefaultTrackingFileManager.class ); 226 addService( NamedLockFactorySelector.class, SimpleNamedLockFactorySelector.class ); 227 } 228 229 private <T> Entry<T> getEntry( Class<T> type, boolean create ) 230 { 231 @SuppressWarnings( "unchecked" ) 232 Entry<T> entry = (Entry<T>) entries.get( requireNonNull( type, "service type cannot be null" ) ); 233 if ( entry == null && create ) 234 { 235 entry = new Entry<>( type ); 236 entries.put( type, entry ); 237 } 238 return entry; 239 } 240 241 /** 242 * Sets the implementation class for a service. The specified class must have a no-arg constructor (of any 243 * visibility). If the service implementation itself requires other services for its operation, it should implement 244 * {@link Service} to gain access to this service locator. 245 * 246 * @param <T> The service type. 247 * @param type The interface describing the service, must not be {@code null}. 248 * @param impl The implementation class of the service, must not be {@code null}. 249 * @return This locator for chaining, never {@code null}. 250 */ 251 public <T> DefaultServiceLocator setService( Class<T> type, Class<? extends T> impl ) 252 { 253 getEntry( type, true ).setService( impl ); 254 return this; 255 } 256 257 /** 258 * Adds an implementation class for a service. The specified class must have a no-arg constructor (of any 259 * visibility). If the service implementation itself requires other services for its operation, it should implement 260 * {@link Service} to gain access to this service locator. 261 * 262 * @param <T> The service type. 263 * @param type The interface describing the service, must not be {@code null}. 264 * @param impl The implementation class of the service, must not be {@code null}. 265 * @return This locator for chaining, never {@code null}. 266 */ 267 public <T> DefaultServiceLocator addService( Class<T> type, Class<? extends T> impl ) 268 { 269 getEntry( type, true ).addService( impl ); 270 return this; 271 } 272 273 /** 274 * Sets the instances for a service. 275 * 276 * @param <T> The service type. 277 * @param type The interface describing the service, must not be {@code null}. 278 * @param services The instances of the service, may be {@code null} but must not contain {@code null} elements. 279 * @return This locator for chaining, never {@code null}. 280 */ 281 public <T> DefaultServiceLocator setServices( Class<T> type, T... services ) 282 { 283 getEntry( type, true ).setServices( services ); 284 return this; 285 } 286 287 public <T> T getService( Class<T> type ) 288 { 289 Entry<T> entry = getEntry( type, false ); 290 return ( entry != null ) ? entry.getInstance() : null; 291 } 292 293 public <T> List<T> getServices( Class<T> type ) 294 { 295 Entry<T> entry = getEntry( type, false ); 296 return ( entry != null ) ? entry.getInstances() : null; 297 } 298 299 private void serviceCreationFailed( Class<?> type, Class<?> impl, Throwable exception ) 300 { 301 if ( errorHandler != null ) 302 { 303 errorHandler.serviceCreationFailed( type, impl, exception ); 304 } 305 } 306 307 /** 308 * Sets the error handler to use. 309 * 310 * @param errorHandler The error handler to use, may be {@code null} to ignore/swallow errors. 311 */ 312 public void setErrorHandler( ErrorHandler errorHandler ) 313 { 314 this.errorHandler = errorHandler; 315 } 316 317 /** 318 * A hook to customize the handling of errors encountered while locating a service implementation. 319 */ 320 public abstract static class ErrorHandler 321 { 322 323 /** 324 * Handles errors during creation of a service. The default implemention does nothing. 325 * 326 * @param type The interface describing the service, must not be {@code null}. 327 * @param impl The implementation class of the service, must not be {@code null}. 328 * @param exception The error that occurred while trying to instantiate the implementation class, must not be 329 * {@code null}. 330 */ 331 public void serviceCreationFailed( Class<?> type, Class<?> impl, Throwable exception ) 332 { 333 } 334 335 } 336 337}