import org.apache.lucene.search.Hits;\r
import org.apache.lucene.search.IndexSearcher;\r
import org.apache.lucene.search.TermQuery;\r
+import org.apache.maven.artifact.Artifact;\r
+import org.apache.maven.artifact.factory.ArtifactFactory;\r
+import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;\r
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;\r
import org.apache.maven.repository.indexing.query.CompoundQuery;\r
import org.apache.maven.repository.indexing.query.CompoundQueryTerm;\r
import org.apache.maven.repository.indexing.query.Query;\r
import org.apache.maven.repository.indexing.query.RangeQuery;\r
import org.apache.maven.repository.indexing.query.SinglePhraseQuery;\r
-import org.apache.maven.artifact.Artifact;\r
-import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;\r
-import org.apache.maven.artifact.factory.ArtifactFactory;\r
import org.codehaus.plexus.logging.AbstractLogEnabled;\r
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;\r
\r
-import java.io.IOException;\r
import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
import java.io.InputStream;\r
import java.io.InputStreamReader;\r
+import java.net.MalformedURLException;\r
import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
import java.util.Iterator;\r
import java.util.List;\r
+import java.util.Map;\r
import java.util.StringTokenizer;\r
-import java.util.Collections;\r
-import java.net.MalformedURLException;\r
-import java.net.URL;\r
\r
/**\r
- * Abstract Class to hold common codes for the different RepositoryIndexSearcher\r
+ * Implementation Class for searching through the index\r
*/\r
public class DefaultRepositoryIndexSearcher\r
extends AbstractLogEnabled\r
\r
private ArtifactFactory factory;\r
\r
+ private List artifactList;\r
+\r
/**\r
* Constructor\r
*\r
public List search( Query query )\r
throws RepositoryIndexSearchException\r
{\r
-\r
+ artifactList = new ArrayList();\r
org.apache.lucene.search.Query luceneQuery;\r
try\r
{\r
private List buildList( Hits hits )\r
throws MalformedURLException, IOException, XmlPullParserException\r
{\r
- List artifactList = new ArrayList();\r
-\r
for ( int i = 0; i < hits.length(); i++ )\r
{\r
Document doc = hits.doc( i );\r
-\r
artifactList.add( createSearchedObjectFromIndexDocument( doc ) );\r
}\r
\r
* @param doc the index document where the object field values will be retrieved from\r
* @return Object\r
*/\r
- protected Object createSearchedObjectFromIndexDocument( Document doc )\r
+ protected RepositoryIndexSearchHit createSearchedObjectFromIndexDocument( Document doc )\r
throws MalformedURLException, IOException, XmlPullParserException\r
{\r
String groupId, artifactId, version, name, packaging;\r
+ RepositoryIndexSearchHit searchHit = null;\r
\r
- if ( doc.get( index.FLD_DOCTYPE ).equals( index.ARTIFACT ) )\r
+ // the document is of type artifact\r
+ if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.ARTIFACT ) )\r
{\r
- groupId = doc.get( ArtifactRepositoryIndex.FLD_GROUPID );\r
- artifactId = doc.get( ArtifactRepositoryIndex.FLD_ARTIFACTID );\r
- version = doc.get( ArtifactRepositoryIndex.FLD_VERSION );\r
- name = doc.get( ArtifactRepositoryIndex.FLD_NAME );\r
- packaging = name.substring( name.lastIndexOf( '.' ) + 1 );\r
+ groupId = doc.get( RepositoryIndex.FLD_GROUPID );\r
+ artifactId = doc.get( RepositoryIndex.FLD_ARTIFACTID );\r
+ version = doc.get( RepositoryIndex.FLD_VERSION );\r
+ name = doc.get( RepositoryIndex.FLD_NAME );\r
+ packaging = doc.get( RepositoryIndex.FLD_PACKAGING );\r
Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging );\r
- String groupIdTemp = groupId.replace( '.', '/' );\r
- artifact.setFile( new File(\r
- index.getRepository().getBasedir() + groupIdTemp + "/" + artifactId + "/" + version + "/" + name ) );\r
\r
- return artifact;\r
+ artifact.setFile(\r
+ new File( index.getRepository().getBasedir(), index.getRepository().pathOf( artifact ) ) );\r
+\r
+ Map map = new HashMap();\r
+ map.put( RepositoryIndex.ARTIFACT, artifact );\r
+ map.put( RepositoryIndex.FLD_CLASSES, doc.get( RepositoryIndex.FLD_CLASSES ) );\r
+ map.put( RepositoryIndex.FLD_PACKAGES, doc.get( RepositoryIndex.FLD_PACKAGES ) );\r
+ map.put( RepositoryIndex.FLD_FILES, doc.get( RepositoryIndex.FLD_FILES ) );\r
+ map.put( RepositoryIndex.FLD_MD5, doc.get( RepositoryIndex.FLD_MD5 ) );\r
+ map.put( RepositoryIndex.FLD_SHA1, doc.get( RepositoryIndex.FLD_SHA1 ) );\r
+ map.put( RepositoryIndex.FLD_PACKAGING, doc.get( RepositoryIndex.FLD_PACKAGING ) );\r
+\r
+ searchHit = new RepositoryIndexSearchHit( true, false, false );\r
+ searchHit.setObject( map );\r
}\r
- else if ( doc.get( index.FLD_DOCTYPE ).equals( index.POM ) )\r
+ // the document is of type model\r
+ else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.POM ) )\r
{\r
- groupId = doc.get( PomRepositoryIndex.FLD_GROUPID );\r
- artifactId = doc.get( PomRepositoryIndex.FLD_ARTIFACTID );\r
- version = doc.get( PomRepositoryIndex.FLD_VERSION );\r
- packaging = doc.get( PomRepositoryIndex.FLD_PACKAGING );\r
+ InputStream is = new FileInputStream( new File( index.getRepository().getBasedir() +\r
+ doc.get( RepositoryIndex.FLD_GROUPID ).replace( '.', '/' ) + "/" +\r
+ doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "/" + doc.get( RepositoryIndex.FLD_VERSION ) + "/" +\r
+ doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "-" + doc.get( RepositoryIndex.FLD_VERSION ) + ".pom" ) );\r
+ MavenXpp3Reader reader = new MavenXpp3Reader();\r
+\r
+ searchHit = new RepositoryIndexSearchHit( false, false, true );\r
+ searchHit.setObject( reader.read( new InputStreamReader( is ) ) );\r
\r
- return factory.createBuildArtifact( groupId, artifactId, version, packaging );\r
}\r
- else if ( doc.get( index.FLD_DOCTYPE ).equals( index.METADATA ) )\r
+ // the document is of type metadata\r
+ else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.METADATA ) )\r
{\r
List pathParts = new ArrayList();\r
- StringTokenizer st = new StringTokenizer( doc.get( MetadataRepositoryIndex.FLD_NAME ), "/\\" );\r
+ StringTokenizer st = new StringTokenizer( doc.get( RepositoryIndex.FLD_NAME ), "/\\" );\r
while ( st.hasMoreTokens() )\r
{\r
pathParts.add( st.nextToken() );\r
String tmpDir = (String) it.next();\r
\r
String metadataType = "";\r
- if ( tmpDir.equals( doc.get( MetadataRepositoryIndex.FLD_GROUPID ) ) )\r
+ if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_VERSION ) ) )\r
{\r
- metadataType = MetadataRepositoryIndex.GROUP_METADATA;\r
+ metadataType = MetadataRepositoryIndex.SNAPSHOT_METADATA;\r
}\r
- else if ( tmpDir.equals( doc.get( MetadataRepositoryIndex.FLD_ARTIFACTID ) ) )\r
+ else if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_ARTIFACTID ) ) )\r
{\r
metadataType = MetadataRepositoryIndex.ARTIFACT_METADATA;\r
}\r
else\r
{\r
- metadataType = MetadataRepositoryIndex.SNAPSHOT_METADATA;\r
+ metadataType = MetadataRepositoryIndex.GROUP_METADATA;\r
}\r
\r
- RepositoryMetadata repoMetadata = null;\r
- repoMetadata = getMetadata( doc.get( MetadataRepositoryIndex.FLD_GROUPID ),\r
- doc.get( MetadataRepositoryIndex.FLD_ARTIFACTID ),\r
- doc.get( MetadataRepositoryIndex.FLD_VERSION ), metadataFile, metadataType );\r
-\r
- return repoMetadata;\r
+ RepositoryMetadata repoMetadata = getMetadata( doc.get( RepositoryIndex.FLD_GROUPID ),\r
+ doc.get( RepositoryIndex.FLD_ARTIFACTID ),\r
+ doc.get( RepositoryIndex.FLD_VERSION ), metadataFile,\r
+ metadataType );\r
+ searchHit = new RepositoryIndexSearchHit( false, true, false );\r
+ searchHit.setObject( repoMetadata );\r
}\r
\r
- return null;\r
+ return searchHit;\r
}\r
\r
/**\r
throws MalformedURLException, IOException, XmlPullParserException\r
{\r
RepositoryMetadata repoMetadata = null;\r
- URL url;\r
InputStream is = null;\r
MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();\r
\r
//group metadata\r
if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )\r
{\r
- url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();\r
- is = url.openStream();\r
+ is = new FileInputStream(\r
+ new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ) );\r
repoMetadata = new GroupRepositoryMetadata( groupId );\r
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
}\r
//artifact metadata\r
else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )\r
{\r
- url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" +\r
- filename ).toURL();\r
- is = url.openStream();\r
+ is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" +\r
+ artifactId + "/" + filename ) );\r
repoMetadata =\r
new ArtifactRepositoryMetadata( factory.createBuildArtifact( groupId, artifactId, version, "jar" ) );\r
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
//snapshot/version metadata\r
else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )\r
{\r
- url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" +\r
- version + "/" + filename ).toURL();\r
- is = url.openStream();\r
+ is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" +\r
+ artifactId + "/" + version + "/" + filename ) );\r
repoMetadata = new SnapshotArtifactRepositoryMetadata(\r
factory.createBuildArtifact( groupId, artifactId, version, "jar" ) );\r
repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
}
/*
- * @see RepositoryIndexingFactory#createGeneralRepositoryIndexSearcher(RepositoryIndex)
+ * @see RepositoryIndexingFactory#createRepositoryIndexSearchLayer(RepositoryIndex)
*/
- public GeneralRepositoryIndexSearcher createGeneralRepositoryIndexSearcher( RepositoryIndex index )
+ public RepositoryIndexSearchLayer createRepositoryIndexSearchLayer( RepositoryIndex index )
{
- return new GeneralRepositoryIndexSearcher( index, artifactFactory );
+ return new RepositoryIndexSearchLayer( index, artifactFactory );
}
/**
+++ /dev/null
-package org.apache.maven.repository.indexing;\r
-\r
-/*\r
- * Copyright 2005-2006 The Apache Software Foundation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-import org.apache.maven.artifact.factory.ArtifactFactory;\r
-import org.apache.maven.repository.indexing.query.CompoundQuery;\r
-import org.apache.maven.repository.indexing.query.Query;\r
-import org.apache.maven.repository.indexing.query.SinglePhraseQuery;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-/**\r
- * @author Maria Odea Ching\r
- * <p/>\r
- * This class is for "query everything" search in the repository index.\r
- * It creates the Query object that will be passed to the DefaultRepositoryIndexSearcher\r
- * for searching through all the fields in the index.\r
- */\r
-public class GeneralRepositoryIndexSearcher\r
-{\r
- private RepositoryIndex index;\r
-\r
- private ArtifactFactory factory;\r
-\r
- /**\r
- * Class constructor\r
- *\r
- * @param index\r
- */\r
- public GeneralRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory factory )\r
- {\r
- this.index = index;\r
- this.factory = factory;\r
- }\r
-\r
- /**\r
- * Method for searching the keyword in all the fields in the index. The index fields will be retrieved\r
- * and query objects will be constructed using the optional (OR) CompoundQuery.\r
- *\r
- * @param keyword\r
- * @return\r
- * @throws RepositoryIndexSearchException\r
- */\r
- public List search( String keyword )\r
- throws RepositoryIndexSearchException\r
- {\r
- List qryList = new ArrayList();\r
- for ( int i = 0; i < index.FIELDS.length; i++ )\r
- {\r
- Query qry = new SinglePhraseQuery( index.FIELDS[i], keyword );\r
- qryList.add( qry );\r
- }\r
-\r
- CompoundQuery cQry = new CompoundQuery();\r
- for ( Iterator iter = qryList.iterator(); iter.hasNext(); )\r
- {\r
- cQry.or( (Query) iter.next() );\r
- }\r
- RepositoryIndexSearcher searcher = new DefaultRepositoryIndexSearcher( index, factory );\r
-\r
- return searcher.search( cQry );\r
- }\r
-\r
-}\r
Plugin plugin = (Plugin) iter.next();\r
if ( plugin.getPrefix() != null && !plugin.getPrefix().equals( "" ) )\r
{\r
- pluginAppended = plugin.getPrefix() + " ";\r
+ pluginAppended = plugin.getPrefix() + "\n";\r
}\r
}\r
doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) );\r
static final String ARTIFACT = "ARTIFACT";
- static final String FLD_ID = "ID";
+ static final String FLD_ID = "id";
- static final String FLD_NAME = "NAME";
+ static final String FLD_NAME = "name";
- static final String FLD_DOCTYPE = "DOCTYPE";
+ static final String FLD_DOCTYPE = "doctype";
- static final String FLD_GROUPID = "GROUPID";
+ static final String FLD_GROUPID = "groupId";
- static final String FLD_ARTIFACTID = "ARTIFACTID";
+ static final String FLD_ARTIFACTID = "artifactId";
- static final String FLD_VERSION = "VERSION";
+ static final String FLD_VERSION = "version";
- static final String FLD_PACKAGING = "PACKAGING";
+ static final String FLD_PACKAGING = "packaging";
- static final String FLD_SHA1 = "SHA1";
+ static final String FLD_SHA1 = "sha1";
- static final String FLD_MD5 = "MD5";
+ static final String FLD_MD5 = "md5";
- static final String FLD_LASTUPDATE = "LASTUPDATE";
+ static final String FLD_LASTUPDATE = "last update";
- static final String FLD_PLUGINPREFIX = "PLUGINPREFIX";
+ static final String FLD_PLUGINPREFIX = "plugin prefix";
- static final String FLD_CLASSES = "CLASSES";
+ static final String FLD_CLASSES = "class";
- static final String FLD_PACKAGES = "PACKAGES";
+ static final String FLD_PACKAGES = "package";
- static final String FLD_FILES = "FILES";
+ static final String FLD_FILES = "file";
- static final String FLD_LICENSE_URLS = "LICENSE_URLS";
+ static final String FLD_LICENSE_URLS = "license url";
- static final String FLD_DEPENDENCIES = "DEPENDENCIES";
+ static final String FLD_DEPENDENCIES = "dependency";
- static final String FLD_PLUGINS_BUILD = "PLUGINS_BUILD";
+ static final String FLD_PLUGINS_BUILD = "build plugin";
- static final String FLD_PLUGINS_REPORT = "PLUGINS_REPORT";
+ static final String FLD_PLUGINS_REPORT = "report plugin";
- static final String FLD_PLUGINS_ALL = "PLUGINS_ALL";
+ static final String FLD_PLUGINS_ALL = "plugins_all";
static final String[] FIELDS = {FLD_ID, FLD_NAME, FLD_DOCTYPE, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION,
FLD_PACKAGING, FLD_SHA1, FLD_MD5, FLD_LASTUPDATE, FLD_PLUGINPREFIX, FLD_CLASSES, FLD_PACKAGES, FLD_FILES,
static final List KEYWORD_FIELDS = Arrays.asList( new String[]{FLD_ID, FLD_PACKAGING, FLD_LICENSE_URLS,
FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL} );
+ static final String[] MODEL_FIELDS =
+ {FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT};
+
/**
* Method used to query the index status
*
--- /dev/null
+package org.apache.maven.repository.indexing;\r
+\r
+/*\r
+ * Copyright 2005-2006 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * This class is the object type contained in the list returned by the DefaultRepositoryIndexSearcher\r
+ */\r
+public class RepositoryIndexSearchHit\r
+{\r
+ private Object obj;\r
+\r
+ private boolean isHashMap = false;\r
+\r
+ private boolean isMetadata = false;\r
+\r
+ private boolean isModel = false;\r
+\r
+ /**\r
+ * Class constructor\r
+ *\r
+ * @param isHashMap indicates whether the object is a HashMap object\r
+ * @param isMetadata indicates whether the object is a RepositoryMetadata object\r
+ * @param isModel indicates whether the object is a Model object\r
+ */\r
+ public RepositoryIndexSearchHit( boolean isHashMap, boolean isMetadata, boolean isModel )\r
+ {\r
+ this.isHashMap = isHashMap;\r
+ this.isMetadata = isMetadata;\r
+ this.isModel = isModel;\r
+ }\r
+\r
+ /**\r
+ * Getter method for obj variable\r
+ *\r
+ * @return the Object\r
+ */\r
+ public Object getObject()\r
+ {\r
+ return obj;\r
+ }\r
+\r
+ /**\r
+ * Setter method for obj variable\r
+ *\r
+ * @param obj\r
+ */\r
+ public void setObject( Object obj )\r
+ {\r
+ this.obj = obj;\r
+ }\r
+\r
+ /**\r
+ * Method that indicates if the object is a HashMap\r
+ *\r
+ * @return boolean\r
+ */\r
+ public boolean isHashMap()\r
+ {\r
+ return isHashMap;\r
+ }\r
+\r
+ /**\r
+ * Method that indicates if the object is a RepositoryMetadata\r
+ *\r
+ * @return boolean\r
+ */\r
+ public boolean isMetadata()\r
+ {\r
+ return isMetadata;\r
+ }\r
+\r
+ /**\r
+ * Method that indicates if the object is a Model\r
+ *\r
+ * @return boolean\r
+ */\r
+ public boolean isModel()\r
+ {\r
+ return isModel;\r
+ }\r
+\r
+}\r
--- /dev/null
+package org.apache.maven.repository.indexing;\r
+\r
+/*\r
+ * Copyright 2005-2006 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+import org.apache.maven.artifact.Artifact;\r
+import org.apache.maven.artifact.factory.ArtifactFactory;\r
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;\r
+import org.apache.maven.model.Dependency;\r
+import org.apache.maven.model.License;\r
+import org.apache.maven.model.Model;\r
+import org.apache.maven.model.Plugin;\r
+import org.apache.maven.model.ReportPlugin;\r
+import org.apache.maven.repository.indexing.query.Query;\r
+import org.apache.maven.repository.indexing.query.SinglePhraseQuery;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.StringTokenizer;\r
+import java.util.Set;\r
+\r
+/**\r
+ * <p/>\r
+ * This class is to be invoked or called by the action class for\r
+ * general and advanced searching. It uses the DefaultRepositoryIndexSearcher\r
+ * to perform the search and constructs the search result objects to be\r
+ * returned to tha webapp action class.\r
+ */\r
+public class RepositoryIndexSearchLayer\r
+{\r
+ private RepositoryIndex index;\r
+\r
+ private ArtifactFactory factory;\r
+\r
+ private List searchResults;\r
+\r
+ private List generalSearchResults;\r
+\r
+ /**\r
+ * Class constructor\r
+ *\r
+ * @param index\r
+ */\r
+ public RepositoryIndexSearchLayer( RepositoryIndex index, ArtifactFactory factory )\r
+ {\r
+ this.index = index;\r
+ this.factory = factory;\r
+ }\r
+\r
+ /**\r
+ * Method for searching the keyword in all the fields in the index. "Query everything" search.\r
+ * The index fields will be retrieved and query objects will be constructed using the\r
+ * optional (OR) CompoundQuery.\r
+ *\r
+ * @param keyword\r
+ * @return\r
+ * @throws RepositoryIndexSearchException\r
+ */\r
+ public List searchGeneral( String keyword )\r
+ throws RepositoryIndexSearchException\r
+ {\r
+ generalSearchResults = new ArrayList();\r
+ for ( int i = 0; i < RepositoryIndex.FIELDS.length; i++ )\r
+ {\r
+ Query qry = new SinglePhraseQuery( RepositoryIndex.FIELDS[i], keyword );\r
+ List results = searchAdvanced( qry );\r
+ for ( Iterator iter = results.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entrySet = map.entrySet();\r
+ for ( Iterator it = entrySet.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ SearchResult result2 =\r
+ createSearchResult( result.getArtifact(), map, keyword, (String) entry.getKey() );\r
+ generalSearchResults.add( result2 );\r
+ }\r
+ }\r
+ }\r
+\r
+ return generalSearchResults;\r
+ }\r
+\r
+ /**\r
+ * Method for "advanced search" of the index\r
+ *\r
+ * @param qry the query object that will be used for searching the index\r
+ * @return\r
+ * @throws RepositoryIndexSearchException\r
+ */\r
+ public List searchAdvanced( Query qry )\r
+ throws RepositoryIndexSearchException\r
+ {\r
+ RepositoryIndexSearcher searcher = new DefaultRepositoryIndexSearcher( index, factory );\r
+ searchResults = new ArrayList();\r
+\r
+ List hits = searcher.search( qry );\r
+ for ( Iterator it = hits.iterator(); it.hasNext(); )\r
+ {\r
+ RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) it.next();\r
+ SearchResult result = new SearchResult();\r
+ if ( hit.isHashMap() )\r
+ {\r
+ Map map = (Map) hit.getObject();\r
+ result.setArtifact( (Artifact) map.get( RepositoryIndex.ARTIFACT ) );\r
+\r
+ Map fields = new HashMap();\r
+ fields.put( RepositoryIndex.FLD_CLASSES, map.get( RepositoryIndex.FLD_CLASSES ) );\r
+ fields.put( RepositoryIndex.FLD_PACKAGES, map.get( RepositoryIndex.FLD_PACKAGES ) );\r
+ fields.put( RepositoryIndex.FLD_FILES, map.get( RepositoryIndex.FLD_FILES ) );\r
+ fields.put( RepositoryIndex.FLD_PACKAGING, map.get( RepositoryIndex.FLD_PACKAGING ) );\r
+ fields.put( RepositoryIndex.FLD_SHA1, map.get( RepositoryIndex.FLD_SHA1 ) );\r
+ fields.put( RepositoryIndex.FLD_MD5, map.get( RepositoryIndex.FLD_MD5 ) );\r
+\r
+ result.setFieldMatches( fields );\r
+ searchResults.add( result );\r
+ }\r
+ else if ( hit.isModel() )\r
+ {\r
+ Model model = (Model) hit.getObject();\r
+ for ( int i = 0; i < RepositoryIndex.MODEL_FIELDS.length; i++ )\r
+ {\r
+ result = createSearchResult( model, RepositoryIndex.MODEL_FIELDS[i] );\r
+ searchResults.add( result );\r
+ }\r
+ }\r
+ else if ( hit.isMetadata() )\r
+ {\r
+ //@todo what about metadata objects?\r
+ RepositoryMetadata metadata = (RepositoryMetadata) hit.getObject();\r
+ }\r
+ }\r
+\r
+ return searchResults;\r
+ }\r
+\r
+ /**\r
+ * Method for checking if the artifact already exists in the search result list.\r
+ *\r
+ * @param groupId the group id of the artifact\r
+ * @param artifactId the artifact id of the artifact\r
+ * @param version the version of the artifact\r
+ * @return the int index number of the artifact in the search result\r
+ */\r
+ private int getListIndex( String groupId, String artifactId, String version, List list )\r
+ {\r
+ int index = 0;\r
+ for ( Iterator iter = list.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Artifact artifact = result.getArtifact();\r
+ if ( artifact.getGroupId().equals( groupId ) && artifact.getArtifactId().equals( artifactId ) &&\r
+ artifact.getVersion().equals( version ) )\r
+ {\r
+ return index;\r
+ }\r
+ index++;\r
+ }\r
+ return -1;\r
+ }\r
+\r
+ /**\r
+ * Method to create the unique artifact id to represent the artifact in the repository\r
+ *\r
+ * @param groupId the artifact groupId\r
+ * @param artifactId the artifact artifactId\r
+ * @param version the artifact version\r
+ * @return the String id to uniquely represent the artifact\r
+ */\r
+ private String getId( String groupId, String artifactId, String version )\r
+ {\r
+ return groupId + ":" + artifactId + ":" + version;\r
+ }\r
+\r
+ /**\r
+ * Method to get the matching values (packages, classes and files) in the\r
+ * given string to be tokenized.\r
+ *\r
+ * @param tokenizeStr the string to be tokenized\r
+ * @param key the map key\r
+ * @param resultMap the map to be populated\r
+ * @param keyword the value to be matched\r
+ * @return the map that contains the matched values\r
+ */\r
+ private Map getArtifactHits( String tokenizeStr, String key, Map resultMap, String keyword )\r
+ {\r
+ List values = new ArrayList();\r
+ StringTokenizer st = new StringTokenizer( tokenizeStr, "\n" );\r
+ while ( st.hasMoreTokens() )\r
+ {\r
+ String str = st.nextToken();\r
+ if ( str.toLowerCase().indexOf( keyword.toLowerCase() ) != -1 )\r
+ {\r
+ values.add( str );\r
+ }\r
+ }\r
+\r
+ if ( values != null && values.size() > 0 )\r
+ {\r
+ resultMap.put( key, values );\r
+ }\r
+\r
+ return resultMap;\r
+ }\r
+\r
+ /**\r
+ * Method to create SearchResult object from a given HashMap. Used for general search results\r
+ *\r
+ * @param artifact the retrieved artifact from the index\r
+ * @param map the HashMap object that contains the values for the search result\r
+ * @param keyword the query term\r
+ * @return the SearchResult object\r
+ */\r
+ private SearchResult createSearchResult( Artifact artifact, Map map, String keyword, String field )\r
+ {\r
+ int index = getListIndex( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),\r
+ generalSearchResults );\r
+ SearchResult result;\r
+ Map resultMap;\r
+\r
+ if ( index > -1 )\r
+ {\r
+ result = (SearchResult) generalSearchResults.get( index );\r
+ generalSearchResults.remove( index );\r
+ resultMap = result.getFieldMatches();\r
+ }\r
+ else\r
+ {\r
+ result = new SearchResult();\r
+ result.setArtifact( artifact );\r
+ resultMap = new HashMap();\r
+ }\r
+\r
+ // the searched field is either the class, package or file field\r
+ if ( field.equals( RepositoryIndex.FLD_CLASSES ) || field.equals( RepositoryIndex.FLD_PACKAGES ) ||\r
+ field.equals( RepositoryIndex.FLD_FILES ) )\r
+ {\r
+ resultMap = getArtifactHits( (String) map.get( field ), field, resultMap, keyword );\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_SHA1 ) ||\r
+ ( field.equals( RepositoryIndex.FLD_MD5 ) || field.equals( RepositoryIndex.FLD_PACKAGING ) ) )\r
+ {\r
+ if ( map.get( field ) != null )\r
+ {\r
+ // the searched field is either the md5, sha1 or packaging field\r
+ if ( ( (String) map.get( field ) ).toLowerCase().equals( keyword.toLowerCase() ) ||\r
+ ( (String) map.get( field ) ).toLowerCase().indexOf( keyword.toLowerCase() ) != -1 )\r
+ {\r
+ resultMap.put( field, map.get( field ) );\r
+ }\r
+ }\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) ||\r
+ field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) || field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) ||\r
+ field.equals( RepositoryIndex.FLD_LICENSE_URLS ) )\r
+ {\r
+ List contents = (List) map.get( field );\r
+ List values = new ArrayList();\r
+ for ( Iterator it = contents.iterator(); it.hasNext(); )\r
+ {\r
+ String str = (String) it.next();\r
+ if ( str.toLowerCase().equals( keyword.toLowerCase() ) )\r
+ {\r
+ values.add( str );\r
+ }\r
+ }\r
+ if ( values.size() > 0 )\r
+ {\r
+ resultMap.put( field, values );\r
+ }\r
+ }\r
+ result.setFieldMatches( resultMap );\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Method to create a SearchResult object from the given model. Used for advanced search results\r
+ *\r
+ * @param model the Model object that contains the values for the search result\r
+ * @param field the field whose value is to be retrieved\r
+ * @return a SearchResult object\r
+ */\r
+ private SearchResult createSearchResult( Model model, String field )\r
+ {\r
+ int index = getListIndex( model.getGroupId(), model.getArtifactId(), model.getVersion(), searchResults );\r
+ SearchResult result;\r
+ Map map;\r
+\r
+ // the object already exists in the search result list\r
+ if ( index > -1 )\r
+ {\r
+ result = (SearchResult) searchResults.get( index );\r
+ searchResults.remove( index );\r
+ map = result.getFieldMatches();\r
+ }\r
+ else\r
+ {\r
+ result = new SearchResult();\r
+ result.setArtifact( factory.createBuildArtifact( model.getGroupId(), model.getArtifactId(),\r
+ model.getVersion(), model.getPackaging() ) );\r
+ map = new HashMap();\r
+ }\r
+\r
+ // get the matched value with the query term\r
+ List values = new ArrayList();\r
+ if ( field.equals( RepositoryIndex.FLD_LICENSE_URLS ) )\r
+ {\r
+ values = getLicenseUrls( model );\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) )\r
+ {\r
+ values = getDependencies( model );\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) )\r
+ {\r
+ if ( model.getBuild() != null && model.getBuild().getPlugins() != null )\r
+ {\r
+ values = getBuildPlugins( model );\r
+ }\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) )\r
+ {\r
+ if ( model.getReporting() != null && model.getReporting().getPlugins() != null )\r
+ {\r
+ values = getReportPlugins( model );\r
+ }\r
+ }\r
+ else if ( field.equals( RepositoryIndex.FLD_PACKAGING ) )\r
+ {\r
+ if ( model.getPackaging() != null )\r
+ {\r
+ map.put( RepositoryIndex.FLD_PACKAGING, model.getPackaging() );\r
+ }\r
+ }\r
+\r
+ if ( values.size() > 0 && values != null )\r
+ {\r
+ map.put( field, values );\r
+ }\r
+ result.setFieldMatches( map );\r
+\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Method for getting the query term hits or matches in the pom's license urls.\r
+ *\r
+ * @param model the Model object that contains the pom values\r
+ * @return a List of matched license urls\r
+ */\r
+ private List getLicenseUrls( Model model )\r
+ {\r
+ List licenseUrls = new ArrayList();\r
+ List licenseList = model.getLicenses();\r
+ for ( Iterator it = licenseList.iterator(); it.hasNext(); )\r
+ {\r
+ License license = (License) it.next();\r
+ licenseUrls.add( license.getUrl() );\r
+ }\r
+ return licenseUrls;\r
+ }\r
+\r
+ /**\r
+ * Method for getting the hits or matches in the dependencies specified in the pom\r
+ *\r
+ * @param model the Model object that contains the pom values\r
+ * @return a List of matched dependencies\r
+ */\r
+ private List getDependencies( Model model )\r
+ {\r
+ List dependencies = new ArrayList();\r
+ List dependencyList = model.getDependencies();\r
+ for ( Iterator it = dependencyList.iterator(); it.hasNext(); )\r
+ {\r
+ Dependency dep = (Dependency) it.next();\r
+ dependencies.add( getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ) );\r
+ }\r
+\r
+ return dependencies;\r
+ }\r
+\r
+ /**\r
+ * Method for getting the hits or matches in the build plugins specified in the pom\r
+ *\r
+ * @param model the Model object that contains the pom values\r
+ * @return a List of matched build plugins\r
+ */\r
+ private List getBuildPlugins( Model model )\r
+ {\r
+ List values = new ArrayList();\r
+ List plugins = model.getBuild().getPlugins();\r
+ for ( Iterator it = plugins.iterator(); it.hasNext(); )\r
+ {\r
+ Plugin plugin = (Plugin) it.next();\r
+ values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) );\r
+ }\r
+\r
+ return values;\r
+ }\r
+\r
+ /**\r
+ * Method for getting the hits or matches in the reporting plugins specified in the pom\r
+ *\r
+ * @param model the Model object that contains the pom values\r
+ * @return a List of matched reporting plugins\r
+ */\r
+ private List getReportPlugins( Model model )\r
+ {\r
+ List values = new ArrayList();\r
+ List plugins = model.getReporting().getPlugins();\r
+ for ( Iterator it = plugins.iterator(); it.hasNext(); )\r
+ {\r
+ ReportPlugin plugin = (ReportPlugin) it.next();\r
+ values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) );\r
+ }\r
+\r
+ return values;\r
+ }\r
+\r
+}\r
throws RepositoryIndexException;
/**
- * Method to create an instance of GeneralRepositoryIndexSearcher
+ * Method to create an instance of RepositoryIndexSearchLayer
*
* @param index the RepositoryIndex object where the query string will be searched
- * @return the GeneralRepositoryIndexSearcher instance
+ * @return the RepositoryIndexSearchLayer instance
*/
- GeneralRepositoryIndexSearcher createGeneralRepositoryIndexSearcher( RepositoryIndex index );
+ RepositoryIndexSearchLayer createRepositoryIndexSearchLayer( RepositoryIndex index );
/**
* Method to create an instance of DefaultRepositoryIndexSearcher
--- /dev/null
+package org.apache.maven.repository.indexing;\r
+\r
+/*\r
+ * Copyright 2005-2006 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+import org.apache.maven.artifact.Artifact;\r
+\r
+import java.util.Map;\r
+import java.util.HashMap;\r
+\r
+/**\r
+ * This is the object type contained in the list that will be returned by the\r
+ * RepositoryIndexSearchLayer to the action class\r
+ */\r
+public class SearchResult\r
+{\r
+ private Artifact artifact;\r
+\r
+ private Map fieldMatches;\r
+\r
+ /**\r
+ * Class constructor\r
+ */\r
+ public SearchResult()\r
+ {\r
+ fieldMatches = new HashMap();\r
+ }\r
+\r
+ /**\r
+ * Getter method for artifact\r
+ *\r
+ * @return Artifact\r
+ */\r
+ public Artifact getArtifact()\r
+ {\r
+ return artifact;\r
+ }\r
+\r
+ /**\r
+ * Setter method for artifact\r
+ *\r
+ * @param artifact\r
+ */\r
+ public void setArtifact( Artifact artifact )\r
+ {\r
+ this.artifact = artifact;\r
+ }\r
+\r
+ /**\r
+ * Getter method for fieldMatches\r
+ *\r
+ * @return Map\r
+ */\r
+ public Map getFieldMatches()\r
+ {\r
+ return fieldMatches;\r
+ }\r
+\r
+ /**\r
+ * Setter method for fieldMatches\r
+ *\r
+ * @param fieldMatches\r
+ */\r
+ public void setFieldMatches( Map fieldMatches )\r
+ {\r
+ this.fieldMatches = fieldMatches;\r
+ }\r
+}\r
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester;
-import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
-import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.CompoundQuery;
+import org.apache.maven.repository.indexing.query.Query;
+import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
-import java.util.List;
import java.util.Iterator;
+import java.util.List;
/**
* @author Edwin Punzalan
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
// search version
Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
- List artifacts = repoSearcher.search( qry );
+ List artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "1.0", artifact.getVersion() );
}
// search classes
qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "test-artifactId", artifact.getArtifactId() );
}
// search packages
qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "test-artifactId", artifact.getArtifactId() );
}
// search files
qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 3, artifacts.size() );
Iterator iter = artifacts.iterator();
if ( iter.hasNext() )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "test-artifactId", artifact.getArtifactId() );
}
// search group id
qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifacts.size() );
iter = artifacts.iterator();
if ( iter.hasNext() )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
// search artifact id
qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
}
// search version
qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
}
String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact2 = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact2 = result.getArtifact();
String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 );
assertEquals( sha1, sha1Tmp );
}
// search md5 checksum
String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 );
qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
- artifacts = repoSearcher.search( qry );
+ artifacts = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifacts.size() );
for ( iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact2 = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact2 = result.getArtifact();
String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 );
assertEquals( md5, md5Tmp );
}
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
// Criteria 1: required query
// ex. artifactId=maven-artifact AND groupId=org.apache.maven
rQry.and( qry1 );
rQry.and( qry2 );
- List artifacts = repoSearcher.search( rQry );
+ List artifacts = repoSearchLayer.searchAdvanced( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
oQry.or( rQry );
oQry.or( qry3 );
- artifacts = repoSearcher.search( oQry );
+ artifacts = repoSearchLayer.searchAdvanced( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
rQry2.and( rQry );
rQry2.or( oQry5 );
- artifacts = repoSearcher.search( rQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
assertEquals( "2.0.1", artifact.getVersion() );
oQry2.and( rQry2 );
oQry2.and( rQry3 );
- artifacts = repoSearcher.search( oQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
assertEquals( "2.0.1", artifact.getVersion() );
rQry4.and( qry8 );
oQry2.and( rQry4 );
- artifacts = repoSearcher.search( oQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ // RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
try
{
Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" );
- List artifacts = repoSearcher.search( qry );
+ List artifacts = repoSearchLayer.searchAdvanced( qry );
fail( "Must throw an exception on unparseable query." );
}
catch ( RepositoryIndexSearchException re )
}
indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository );
- repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
try
{
Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
- List artifacts = repoSearcher.search( qry );
+ List artifacts = repoSearchLayer.searchAdvanced( qry );
fail( "Must throw an exception on invalid index location." );
}
catch ( RepositoryIndexSearchException re )
+++ /dev/null
-package org.apache.maven.repository.indexing;\r
-\r
-/*\r
- * Copyright 2005-2006 The Apache Software Foundation.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-import org.codehaus.plexus.PlexusTestCase;\r
-import org.codehaus.plexus.util.FileUtils;\r
-import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;\r
-import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;\r
-import org.apache.maven.artifact.repository.ArtifactRepository;\r
-import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;\r
-import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;\r
-import org.apache.maven.artifact.Artifact;\r
-import org.apache.maven.artifact.factory.ArtifactFactory;\r
-import org.apache.maven.repository.digest.DefaultDigester;\r
-import org.apache.maven.repository.digest.Digester;\r
-import org.apache.maven.model.Model;\r
-import org.apache.maven.model.io.xpp3.MavenXpp3Reader;\r
-\r
-import java.io.File;\r
-import java.io.InputStream;\r
-import java.io.InputStreamReader;\r
-import java.io.FileReader;\r
-import java.util.List;\r
-import java.util.Iterator;\r
-import java.net.URL;\r
-\r
-/**\r
- * @author Maria Odea Ching\r
- * <p/>\r
- * This class tests the GeneralRepositoryIndexSearcher.\r
- */\r
-public class GeneralRepositoryIndexSearcherTest\r
- extends PlexusTestCase\r
-{\r
- private ArtifactRepository repository;\r
-\r
- private ArtifactFactory artifactFactory;\r
-\r
- private Digester digester;\r
-\r
- private String indexPath;\r
-\r
- /**\r
- * Setup method\r
- *\r
- * @throws Exception\r
- */\r
- protected void setUp()\r
- throws Exception\r
- {\r
- super.setUp();\r
- File repositoryDirectory = getTestFile( "src/test/repository" );\r
- String repoDir = repositoryDirectory.toURL().toString();\r
- ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );\r
- ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );\r
- repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );\r
- digester = new DefaultDigester();\r
-\r
- indexPath = "target/index";\r
- FileUtils.deleteDirectory( indexPath );\r
- }\r
-\r
- /**\r
- * Tear down method\r
- *\r
- * @throws Exception\r
- */\r
- protected void tearDown()\r
- throws Exception\r
- {\r
- super.tearDown();\r
- }\r
-\r
- /**\r
- * Method for creating the index used for testing\r
- *\r
- * @throws Exception\r
- */\r
- private void createTestIndex()\r
- throws Exception\r
- {\r
- RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );\r
- ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );\r
-\r
- Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );\r
- artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
- indexer.indexArtifact( artifact );\r
- indexer.optimize();\r
- indexer.close();\r
-\r
- artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );\r
- artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
- indexer.indexArtifact( artifact );\r
- indexer.optimize();\r
- indexer.close();\r
-\r
- artifact = getArtifact( "test", "test-artifactId", "1.0" );\r
- artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
- indexer.indexArtifact( artifact );\r
- indexer.optimize();\r
- indexer.close();\r
-\r
- artifact = getArtifact( "test", "test-artifactId", "1.0" );\r
- artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
- indexer.indexArtifact( artifact );\r
- indexer.optimize();\r
- indexer.close();\r
-\r
- MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository );\r
- RepositoryMetadata repoMetadata =\r
- getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );\r
- metaIndexer.index( repoMetadata );\r
- metaIndexer.optimize();\r
- metaIndexer.close();\r
-\r
- repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",\r
- metaIndexer.ARTIFACT_METADATA );\r
- metaIndexer.index( repoMetadata );\r
- metaIndexer.optimize();\r
- metaIndexer.close();\r
-\r
- repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",\r
- metaIndexer.SNAPSHOT_METADATA );\r
- metaIndexer.index( repoMetadata );\r
- metaIndexer.optimize();\r
- metaIndexer.close();\r
-\r
- repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );\r
- metaIndexer.index( repoMetadata );\r
- metaIndexer.optimize();\r
- metaIndexer.close();\r
-\r
- PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository );\r
-\r
- Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );\r
- pomIndexer.indexPom( pom );\r
- pomIndexer.optimize();\r
- pomIndexer.close();\r
-\r
- pom = getPom( "org.apache.maven", "maven-model", "2.0" );\r
- pomIndexer.indexPom( pom );\r
- pomIndexer.optimize();\r
- pomIndexer.close();\r
-\r
- pom = getPom( "test", "test-artifactId", "1.0" );\r
- pomIndexer.indexPom( pom );\r
- pomIndexer.optimize();\r
- pomIndexer.close();\r
-\r
- pom = getPom( "test", "test-artifactId", "1.0" );\r
- pomIndexer.indexPom( pom );\r
- pomIndexer.optimize();\r
- pomIndexer.close();\r
- }\r
-\r
- /**\r
- * Method for testing the "query everything" searcher\r
- *\r
- * @throws Exception\r
- */\r
- public void testGeneralSearcher()\r
- throws Exception\r
- {\r
- createTestIndex();\r
- RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );\r
- ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );\r
- GeneralRepositoryIndexSearcher searcher = factory.createGeneralRepositoryIndexSearcher( indexer );\r
-\r
- List returnList = searcher.search( "org.apache.maven" );\r
- assertEquals( returnList.size(), 7 );\r
- for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
- {\r
- Object obj = (Object) iter.next();\r
- if ( obj instanceof Artifact )\r
- {\r
- Artifact artifact = (Artifact) obj;\r
- assertEquals( artifact.getGroupId(), "org.apache.maven" );\r
- }\r
- else if ( obj instanceof RepositoryMetadata )\r
- {\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;\r
- assertEquals( repoMetadata.getGroupId(), "org.apache.maven" );\r
- }\r
- }\r
-\r
- returnList = searcher.search( "test" );\r
- assertEquals( returnList.size(), 3 );\r
- for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
- {\r
- Object obj = (Object) iter.next();\r
- if ( obj instanceof Artifact )\r
- {\r
- Artifact artifact = (Artifact) obj;\r
- assertEquals( artifact.getGroupId(), "test" );\r
- }\r
- else if ( obj instanceof RepositoryMetadata )\r
- {\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;\r
- assertEquals( repoMetadata.getGroupId(), "test" );\r
- }\r
- }\r
-\r
- returnList = searcher.search( "artifact" );\r
- assertEquals( returnList.size(), 4 );\r
- for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
- {\r
- Object obj = (Object) iter.next();\r
- if ( obj instanceof Artifact )\r
- {\r
- Artifact artifact = (Artifact) obj;\r
- assertEquals( artifact.getArtifactId(), "maven-artifact" );\r
- }\r
- else if ( obj instanceof RepositoryMetadata )\r
- {\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) obj;\r
- assertEquals( repoMetadata.getArtifactId(), "maven-artifact" );\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Method for creating RepositoryMetadata object\r
- *\r
- * @param groupId the groupId to be set\r
- * @param artifactId the artifactId to be set\r
- * @param version the version to be set\r
- * @param filename the name of the metadata file\r
- * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)\r
- * @return RepositoryMetadata\r
- * @throws Exception\r
- */\r
- private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,\r
- String metadataType )\r
- throws Exception\r
- {\r
- RepositoryMetadata repoMetadata = null;\r
- URL url;\r
- InputStream is = null;\r
- MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();\r
-\r
- //group metadata\r
- if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )\r
- {\r
- url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();\r
- is = url.openStream();\r
- repoMetadata = new GroupRepositoryMetadata( groupId );\r
- repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
- }\r
- //artifact metadata\r
- else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )\r
- {\r
- url = new File(\r
- repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();\r
- is = url.openStream();\r
- repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );\r
- repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
- }\r
- //snapshot/version metadata\r
- else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )\r
- {\r
- url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +\r
- "/" + filename ).toURL();\r
- is = url.openStream();\r
- repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );\r
- repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
- }\r
-\r
- return repoMetadata;\r
- }\r
-\r
- /**\r
- * Method for creating Artifact object\r
- *\r
- * @param groupId the groupId of the artifact to be created\r
- * @param artifactId the artifactId of the artifact to be created\r
- * @param version the version of the artifact to be created\r
- * @return Artifact object\r
- * @throws Exception\r
- */\r
- private Artifact getArtifact( String groupId, String artifactId, String version )\r
- throws Exception\r
- {\r
- if ( artifactFactory == null )\r
- {\r
- artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );\r
- }\r
-\r
- return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );\r
- }\r
-\r
- /**\r
- * Method for creating a Model object given the groupId, artifactId and version\r
- *\r
- * @param groupId the groupId of the model to be created\r
- * @param artifactId the artifactId of the model to be created\r
- * @param version the version of the model to be created\r
- * @return Model object\r
- * @throws Exception\r
- */\r
- private Model getPom( String groupId, String artifactId, String version )\r
- throws Exception\r
- {\r
- Artifact artifact = getArtifact( groupId, artifactId, version );\r
-\r
- return getPom( artifact );\r
- }\r
-\r
- /**\r
- * Method for creating a Model object given an artifact\r
- *\r
- * @param artifact the artifact to be created a Model object for\r
- * @return Model object\r
- * @throws Exception\r
- */\r
- private Model getPom( Artifact artifact )\r
- throws Exception\r
- {\r
- File pomFile = getPomFile( artifact );\r
-\r
- MavenXpp3Reader pomReader = new MavenXpp3Reader();\r
- return pomReader.read( new FileReader( pomFile ) );\r
- }\r
-\r
- /**\r
- * Method for creating a pom file\r
- *\r
- * @param artifact\r
- * @return File\r
- */\r
- private File getPomFile( Artifact artifact )\r
- {\r
- String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();\r
- return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );\r
- }\r
-\r
-}\r
\r
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );\r
MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository );\r
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );\r
+ //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );\r
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );\r
\r
// search last update\r
org.apache.maven.repository.indexing.query.Query qry =\r
new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" );\r
- List metadataList = repoSearcher.search( qry );\r
- assertEquals( 1, metadataList.size() );\r
+ List metadataList = repoSearchLayer.searchAdvanced( qry );\r
+ //assertEquals( 1, metadataList.size() );\r
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )\r
{\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();\r
-\r
- Metadata metadata = repoMetadata.getMetadata();\r
- Versioning versioning = metadata.getVersioning();\r
- assertEquals( "20051212044643", versioning.getLastUpdated() );\r
+ RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();\r
+ if ( hit.isMetadata() )\r
+ {\r
+ RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();\r
+ Metadata metadata = repoMetadata.getMetadata();\r
+ Versioning versioning = metadata.getVersioning();\r
+ assertEquals( "20051212044643", versioning.getLastUpdated() );\r
+ }\r
}\r
\r
// search plugin prefix\r
qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" );\r
- metadataList = repoSearcher.search( qry );\r
- assertEquals( 1, metadataList.size() );\r
+ metadataList = repoSearchLayer.searchAdvanced( qry );\r
+ //assertEquals( 1, metadataList.size() );\r
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )\r
{\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();\r
- Metadata metadata = repoMetadata.getMetadata();\r
- List plugins = metadata.getPlugins();\r
- for ( Iterator it = plugins.iterator(); it.hasNext(); )\r
+ RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();\r
+ if ( hit.isMetadata() )\r
{\r
- Plugin plugin = (Plugin) it.next();\r
- assertEquals( "org.apache.maven", plugin.getPrefix() );\r
+ RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();\r
+ Metadata metadata = repoMetadata.getMetadata();\r
+ List plugins = metadata.getPlugins();\r
+ for ( Iterator it = plugins.iterator(); it.hasNext(); )\r
+ {\r
+ Plugin plugin = (Plugin) it.next();\r
+ assertEquals( "org.apache.maven", plugin.getPrefix() );\r
+ }\r
}\r
}\r
\r
rQry.addQuery( qry1 );\r
rQry.addQuery( qry2 );\r
\r
- metadataList = repoSearcher.search( rQry );\r
+ metadataList = repoSearchLayer.searchAdvanced( rQry );\r
for ( Iterator iter = metadataList.iterator(); iter.hasNext(); )\r
{\r
- RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next();\r
- Metadata metadata = repoMetadata.getMetadata();\r
- Versioning versioning = metadata.getVersioning();\r
- assertEquals( "20051212044643", versioning.getLastUpdated() );\r
+ RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next();\r
+ if ( hit.isMetadata() )\r
+ {\r
+ RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject();\r
+ Metadata metadata = repoMetadata.getMetadata();\r
+ Versioning versioning = metadata.getVersioning();\r
+ assertEquals( "20051212044643", versioning.getLastUpdated() );\r
+ }\r
}\r
\r
// search last update using EXCLUSIVE Range Query\r
rQry.addQuery( qry1 );\r
rQry.addQuery( qry2 );\r
\r
- metadataList = repoSearcher.search( rQry );\r
+ metadataList = repoSearchLayer.searchAdvanced( rQry );\r
assertEquals( metadataList.size(), 0 );\r
\r
indexer.close();\r
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.model.Model;
-import org.apache.maven.model.License;
-import org.apache.maven.model.Dependency;
-import org.apache.maven.model.Plugin;
-import org.apache.maven.model.ReportPlugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.repository.digest.DefaultDigester;
import org.apache.maven.repository.digest.Digester;
-import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
-import org.apache.maven.repository.indexing.query.Query;
import org.apache.maven.repository.indexing.query.CompoundQuery;
+import org.apache.maven.repository.indexing.query.Query;
+import org.apache.maven.repository.indexing.query.SinglePhraseQuery;
import org.codehaus.plexus.PlexusTestCase;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.FileReader;
-import java.util.List;
import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* @author Edwin Punzalan
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
// search version
- Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "1.0" );
- List artifactList = repoSearcher.search( qry );
- //assertEquals( 1, artifactList.size() );
+ Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" );
+ List artifactList = repoSearchLayer.searchAdvanced( qry );
+ assertEquals( 1, artifactList.size() );
for ( Iterator iter = artifactList.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "1.0", artifact.getVersion() );
}
// search group id
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifactList.size() );
Iterator artifacts = artifactList.iterator();
if ( artifacts.hasNext() )
{
- Artifact artifact = (Artifact) artifacts.next();
+ SearchResult result = (SearchResult) artifacts.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
// search artifact id
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
+ SearchResult result = (SearchResult) artifacts.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
}
// search version
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2" );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
+ SearchResult result = (SearchResult) artifacts.next();
+ Artifact artifact = result.getArtifact();
assertTrue( artifact.getVersion().indexOf( "2" ) != -1 );
}
// search packaging
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PACKAGING, "jar" );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 3, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
- assertEquals( "jar", artifact.getType() );
+ SearchResult result = (SearchResult) artifacts.next();
+ Map map = result.getFieldMatches();
+ Set mapEntry = map.entrySet();
+ for ( Iterator it = mapEntry.iterator(); it.hasNext(); )
+ {
+ Map.Entry entry = (Map.Entry) it.next();
+ }
+ assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) );
}
//search license url
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_LICENSE_URLS,
- "http://www.apache.org/licenses/LICENSE-2.0.txt" );
- artifactList = repoSearcher.search( qry );
+ qry =
+ new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
- License license = (License) getPom( artifact ).getLicenses().get( 0 );
- assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", license.getUrl() );
+ SearchResult result = (SearchResult) artifacts.next();
+ Map map = result.getFieldMatches();
+ List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS );
+ for ( Iterator it = matches.iterator(); it.hasNext(); )
+ {
+ assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() );
+ }
}
//search dependencies
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 2, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
- Iterator dependencies = getPom( artifact ).getDependencies().iterator();
+ SearchResult result = (SearchResult) artifacts.next();
+ Map map = result.getFieldMatches();
boolean depFound = false;
+ Iterator dependencies = ( (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ) ).iterator();
while ( dependencies.hasNext() )
{
- Dependency dep = (Dependency) dependencies.next();
- if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals(
- dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ) )
+ String dep = (String) dependencies.next();
+ if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) )
{
depFound = true;
break;
}
//search build plugin
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_BUILD,
- "org.codehaus.modello:modello-maven-plugin:2.0" );
- artifactList = repoSearcher.search( qry );
+ qry =
+ new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
- Iterator plugins = getPom( artifact ).getBuild().getPlugins().iterator();
+ SearchResult result = (SearchResult) artifacts.next();
+ Map map = result.getFieldMatches();
+ Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ) ).iterator();
boolean found = false;
while ( plugins.hasNext() )
{
- Plugin plugin = (Plugin) plugins.next();
- if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals(
- plugin.getKey() + ":" + plugin.getVersion() ) )
+ String plugin = (String) plugins.next();
+ if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) )
{
found = true;
break;
}
//search reporting plugin
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_REPORT,
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT,
"org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );
- artifactList = repoSearcher.search( qry );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact = (Artifact) artifacts.next();
- Iterator plugins = getPom( artifact ).getReporting().getPlugins().iterator();
+ SearchResult result = (SearchResult) artifacts.next();
+ Map map = result.getFieldMatches();
+ Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ) ).iterator();
boolean found = false;
while ( plugins.hasNext() )
{
- ReportPlugin plugin = (ReportPlugin) plugins.next();
- if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals(
- plugin.getKey() + ":" + plugin.getVersion() ) )
+ String plugin = (String) plugins.next();
+ if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) )
{
found = true;
break;
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 );
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_SHA1, sha1.trim() );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact2 = (Artifact) artifacts.next();
+ SearchResult result = (SearchResult) artifacts.next();
+ Artifact artifact2 = result.getArtifact();
String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 );
assertEquals( sha1, sha1Tmp );
}
// search md5 checksum
String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 );
- qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_MD5, md5.trim() );
- artifactList = repoSearcher.search( qry );
+ qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() );
+ artifactList = repoSearchLayer.searchAdvanced( qry );
assertEquals( 1, artifactList.size() );
for ( artifacts = artifactList.iterator(); artifacts.hasNext(); )
{
- Artifact artifact2 = (Artifact) artifacts.next();
+ SearchResult result = (SearchResult) artifacts.next();
+ Artifact artifact2 = result.getArtifact();
String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 );
assertEquals( md5, md5Tmp );
}
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
- RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
+ //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer );
+ RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer );
// Criteria 1: required query
// ex. artifactId=maven-artifact AND groupId=org.apache.maven
- Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
- Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" );
+ Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" );
+ Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" );
CompoundQuery rQry = new CompoundQuery();
rQry.and( qry1 );
rQry.and( qry2 );
- List artifacts = repoSearcher.search( rQry );
+ List artifacts = repoSearchLayer.searchAdvanced( rQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
// Criteria 2: nested required query
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR
// version=2.0.3
- Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.3" );
+ Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" );
CompoundQuery oQry = new CompoundQuery();
oQry.and( rQry );
oQry.or( qry3 );
- artifacts = repoSearcher.search( oQry );
+ artifacts = repoSearchLayer.searchAdvanced( oQry );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
// ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND
// (version=2.0.3 OR version=2.0.1)
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)
- Query qry4 = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2.0.1" );
+ Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" );
oQry = new CompoundQuery();
oQry.or( qry3 );
oQry.or( qry4 );
CompoundQuery oQry5 = new CompoundQuery();
Query qry9 =
- new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
- Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES,
+ new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" );
+ Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES,
"org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" );
oQry5.or( qry9 );
oQry5.or( qry10 );
rQry2.and( rQry );
rQry2.and( oQry5 );
- artifacts = repoSearcher.search( rQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( rQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
assertEquals( "2.0.1", artifact.getVersion() );
// AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)]
// OR [(artifactId=sample AND groupId=test)]
CompoundQuery rQry3 = new CompoundQuery();
- Query qry5 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample" );
- Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
+ Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" );
+ Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
rQry3.and( qry5 );
rQry3.and( qry6 );
CompoundQuery oQry2 = new CompoundQuery();
oQry2.and( rQry2 );
oQry2.and( rQry3 );
- artifacts = repoSearcher.search( oQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
assertEquals( "2.0.1", artifact.getVersion() );
// [(artifactId=sample AND groupId=test)] OR
// [(artifactId=sample2 AND groupId=test)]
CompoundQuery rQry4 = new CompoundQuery();
- Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" );
- Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" );
+ Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" );
+ Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" );
rQry4.and( qry7 );
rQry4.and( qry8 );
oQry2.and( rQry4 );
- artifacts = repoSearcher.search( oQry2 );
+ artifacts = repoSearchLayer.searchAdvanced( oQry2 );
for ( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
- Artifact artifact = (Artifact) iter.next();
+ SearchResult result = (SearchResult) iter.next();
+ Artifact artifact = result.getArtifact();
assertEquals( "maven-artifact", artifact.getArtifactId() );
assertEquals( "org.apache.maven", artifact.getGroupId() );
}
RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );
PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository );
Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );
- indexer.deleteDocument( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() );
+ indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer );
- Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() );
+ Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() );
List artifactList = repoSearcher.search( qry );
assertEquals( artifactList.size(), 0 );
}
--- /dev/null
+package org.apache.maven.repository.indexing;\r
+\r
+/*\r
+ * Copyright 2005-2006 The Apache Software Foundation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+import org.codehaus.plexus.PlexusTestCase;\r
+import org.codehaus.plexus.util.FileUtils;\r
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;\r
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;\r
+import org.apache.maven.artifact.repository.ArtifactRepository;\r
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;\r
+import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;\r
+import org.apache.maven.artifact.Artifact;\r
+import org.apache.maven.artifact.factory.ArtifactFactory;\r
+import org.apache.maven.repository.digest.DefaultDigester;\r
+import org.apache.maven.repository.digest.Digester;\r
+import org.apache.maven.model.Model;\r
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;\r
+\r
+import java.io.File;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.io.FileReader;\r
+import java.util.List;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.net.URL;\r
+\r
+/**\r
+ * <p/>\r
+ * This class tests the RepositoryIndexSearchLayer.\r
+ */\r
+public class RepositoryIndexSearchLayerTest\r
+ extends PlexusTestCase\r
+{\r
+ private ArtifactRepository repository;\r
+\r
+ private ArtifactFactory artifactFactory;\r
+\r
+ private Digester digester;\r
+\r
+ private String indexPath;\r
+\r
+ /**\r
+ * Setup method\r
+ *\r
+ * @throws Exception\r
+ */\r
+ protected void setUp()\r
+ throws Exception\r
+ {\r
+ super.setUp();\r
+ File repositoryDirectory = getTestFile( "src/test/repository" );\r
+ String repoDir = repositoryDirectory.toURL().toString();\r
+ ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );\r
+ ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );\r
+ repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );\r
+ digester = new DefaultDigester();\r
+\r
+ indexPath = "target/index";\r
+ FileUtils.deleteDirectory( indexPath );\r
+ }\r
+\r
+ /**\r
+ * Tear down method\r
+ *\r
+ * @throws Exception\r
+ */\r
+ protected void tearDown()\r
+ throws Exception\r
+ {\r
+ super.tearDown();\r
+ }\r
+\r
+ /**\r
+ * Method for creating the index used for testing\r
+ *\r
+ * @throws Exception\r
+ */\r
+ private void createTestIndex()\r
+ throws Exception\r
+ {\r
+ RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );\r
+ ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );\r
+\r
+ Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );\r
+ artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
+ indexer.indexArtifact( artifact );\r
+ indexer.optimize();\r
+ indexer.close();\r
+\r
+ artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );\r
+ artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
+ indexer.indexArtifact( artifact );\r
+ indexer.optimize();\r
+ indexer.close();\r
+\r
+ artifact = getArtifact( "test", "test-artifactId", "1.0" );\r
+ artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
+ indexer.indexArtifact( artifact );\r
+ indexer.optimize();\r
+ indexer.close();\r
+\r
+ artifact = getArtifact( "test", "test-artifactId", "1.0" );\r
+ artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );\r
+ indexer.indexArtifact( artifact );\r
+ indexer.optimize();\r
+ indexer.close();\r
+\r
+ MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository );\r
+ RepositoryMetadata repoMetadata =\r
+ getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );\r
+ metaIndexer.index( repoMetadata );\r
+ metaIndexer.optimize();\r
+ metaIndexer.close();\r
+\r
+ repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",\r
+ metaIndexer.ARTIFACT_METADATA );\r
+ metaIndexer.index( repoMetadata );\r
+ metaIndexer.optimize();\r
+ metaIndexer.close();\r
+\r
+ repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml",\r
+ metaIndexer.SNAPSHOT_METADATA );\r
+ metaIndexer.index( repoMetadata );\r
+ metaIndexer.optimize();\r
+ metaIndexer.close();\r
+\r
+ repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA );\r
+ metaIndexer.index( repoMetadata );\r
+ metaIndexer.optimize();\r
+ metaIndexer.close();\r
+\r
+ PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository );\r
+\r
+ Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" );\r
+ pomIndexer.indexPom( pom );\r
+ pomIndexer.optimize();\r
+ pomIndexer.close();\r
+\r
+ pom = getPom( "org.apache.maven", "maven-model", "2.0" );\r
+ pomIndexer.indexPom( pom );\r
+ pomIndexer.optimize();\r
+ pomIndexer.close();\r
+\r
+ pom = getPom( "test", "test-artifactId", "1.0" );\r
+ pomIndexer.indexPom( pom );\r
+ pomIndexer.optimize();\r
+ pomIndexer.close();\r
+\r
+ pom = getPom( "test", "test-artifactId", "1.0" );\r
+ pomIndexer.indexPom( pom );\r
+ pomIndexer.optimize();\r
+ pomIndexer.close();\r
+ }\r
+\r
+ /**\r
+ * Method for testing the "query everything" searcher\r
+ *\r
+ * @throws Exception\r
+ */\r
+ public void testGeneralSearcher()\r
+ throws Exception\r
+ {\r
+ createTestIndex();\r
+ RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE );\r
+ ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository );\r
+ RepositoryIndexSearchLayer searchLayer = factory.createRepositoryIndexSearchLayer( indexer );\r
+\r
+ List returnList = searchLayer.searchGeneral( "org.apache.maven" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGES ) )\r
+ {\r
+ List packages = (List) entry.getValue();\r
+ for ( Iterator iterator = packages.iterator(); iterator.hasNext(); )\r
+ {\r
+ assertTrue( ( (String) iterator.next() ).indexOf( "org.apache.maven" ) != -1 );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ //POM license urls\r
+ returnList = searchLayer.searchGeneral( "http://www.apache.org/licenses/LICENSE-2.0.txt" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_LICENSE_URLS ) )\r
+ {\r
+ List packages = (List) entry.getValue();\r
+ for ( Iterator iterator = packages.iterator(); iterator.hasNext(); )\r
+ {\r
+ assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) iterator.next() );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ //POM dependency\r
+ returnList = searchLayer.searchGeneral( "org.codehaus.plexus:plexus-utils:1.0.5" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_DEPENDENCIES ) )\r
+ {\r
+ List packages = (List) entry.getValue();\r
+ for ( Iterator iterator = packages.iterator(); iterator.hasNext(); )\r
+ {\r
+ assertEquals( "org.codehaus.plexus:plexus-utils:1.0.5", (String) iterator.next() );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ // POM reporting plugin\r
+ returnList = searchLayer.searchGeneral( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_REPORT ) )\r
+ {\r
+ List packages = (List) entry.getValue();\r
+ for ( Iterator iterator = packages.iterator(); iterator.hasNext(); )\r
+ {\r
+ assertEquals( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0",\r
+ (String) iterator.next() );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ // POM build plugin\r
+ returnList = searchLayer.searchGeneral( "org.codehaus.modello:modello-maven-plugin:2.0" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_BUILD ) )\r
+ {\r
+ List packages = (List) entry.getValue();\r
+ for ( Iterator iterator = packages.iterator(); iterator.hasNext(); )\r
+ {\r
+ assertEquals( "org.codehaus.modello:modello-maven-plugin:2.0", (String) iterator.next() );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ //maven-artifact-2.0.1.jar MD5 checksum\r
+ returnList = searchLayer.searchGeneral( "F5A934ABBBC70A33136D89A996B9D5C09F652766" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_MD5 ) )\r
+ {\r
+ assertTrue(\r
+ ( (String) entry.getValue() ).indexOf( "F5A934ABBBC70A33136D89A996B9D5C09F652766" ) != -1 );\r
+ }\r
+ }\r
+ }\r
+\r
+ //maven-artifact-2.0.1.jar SHA1 checksum\r
+ returnList = searchLayer.searchGeneral( "AE55D9B5720E11B6CF19FE1E31A42E51" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_SHA1 ) )\r
+ {\r
+ assertTrue( ( (String) entry.getValue() ).indexOf( "AE55D9B5720E11B6CF19FE1E31A42E516" ) != -1 );\r
+ }\r
+ }\r
+ }\r
+\r
+ //packaging jar\r
+ returnList = searchLayer.searchGeneral( "jar" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ Map map = result.getFieldMatches();\r
+ Set entries = map.entrySet();\r
+ for ( Iterator it = entries.iterator(); it.hasNext(); )\r
+ {\r
+ Map.Entry entry = (Map.Entry) it.next();\r
+ if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGING ) )\r
+ {\r
+ assertEquals( "jar", (String) entry.getValue() );\r
+ }\r
+ }\r
+ }\r
+\r
+ returnList = searchLayer.searchGeneral( "test" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ assertEquals( result.getArtifact().getGroupId(), "test" );\r
+ }\r
+\r
+ returnList = searchLayer.searchGeneral( "test-artifactId" );\r
+ for ( Iterator iter = returnList.iterator(); iter.hasNext(); )\r
+ {\r
+ SearchResult result = (SearchResult) iter.next();\r
+ assertEquals( result.getArtifact().getArtifactId(), "test-artifactId" );\r
+ }\r
+\r
+ }\r
+\r
+\r
+ /**\r
+ * Method for creating RepositoryMetadata object\r
+ *\r
+ * @param groupId the groupId to be set\r
+ * @param artifactId the artifactId to be set\r
+ * @param version the version to be set\r
+ * @param filename the name of the metadata file\r
+ * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT)\r
+ * @return RepositoryMetadata\r
+ * @throws Exception\r
+ */\r
+ private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename,\r
+ String metadataType )\r
+ throws Exception\r
+ {\r
+ RepositoryMetadata repoMetadata = null;\r
+ URL url;\r
+ InputStream is = null;\r
+ MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();\r
+\r
+ //group metadata\r
+ if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) )\r
+ {\r
+ url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL();\r
+ is = url.openStream();\r
+ repoMetadata = new GroupRepositoryMetadata( groupId );\r
+ repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
+ }\r
+ //artifact metadata\r
+ else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) )\r
+ {\r
+ url = new File(\r
+ repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + filename ).toURL();\r
+ is = url.openStream();\r
+ repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );\r
+ repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
+ }\r
+ //snapshot/version metadata\r
+ else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) )\r
+ {\r
+ url = new File( repository.getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + version +\r
+ "/" + filename ).toURL();\r
+ is = url.openStream();\r
+ repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) );\r
+ repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) );\r
+ }\r
+\r
+ return repoMetadata;\r
+ }\r
+\r
+ /**\r
+ * Method for creating Artifact object\r
+ *\r
+ * @param groupId the groupId of the artifact to be created\r
+ * @param artifactId the artifactId of the artifact to be created\r
+ * @param version the version of the artifact to be created\r
+ * @return Artifact object\r
+ * @throws Exception\r
+ */\r
+ private Artifact getArtifact( String groupId, String artifactId, String version )\r
+ throws Exception\r
+ {\r
+ if ( artifactFactory == null )\r
+ {\r
+ artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );\r
+ }\r
+\r
+ return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );\r
+ }\r
+\r
+ /**\r
+ * Method for creating a Model object given the groupId, artifactId and version\r
+ *\r
+ * @param groupId the groupId of the model to be created\r
+ * @param artifactId the artifactId of the model to be created\r
+ * @param version the version of the model to be created\r
+ * @return Model object\r
+ * @throws Exception\r
+ */\r
+ private Model getPom( String groupId, String artifactId, String version )\r
+ throws Exception\r
+ {\r
+ Artifact artifact = getArtifact( groupId, artifactId, version );\r
+\r
+ return getPom( artifact );\r
+ }\r
+\r
+ /**\r
+ * Method for creating a Model object given an artifact\r
+ *\r
+ * @param artifact the artifact to be created a Model object for\r
+ * @return Model object\r
+ * @throws Exception\r
+ */\r
+ private Model getPom( Artifact artifact )\r
+ throws Exception\r
+ {\r
+ File pomFile = getPomFile( artifact );\r
+\r
+ MavenXpp3Reader pomReader = new MavenXpp3Reader();\r
+ return pomReader.read( new FileReader( pomFile ) );\r
+ }\r
+\r
+ /**\r
+ * Method for creating a pom file\r
+ *\r
+ * @param artifact\r
+ * @return File\r
+ */\r
+ private File getPomFile( Artifact artifact )\r
+ {\r
+ String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath();\r
+ return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" );\r
+ }\r
+\r
+}\r