This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 186939
Collapse All | Expand All

(-)a/maven.indexer/nbproject/project.xml (+5 lines)
Lines 164-169 Link Here
164
                        <recursive/>
164
                        <recursive/>
165
                        <compile-dependency/>
165
                        <compile-dependency/>
166
                    </test-dependency>
166
                    </test-dependency>
167
                    <test-dependency>
168
                        <code-name-base>org.openide.util</code-name-base>
169
                        <compile-dependency/>
170
                        <test/>
171
                    </test-dependency>
167
                </test-type>
172
                </test-type>
168
            </test-dependencies>
173
            </test-dependencies>
169
            <friend-packages>
174
            <friend-packages>
(-)a/maven.indexer/src/org/netbeans/modules/maven/indexer/api/PluginIndexManager.java (-364 / +179 lines)
Lines 39-136 Link Here
39
 *
39
 *
40
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
40
 * Portions Copyrighted 2009 Sun Microsystems, Inc.
41
 */
41
 */
42
42
package org.netbeans.modules.maven.indexer.api;
43
package org.netbeans.modules.maven.indexer.api;
43
44
44
import org.codehaus.plexus.util.Base64;
45
import org.codehaus.plexus.util.FileUtils;
46
import org.codehaus.plexus.util.IOUtil;
47
import org.codehaus.plexus.util.StringUtils;
48
import java.io.BufferedOutputStream;
49
import java.io.File;
45
import java.io.File;
50
import java.io.FileOutputStream;
46
import java.util.Collections;
51
import java.io.IOException;
52
import java.io.InputStream;
53
import java.util.ArrayList;
54
import java.util.Arrays;
55
import java.util.BitSet;
56
import java.util.Comparator;
47
import java.util.Comparator;
57
import java.util.LinkedHashMap;
58
import java.util.List;
48
import java.util.List;
59
import java.util.Map;
49
import java.util.Map;
60
import java.util.Set;
50
import java.util.Set;
61
import java.util.TreeSet;
51
import java.util.TreeSet;
62
import java.util.zip.ZipEntry;
52
import java.util.logging.Level;
63
import java.util.zip.ZipInputStream;
53
import java.util.logging.Logger;
64
import org.apache.lucene.document.Document;
54
import org.apache.maven.index.ArtifactInfo;
65
import org.apache.lucene.document.Field;
55
import org.netbeans.api.annotations.common.CheckForNull;
66
import org.apache.lucene.index.IndexReader;
56
import org.netbeans.api.annotations.common.NullAllowed;
67
import org.apache.lucene.index.Term;
68
import org.apache.lucene.search.BooleanClause;
69
import org.apache.lucene.search.BooleanQuery;
70
import org.apache.lucene.search.Collector;
71
import org.apache.lucene.search.IndexSearcher;
72
import org.apache.lucene.search.PrefixQuery;
73
import org.apache.lucene.search.Scorer;
74
import org.apache.lucene.search.TermQuery;
75
import org.apache.lucene.store.FSDirectory;
76
import org.openide.filesystems.FileUtil;
77
import org.openide.util.Exceptions;
78
import org.openide.util.NbBundle;
57
import org.openide.util.NbBundle;
58
import org.openide.xml.XMLUtil;
59
import org.w3c.dom.Document;
60
import org.w3c.dom.Element;
61
import org.xml.sax.InputSource;
79
62
80
/**
63
/**
81
 * Provides information about available plugins, including their goals and parameters.
64
 * Provides information about available plugins, including their goals and parameters.
82
 * @author mkleint
83
 */
65
 */
