Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61e33f7718fc5615e161df986de581d1ac1e22ab (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2008-2010 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
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.core.internal.archetype;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.IOUtil;

import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.common.ArchetypeArtifactManager;
import org.apache.maven.archetype.exception.UnknownArchetype;
import org.apache.maven.archetype.metadata.ArchetypeDescriptor;
import org.apache.maven.archetype.source.ArchetypeDataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ICallable;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.internal.NoSuchComponentException;


/**
 * Archetype Manager
 * 
 * @author Eugene Kuleshov
 */
public class ArchetypeManager {

  private final Map<String, ArchetypeCatalogFactory> catalogs = new LinkedHashMap<String, ArchetypeCatalogFactory>();

  private final File configFile;

  private final ArchetypeCatalogsWriter writer;

  final ArchetypeArtifactManager aaMgr;

  private final org.apache.maven.archetype.Archetype archetyper;

  private final PlexusContainer container;

  public ArchetypeManager(PlexusContainer container, File configFile) {
    this.container = container;
    this.configFile = configFile;
    this.writer = new ArchetypeCatalogsWriter();
    try {
      this.aaMgr = container.lookup(ArchetypeArtifactManager.class);
      this.archetyper = container.lookup(org.apache.maven.archetype.Archetype.class);
    } catch(ComponentLookupException ex) {
      throw new NoSuchComponentException(ex);
    }
  }

  /**
   * @return Collection of ArchetypeCatalogFactory
   */
  public Collection<ArchetypeCatalogFactory> getArchetypeCatalogs() {
    return new ArrayList<ArchetypeCatalogFactory>(catalogs.values());
  }

  public void addArchetypeCatalogFactory(ArchetypeCatalogFactory factory) {
    if(factory != null) {
      catalogs.put(factory.getId(), factory);
    }
  }

  public void removeArchetypeCatalogFactory(String catalogId) {
    catalogs.remove(catalogId);
  }

  public ArchetypeCatalogFactory getArchetypeCatalogFactory(String catalogId) {
    return catalogs.get(catalogId);
  }

  public void readCatalogs() throws IOException {
    if(configFile.exists()) {
      InputStream is = null;
      try {
        is = new FileInputStream(configFile);
        Collection<ArchetypeCatalogFactory> catalogs = writer.readArchetypeCatalogs(is);
        for(Iterator<ArchetypeCatalogFactory> it = catalogs.iterator(); it.hasNext();) {
          addArchetypeCatalogFactory(it.next());
        }
      } finally {
        IOUtil.close(is);
      }
    }
  }

  public void saveCatalogs() throws IOException {
    OutputStream os = null;
    try {
      os = new FileOutputStream(configFile);
      writer.writeArchetypeCatalogs(getArchetypeCatalogs(), os);
    } finally {
      IOUtil.close(os);
    }
  }

  /**
   * Gets the remote {@link ArtifactRepository} of the given {@link Archetype}, or null if none is found. The repository
   * url is extracted from {@link Archetype#getRepository(). The {@link ArtifactRepository} id is set to
   * <strong>archetypeId+"-repo"</strong>, to enable authentication on that repository.
   * 
   * @see <a
   *      href="http://maven.apache.org/archetype/maven-archetype-plugin/faq.html">http://maven.apache.org/archetype/maven-archetype-plugin/faq.html</a>
   * @param archetype
   * @return the remote {@link ArtifactRepository} of the given {@link Archetype}, or null if none is found.
   * @throws CoreException
   */
  public ArtifactRepository getArchetypeRepository(Archetype archetype) throws CoreException {
    String repoUrl = archetype.getRepository();
    if(repoUrl == null || repoUrl.trim().isEmpty()) {
      return null;
    }
    return MavenPlugin.getMaven().createArtifactRepository(archetype.getArtifactId() + "-repo", repoUrl); //$NON-NLS-1$
  }

  /**
   * Gets the required properties of an {@link Archetype}.
   * 
   * @param archetype the archetype possibly declaring required properties
   * @param remoteArchetypeRepository the remote archetype repository, can be null.
   * @param monitor the progress monitor, can be null.
   * @return the required properties of the archetypes, null if none is found.
   * @throws UnknownArchetype thrown if no archetype is can be resolved
   * @throws CoreException
   */
  public List<?> getRequiredProperties(Archetype archetype, ArtifactRepository remoteArchetypeRepository,
      IProgressMonitor monitor) throws UnknownArchetype, CoreException {
    Assert.isNotNull(archetype, "Archetype can not be null");

    if(monitor == null) {
      monitor = new NullProgressMonitor();
    }

    final String groupId = archetype.getGroupId();
    final String artifactId = archetype.getArtifactId();
    final String version = archetype.getVersion();

    IMaven maven = MavenPlugin.getMaven();

    final List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>(maven.getArtifactRepositories());
    if(remoteArchetypeRepository != null) {
      repositories.add(0, remoteArchetypeRepository);
    }

    @SuppressWarnings("serial")
    final class WrappedUnknownArchetype extends RuntimeException {
      public WrappedUnknownArchetype(UnknownArchetype ex) {
        super(ex);
      }
    }

    try {
      return maven.execute(new ICallable<List<?>>() {
        public List<?> call(IMavenExecutionContext context, IProgressMonitor monitor) {
          ArtifactRepository localRepository = context.getLocalRepository();
          if(aaMgr.isFileSetArchetype(groupId, artifactId, version, null, localRepository, repositories)) {
            ArchetypeDescriptor descriptor;
            try {
              descriptor = aaMgr.getFileSetArchetypeDescriptor(groupId, artifactId, version, null, localRepository,
                  repositories);
            } catch(UnknownArchetype ex) {
              throw new WrappedUnknownArchetype(ex);
            }
            return descriptor.getRequiredProperties();
          }
          return null;
        }
      }, monitor);
    } catch(WrappedUnknownArchetype e) {
      throw (UnknownArchetype) e.getCause();
    }
  }

  /**
   * @since 1.5
   */
  public ArchetypeArtifactManager getArchetypeArtifactManager() {
    return aaMgr;
  }

  /**
   * @since 1.5
   */
  public org.apache.maven.archetype.Archetype getArchetyper() {
    return archetyper;
  }

  /**
   * @since 1.5
   */
  public ArchetypeDataSource getArchetypeDataSource(String hint) {
    try {
      return container.lookup(ArchetypeDataSource.class, hint);
    } catch(ComponentLookupException ex) {
      throw new NoSuchComponentException(ex);
    }
  }
}

Back to the top