Author: olamy Date: Sat Aug 13 18:04:10 2011 New Revision: 1157401 URL: http://svn.apache.org/viewvc?rev=1157401&view=rev Log: [MINDEXER-36] oups missed to add some files
Added: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java (with props) maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java (with props) maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java (with props) Added: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java?rev=1157401&view=auto ============================================================================== --- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java (added) +++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java Sat Aug 13 18:04:10 2011 @@ -0,0 +1,38 @@ +package org.apache.maven.index; +/* + * 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. + */ + +/** + * Maven ontology. + * @author Olivier Lamy + * @since 4.1.1 + */ +public interface OSGI +{ + + /** OSGI namespace */ + String OSGI_NAMESPACE = "urn:osgi#"; + + Field SYMBOLIC_NAME = new Field( null, OSGI_NAMESPACE, "symbolicName", "Bundle Symbolic Name" ); + + Field VERSION = new Field( null, OSGI_NAMESPACE, "version", "Bundle Version" ); + + Field EXPORT_PACKAGE = new Field( null, OSGI_NAMESPACE, "exportPackage", "Bundle Export-Package" ); + +} Propchange: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/OSGI.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java?rev=1157401&view=auto ============================================================================== --- maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java (added) +++ maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java Sat Aug 13 18:04:10 2011 @@ -0,0 +1,223 @@ +package org.apache.maven.index.creator; + +/* + * 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. + */ + +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +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.NEXUS; +import org.apache.maven.index.OSGI; +import org.apache.maven.index.context.IndexCreator; +import org.apache.maven.index.util.zip.ZipFacade; +import org.apache.maven.index.util.zip.ZipHandle; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +/** + * @author Olivier Lamy + * @since 4.1.2 + */ +@Component( role = IndexCreator.class, hint = OSGIArtifactIndexCreator.ID ) +public class OSGIArtifactIndexCreator + extends AbstractIndexCreator +{ + public static final String ID = "osgi-metadatas"; + + private static final String BSN = "Bundle-SymbolicName"; + + public static final IndexerField FLD_BUNDLE_SYMBOLIC_NAME = + new IndexerField( OSGI.SYMBOLIC_NAME, IndexerFieldVersion.V4, "bsn", "Bundle-SymbolicName (indexed, stored)", + Field.Store.YES, Field.Index.ANALYZED ); + + + private static final String BV = "Bundle-Version"; + + public static final IndexerField FLD_BUNDLE_VERSION = + new IndexerField( OSGI.VERSION, IndexerFieldVersion.V4, "bv", "Bundle-Version (indexed, stored)", Field.Store.YES, + Field.Index.ANALYZED ); + + + private static final String BEP = "Export-Package"; + + public static final IndexerField FLD_BUNDLE_EXPORT_PACKAGE = + new IndexerField( OSGI.EXPORT_PACKAGE, IndexerFieldVersion.V4, "bv", "Export-Package (indexed, stored)", Field.Store.YES, + Field.Index.ANALYZED ); + + public Collection<IndexerField> getIndexerFields() + { + return Arrays.asList( FLD_BUNDLE_SYMBOLIC_NAME, FLD_BUNDLE_VERSION, FLD_BUNDLE_EXPORT_PACKAGE ); + } + + public void populateArtifactInfo( ArtifactContext artifactContext ) + throws IOException + { + ArtifactInfo ai = artifactContext.getArtifactInfo(); + + File artifactFile = artifactContext.getArtifact(); + + // TODO : olamy : supports only jars ? + + if ( artifactFile != null && artifactFile.isFile() && artifactFile.getName().endsWith( ".jar" ) ) + { + updateArtifactInfo( ai, artifactFile ); + } + } + + public void updateDocument( ArtifactInfo artifactInfo, Document document ) + { + + if ( artifactInfo.bundleSymbolicName != null ) + { + document.add( FLD_BUNDLE_SYMBOLIC_NAME.toField( artifactInfo.bundleSymbolicName ) ); + } + + if ( artifactInfo.bundleVersion != null ) + { + document.add( FLD_BUNDLE_VERSION.toField( artifactInfo.bundleVersion ) ); + } + + if ( artifactInfo.bundleExportPackage != null ) + { + document.add( FLD_BUNDLE_EXPORT_PACKAGE.toField( artifactInfo.bundleExportPackage ) ); + } + + } + + public boolean updateArtifactInfo( Document document, ArtifactInfo artifactInfo ) + { + boolean updated = false; + + String bundleSymbolicName = document.get( FLD_BUNDLE_SYMBOLIC_NAME.getKey() ); + + if ( bundleSymbolicName != null ) + { + artifactInfo.bundleSymbolicName = bundleSymbolicName; + + updated = true; + } + + String bundleVersion = document.get( FLD_BUNDLE_VERSION.getKey() ); + + if ( bundleVersion != null ) + { + artifactInfo.bundleVersion = bundleVersion; + + updated = true; + } + + String bundleExportPackage = document.get( FLD_BUNDLE_EXPORT_PACKAGE.getKey() ); + + if ( bundleExportPackage != null ) + { + artifactInfo.bundleExportPackage = bundleExportPackage; + + updated = true; + + } + + return updated; + } + + private boolean updateArtifactInfo( ArtifactInfo ai, File f ) + throws IOException + { + ZipHandle handle = null; + + boolean updated = false; + + try + { + handle = ZipFacade.getZipHandle( f ); + + final List<String> entries = handle.getEntries(); + + for ( String name : entries ) + { + if ( name.equals( "META-INF/MANIFEST.MF" ) ) + { + Manifest manifest = new Manifest( handle.getEntryContent( name ) ); + + Attributes mainAttributes = manifest.getMainAttributes(); + + if ( mainAttributes != null ) + { + String attValue = mainAttributes.getValue( BSN ); + if ( StringUtils.isNotBlank( attValue ) ) + { + ai.bundleSymbolicName = attValue; + updated = true; + } + else + { + ai.bundleSymbolicName = null; + } + + attValue = mainAttributes.getValue( BV ); + if ( StringUtils.isNotBlank( attValue ) ) + { + ai.bundleVersion = attValue; + updated = true; + } + else + { + ai.bundleVersion = null; + } + + attValue = mainAttributes.getValue( BEP ); + if ( StringUtils.isNotBlank( attValue ) ) + { + ai.bundleExportPackage = attValue; + updated = true; + } + else + { + ai.bundleExportPackage = null; + } + + } + } + } + + } + finally + { + try + { + ZipFacade.close( handle ); + } + catch ( Exception e ) + { + getLogger().error( "Could not close jar file properly.", e ); + } + } + return updated; + } +} Propchange: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/indexer/trunk/indexer-core/src/main/java/org/apache/maven/index/creator/OSGIArtifactIndexCreator.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java URL: http://svn.apache.org/viewvc/maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java?rev=1157401&view=auto ============================================================================== --- maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java (added) +++ maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java Sat Aug 13 18:04:10 2011 @@ -0,0 +1,179 @@ +package org.apache.maven.index.creator; + +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.maven.index.ArtifactContext; +import org.apache.maven.index.ArtifactInfo; +import org.apache.maven.index.FlatSearchRequest; +import org.apache.maven.index.FlatSearchResponse; +import org.apache.maven.index.NexusIndexer; +import org.apache.maven.index.OSGI; +import org.apache.maven.index.context.IndexCreator; +import org.apache.maven.index.context.IndexingContext; +import org.apache.maven.index.expr.StringSearchExpression; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +/** + * @author Olivier Lamy + */ +public class OSGIArtifactIndexCreatorTest + extends PlexusTestCase +{ + protected IndexCreator indexCreator; + + private NexusIndexer nexusIndexer; + + static final String INDEX_ID = "osgi-test1"; + + @Override + public void setUp() + throws Exception + { + super.setUp(); + + indexCreator = this.lookup( IndexCreator.class, OSGIArtifactIndexCreator.ID ); + + nexusIndexer = this.lookup( NexusIndexer.class ); + } + + public void testAssertIndexCreatorComponentExists() + throws Exception + { + assertNotNull( indexCreator ); + } + + public void testPopulateArtifactInfo() + throws Exception + { + File artifact = new File( getBasedir(), + "src/test/repo-with-osgi/org/apache/karaf/features/org.apache.karaf.features.command/2.2.2/org.apache.karaf.features.command-2.2.2.jar" ); + + File pom = new File( getBasedir(), + "src/test/repo-with-osgi/org/apache/karaf/features/org.apache.karaf.features.command/2.2.2/org.apache.karaf.features.command-2.2.2.pom" ); + + ArtifactInfo artifactInfo = + new ArtifactInfo( "test", "org.apache.karaf.features", "org.apache.karaf.features.command", "2.2.2", null ); + + ArtifactContext artifactContext = new ArtifactContext( pom, artifact, null, artifactInfo, null ); + + indexCreator.populateArtifactInfo( artifactContext ); + + assertNotNull( "bundleSymbolicName", artifactContext.getArtifactInfo().bundleSymbolicName ); + + assertNotNull( "bundleVersion", artifactContext.getArtifactInfo().bundleVersion ); + + assertNotNull( "bundleExportPackage", artifactContext.getArtifactInfo().bundleExportPackage ); + + assertEquals( "org.apache.karaf.features.command", artifactContext.getArtifactInfo().bundleSymbolicName ); + + assertEquals( "2.2.2", artifactContext.getArtifactInfo().bundleVersion ); + + assertEquals( + "org.apache.karaf.features.command.completers;uses:=\"org.apache.karaf.features,org.apache.karaf.shell.console,org.apache.karaf.shell.console.completer\";version=\"2.2.2\",org.apache.karaf.features.command;uses:=\"org.apache.felix.gogo.commands,org.apache.karaf.features,org.apache.karaf.shell.console,org.osgi.framework,org.apache.felix.service.command\";version=\"2.2.2\"", + artifactContext.getArtifactInfo().bundleExportPackage ); + } + + + private void indexOSGIRepo() + throws Exception + { + + File repo = new File( getBasedir(), "src/test/repo-with-osgi" ); + + File repoIndexDir = new File( getBasedir(), "target/test/repo-with-osgi/.index/" ); + + if ( repoIndexDir.exists() ) + { + FileUtils.deleteDirectory( repoIndexDir ); + } + + repoIndexDir.mkdirs(); + + List<IndexCreator> indexCreators = + Arrays.<IndexCreator>asList( new MinimalArtifactInfoIndexCreator(), new JarFileContentsIndexCreator(), + new MavenPluginArtifactInfoIndexCreator(), new OSGIArtifactIndexCreator() ); + + IndexingContext indexingContext = + nexusIndexer.addIndexingContext( INDEX_ID, INDEX_ID, repo, repoIndexDir, "http://www.apache.org", + "http://www.apache.org/.index", indexCreators ); + indexingContext.setSearchable( true ); + nexusIndexer.scan( indexingContext, false ); + + + } + + public void testIndexOSGIRepoThenSearch() + throws Exception + { + + try + { + indexOSGIRepo(); + + BooleanQuery q = new BooleanQuery(); + + q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME, + new StringSearchExpression( "org.apache.karaf.features.command" ) ), + BooleanClause.Occur.MUST ); + + FlatSearchRequest request = new FlatSearchRequest( q ); + FlatSearchResponse response = nexusIndexer.searchFlat( request ); + + // here only one results ! + assertEquals( 1, response.getResults().size() ); + + q = new BooleanQuery(); + + q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME, + new StringSearchExpression( "org.apache.karaf.features.core" ) ), + BooleanClause.Occur.MUST ); + + request = new FlatSearchRequest( q ); + response = nexusIndexer.searchFlat( request ); + + // here two results ! + assertEquals( 2, response.getResults().size() ); + } + finally + { + nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true ); + } + } + + public void testIndexOSGIRepoThenSearchWithVersion() + throws Exception + { + + indexOSGIRepo(); + + try + { + + BooleanQuery q = new BooleanQuery(); + + q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME, + new StringSearchExpression( "org.apache.karaf.features.core" ) ), + BooleanClause.Occur.MUST ); + + q.add( nexusIndexer.constructQuery( OSGI.VERSION, new StringSearchExpression( "2.2.1" ) ), + BooleanClause.Occur.MUST ); + + FlatSearchRequest request = new FlatSearchRequest( q ); + FlatSearchResponse response = nexusIndexer.searchFlat( request ); + + // here only one results as we use version + assertEquals( 1, response.getResults().size() ); + } + finally + { + nexusIndexer.getIndexingContexts().get( INDEX_ID ).close( true ); + } + + } + +} Propchange: maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/indexer/trunk/indexer-core/src/test/java/org/apache/maven/index/creator/OSGIArtifactIndexCreatorTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision