Skip to content

Instantly share code, notes, and snippets.

@cstamas
Created December 23, 2010 10:44
Show Gist options
  • Save cstamas/752823 to your computer and use it in GitHub Desktop.
Save cstamas/752823 to your computer and use it in GitHub Desktop.
package craig;
import org.apache.maven.index.Field;
/**
* Craig's Ontology
*
* @author cstamas
*/
public interface CRAIG
{
/** Craig's namespace */
public static final String NAMESPACE = "urn:craig#";
public static final Field BUILD_ID = new Field( null, NAMESPACE, "buildId", "Artifact's Build ID" );
}
===
package craig;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.maven.index.ArtifactContext;
import org.apache.maven.index.ArtifactInfo;
import org.apache.maven.index.IndexerField;
import org.apache.maven.index.IndexerFieldVersion;
import org.apache.maven.index.context.IndexCreator;
import org.codehaus.plexus.component.annotations.Component;
/**
* Craig's index creator
*/
@Component( role = IndexCreator.class, hint = CraigsIndexCreator.ID )
public class CraigsIndexCreator
extends AbstractIndexCreator
{
// TODO: add some meaningful ID here
public static final String ID = "craigsIndexCreator";
public static final IndexerField FLD_BUILD_ID = new IndexerField( CRAIG.BUILD_ID, IndexerFieldVersion.V3,
"buildId", "Artifact buildId (untokenized)", Store.YES, Index.NOT_ANALYZED_NO_NORMS );
public void populateArtifactInfo( ArtifactContext artifactContext )
throws IOException
{
ArtifactInfo ai = artifactContext.getArtifactInfo();
File artifactFile = artifactContext.getArtifact();
if ( artifactFile != null && artifactFile.exists() && artifactFile.getName().endsWith( ".jar" ) )
{
updateArtifactInfo( ai, artifactFile );
}
}
public void updateDocument( ArtifactInfo ai, Document doc )
{
if ( ai.getAttributes().containsValue( FLD_BUILD_ID.getKey() ) )
{
doc.add( FLD_BUILD_ID.toField( ai.getAttributes().get( FLD_BUILD_ID.getKey() ) ) );
}
}
public boolean updateArtifactInfo( Document doc, ArtifactInfo artifactInfo )
{
String buildId = doc.get( FLD_BUILD_ID.getKey() );
if ( buildId != null )
{
artifactInfo.getAttributes().put( FLD_BUILD_ID.getKey(), buildId );
return true;
}
return false;
}
private void updateArtifactInfo( ArtifactInfo ai, File f )
throws IOException
{
JarFile jar = null;
try
{
jar = new JarFile( f );
Manifest mf = jar.getManifest();
Attributes attr = mf.getMainAttributes();
String buildId = attr.getValue( "BuildId" );
if ( buildId != null && buildId.trim().length() > 0 )
{
ai.getAttributes().put( FLD_BUILD_ID.getKey(), buildId );
}
}
finally
{
if ( jar != null )
{
try
{
jar.close();
}
catch ( Exception e )
{
getLogger().error( "Could not close jar file properly.", e );
}
}
}
}
@Override
public String toString()
{
return ID;
}
public Collection<IndexerField> getIndexerFields()
{
return Arrays.asList( FLD_BUILD_ID );
}
}
===
Using it:
// Exact search -- buildId is known exactly
Query q = nexusIndexer.constructQuery( CRAIG.BUILD_ID, "123456", SearchType.EXACT );
FlatSearchRequest req = new FlatSearchRequest( q );
FlatSearchResponse res = nexusIndexer.searchFlat( req );
// Scored search -- buildId's prefix is known only (ie. user input) Only prefix search is possible!
Query q = nexusIndexer.constructQuery( CRAIG.BUILD_ID, "123", SearchType.SCORED );
FlatSearchRequest req = new FlatSearchRequest( q );
FlatSearchResponse res = nexusIndexer.searchFlat( req );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment