Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c82854ecae986337b0d4b2f77067701f09b690e (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * Copyright (c) 2008, 2019 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

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

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;

import org.apache.maven.archetype.ArchetypeManager;
import org.apache.maven.archetype.catalog.Archetype;
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
import org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader;
import org.apache.maven.archetype.source.ArchetypeDataSource;
import org.apache.maven.archetype.source.ArchetypeDataSourceException;

import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.Messages;


/**
 * Abstract ArchetypeCatalog factory
 */
public abstract class ArchetypeCatalogFactory {
  private static final Logger log = LoggerFactory.getLogger(ArchetypeCatalogFactory.class);

  private final String id;

  private final String description;

  private final boolean editable;

  private boolean enabled;

  public ArchetypeCatalogFactory(String id, String description, boolean editable) {
    this(id, description, editable, true);
  }

  public ArchetypeCatalogFactory(String id, String description, boolean editable, boolean enabled) {
    this.id = id;
    this.description = description;
    this.editable = editable;
    this.enabled = enabled;
  }

  public String getId() {
    return this.id;
  }

  public String getDescription() {
    return this.description;
  }

  public boolean isEditable() {
    return editable;
  }

  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
  }

  public abstract ArchetypeCatalog getArchetypeCatalog() throws CoreException;

  public String toString() {
    return getId();
  }

  protected ArchetypeManager getArchetyper() {
    return MavenPluginActivator.getDefault().getArchetypeManager().getArchetyper();
  }

  /**
   * Factory for Nexus Indexer ArchetypeCatalog
   */
  public static class NexusIndexerCatalogFactory extends ArchetypeCatalogFactory {
    public static final String ID = "nexusIndexer"; //$NON-NLS-1$

    public NexusIndexerCatalogFactory() {
      super(ID, Messages.ArchetypeCatalogFactory_indexer_catalog, false);
    }

    public ArchetypeCatalog getArchetypeCatalog() throws CoreException {
      try {
        ArchetypeDataSource source = MavenPluginActivator.getDefault().getIndexManager().getArchetypeCatalog();
        return source.getArchetypeCatalog(new Properties());
      } catch(ArchetypeDataSourceException ex) {
        String msg = NLS.bind(Messages.ArchetypeCatalogFactory_error_missing_catalog, ex.getMessage());
        log.error(msg, ex);
        throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, msg, ex));
      }
    }

  }

  /**
   * Factory for internal ArchetypeCatalog
   */
  public static class InternalCatalogFactory extends ArchetypeCatalogFactory {
    public static final String ID = "internal"; //$NON-NLS-1$

    public InternalCatalogFactory() {
      super(ID, Messages.ArchetypeCatalogFactory_internal, false);
    }

    public ArchetypeCatalog getArchetypeCatalog() {
      return getArchetyper().getInternalCatalog();
    }
  }

  /**
   * Factory for default local ArchetypeCatalog
   */
  public static class DefaultLocalCatalogFactory extends ArchetypeCatalogFactory {
    public static final String ID = "defaultLocal"; //$NON-NLS-1$

    public DefaultLocalCatalogFactory() {
      super(ID, Messages.ArchetypeCatalogFactory_default_local, false);
    }

    public ArchetypeCatalog getArchetypeCatalog() {
      return getArchetyper().getDefaultLocalCatalog();
    }
  }

  /**
   * Factory for local ArchetypeCatalog
   */
  public static class LocalCatalogFactory extends ArchetypeCatalogFactory {

    public LocalCatalogFactory(String path, String description, boolean editable) {
      this(path, description, editable, true);
    }

    public LocalCatalogFactory(String path, String description, boolean editable, boolean enabled) {
      super(path, description == null || description.trim().length() == 0
          ? NLS.bind(Messages.ArchetypeCatalogFactory_local, path)
          : description, editable, enabled);
    }

    public ArchetypeCatalog getArchetypeCatalog() throws CoreException {
      ArchetypeCatalog catalog = getEmbeddedCatalog();
      if(catalog == null) {
        //local but not embedded catalog
        catalog = getArchetyper().getLocalCatalog(getId());
      }
      return catalog;
    }

    private ArchetypeCatalog getEmbeddedCatalog() throws CoreException {
      URL url = getEmbeddedUrl();
      if(url == null) {
        //Not an embedded catalog, nothing else to do
        return null;
      }
      try (InputStream is = new BufferedInputStream(url.openStream())) {
        return new ArchetypeCatalogXpp3Reader().read(is);
      } catch(Exception ex) {
        String msg = NLS.bind(Messages.ArchetypeCatalogFactory_error_missing_catalog, ex.getMessage());
        log.error(msg, ex);
        throw new CoreException(new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, -1, msg, ex));
      }
    }

    private URL getEmbeddedUrl() {
      String path = getId();
      if(path != null && path.startsWith("bundleentry://")) {
        try {
          return new URL(path);
        } catch(Exception ex) {
          log.error(ex.getMessage(), ex);
        }
      }
      return null;
    }
  }

  /**
   * Factory for remote ArchetypeCatalog
   */
  public static class RemoteCatalogFactory extends ArchetypeCatalogFactory {

    private String repositoryUrl = null;

    public RemoteCatalogFactory(String url, String description, boolean editable) {
      this(url, description, editable, true);
    }

    public RemoteCatalogFactory(String url, String description, boolean editable, boolean enabled) {
      super(url, description == null || description.trim().length() == 0
          ? NLS.bind(Messages.ArchetypeCatalogFactory_remote, url)
          : description, editable, enabled);
      repositoryUrl = parseCatalogUrl(url);
    }

    /**
     * @param url
     * @return
     */
    private String parseCatalogUrl(String url) {
      if(url == null) {
        return null;
      }
      int length = url.length();
      if(length > 1 && url.endsWith("/")) //$NON-NLS-1$
      {
        return url.substring(0, url.length() - 1);
      }
      int idx = url.lastIndexOf("/"); //$NON-NLS-1$
      idx = (idx > 0) ? idx : 0;
      if(url.lastIndexOf(".") >= idx) { //$NON-NLS-1$
        //Assume last fragment of the url is a file, let's keep its parent folder
        return url.substring(0, idx);
      }
      return url;
    }

    public ArchetypeCatalog getArchetypeCatalog() {
      String url = getId();
      int idx = url.lastIndexOf("/archetype-catalog.xml");
      if(idx > -1) {
        url = url.substring(0, idx);
      }
      final ArchetypeCatalog catalog = getArchetyper().getRemoteCatalog(url);
      final String remoteUrl = url;
      @SuppressWarnings("serial")
      ArchetypeCatalog catalogWrapper = new ArchetypeCatalog() {
        public void addArchetype(Archetype archetype) {
          catalog.addArchetype(archetype);
        }

        public List<Archetype> getArchetypes() {
          List<Archetype> archetypes = new ArrayList<>(catalog.getArchetypes());
          for(Archetype arch : archetypes) {
            if(arch.getRepository() == null || arch.getRepository().trim().isEmpty()) {
              arch.setRepository(remoteUrl);
            }
          }
          return archetypes;
        }

        public String getModelEncoding() {
          return catalog.getModelEncoding();
        }

        public void removeArchetype(Archetype archetype) {
          catalog.removeArchetype(archetype);
        }

        public void setModelEncoding(String modelEncoding) {
          catalog.setModelEncoding(modelEncoding);
        }

        public void setArchetypes(List<Archetype> archetypes) {
          catalog.setArchetypes(archetypes);
        }

        public String toString() {
          return catalog.toString();
        }
      };

      return catalogWrapper;
    }

    /**
     * @return the url of the remote repository hosting the catalog
     */
    public String getRepositoryUrl() {
      return repositoryUrl;
    }
  }

}

Back to the top