1 package org.apache.maven.repository.indexing;
\r
4 * Copyright 2005-2006 The Apache Software Foundation.
\r
6 * Licensed under the Apache License, Version 2.0 (the "License");
\r
7 * you may not use this file except in compliance with the License.
\r
8 * You may obtain a copy of the License at
\r
10 * http://www.apache.org/licenses/LICENSE-2.0
\r
12 * Unless required by applicable law or agreed to in writing, software
\r
13 * distributed under the License is distributed on an "AS IS" BASIS,
\r
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 * See the License for the specific language governing permissions and
\r
16 * limitations under the License.
\r
19 import org.apache.lucene.document.Document;
\r
20 import org.apache.lucene.document.Field;
\r
21 import org.apache.maven.artifact.repository.ArtifactRepository;
\r
22 import org.apache.maven.artifact.repository.metadata.Metadata;
\r
23 import org.apache.maven.artifact.repository.metadata.Plugin;
\r
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
\r
25 import org.apache.maven.artifact.repository.metadata.Versioning;
\r
27 import java.io.IOException;
\r
28 import java.util.Iterator;
\r
29 import java.util.List;
\r
32 * This class indexes the metadata in the repository.
\r
34 public class MetadataRepositoryIndex
\r
35 extends AbstractRepositoryIndex
\r
37 protected static final String GROUP_METADATA = "GROUP_METADATA";
\r
39 protected static final String ARTIFACT_METADATA = "ARTIFACT_METADATA";
\r
41 protected static final String SNAPSHOT_METADATA = "SNAPSHOT_METADATA";
\r
46 * @param indexPath the path to the index
\r
47 * @param repository the repository where the metadata to be indexed is located
\r
49 public MetadataRepositoryIndex( String indexPath, ArtifactRepository repository )
\r
51 super( indexPath, repository );
\r
55 * Index the paramater object
\r
58 * @throws RepositoryIndexException
\r
60 public void index( Object obj )
\r
61 throws RepositoryIndexException
\r
63 if ( obj instanceof RepositoryMetadata )
\r
65 indexMetadata( (RepositoryMetadata) obj );
\r
69 throw new RepositoryIndexException(
\r
70 "This instance of indexer cannot index instances of " + obj.getClass().getName() );
\r
75 * Index the contents of the specified RepositoryMetadata paramter object
\r
77 * @param repoMetadata the metadata object to be indexed
\r
78 * @throws RepositoryIndexException
\r
80 private void indexMetadata( RepositoryMetadata repoMetadata )
\r
81 throws RepositoryIndexException
\r
83 //get lastUpdated from Versioning (specified in Metadata object)
\r
84 //get pluginPrefixes from Plugin (spcified in Metadata object) -----> concatenate/append???
\r
85 //get the metadatapath: check where metadata is located, then concatenate the groupId,
\r
86 // artifactId, version based on its location
\r
87 Document doc = new Document();
\r
88 doc.add( Field.Keyword( FLD_ID, (String) repoMetadata.getKey() ) );
\r
90 Metadata metadata = repoMetadata.getMetadata();
\r
92 if ( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
\r
94 path = repoMetadata.getGroupId() + "/";
\r
96 else if ( !repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
\r
98 path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/";
\r
100 else if ( !repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory() )
\r
102 path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" +
\r
103 repoMetadata.getBaseVersion() + "/";
\r
106 if ( !"".equals( repoMetadata.getRemoteFilename() ) && repoMetadata.getRemoteFilename() != null )
\r
108 path = path + repoMetadata.getRemoteFilename();
\r
112 path = path + repoMetadata.getLocalFilename( repository );
\r
114 doc.add( Field.Text( FLD_NAME, path ) );
\r
116 Versioning versioning = metadata.getVersioning();
\r
117 if ( versioning != null )
\r
119 doc.add( Field.Text( FLD_LASTUPDATE, versioning.getLastUpdated() ) );
\r
123 doc.add( Field.Text( FLD_LASTUPDATE, "" ) );
\r
126 List plugins = metadata.getPlugins();
\r
127 String pluginAppended = "";
\r
128 for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
\r
130 Plugin plugin = (Plugin) iter.next();
\r
131 if ( plugin.getPrefix() != null && !"".equals( plugin.getPrefix() ) )
\r
133 pluginAppended = plugin.getPrefix() + "\n";
\r
136 doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) );
\r
137 doc.add( Field.Text( FLD_GROUPID, metadata.getGroupId() ) );
\r
139 if ( metadata.getArtifactId() != null && !"".equals( metadata.getArtifactId() ) )
\r
141 doc.add( Field.Text( FLD_ARTIFACTID, metadata.getArtifactId() ) );
\r
145 doc.add( Field.Text( FLD_ARTIFACTID, "" ) );
\r
148 if ( metadata.getVersion() != null && !"".equals( metadata.getVersion() ) )
\r
150 doc.add( Field.Text( FLD_VERSION, metadata.getVersion() ) );
\r
154 doc.add( Field.Text( FLD_VERSION, "" ) );
\r
156 doc.add( Field.Text( FLD_DOCTYPE, METADATA ) );
\r
157 doc.add( Field.Keyword( FLD_PACKAGING, "" ) );
\r
158 doc.add( Field.Text( FLD_SHA1, "" ) );
\r
159 doc.add( Field.Text( FLD_MD5, "" ) );
\r
160 doc.add( Field.Text( FLD_CLASSES, "" ) );
\r
161 doc.add( Field.Text( FLD_PACKAGES, "" ) );
\r
162 doc.add( Field.Text( FLD_FILES, "" ) );
\r
163 doc.add( Field.Keyword( FLD_LICENSE_URLS, "" ) );
\r
164 doc.add( Field.Keyword( FLD_DEPENDENCIES, "" ) );
\r
165 doc.add( Field.Keyword( FLD_PLUGINS_BUILD, "" ) );
\r
166 doc.add( Field.Keyword( FLD_PLUGINS_REPORT, "" ) );
\r
167 doc.add( Field.Keyword( FLD_PLUGINS_ALL, "" ) );
\r
171 deleteIfIndexed( repoMetadata );
\r
176 getIndexWriter().addDocument( doc );
\r
178 catch ( IOException e )
\r
180 throw new RepositoryIndexException( "Error opening index", e );
\r
185 * @see org.apache.maven.repository.indexing.AbstractRepositoryIndex#deleteIfIndexed(Object)
\r
187 public void deleteIfIndexed( Object object )
\r
188 throws RepositoryIndexException, IOException
\r
190 if ( object instanceof RepositoryMetadata )
\r
192 RepositoryMetadata repoMetadata = (RepositoryMetadata) object;
\r
193 if ( indexExists() )
\r
195 validateIndex( FIELDS );
\r
196 deleteDocument( FLD_ID, (String) repoMetadata.getKey() );
\r
201 throw new RepositoryIndexException( "Object is not of type metadata." );
\r