This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
DefaultServiceLocator.java
201 lines (182 loc) · 7.38 KB
/
DefaultServiceLocator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.sonatype.aether.impl.internal;
/*******************************************************************************
* Copyright (c) 2010-2011 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.impl.ArtifactResolver;
import org.sonatype.aether.impl.DependencyCollector;
import org.sonatype.aether.impl.Deployer;
import org.sonatype.aether.impl.Installer;
import org.sonatype.aether.impl.LocalRepositoryProvider;
import org.sonatype.aether.impl.MetadataResolver;
import org.sonatype.aether.impl.RemoteRepositoryManager;
import org.sonatype.aether.impl.RepositoryEventDispatcher;
import org.sonatype.aether.impl.SyncContextFactory;
import org.sonatype.aether.impl.UpdateCheckManager;
import org.sonatype.aether.spi.io.FileProcessor;
import org.sonatype.aether.spi.localrepo.LocalRepositoryManagerFactory;
import org.sonatype.aether.spi.locator.Service;
import org.sonatype.aether.spi.locator.ServiceLocator;
/**
* A simple service locator that is already setup with all components from this library. To acquire a complete
* repository system, clients need to add an artifact descriptor, a version resolver, a version range resolver and some
* repository connectors.
*
* @author Benjamin Bentmann
*/
public class DefaultServiceLocator
implements ServiceLocator
{
private Map<Class<?>, Collection<Class<?>>> classes;
private Map<Class<?>, List<?>> instances;
/**
* Creates a new service locator that already knows about all service implementations included this library.
*/
public DefaultServiceLocator()
{
classes = new HashMap<Class<?>, Collection<Class<?>>>();
instances = new HashMap<Class<?>, List<?>>();
addService( RepositorySystem.class, DefaultRepositorySystem.class );
addService( ArtifactResolver.class, DefaultArtifactResolver.class );
addService( DependencyCollector.class, DefaultDependencyCollector.class );
addService( Deployer.class, DefaultDeployer.class );
addService( Installer.class, DefaultInstaller.class );
addService( MetadataResolver.class, DefaultMetadataResolver.class );
addService( RemoteRepositoryManager.class, DefaultRemoteRepositoryManager.class );
addService( UpdateCheckManager.class, DefaultUpdateCheckManager.class );
addService( FileProcessor.class, DefaultFileProcessor.class );
addService( SyncContextFactory.class, DefaultSyncContextFactory.class );
addService( RepositoryEventDispatcher.class, DefaultRepositoryEventDispatcher.class );
addService( LocalRepositoryProvider.class, DefaultLocalRepositoryProvider.class );
addService( LocalRepositoryManagerFactory.class, SimpleLocalRepositoryManagerFactory.class );
addService( LocalRepositoryManagerFactory.class, EnhancedLocalRepositoryManagerFactory.class );
}
/**
* Sets the implementation class for a service.
*
* @param <T> The service type.
* @param type The interface describing the service, must not be {@code null}.
* @param impl The implementation class of the service, must not be {@code null}.
* @return This locator for chaining, never {@code null}.
*/
public <T> DefaultServiceLocator setService( Class<T> type, Class<? extends T> impl )
{
classes.remove( type );
return addService( type, impl );
}
/**
* Adds the implementation class for a service.
*
* @param <T> The service type.
* @param type The interface describing the service, must not be {@code null}.
* @param impl The implementation class of the service, must not be {@code null}.
* @return This locator for chaining, never {@code null}.
*/
public <T> DefaultServiceLocator addService( Class<T> type, Class<? extends T> impl )
{
if ( impl == null )
{
throw new IllegalArgumentException( "implementation class must not be null" );
}
Collection<Class<?>> impls = classes.get( type );
if ( impls == null )
{
impls = new LinkedHashSet<Class<?>>();
classes.put( type, impls );
}
impls.add( impl );
return this;
}
/**
* Sets the instances for a service.
*
* @param <T> The service type.
* @param type The interface describing the service, must not be {@code null}.
* @param services The instances of the service, must not be {@code null}.
* @return This locator for chaining, never {@code null}.
*/
public <T> DefaultServiceLocator setServices( Class<T> type, T... services )
{
synchronized ( instances )
{
instances.put( type, Arrays.asList( services ) );
}
return this;
}
public <T> T getService( Class<T> type )
{
List<T> objs = getServices( type );
return objs.isEmpty() ? null : objs.get( 0 );
}
public <T> List<T> getServices( Class<T> type )
{
synchronized ( instances )
{
@SuppressWarnings( "unchecked" )
List<T> objs = (List<T>) instances.get( type );
if ( objs == null )
{
Iterator<T> it;
Collection<Class<?>> impls = classes.get( type );
if ( impls == null || impls.isEmpty() )
{
objs = Collections.emptyList();
it = objs.iterator();
}
else
{
objs = new ArrayList<T>( impls.size() );
for ( Class<?> impl : impls )
{
try
{
Object obj = impl.newInstance();
objs.add( type.cast( obj ) );
}
catch ( Exception e )
{
serviceCreationFailed( type, impl, e );
}
}
it = objs.iterator();
objs = Collections.unmodifiableList( objs );
}
instances.put( type, objs );
while ( it.hasNext() )
{
T obj = it.next();
if ( obj instanceof Service )
{
try
{
( (Service) obj ).initService( this );
}
catch ( Exception e )
{
it.remove();
serviceCreationFailed( type, obj.getClass(), e );
}
}
}
}
return objs;
}
}
protected void serviceCreationFailed( Class<?> type, Class<?> impl, Throwable exception )
{
exception.printStackTrace();
}
}