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
48 * @throws RepositoryIndexException
\r
50 public MetadataRepositoryIndex( String indexPath, ArtifactRepository repository )
\r
51 throws RepositoryIndexException
\r
53 super( indexPath, repository );
\r
57 * Index the paramater object
\r
60 * @throws RepositoryIndexException
\r
62 public void index( Object obj )
\r
63 throws RepositoryIndexException
\r
65 if ( obj instanceof RepositoryMetadata )
\r
67 indexMetadata( (RepositoryMetadata) obj );
\r
71 throw new RepositoryIndexException(
\r
72 "This instance of indexer cannot index instances of " + obj.getClass().getName() );
\r
77 * Index the contents of the specified RepositoryMetadata paramter object
\r
79 * @param repoMetadata the metadata object to be indexed
\r
80 * @throws RepositoryIndexException
\r
82 private void indexMetadata( RepositoryMetadata repoMetadata )
\r
83 throws RepositoryIndexException
\r
85 //get lastUpdated from Versioning (specified in Metadata object)
\r
86 //get pluginPrefixes from Plugin (spcified in Metadata object) -----> concatenate/append???
\r
87 //get the metadatapath: check where metadata is located, then concatenate the groupId,
\r
88 // artifactId, version based on its location
\r
89 Document doc = new Document();
\r
90 doc.add( Field.Keyword( FLD_ID, (String) repoMetadata.getKey() ) );
\r
92 Metadata metadata = repoMetadata.getMetadata();
\r
94 if ( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
\r
96 path = repoMetadata.getGroupId() + "/";
\r
98 else if ( !repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory() )
\r
100 path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/";
\r
102 else if ( !repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory() )
\r
104 path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" +
\r
105 repoMetadata.getBaseVersion() + "/";
\r
108 if ( !repoMetadata.getRemoteFilename().equals( "" ) && repoMetadata.getRemoteFilename() != null )
\r
110 path = path + repoMetadata.getRemoteFilename();
\r
114 path = path + repoMetadata.getLocalFilename( repository );
\r
116 doc.add( Field.Text( FLD_NAME, path ) );
\r
118 Versioning versioning = metadata.getVersioning();
\r
119 if ( versioning != null )
\r
121 doc.add( Field.Text( FLD_LASTUPDATE, versioning.getLastUpdated() ) );
\r
125 doc.add( Field.Text( FLD_LASTUPDATE, "" ) );
\r
128 List plugins = metadata.getPlugins();
\r
129 String pluginAppended = "";
\r
130 for ( Iterator iter = plugins.iterator(); iter.hasNext(); )
\r
132 Plugin plugin = (Plugin) iter.next();
\r
133 if ( plugin.getPrefix() != null && !plugin.getPrefix().equals( "" ) )
\r
135 pluginAppended = plugin.getPrefix() + "\n";
\r
138 doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) );
\r
139 doc.add( Field.Text( FLD_GROUPID, metadata.getGroupId() ) );
\r
141 if ( metadata.getArtifactId() != null && !metadata.getArtifactId().equals( "" ) )
\r
143 doc.add( Field.Text( FLD_ARTIFACTID, metadata.getArtifactId() ) );
\r
147 doc.add( Field.Text( FLD_ARTIFACTID, "" ) );
\r
150 if ( metadata.getVersion() != null && !metadata.getVersion().equals( "" ) )
\r
152 doc.add( Field.Text( FLD_VERSION, metadata.getVersion() ) );
\r
156 doc.add( Field.Text( FLD_VERSION, "" ) );
\r
158 doc.add( Field.Text( FLD_DOCTYPE, METADATA ) );
\r
159 doc.add( Field.Keyword( FLD_PACKAGING, "" ) );
\r
160 doc.add( Field.Text( FLD_SHA1, "" ) );
\r
161 doc.add( Field.Text( FLD_MD5, "" ) );
\r
162 doc.add( Field.Text( FLD_CLASSES, "" ) );
\r
163 doc.add( Field.Text( FLD_PACKAGES, "" ) );
\r
164 doc.add( Field.Text( FLD_FILES, "" ) );
\r
165 doc.add( Field.Keyword( FLD_LICENSE_URLS, "" ) );
\r
166 doc.add( Field.Keyword( FLD_DEPENDENCIES, "" ) );
\r
167 doc.add( Field.Keyword( FLD_PLUGINS_BUILD, "" ) );
\r
168 doc.add( Field.Keyword( FLD_PLUGINS_REPORT, "" ) );
\r
169 doc.add( Field.Keyword( FLD_PLUGINS_ALL, "" ) );
\r
173 isIndexed( repoMetadata );
\r
178 getIndexWriter().addDocument( doc );
\r
180 catch ( IOException e )
\r
182 throw new RepositoryIndexException( "Error opening index", e );
\r
187 * @see org.apache.maven.repository.indexing.AbstractRepositoryIndex#isIndexed(Object)
\r
189 public void isIndexed( Object object )
\r
190 throws RepositoryIndexException, IOException
\r
192 if ( object instanceof RepositoryMetadata )
\r
194 RepositoryMetadata repoMetadata = (RepositoryMetadata) object;
\r
195 checkIfIndexExists();
\r
198 validateIndex( FIELDS );
\r
199 deleteDocument( FLD_ID, (String) repoMetadata.getKey() );
\r
204 throw new RepositoryIndexException( "Object is not of type metadata." );
\r