3939import com .google .inject .Injector ;
4040import com .google .inject .Module ;
4141
42+ import org .eclipse .aether .RepositorySystem ;
4243import org .eclipse .aether .repository .RemoteRepository ;
4344import org .eclipse .core .runtime .Assert ;
4445import org .eclipse .core .runtime .CoreException ;
5152import org .eclipse .sisu .space .SpaceModule ;
5253import org .eclipse .sisu .wire .WireModule ;
5354
54- import org .codehaus .plexus .ContainerConfiguration ;
55- import org .codehaus .plexus .DefaultContainerConfiguration ;
56- import org .codehaus .plexus .DefaultPlexusContainer ;
57- import org .codehaus .plexus .PlexusConstants ;
58- import org .codehaus .plexus .PlexusContainerException ;
59- import org .codehaus .plexus .classworlds .ClassWorld ;
60- import org .codehaus .plexus .component .repository .exception .ComponentLookupException ;
61-
6255import org .apache .maven .RepositoryUtils ;
6356import org .apache .maven .archetype .catalog .Archetype ;
6457import org .apache .maven .archetype .common .ArchetypeArtifactManager ;
65- import org .apache .maven .archetype .common .DefaultArchetypeArtifactManager ;
66- import org .apache .maven .archetype .downloader .Downloader ;
6758import org .apache .maven .archetype .exception .UnknownArchetype ;
6859import org .apache .maven .archetype .metadata .ArchetypeDescriptor ;
6960import org .apache .maven .archetype .metadata .RequiredProperty ;
70- import org .apache .maven .archetype .source .ArchetypeDataSource ;
7161import org .apache .maven .archetype .source .ArchetypeDataSourceException ;
62+ import org .apache .maven .archetype .source .InternalCatalogArchetypeDataSource ;
63+ import org .apache .maven .archetype .source .LocalCatalogArchetypeDataSource ;
64+ import org .apache .maven .archetype .source .RemoteCatalogArchetypeDataSource ;
7265
7366import org .eclipse .m2e .core .embedder .IMaven ;
7467import org .eclipse .m2e .core .embedder .IMavenExecutionContext ;
8780@ Component (service = ArchetypePlugin .class )
8881public class ArchetypePlugin {
8982
83+ private static final InternalCatalogArchetypeDataSource INTERNAL_CATALOG_ARCHETYPE_DATA_SOURCE = new InternalCatalogArchetypeDataSource ();
84+
85+ private static final LocalCatalogArchetypeDataSource LOCAL_CATALOG = new LocalCatalogArchetypeDataSource ();
86+
9087 public static final String ARCHETYPE_PREFIX = "archetype" ;
9188
9289 private final Map <String , ArchetypeCatalogFactory > catalogs = new LinkedHashMap <>();
@@ -103,49 +100,37 @@ public class ArchetypePlugin {
103100
104101 private ArchetypeArtifactManager archetypeArtifactManager ;
105102
106- private Map <String , ArchetypeDataSource > archetypeDataSourceMap ;
107-
108- private DefaultPlexusContainer container ;
109-
110103 public ArchetypePlugin () {
111104 this .configFile = new File (MavenPluginActivator .getDefault ().getStateLocation ().toFile (),
112105 M2EUIPluginActivator .PREFS_ARCHETYPES );
113106 this .writer = new ArchetypeCatalogsWriter ();
114107 }
115108
116109 @ Activate
117- void activate () throws PlexusContainerException , ComponentLookupException {
110+ void activate () {
118111 final Module logginModule = new AbstractModule () {
119112 @ Override
120113 protected void configure () {
121114 bind (ILoggerFactory .class ).toInstance (LoggerFactory .getILoggerFactory ());
122115 }
123116 };
124- final ContainerConfiguration cc = new DefaultContainerConfiguration () //
125- .setClassWorld (new ClassWorld ("plexus.core" , ArchetypeArtifactManager .class .getClassLoader ())) //$NON-NLS-1$
126- .setClassPathScanning (PlexusConstants .SCANNING_INDEX ) //
127- .setAutoWiring (true ) //
128- .setJSR250Lifecycle (true ) //
129- .setName ("plexus" ); //$NON-NLS-1$
130-
131117 ClassSpace space = new BundleClassSpace (FrameworkUtil .getBundle (ArchetypeArtifactManager .class ));
132- Injector injector = Guice .createInjector (logginModule ,
133- new WireModule (new SpaceModule (space , BeanScanning .INDEX , false )));
134- ArchetypeArtifactManager mgr1 = injector .getInstance (DefaultArchetypeArtifactManager .class );
135- try {
136- ArchetypeArtifactManager mgr2 = injector .getInstance (ArchetypeArtifactManager .class );
137- } catch (Exception ex ) {
138- //TODO: doesn't work?
139- }
140- container = new DefaultPlexusContainer (cc , logginModule );
141- Downloader downloader = container .lookup (Downloader .class );
142- //TODO: a Downloader can be created, but isn't injected into the fields of a created archetypeArtifactManager
143- archetypeArtifactManager = container .lookup (ArchetypeArtifactManager .class );
144- archetypeDataSourceMap = container .lookupMap (ArchetypeDataSource .class );
145- addArchetypeCatalogFactory (
146- new ArchetypeCatalogFactory .InternalCatalogFactory (archetypeDataSourceMap .get ("internal-catalog" )));
118+ final Module repositorySystemModule = new AbstractModule () {
119+ @ Override
120+ protected void configure () {
121+ try {
122+ bind (RepositorySystem .class ).toInstance (MavenPluginActivator .getDefault ().getRepositorySystem ());
123+ } catch (CoreException ex ) {
124+ ex .printStackTrace ();
125+ }
126+ }
127+ };
128+ Injector injector = Guice .createInjector (
129+ new WireModule (logginModule , repositorySystemModule , new SpaceModule (space , BeanScanning .INDEX , false )));
130+ archetypeArtifactManager = injector .getInstance (ArchetypeArtifactManager .class );
147131 addArchetypeCatalogFactory (
148- new ArchetypeCatalogFactory .DefaultLocalCatalogFactory (maven , archetypeDataSourceMap .get ("catalog" )));
132+ new ArchetypeCatalogFactory .InternalCatalogFactory (INTERNAL_CATALOG_ARCHETYPE_DATA_SOURCE ));
133+ addArchetypeCatalogFactory (new ArchetypeCatalogFactory .DefaultLocalCatalogFactory (maven , LOCAL_CATALOG ));
149134 for (ArchetypeCatalogFactory archetypeCatalogFactory : ExtensionReader .readArchetypeExtensions (this )) {
150135 addArchetypeCatalogFactory (archetypeCatalogFactory );
151136 }
@@ -159,18 +144,16 @@ protected void configure() {
159144 @ Deactivate
160145 void shutdown () throws IOException {
161146 saveCatalogs ();
162- container .dispose ();
163147 }
164148
165149 public LocalCatalogFactory newLocalCatalogFactory (String path , String description , boolean editable ,
166150 boolean enabled ) {
167- return new LocalCatalogFactory (path , description , editable , enabled , maven , archetypeDataSourceMap . get ( "catalog" ) );
151+ return new LocalCatalogFactory (path , description , editable , enabled , maven , LOCAL_CATALOG );
168152 }
169153
170154 public RemoteCatalogFactory newRemoteCatalogFactory (String url , String description , boolean editable ,
171155 boolean enabled ) {
172- return new RemoteCatalogFactory (url , description , editable , enabled , maven ,
173- archetypeDataSourceMap .get ("remote-catalog" ));
156+ return new RemoteCatalogFactory (url , description , editable , enabled , maven , new RemoteCatalogArchetypeDataSource ());
174157 }
175158
176159 public ArchetypeGenerator getGenerator () {
@@ -261,8 +244,7 @@ public List<RequiredProperty> getRequiredProperties(IArchetype archetype, IProgr
261244 public void updateLocalCatalog (Archetype archetype ) throws CoreException {
262245 maven .createExecutionContext ().execute ((ctx , m ) -> {
263246 try {
264- ArchetypeDataSource source = archetypeDataSourceMap .get ("catalog" );
265- source .updateCatalog (ctx .getRepositorySession (), archetype );
247+ LOCAL_CATALOG .updateCatalog (ctx .getRepositorySession (), archetype );
266248 } catch (ArchetypeDataSourceException e ) {
267249 }
268250 return null ;
0 commit comments