84
public class PluginIndexManager {
66
public class PluginIndexManager {
85
67
86
    private static final String ZIP_LOCATION = "org/netbeans/modules/maven/indexer/pluginz.zip"; //NOI18N
68
    private static final Logger LOG = Logger.getLogger(PluginIndexManager.class.getName());
87
88
    private static final String INDEX_PATH = "maven-plugins-index"; //NOI18N
89
    private static IndexReader indexReader;
90
    
91
    /**
92
     * groupId + "|" + artifactId + "|" + version;
93
     */
94
    private static String FIELD_ID = "id";//NOI18N
95
    /**
96
     * 2.0.x or similar name of maven core. a document has either this or id field.
97
     */
98
    private static String FIELD_MVN_VERSION = "mvn";//NOI18N
99
    /**
100
     * space separated list of goal names
101
     */
102
    private static String FIELD_GOALS = "gls";//NOI18N
103
    /**
104
     * goal prefix
105
     */
106
    private static String FIELD_PREFIX = "prfx";//NOI18N
107
    
108
    /**
109
     * | is the separator
110
     * [0] - name
111
     * [1] - editable
112
     * [2] - required
113
     * [3] - expression or "null"
114
     * [4] - default value or "null"
115
     */
116
    private static String PREFIX_FIELD_GOAL = "mj_";//NOI18N
117
    /**
118
     * space separated list of lifecycles/packagings
119
     */
120
    private static String FIELD_CYCLES = "ccls";//NOI18N
121
    private static String PREFIX_FIELD_CYCLE = "ccl_";//NOI18N
122
123
124
    final static int BUFFER = 2048;
125
126
    private static synchronized IndexSearcher getIndexSearcher() throws Exception {
127
        if (indexReader == null) {
128
            FSDirectory dir = FSDirectory.open(getDefaultIndexLocation());
129
            indexReader = IndexReader.open(dir);
130
        }
131
        //TODO shall the searcher be stored as field??
132
        return new IndexSearcher(indexReader);
133
    }
134
69
135
    /**
70
    /**
136
     * Gets available goals from known plugins.
71
     * Gets available goals from known plugins.
Lines 138-164 Link Here
138
     * @return e.g. {@code [..., dependency:copy, ..., release:perform, ...]}
73
     * @return e.g. {@code [..., dependency:copy, ..., release:perform, ...]}
139
     */
74
     */
140
    public static Set<String> getPluginGoalNames(Set<String> groups) throws Exception {
75
    public static Set<String> getPluginGoalNames(Set<String> groups) throws Exception {
141
        IndexSearcher searcher = getIndexSearcher();
76
        Set<String> result = new TreeSet<String>();
142
        BooleanQuery bq = new BooleanQuery();
77
        RepositoryInfo[] infos = RepositoryQueries.getLoadedContexts().toArray(new RepositoryInfo[0]);
143
        for (String grp : groups) {
78
        // XXX rather use ArtifactInfo.PLUGIN_GOALS
144
            PrefixQuery pq = new PrefixQuery(new Term(FIELD_ID, grp));
79
        for (String groupId : groups) {
145
            bq.add(new BooleanClause(pq, BooleanClause.Occur.SHOULD));
80
            for (String artifactId : RepositoryQueries.filterPluginArtifactIds(groupId, "", infos)) {
146
        }
81
                for (NBVersionInfo v : RepositoryQueries.getVersions(groupId, artifactId, infos)) {
147
        final BitSetCollector searchRes = new BitSetCollector();
82
                    if (v.getVersion().endsWith("-SNAPSHOT") && !v.getRepoId().equals("local")) {
148
        searcher.search(bq, searchRes);
83
                        continue;
149
        final BitSet bitSet = searchRes.getMatchedDocs();
84
                    }
150
        TreeSet<String> toRet = new TreeSet<String>();
85
                    File jar = RepositoryUtil.downloadArtifact(v);
151
        for (int docNum = bitSet.nextSetBit(0); docNum >= 0; docNum = bitSet.nextSetBit(docNum+1)) {
86
                    Document pluginXml = loadPluginXml(jar);
152
            Document doc = searcher.getIndexReader().document(docNum);
87
                    if (pluginXml == null) {
153
            //TODO shall we somehow pick just one version fom a given plugin here? how?
88
                        continue;
154
            String prefix = doc.getField(FIELD_PREFIX).stringValue();
89
                    }
155
            String goals = doc.getField(FIELD_GOALS).stringValue();
90
                    Element root = pluginXml.getDocumentElement();
156
            String[] gls = StringUtils.split(goals, " ");//NOI18N
91
                    Element goalPrefix = XMLUtil.findElement(root, "goalPrefix", null);
157
            for (String goal : gls) {
92
                    if (goalPrefix == null) {
158
                toRet.add(prefix + ":" + goal); //NOI18N
93
                        LOG.log(Level.WARNING, "no goalPrefix in {0}", jar);
94
                        continue;
95
                    }
96
                    Element mojos = XMLUtil.findElement(root, "mojos", null);
97
                    if (mojos == null) {
98
                        LOG.log(Level.WARNING, "no mojos in {0}", jar);
99
                        continue;
100
                    }
101
                    for (Element mojo : XMLUtil.findSubElements(mojos)) {
102
                        if (!mojo.getTagName().equals("mojo")) {
103
                            continue;
104
                        }
105
                        Element goal = XMLUtil.findElement(mojo, "goal", null);
106
                        if (goal == null) {
107
                            LOG.log(Level.WARNING, "mojo missing goal in {0}", jar);
108
                            continue;
109
                        }
110
                        result.add(XMLUtil.findText(goalPrefix) + ':' + XMLUtil.findText(goal));
111
                    }
112
                    break;
113
                }
159
            }
114
            }
160
        }
115
        }
161
        return toRet;
116
        LOG.log(Level.FINE, "found goal names: {0}", result);
117
        return result;
162
    }
118
    }
163
119
164
    /**
120
    /**
Lines 170-194 Link Here
170
     */
126
     */
171
    public static Set<String> getPluginGoals(String groupId, String artifactId, String version) throws Exception {
127
    public static Set<String> getPluginGoals(String groupId, String artifactId, String version) throws Exception {
172
        assert groupId != null && artifactId != null && version != null;
128
        assert groupId != null && artifactId != null && version != null;
173
        IndexSearcher searcher = getIndexSearcher();
129
        for (NBVersionInfo v : RepositoryQueries.getVersions(groupId, artifactId, RepositoryQueries.getLoadedContexts().toArray(new RepositoryInfo[0]))) {
174
        String id = groupId + "|" + artifactId + "|" + version; //NOI18N
130
            if (!v.getVersion().equals(version)) {
175
        TermQuery tq = new TermQuery(new Term(FIELD_ID, id));
131
                continue;
176
        final BitSetCollector searchRes = new BitSetCollector();
132
            }
177
        searcher.search(tq, searchRes);
133
            File jar = RepositoryUtil.downloadArtifact(v);
178
        final BitSet bitSet = searchRes.getMatchedDocs();
134
            Document pluginXml = loadPluginXml(jar);
179
        if (bitSet.isEmpty()) {
135
            if (pluginXml == null) {
180
            return null;
136
                continue;
137
            }
138
            Element root = pluginXml.getDocumentElement();
139
            Element mojos = XMLUtil.findElement(root, "mojos", null);
140
            if (mojos == null) {
141
                LOG.log(Level.WARNING, "no mojos in {0}", jar);
142
                continue;
143
            }
144
            Set<String> goals = new TreeSet<String>();
145
            for (Element mojo : XMLUtil.findSubElements(mojos)) {
146
                if (!mojo.getTagName().equals("mojo")) {
147
                    continue;
148
                }
149
                Element goal = XMLUtil.findElement(mojo, "goal", null);
150
                if (goal == null) {
151
                    LOG.log(Level.WARNING, "mojo missing goal in {0}", jar);
152
                    continue;
153
                }
154
                goals.add(XMLUtil.findText(goal));
155
            }
156
            LOG.log(Level.FINE, "found goals: {0}", goals);
157
            return goals;
181
        }
158
        }
182
        TreeSet<String> toRet = new TreeSet<String>();
159
        return Collections.emptySet();
183
        for (int docNum = bitSet.nextSetBit(0); docNum >= 0; docNum = bitSet.nextSetBit(docNum+1)) {
184
            Document doc = searcher.getIndexReader().document(docNum);
185
            String goals = doc.getField(FIELD_GOALS).stringValue();
186
            String[] gls = StringUtils.split(goals, " "); //NOI18N
187
            for (String goal : gls) {
188
                toRet.add(goal);
189
            }
190
        }
191
        return toRet;
192
    }
160
    }
193
161
194
    /**
162
    /**
Lines 199-255 Link Here
199
     * @param mojo e.g. {@code "compile"}
167
     * @param mojo e.g. {@code "compile"}
200
     * @return null if not found, else e.g. {@code [..., <verbose>${maven.compiler.verbose}=false</>, ...]}
168
     * @return null if not found, else e.g. {@code [..., <verbose>${maven.compiler.verbose}=false</>, ...]}
201
     */
169
     */
202
    public static Set<ParameterDetail> getPluginParameters(String groupId, String artifactId, String version, String mojo) throws Exception {
170
    public static @CheckForNull Set<ParameterDetail> getPluginParameters(String groupId, String artifactId, String version, @NullAllowed String mojo) throws Exception {
203
        assert groupId != null && artifactId != null && version != null;
171
        assert groupId != null && artifactId != null && version != null;
204
        IndexSearcher searcher = getIndexSearcher();
172
        for (NBVersionInfo v : RepositoryQueries.getVersions(groupId, artifactId, RepositoryQueries.getLoadedContexts().toArray(new RepositoryInfo[0]))) {
205
        String id = groupId + "|" + artifactId + "|" + version; //NOI18N
173
            if (!v.getVersion().equals(version)) {
206
        TermQuery tq = new TermQuery(new Term(FIELD_ID, id));
174
                continue;
207
        final BitSetCollector searchRes = new BitSetCollector();
175
            }
208
        searcher.search(tq, searchRes);
176
            File jar = RepositoryUtil.downloadArtifact(v);
209
        final BitSet bitSet = searchRes.getMatchedDocs();
177
            Document pluginXml = loadPluginXml(jar);
210
        if (bitSet.isEmpty()) {
178
            if (pluginXml == null) {
211
            return null;
179
                continue;
212
        }        
180
            }
213
        TreeSet<ParameterDetail> toRet = new TreeSet<ParameterDetail>(new PComparator());
181
            Element root = pluginXml.getDocumentElement();
214
        for (int docNum = bitSet.nextSetBit(0); docNum >= 0; docNum = bitSet.nextSetBit(docNum+1)) {
182
            Element mojos = XMLUtil.findElement(root, "mojos", null);
215
            Document doc = searcher.getIndexReader().document(docNum);
183
            if (mojos == null) {
216
            String goals = doc.getField(FIELD_GOALS).stringValue();
184
                LOG.log(Level.WARNING, "no mojos in {0}", jar);
217
            String[] gls = StringUtils.split(goals, " "); //NOI18N
185
                continue;
218
            for (String goal : gls) {
186
            }
219
                if (mojo == null || mojo.equals(goal)) {
187
            for (Element mojoEl : XMLUtil.findSubElements(mojos)) {
220
                    String params = doc.getField(PREFIX_FIELD_GOAL + goal).stringValue();
188
                if (!mojoEl.getTagName().equals("mojo")) {
221
                    String[] lines = StringUtils.split(params, "\n"); //NOI18N
189
                    continue;
222
                    for (String line : lines) {
190
                }
223
                        String[] paramDet = StringUtils.split(line, "|"); //NOI18N
191
                Element goal = XMLUtil.findElement(mojoEl, "goal", null);
224
                        String name = paramDet[0];
192
                if (goal == null) {
225
                        String editable = paramDet[1];
193
                    LOG.log(Level.WARNING, "mojo missing goal in {0}", jar);
226
                        String required = paramDet[2];
194
                    continue;
227
                        boolean req = "true".equals(required); //NOI18N
195
                }
228
                        if ("true".equals(editable)) { //NOI18N
196
                if (mojo != null && !mojo.equals(XMLUtil.findText(goal))) {
229
                            String expr = paramDet[3];
197
                    continue;
230
                            if (expr != null && "null".equals(expr)) { //NOI18N
198
                }
231
                                expr = null;
199
                Set<ParameterDetail> params = new TreeSet<ParameterDetail>(new Comparator<ParameterDetail>() {
200
                    @Override public int compare(ParameterDetail o1, ParameterDetail o2) {
201
                        return o1.getName().compareTo(o2.getName());
202
                    }
203
                });
204
                Element parameters = XMLUtil.findElement(mojoEl, "parameters", null);
205
                Element configuration = XMLUtil.findElement(mojoEl, "configuration", null);
206
                if (parameters != null) {
207
                    for (Element parameter : XMLUtil.findSubElements(parameters)) {
208
                        if (!parameter.getTagName().equals("parameter")) {
209
                            continue;
210
                        }
211
                        Element name = XMLUtil.findElement(parameter, "name", null);
212
                        if (name == null) {
213
                            LOG.log(Level.WARNING, "parameter missing name in {0}", jar);
214
                        }
215
                        Element description = XMLUtil.findElement(parameter, "description", null);
216
                        if (description == null) {
217
                            LOG.log(Level.WARNING, "parameter missing description in {0}", jar);
218
                        }
219
                        Element required = XMLUtil.findElement(parameter, "required", null);
220
                        if (required == null) {
221
                            LOG.log(Level.WARNING, "parameter missing required in {0}", jar);
222
                        }
223
                        String defaultValue = null;
224
                        String expression = null;
225
                        if (configuration != null) {
226
                            Element sample = XMLUtil.findElement(configuration, XMLUtil.findText(name), null);
227
                            if (sample != null) {
228
                                defaultValue = sample.getAttribute("default-value");
229
                                if (defaultValue.isEmpty()) {
230
                                    defaultValue = null;
231
                                }
232
                                String expressionWithSheBraces = XMLUtil.findText(sample);
233
                                if (expressionWithSheBraces != null && expressionWithSheBraces.matches("[$][{].+[}]")) {
234
                                    expression = expressionWithSheBraces.substring(2, expressionWithSheBraces.length() - 1);
235
                                }
232
                            }
236
                            }
233
                            String defVal = paramDet[4];
234
                            if (defVal != null && "null".equals(defVal)) { //NOI18N
235
                                defVal = null;
236
                            }
237
                            String desc;
238
                            if (paramDet.length > 5) {
239
                                desc = paramDet[5];
240
                                byte[] dec = Base64.decodeBase64(desc.getBytes());
241
                                desc =  new String(dec, "UTF-8"); //NOI18N
242
                            } else {
243
                                desc = null;
244
                            }
245
                            ParameterDetail pm = new ParameterDetail(name, expr, defVal, req, desc);
246
                            toRet.add(pm);
247
                        }
237
                        }
238
                        params.add(new ParameterDetail(XMLUtil.findText(name), expression, defaultValue, Boolean.parseBoolean(XMLUtil.findText(required)), XMLUtil.findText(description)));
248
                    }
239
                    }
249
                }
240
                }
241
                LOG.log(Level.FINE, "found params {0}", params);
242
                return params;
250
            }
243
            }
251
        }
244
        }
252
        return toRet;
245
        return null;
253
    }
246
    }
254
247
255
248
Lines 261-281 Link Here
261
     */
254
     */
262
    public static Set<String> getPluginsForGoalPrefix(String prefix) throws Exception {
255
    public static Set<String> getPluginsForGoalPrefix(String prefix) throws Exception {
263
        assert prefix != null;
256
        assert prefix != null;
264
        IndexSearcher searcher = getIndexSearcher();
257
        Set<String> result = new TreeSet<String>();
265
        TermQuery tq = new TermQuery(new Term(FIELD_PREFIX, prefix));
258
        RepositoryInfo[] infos = RepositoryQueries.getLoadedContexts().toArray(new RepositoryInfo[0]);
266
        final BitSetCollector searchRes = new BitSetCollector();
259
        QueryField qf = new QueryField();
267
        searcher.search(tq, searchRes);
260
        qf.setField(ArtifactInfo.PLUGIN_PREFIX);
268
        final BitSet bitSet = searchRes.getMatchedDocs();
261
        qf.setValue(prefix);
269
        if (bitSet.isEmpty()) {
262
        qf.setOccur(QueryField.OCCUR_MUST);
270
            return null;
263
        qf.setMatch(QueryField.MATCH_EXACT);
264
        // XXX why does this not work in fresh userdir?
265
        for (NBVersionInfo v : RepositoryQueries.find(Collections.singletonList(qf), infos)) {
266
            result.add(v.getGroupId() + '|' + v.getArtifactId() + '|' + v.getVersion());
271
        }
267
        }
272
        TreeSet<String> toRet = new TreeSet<String>();
268
        LOG.log(Level.FINE, "found plugins {0}", result);
273
        for (int docNum = bitSet.nextSetBit(0); docNum >= 0; docNum = bitSet.nextSetBit(docNum+1)) {
269
        return result;
274
            Document doc = searcher.getIndexReader().document(docNum);
275
            String id = doc.getField(FIELD_ID).stringValue();
276
            toRet.add(id);
277
        }
278
        return toRet;
279
    }
270
    }
280
271
281
    /**
272
    /**
Lines 287-448 Link Here
287
     */
278
     */
288
    public static Map<String, List<String>> getLifecyclePlugins(String packaging, String mvnVersion, String[] extensionPlugins) throws Exception {
279
    public static Map<String, List<String>> getLifecyclePlugins(String packaging, String mvnVersion, String[] extensionPlugins) throws Exception {
289
        assert packaging != null;
280
        assert packaging != null;
290
        IndexSearcher searcher = getIndexSearcher();
281
        return Collections.emptyMap();//XXX
291
        BooleanQuery bq = new BooleanQuery();
282
    }
292
        TermQuery tq = new TermQuery(new Term(FIELD_CYCLES, packaging));
293
        bq.add(tq, BooleanClause.Occur.MUST);
294
        if (mvnVersion == null) {
295
            mvnVersion = "2.0.9"; //oh well we need something.. //NOI18N
296
        }
297
        BooleanQuery bq2 = new BooleanQuery();
298
        tq = new TermQuery(new Term(FIELD_MVN_VERSION, mvnVersion));
299
        bq2.add(tq, BooleanClause.Occur.SHOULD);
300
283
301
        for (String ext : extensionPlugins) {
284
    private static @CheckForNull Document loadPluginXml(File jar) {
302
            tq = new TermQuery(new Term(FIELD_ID, ext));
285
        if (!jar.isFile() || !jar.getName().endsWith(".jar")) {
303
            bq2.add(tq, BooleanClause.Occur.SHOULD);
304
        }
305
        bq.add(bq2, BooleanClause.Occur.SHOULD); //why doesn't MUST work?
306
307
        final BitSetCollector searchRes = new BitSetCollector();
308
        searcher.search(bq, searchRes);
309
        final BitSet bitSet = searchRes.getMatchedDocs();
310
        if (bitSet.isEmpty()) {
311
            return null;
286
            return null;
312
        }
287
        }
313
        LinkedHashMap<String, List<String>> toRet = new LinkedHashMap<String, List<String>>();
288
        LOG.log(Level.FINE, "parsing plugin.xml from {0}", jar);
314
        for (int docNum = bitSet.nextSetBit(0); docNum >= 0; docNum = bitSet.nextSetBit(docNum+1)) {
315
            Document doc = searcher.getIndexReader().document(docNum);
316
            Field prefixed = doc.getField(PREFIX_FIELD_CYCLE + packaging);
317
            if (prefixed != null) {
318
                String mapping = prefixed.stringValue();
319
                String[] phases = StringUtils.split(mapping, "\n"); //NOI18N
320
                for (String phase : phases) {
321
                    String[] ph = StringUtils.split(phase, "="); //NOI18N
322
                    String[] plugins = StringUtils.split(ph[1], ","); //NOI18N
323
                    List<String> plgs = new ArrayList<String>(Arrays.asList(plugins));
324
                    toRet.put(ph[0], plgs);
325
                }
326
            }
327
        }
328
        return toRet;
329
    }
330
331
332
    private static int checkLocalVersion(File[] fls) {
333
        for (File fl : fls) {
334
            try {
289
            try {
335
                int intVersion = Integer.parseInt(fl.getName());
290
            return XMLUtil.parse(new InputSource("jar:" + jar.toURI() + "!/META-INF/maven/plugin.xml"), false, false, XMLUtil.defaultErrorHandler(), null);
336
                return intVersion;
291
        } catch (Exception x) {
337
            } catch (NumberFormatException e) {
292
            LOG.log(Level.INFO, "could not parse " + jar, x);
338
                Exceptions.printStackTrace(e);
293
            return null;
339
            }
340
        }
341
        //if there is a folder, but not a number, return max value to be sure
342
        //we don't overwrite stuff..
343
        return fls.length > 0 ? Integer.MAX_VALUE : 0;
344
    }
345
346
    private static int checkZipVersion(File cacheDir) {
347
        InputStream is = null;
348
        try {
349
            is = PluginIndexManager.class.getClassLoader().getResourceAsStream(ZIP_LOCATION); //NOI18N
350
            ZipInputStream zis = new ZipInputStream(is);
351
            ZipEntry entry = zis.getNextEntry();
352
            if (entry != null) {
353
                File fl = new File(cacheDir, entry.getName());
354
                if (!fl.getParentFile().equals(cacheDir)) {
355
                    String version = fl.getParentFile().getName();
356
                    try {
357
                        int intVersion = Integer.parseInt(version);
358
                        return intVersion;
359
                    } catch (NumberFormatException e) {
360
                        Exceptions.printStackTrace(e);
361
                    }
362
                }
363
            }
364
        } catch (IOException io) {
365
            Exceptions.printStackTrace(io);
366
        } finally {
367
            IOUtil.close(is);
368
        }
369
        return 0; //a fallback
370
    }
371
372
    private static File getDefaultIndexLocation() {
373
        String userdir = System.getProperty("netbeans.user"); //NOI18N
374
        File cacheDir;
375
        if (userdir != null) {
376
            cacheDir = new File(new File(new File(userdir, "var"), "cache"), INDEX_PATH);//NOI18N
377
        } else {
378
            File root = FileUtil.toFile(FileUtil.getConfigRoot());
379
            cacheDir = new File(root, INDEX_PATH);//NOI18N
380
        }
381
        cacheDir.mkdirs();
382
        File[] fls = cacheDir.listFiles();
383
        if (fls == null || fls.length == 0) {
384
            //copy the preexisting index in module into place..
385
            InputStream is = null;
386
            try {
387
                is = PluginIndexManager.class.getClassLoader().getResourceAsStream(ZIP_LOCATION); //NOI18N
388
                ZipInputStream zis = new ZipInputStream(is);
389
                unzip(zis, cacheDir);
390
            } finally {
391
                IOUtil.close(is);
392
            }
393
        } else {
394
            int zipped = checkZipVersion(cacheDir);
395
            int local = checkLocalVersion(fls);
396
            if (zipped > local && local > 0) {
397
                try {
398
                    FileUtils.deleteDirectory(new File(cacheDir, "" + local));
399
                    //copy the preexisting index in module into place..
400
                    InputStream is = null;
401
                    try {
402
                        is = PluginIndexManager.class.getClassLoader().getResourceAsStream(ZIP_LOCATION); //NOI18N
403
                        ZipInputStream zis = new ZipInputStream(is);
404
                        unzip(zis, cacheDir);
405
                    } finally {
406
                        IOUtil.close(is);
407
                    }
408
                } catch (IOException ex) {
409
                    Exceptions.printStackTrace(ex);
410
                }
411
            }
412
        }
413
        File[] files = cacheDir.listFiles();
414
        assert files != null && files.length == 1;
415
        cacheDir = files[0];
416
        return cacheDir;
417
    }
418
419
    private static void unzip(ZipInputStream zis, File cacheDir) {
420
        try {
421
            BufferedOutputStream dest = null;
422
            ZipEntry entry;
423
            while ((entry = zis.getNextEntry()) != null) {
424
                int count;
425
                byte data[] = new byte[BUFFER];
426
                File fl = new File(cacheDir, entry.getName());
427
                fl.getParentFile().mkdirs();
428
                FileOutputStream fos = new FileOutputStream(fl);
429
                dest = new BufferedOutputStream(fos, BUFFER);
430
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
431
                    dest.write(data, 0, count);
432
                }
433
                dest.flush();
434
                dest.close();
435
            }
436
        } catch (Exception e) {
437
            e.printStackTrace();
438
        } finally {
439
            IOUtil.close(zis);
440
        }
441
    }
442
443
    private static class PComparator implements Comparator<ParameterDetail> {
444
        public int compare(ParameterDetail o1, ParameterDetail o2) {
445
            return o1.getName().compareTo(o2.getName());
446
        }
294
        }
447
    }
295
    }
448
296
Lines 451-462 Link Here
451
     */
299
     */
452
    public static class ParameterDetail {
300
    public static class ParameterDetail {
453
        private String name;
301
        private String name;
454
        private String expression;
302
        private @NullAllowed String expression;
455
        private String defaultValue;
303
        private @NullAllowed String defaultValue;
456
        private boolean required;
304
        private boolean required;
457
        private String description;
305
        private String description;
458
306
459
        private ParameterDetail(String name, String expression, String defaultValue, boolean required, String description) {
307
        private ParameterDetail(String name, @NullAllowed String expression, @NullAllowed String defaultValue, boolean required, String description) {
460
            this.name = name;
308
            this.name = name;
461
            this.expression = expression;
309
            this.expression = expression;
462
            this.defaultValue = defaultValue;
310
            this.defaultValue = defaultValue;
Lines 467-473 Link Here
467
        /**
315
        /**
468
         * @return null, or e.g. {@code false}
316
         * @return null, or e.g. {@code false}
469
         */
317
         */
470
        public String getDefaultValue() {
318
        @CheckForNull public String getDefaultValue() {
471
            return defaultValue;
319
            return defaultValue;
472
        }
320
        }
473
321
Lines 478-489 Link Here
478
        /**
326
        /**
479
         * @return null, or e.g. {@code maven.compiler.verbose}
327
         * @return null, or e.g. {@code maven.compiler.verbose}
480
         */
328
         */
481
        public String getExpression() {
329
        @CheckForNull public String getExpression() {
482
            return expression;
330
            return expression;
483
        }
331
        }
484
332
485
        /**
333
        /**
486
         * @return null, or e.g. {@code verbose}
334
         * e.g. {@code verbose}
487
         */
335
         */
488
        public String getName() {
336
        public String getName() {
489
            return name;
337
            return name;
Lines 506-544 Link Here
506
354
507
    }
355
    }
508
356
509
357
    private PluginIndexManager() {}
510
    private static final class BitSetCollector extends Collector {
511
512
        private int docBase;
513
        private final BitSet bits = new BitSet();
514
515
        public BitSet getMatchedDocs() {
516
            return this.bits;
517
        }
518
519
        
520
        @Override
521
        public void setScorer(Scorer scorer) {
522
            //Todo: ignoring scorer for now, if ordering accoring to score needed
523
            // this will need to be implemented
524
        }
525
526
        // accept docs out of order (for a BitSet it doesn't matter)
527
        @Override
528
        public boolean acceptsDocsOutOfOrder() {
529
          return true;
530
        }
531
532
        @Override
533
        public void collect(int doc) {
534
          bits.set(doc + docBase);
535
        }
536
537
        @Override
538
        public void setNextReader(IndexReader reader, int docBase) {
539
          this.docBase = docBase;
540
        }
541
542
    }
543
358
544
}
359
}
(-)a/maven.indexer/src/org/netbeans/modules/maven/indexer/api/RepositoryUtil.java (+23 lines)
Lines 48-55 Link Here
48
import java.math.BigInteger;
48
import java.math.BigInteger;
49
import java.security.MessageDigest;
49
import java.security.MessageDigest;
50
import java.security.NoSuchAlgorithmException;
50
import java.security.NoSuchAlgorithmException;
51
import java.util.Collections;
52
import java.util.List;
51
import org.apache.maven.artifact.Artifact;
53
import org.apache.maven.artifact.Artifact;
52
import org.apache.maven.artifact.repository.ArtifactRepository;
54
import org.apache.maven.artifact.repository.ArtifactRepository;
55
import org.apache.maven.artifact.resolver.AbstractArtifactResolutionException;
53
import org.netbeans.modules.maven.embedder.EmbedderFactory;
56
import org.netbeans.modules.maven.embedder.EmbedderFactory;
54
import org.codehaus.plexus.util.IOUtil;
57
import org.codehaus.plexus.util.IOUtil;
55
import org.netbeans.modules.maven.embedder.MavenEmbedder;
58
import org.netbeans.modules.maven.embedder.MavenEmbedder;
Lines 94-99 Link Here
94
        return art;
97
        return art;
95
    }
98
    }
96
    
99
    
100
    /**
101
     * Tries to download an artifact.
102
     * @param info a version of an artifact
103
     * @return the file in the local repository (might not exist if download failed)
104
     * @throws AbstractArtifactResolutionException currently never?
105
     */
106
    static File downloadArtifact(NBVersionInfo info) throws AbstractArtifactResolutionException {
107
        Artifact a = createArtifact(info);
108
        MavenEmbedder online = EmbedderFactory.getOnlineEmbedder();
109
        List<ArtifactRepository> remotes;
110
        RepositoryInfo repo = RepositoryPreferences.getInstance().getRepositoryInfoById(info.getRepoId());
111
        if (repo != null && repo.isRemoteDownloadable()) {
112
            remotes = Collections.singletonList(EmbedderFactory.createRemoteRepository(online, repo.getRepositoryUrl(), repo.getId()));
113
        } else {
114
            remotes = Collections.emptyList();
115
        }
116
        online.resolve(createArtifact(info), remotes, online.getLocalRepository());
117
        return a.getFile();
118
    }
119
    
97
    public static String calculateSHA1Checksum(File file) throws IOException {
120
    public static String calculateSHA1Checksum(File file) throws IOException {
98
        byte[] buffer = readFile(file);
121
        byte[] buffer = readFile(file);
99
        MessageDigest digest;
122
        MessageDigest digest;
(-)a/maven.indexer/test/unit/src/org/netbeans/modules/maven/indexer/api/RepositoryQueriesTest.java (+106 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.modules.maven.indexer.api;
44
45
import java.io.File;
46
import java.util.Collections;
47
import org.apache.maven.artifact.installer.ArtifactInstaller;
48
import org.apache.maven.artifact.repository.ArtifactRepository;
49
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
50
import org.apache.maven.index.ArtifactInfo;
51
import org.netbeans.junit.NbTestCase;
52
import org.netbeans.modules.maven.embedder.EmbedderFactory;
53
import org.netbeans.modules.maven.embedder.MavenEmbedder;
54
import org.openide.util.test.TestFileUtils;
55
56
public class RepositoryQueriesTest extends NbTestCase {
57
58
    public RepositoryQueriesTest(String n) {
59
        super(n);
60
    }
61
62
    private ArtifactRepository defaultArtifactRepository;
63
    private MavenEmbedder embedder;
64
    private ArtifactInstaller artifactInstaller;
65
    private RepositoryInfo info;
66
    
67
    @SuppressWarnings("deprecation") // no documented replacement
68
    @Override protected void setUp() throws Exception {
69
        clearWorkDir();
70
        System.setProperty("netbeans.user", getWorkDirPath());
71
        File repo = new File(getWorkDir(), "repo");
72
        defaultArtifactRepository = new org.apache.maven.artifact.repository.DefaultArtifactRepository("test", repo.toURI().toString(), new DefaultRepositoryLayout());
73
        embedder = EmbedderFactory.getProjectEmbedder();
74
        embedder.setUpLegacySupport();
75
        artifactInstaller = embedder.lookupComponent(ArtifactInstaller.class);
76
        info = new RepositoryInfo("test", RepositoryPreferences.TYPE_NEXUS, "Test", repo.getAbsolutePath(), null);
77
        RepositoryPreferences.getInstance().addOrModifyRepositoryInfo(info);
78
    }
79
80
    @SuppressWarnings("deprecation") // no documented replacement
81
    private void install(File f, String groupId, String artifactId, String version, String packaging) throws Exception {
82
        artifactInstaller.install(f, embedder.lookupComponent(org.apache.maven.artifact.factory.ArtifactFactory.class).createBuildArtifact(groupId, artifactId, version, packaging), defaultArtifactRepository);
83
    }
84
85
    public void testFilterGroupIds() throws Exception {
86
        install(File.createTempFile("whatever", ".txt", getWorkDir()), "test", "spin", "1.1", "txt");
87
        assertEquals(Collections.singleton("test"), RepositoryQueries.filterGroupIds("", info));
88
    }
89
90
    public void testFind() throws Exception {
91
        install(TestFileUtils.writeFile(new File(getWorkDir(), "plugin.pom"),
92
                "<project><modelVersion>4.0.0</modelVersion>" +
93
                "<groupId>test</groupId><artifactId>plugin</artifactId>" +
94
                "<version>0</version><packaging>maven-plugin</packaging></project>"), "test", "plugin", "0", "pom");
95
        File jar = new File(getWorkDir(), "plugin.jar");
96
        TestFileUtils.writeZipFile(jar, "META-INF/maven/plugin.xml:<plugin><goalPrefix>stuff</goalPrefix></plugin>");
97
        install(jar, "test", "plugin", "0", "maven-plugin");
98
        QueryField qf = new QueryField();
99
        qf.setField(ArtifactInfo.PLUGIN_PREFIX);
100
        qf.setValue("stuff");
101
        qf.setOccur(QueryField.OCCUR_MUST);
102
        qf.setMatch(QueryField.MATCH_EXACT);
103
        assertEquals("[test:plugin:0:test]", RepositoryQueries.find(Collections.singletonList(qf), info).toString());
104
    }
105
    
106
}

Return to bug 186939