Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de52ffcc3d6b7baba2e47102ae0ce207607a5d59 (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
/*******************************************************************************
 * 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.index.nexus;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

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

import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;

import org.apache.maven.index.Field;
import org.apache.maven.index.MAVEN;

import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.internal.index.IIndex;
import org.eclipse.m2e.core.internal.index.IMutableIndex;
import org.eclipse.m2e.core.internal.index.IndexedArtifact;
import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
import org.eclipse.m2e.core.internal.index.SearchExpression;
import org.eclipse.m2e.core.repository.IRepository;


/**
 * NexusIndex
 * 
 * @author igor
 */
public class NexusIndex implements IIndex, IMutableIndex {

  /**
   * Repository index is disabled.
   */
  public static final String DETAILS_DISABLED = "off"; //$NON-NLS-1$

  /**
   * Only artifact index information is used. Classname index is disabled.
   */
  public static final String DETAILS_MIN = "min"; //$NON-NLS-1$

  /**
   * Both artifact and classname indexes are used.
   */
  public static final String DETAILS_FULL = "full"; //$NON-NLS-1$

  private final NexusIndexManager indexManager;

  private final IRepository repository;

  private final String indexDetails;

  NexusIndex(NexusIndexManager indexManager, IRepository repository, String indexDetails) {
    this.indexManager = indexManager;
    this.repository = repository;
    this.indexDetails = indexDetails;
  }

  public String getRepositoryUrl() {
    return this.repository.getUrl();
  }

  public String getIndexDetails() {
    return this.indexDetails;
  }

  public void addArtifact(File pomFile, ArtifactKey artifactKey) {
    indexManager.addDocument(repository, pomFile, artifactKey);
  }

  public void removeArtifact(File pomFile, ArtifactKey artifactKey) {
    indexManager.removeDocument(repository, pomFile, artifactKey, null);
  }

  public Collection<IndexedArtifact> find(SearchExpression groupId, SearchExpression artifactId,
      SearchExpression version, SearchExpression packaging) throws CoreException {
    return find(wrapIfNotNull(groupId), wrapIfNotNull(artifactId), wrapIfNotNull(version), wrapIfNotNull(packaging));
  }

  /**
   * Method wrapping one SearchExpression into a collection, if it is not null.
   * 
   * @param sex
   * @return
   */
  private Collection<SearchExpression> wrapIfNotNull(SearchExpression se) {
    if(se == null) {
      return null;
    }
    return Collections.singleton(se);
  }

  public Collection<IndexedArtifact> find(Collection<SearchExpression> groupId,
      Collection<SearchExpression> artifactId, Collection<SearchExpression> version,
      Collection<SearchExpression> packaging) throws CoreException {
    BooleanQuery query = new BooleanQuery();

    addQueryFromSearchExpressionCollection(query, MAVEN.PACKAGING, packaging);

    addQueryFromSearchExpressionCollection(query, MAVEN.GROUP_ID, groupId);

    addQueryFromSearchExpressionCollection(query, MAVEN.ARTIFACT_ID, artifactId);

    addQueryFromSearchExpressionCollection(query, MAVEN.VERSION, version);

    return indexManager.search(repository, query).values();
  }

  private void addQueryFromSearchExpressionCollection(final BooleanQuery query, final Field field,
      final Collection<SearchExpression> sec) {
    if(sec != null && !sec.isEmpty()) {
      if(sec.size() > 1) {
        BooleanQuery q = new BooleanQuery();
        for(SearchExpression se : sec) {
          q.add(indexManager.constructQuery(field, se), Occur.SHOULD);
        }
        query.add(q, Occur.MUST);
      } else {
        query.add(indexManager.constructQuery(field, sec.iterator().next()), Occur.MUST);
      }
    }
  }

  public IndexedArtifactFile getIndexedArtifactFile(ArtifactKey artifact) throws CoreException {
    return indexManager.getIndexedArtifactFile(repository, artifact);
  }

  public IndexedArtifactFile identify(File file) throws CoreException {
    return indexManager.identify(repository, file);
  }

  public void updateIndex(boolean force, IProgressMonitor monitor) throws CoreException {
    indexManager.updateIndex(repository, force, monitor);
  }

  public void scheduleIndexUpdate(boolean force) {
    indexManager.scheduleIndexUpdate(repository, force);
  }

  public IndexedArtifactGroup[] getRootIndexedArtifactGroups() throws CoreException {
    return indexManager.getRootIndexedArtifactGroups(repository);
  }

  public boolean isUpdating() {
    return indexManager.isUpdatingIndex(repository);
  }

  public IRepository getRepository() {
    return repository;
  }

  public boolean isEnabled() {
    return DETAILS_MIN.equals(indexDetails) || DETAILS_FULL.equals(indexDetails);
  }

  public void setIndexDetails(String details) throws CoreException {
    indexManager.setIndexDetails(repository, details, null/*async*/);
  }

  public Map<String, IndexedArtifact> search(SearchExpression term, String searchType) throws CoreException {
    return indexManager.search(getRepository(), term, searchType);
  }

  public Map<String, IndexedArtifact> search(SearchExpression term, String searchType, int classifier)
      throws CoreException {
    return indexManager.search(getRepository(), term, searchType, classifier);
  }
}

Back to the top