diff options
5 files changed, 202 insertions, 8 deletions
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/NexusIndexManager.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/NexusIndexManager.java index 47184423..0839230f 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/NexusIndexManager.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/index/NexusIndexManager.java @@ -746,13 +746,17 @@ public class NexusIndexManager implements IndexManager, IMavenProjectChangedList // workspace indexing context can by null during startup due to MNGECLIPSE-1633 for(MavenProjectChangedEvent event : events) { IMavenProjectFacade oldFacade = event.getOldMavenProject(); - if(oldFacade != null) { - removeDocument(repositoryRegistry.getWorkspaceRepository(), oldFacade.getPomFile(), - oldFacade.getArtifactKey(), oldFacade); - fireIndexRemoved(repositoryRegistry.getWorkspaceRepository()); - } IMavenProjectFacade facade = event.getMavenProject(); - if(facade != null) { + if(oldFacade != null) { + if(facade != null) { + addDocument(repositoryRegistry.getWorkspaceRepository(), facade.getPomFile(), facade.getArtifactKey()); + fireIndexChanged(repositoryRegistry.getWorkspaceRepository()); + } else { + removeDocument(repositoryRegistry.getWorkspaceRepository(), oldFacade.getPomFile(), + oldFacade.getArtifactKey(), oldFacade); + fireIndexRemoved(repositoryRegistry.getWorkspaceRepository()); + } + } else if(facade != null) { addDocument(repositoryRegistry.getWorkspaceRepository(), facade.getPomFile(), facade.getArtifactKey()); fireIndexAdded(repositoryRegistry.getWorkspaceRepository()); } diff --git a/org.eclipse.m2e.maven.indexer/.classpath b/org.eclipse.m2e.maven.indexer/.classpath index 047bf4f7..c1026e8b 100644 --- a/org.eclipse.m2e.maven.indexer/.classpath +++ b/org.eclipse.m2e.maven.indexer/.classpath @@ -2,6 +2,7 @@ <classpath> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> + <classpathentry kind="src" path="src/"/> <classpathentry exported="true" kind="lib" path="jars/indexer-artifact-3.1.0.jar"/> <classpathentry exported="true" kind="lib" path="jars/indexer-core-3.1.0.jar"/> <classpathentry exported="true" kind="lib" path="jars/lucene-core-2.4.1.jar"/> diff --git a/org.eclipse.m2e.maven.indexer/META-INF/MANIFEST.MF b/org.eclipse.m2e.maven.indexer/META-INF/MANIFEST.MF index 09a764ca..a56a54db 100644 --- a/org.eclipse.m2e.maven.indexer/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.maven.indexer/META-INF/MANIFEST.MF @@ -26,7 +26,8 @@ Export-Package: META-INF.plexus, org.apache.maven.index.search.grouping, org.apache.maven.index.treeview, org.apache.maven.index.updater -Bundle-ClassPath: jars/indexer-artifact-3.1.0.jar, +Bundle-ClassPath: local-patches.jar, + jars/indexer-artifact-3.1.0.jar, jars/indexer-core-3.1.0.jar, jars/lucene-core-2.4.1.jar, jars/lucene-highlighter-2.4.1.jar diff --git a/org.eclipse.m2e.maven.indexer/build.properties b/org.eclipse.m2e.maven.indexer/build.properties index 5a4515c2..6331284d 100644 --- a/org.eclipse.m2e.maven.indexer/build.properties +++ b/org.eclipse.m2e.maven.indexer/build.properties @@ -11,4 +11,8 @@ bin.includes = META-INF/,\ jars/,\ - OSGI-INF/ + OSGI-INF/,\ + local-patches.jar +jars.compile.order = local-patches.jar +source.local-patches.jar = src/ +source.local-patches.jar = src/ diff --git a/org.eclipse.m2e.maven.indexer/src/org/apache/maven/index/DefaultIndexerEngine.java b/org.eclipse.m2e.maven.indexer/src/org/apache/maven/index/DefaultIndexerEngine.java new file mode 100644 index 00000000..4b155c1b --- /dev/null +++ b/org.eclipse.m2e.maven.indexer/src/org/apache/maven/index/DefaultIndexerEngine.java @@ -0,0 +1,184 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.index; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.Fieldable; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TopDocs; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.creator.MinimalArtifactInfoIndexCreator; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +/** + * A default {@link IndexerEngine} implementation. + * + * @author Tamas Cservenak + */ +@Component( role = IndexerEngine.class ) +public class DefaultIndexerEngine + extends AbstractLogEnabled + implements IndexerEngine +{ + + public void index( IndexingContext context, ArtifactContext ac ) + throws IOException + { + // skip artifacts not obeying repository layout (whether m1 or m2) + if ( ac != null && ac.getGav() != null ) + { + Document d = ac.createDocument( context ); + + if ( d != null ) + { + context.getIndexWriter().addDocument( d ); + + context.updateTimestamp(); + } + } + } + + public void update( IndexingContext context, ArtifactContext ac ) + throws IOException + { + Document d = ac.createDocument( context ); + + if ( d != null ) + { + Document old = getOldDocument( context, ac ); + + if ( !equals(d, old) ) + { + IndexWriter w = context.getIndexWriter(); + + w.updateDocument( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ), d ); + + updateGroups( context, ac ); + + w.commit(); + + context.updateTimestamp(); + } + } + } + + private boolean equals( Document d1, Document d2 ) + { + if ( d1 == null && d2 == null ) + { + return true; + } + if ( d1 == null || d2 == null ) + { + return false; + } + + Map<String, String> m1 = toMap( d1 ); + Map<String, String> m2 = toMap( d2 ); + + m1.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() ); + m2.remove( MinimalArtifactInfoIndexCreator.FLD_LAST_MODIFIED.getKey() ); + + return m1.equals( m2 ); + } + + private Map<String, String> toMap( Document d ) + { + final HashMap<String, String> result = new HashMap<String, String>(); + + for ( Object o : d.getFields() ) + { + Fieldable f = (Fieldable) o; + if ( f.isStored() ) + { + result.put( f.name(), f.stringValue() ); + } + } + + return result; + } + + private Document getOldDocument( IndexingContext context, ArtifactContext ac ) + { + try + { + IndexSearcher indexSearcher = context.getIndexSearcher(); + TopDocs result = + indexSearcher.search( new TermQuery( new Term( ArtifactInfo.UINFO, ac.getArtifactInfo().getUinfo() ) ), + 2 ); + if ( result.totalHits == 1 ) + { + return indexSearcher.doc( result.scoreDocs[0].doc ); + } + } + catch ( IOException e ) + { + } + return null; + } + + private void updateGroups( IndexingContext context, ArtifactContext ac ) + throws IOException + { + String rootGroup = ac.getArtifactInfo().getRootGroup(); + Set<String> rootGroups = context.getRootGroups(); + if ( !rootGroups.contains( rootGroup ) ) + { + rootGroups.add( rootGroup ); + context.setRootGroups( rootGroups ); + } + + Set<String> allGroups = context.getAllGroups(); + if ( !allGroups.contains( ac.getArtifactInfo().groupId ) ) + { + allGroups.add( ac.getArtifactInfo().groupId ); + context.setAllGroups( allGroups ); + } + } + + public void remove( IndexingContext context, ArtifactContext ac ) + throws IOException + { + if ( ac != null ) + { + String uinfo = ac.getArtifactInfo().getUinfo(); + // add artifact deletion marker + Document doc = new Document(); + doc.add( new Field( ArtifactInfo.DELETED, uinfo, Field.Store.YES, Field.Index.NO ) ); + doc.add( new Field( ArtifactInfo.LAST_MODIFIED, // + Long.toString( System.currentTimeMillis() ), Field.Store.YES, Field.Index.NO ) ); + IndexWriter w = context.getIndexWriter(); + w.addDocument( doc ); + w.deleteDocuments( new Term( ArtifactInfo.UINFO, uinfo ) ); + w.commit(); + context.updateTimestamp(); + } + } + +} |