diff options
author | Brett Porter <brett@apache.org> | 2009-10-16 11:30:46 +0000 |
---|---|---|
committer | Brett Porter <brett@apache.org> | 2009-10-16 11:30:46 +0000 |
commit | 4e4362191f12adbffc2af970b6e1e4affea94954 (patch) | |
tree | 83a64717895955a8c584970f0bed72b44708e5e1 /archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers | |
parent | e75014219beca14e8128dc65b28e838c96ba36d7 (diff) | |
parent | 61336c8774e7341d5d16d4adba809ef2ca54a969 (diff) | |
download | archiva-4e4362191f12adbffc2af970b6e1e4affea94954.tar.gz archiva-4e4362191f12adbffc2af970b6e1e4affea94954.zip |
recreate from trunk
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@825849 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers')
14 files changed, 860 insertions, 1099 deletions
diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml index f26574969..e87fda89c 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/pom.xml @@ -1,18 +1,21 @@ <?xml version="1.0" encoding="UTF-8"?> <!-- - ~ Copyright 2005-2006 The Apache Software Foundation. + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at ~ - ~ Licensed under the Apache License, Version 2.0 (the "License"); - ~ you may not use this file except in compliance with the License. - ~ You may obtain a copy of the License at + ~ http://www.apache.org/licenses/LICENSE-2.0 ~ - ~ http://www.apache.org/licenses/LICENSE-2.0 - ~ - ~ Unless required by applicable law or agreed to in writing, software - ~ distributed under the License is distributed on an "AS IS" BASIS, - ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - ~ See the License for the specific language governing permissions and - ~ limitations under the License. + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> @@ -20,13 +23,11 @@ <parent> <groupId>org.apache.archiva</groupId> <artifactId>archiva-consumers</artifactId> - <version>1.2-MRM1025-SNAPSHOT</version> - <relativePath>../pom.xml</relativePath> + <version>1.2.3-SNAPSHOT</version> </parent> <artifactId>archiva-lucene-consumers</artifactId> <name>Archiva Consumers :: Lucene Consumers</name> - <packaging>jar</packaging> <dependencies> <dependency> @@ -50,6 +51,10 @@ <artifactId>archiva-repository-layer</artifactId> </dependency> <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>archiva-scheduled</artifactId> + </dependency> + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-spring</artifactId> <scope>test</scope> @@ -59,5 +64,10 @@ <artifactId>bcel</artifactId> <version>5.2</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumer.java new file mode 100644 index 000000000..63b3c4de1 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumer.java @@ -0,0 +1,153 @@ +package org.apache.archiva.consumers.lucene; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.database.updater.DatabaseCleanupConsumer; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.RepositoryContentFactory; +import org.apache.maven.archiva.repository.RepositoryException; +import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; +import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; +import org.apache.maven.archiva.scheduled.tasks.TaskCreator; +import org.codehaus.plexus.taskqueue.TaskQueueException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonatype.nexus.index.context.IndexingContext; +import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; + +/** + * LuceneCleanupRemoveIndexedConsumer Clean up the index of artifacts that are no longer existing in the file system + * (managed repositories). + * + * @version $Id$ + */ +public class LuceneCleanupRemoveIndexedConsumer + extends AbstractMonitoredConsumer + implements DatabaseCleanupConsumer +{ + private static final Logger log = LoggerFactory.getLogger( LuceneCleanupRemoveIndexedConsumer.class ); + + private RepositoryContentFactory repoFactory; + + private ArchivaTaskScheduler scheduler; + + public LuceneCleanupRemoveIndexedConsumer( RepositoryContentFactory repoFactory, ArchivaTaskScheduler scheduler ) + { + this.repoFactory = repoFactory; + this.scheduler = scheduler; + } + + public void beginScan() + { + } + + public void completeScan() + { + } + + public List<String> getIncludedTypes() + { + return null; + } + + public void processArchivaArtifact( ArchivaArtifact artifact ) + throws ConsumerException + { + ManagedRepositoryContent repoContent = null; + + try + { + repoContent = repoFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() ); + } + catch ( RepositoryException e ) + { + throw new ConsumerException( "Can't run index cleanup consumer: " + e.getMessage() ); + } + + ManagedRepositoryConfiguration repository = repoContent.getRepository(); + + IndexingContext context = null; + try + { + File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) ); + + if ( !artifactFile.exists() ) + { + context = TaskCreator.createContext( repository ); + + ArtifactIndexingTask task = + TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.DELETE, + context ); + + log.debug( "Queueing indexing task '" + task + "' to remove the artifact from the index." ); + scheduler.queueIndexingTask( task ); + + // note we finish immediately here since it isn't done repo-by-repo. It might be nice to ensure that is + // the case for optimisation though + task = + TaskCreator.createIndexingTask( repository, artifactFile, ArtifactIndexingTask.Action.FINISH, + context ); + log.debug( "Queueing indexing task + '" + task + "' to finish indexing." ); + scheduler.queueIndexingTask( task ); + } + + } + catch ( TaskQueueException e ) + { + throw new ConsumerException( e.getMessage() ); + } + catch ( IOException e ) + { + throw new ConsumerException( e.getMessage(), e ); + } + catch ( UnsupportedExistingLuceneIndexException e ) + { + throw new ConsumerException( e.getMessage(), e ); + } + } + + public String getDescription() + { + return "Remove indexed content if not present on filesystem."; + } + + public String getId() + { + return "not-present-remove-indexed"; + } + + public boolean isPermanent() + { + return false; + } + + public void setRepositoryContentFactory( RepositoryContentFactory repoFactory ) + { + this.repoFactory = repoFactory; + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java new file mode 100644 index 000000000..775d7650a --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumer.java @@ -0,0 +1,190 @@ +package org.apache.archiva.consumers.lucene; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.ConfigurationNames; +import org.apache.maven.archiva.configuration.FileTypes; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; +import org.apache.maven.archiva.consumers.ConsumerException; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; +import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; +import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; +import org.apache.maven.archiva.scheduled.tasks.TaskCreator; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; +import org.codehaus.plexus.registry.Registry; +import org.codehaus.plexus.registry.RegistryListener; +import org.codehaus.plexus.taskqueue.TaskQueueException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonatype.nexus.index.context.IndexingContext; +import org.sonatype.nexus.index.context.UnsupportedExistingLuceneIndexException; + +/** + * Consumer for indexing the repository to provide search and IDE integration features. + */ +public class NexusIndexerConsumer + extends AbstractMonitoredConsumer + implements KnownRepositoryContentConsumer, RegistryListener, Initializable +{ + private static final Logger log = LoggerFactory.getLogger( NexusIndexerConsumer.class ); + + private ArchivaConfiguration configuration; + + private FileTypes filetypes; + + private ManagedDefaultRepositoryContent repositoryContent; + + private File managedRepository; + + private ArchivaTaskScheduler scheduler; + + private IndexingContext context; + + private List<String> includes = new ArrayList<String>(); + + public NexusIndexerConsumer( ArchivaTaskScheduler scheduler, ArchivaConfiguration configuration, FileTypes filetypes ) + { + this.configuration = configuration; + this.filetypes = filetypes; + this.scheduler = scheduler; + } + + public String getDescription() + { + return "Indexes the repository to provide search and IDE integration features"; + } + + public String getId() + { + return "index-content"; + } + + public boolean isPermanent() + { + return false; + } + + public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered ) + throws ConsumerException + { + managedRepository = new File( repository.getLocation() ); + + repositoryContent = new ManagedDefaultRepositoryContent(); + repositoryContent.setRepository( repository ); + + try + { + context = TaskCreator.createContext( repository ); + } + catch ( IOException e ) + { + throw new ConsumerException( e.getMessage(), e ); + } + catch ( UnsupportedExistingLuceneIndexException e ) + { + throw new ConsumerException( e.getMessage(), e ); + } + } + + public void processFile( String path ) + throws ConsumerException + { + File artifactFile = new File( managedRepository, path ); + + ArtifactIndexingTask task = + TaskCreator.createIndexingTask( repositoryContent.getRepository(), artifactFile, + ArtifactIndexingTask.Action.ADD, context ); + try + { + log.debug( "Queueing indexing task + '" + task + "' to add or update the artifact in the index." ); + scheduler.queueIndexingTask( task ); + } + catch ( TaskQueueException e ) + { + throw new ConsumerException( e.getMessage(), e ); + } + } + + public void completeScan() + { + ArtifactIndexingTask task = + TaskCreator.createIndexingTask( repositoryContent.getRepository(), null, + ArtifactIndexingTask.Action.FINISH, context ); + try + { + log.debug( "Queueing indexing task + '" + task + "' to finish indexing." ); + scheduler.queueIndexingTask( task ); + } + catch ( TaskQueueException e ) + { + log.error( "Error queueing task: " + task + ": " + e.getMessage(), e ); + } + context = null; + } + + public List<String> getExcludes() + { + return Collections.emptyList(); + } + + public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) + { + initIncludes(); + } + } + + public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) + { + /* do nothing */ + } + + private void initIncludes() + { + includes.clear(); + + includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) ); + } + + public void initialize() + throws InitializationException + { + configuration.addChangeListener( this ); + + initIncludes(); + } + + public List<String> getIncludes() + { + return includes; + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexArtifactConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexArtifactConsumer.java deleted file mode 100644 index 45517a663..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexArtifactConsumer.java +++ /dev/null @@ -1,216 +0,0 @@ -package org.apache.maven.archiva.consumers.lucene; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang.StringUtils; -import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.ConfigurationNames; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; -import org.apache.maven.archiva.consumers.ConsumerException; -import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; -import org.apache.maven.archiva.indexer.RepositoryIndexException; -import org.apache.maven.archiva.indexer.hashcodes.HashcodesRecord; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.apache.maven.archiva.repository.ManagedRepositoryContent; -import org.apache.maven.archiva.repository.RepositoryContentFactory; -import org.apache.maven.archiva.repository.RepositoryException; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.codehaus.plexus.registry.Registry; -import org.codehaus.plexus.registry.RegistryListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * IndexArtifactConsumer - * - * @version $Id$ - * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer" - * role-hint="index-artifact" - * instantiation-strategy="per-lookup" - */ -public class IndexArtifactConsumer - extends AbstractMonitoredConsumer - implements DatabaseUnprocessedArtifactConsumer, RegistryListener, Initializable -{ - private Logger log = LoggerFactory.getLogger( IndexArtifactConsumer.class ); - - private static final String INDEX_ERROR = "indexing_error"; - - /** - * @plexus.configuration default-value="index-artifact" - */ - private String id; - - /** - * @plexus.configuration default-value="Index the artifact checksums for Find functionality." - */ - private String description; - - /** - * @plexus.requirement - */ - private ArchivaConfiguration configuration; - - /** - * @plexus.requirement - */ - private RepositoryContentFactory repositoryFactory; - - /** - * @plexus.requirement role-hint="lucene" - */ - private RepositoryContentIndexFactory indexFactory; - - private Map<String, IndexedRepositoryDetails> repositoryMap = new HashMap<String, IndexedRepositoryDetails>(); - - public void beginScan() - { - /* nothing to do here */ - } - - public void completeScan() - { - /* nothing to do here */ - } - - public List<String> getIncludedTypes() - { - return null; // TODO: define these as a list of artifacts. - } - - public void processArchivaArtifact( ArchivaArtifact artifact ) - throws ConsumerException - { - HashcodesRecord record = new HashcodesRecord(); - record.setRepositoryId( artifact.getModel().getRepositoryId() ); - record.setArtifact( artifact ); - - IndexedRepositoryDetails pnl = getIndexedRepositoryDetails( artifact ); - - String artifactPath = pnl.repository.toPath( artifact ); - record.setFilename( artifactPath ); - - try - { - pnl.index.modifyRecord( record ); - } - catch ( RepositoryIndexException e ) - { - triggerConsumerError( INDEX_ERROR, "Unable to index hashcodes: " + e.getMessage() ); - } - } - - private IndexedRepositoryDetails getIndexedRepositoryDetails( ArchivaArtifact artifact ) - { - String repoId = artifact.getModel().getRepositoryId(); - if ( StringUtils.isBlank( repoId ) ) - { - throw new IllegalStateException( - "Unable to process artifact [" + artifact + "] as it has no repository id associated with it." ); - } - - return getIndexedRepositoryDetails( repoId ); - } - - private IndexedRepositoryDetails getIndexedRepositoryDetails( String id ) - { - return this.repositoryMap.get( id ); - } - - public String getDescription() - { - return description; - } - - public String getId() - { - return id; - } - - public boolean isPermanent() - { - return false; - } - - public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) - { - if ( ConfigurationNames.isManagedRepositories( propertyName ) ) - { - initRepositoryMap(); - } - } - - public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) - { - /* do nothing */ - } - - public void initialize() - throws InitializationException - { - initRepositoryMap(); - configuration.addChangeListener( this ); - } - - private void initRepositoryMap() - { - synchronized ( this.repositoryMap ) - { - this.repositoryMap.clear(); - - Iterator<ManagedRepositoryConfiguration> it = configuration.getConfiguration().getManagedRepositories().iterator(); - while ( it.hasNext() ) - { - ManagedRepositoryConfiguration repository = it.next(); - - try - { - IndexedRepositoryDetails pnl = new IndexedRepositoryDetails(); - - pnl.repository = repositoryFactory.getManagedRepositoryContent( repository.getId() ); - - pnl.index = indexFactory.createHashcodeIndex( repository ); - - this.repositoryMap.put( repository.getId(), pnl ); - } - catch ( RepositoryException e ) - { - log.error( "Unable to load repository content object: " + e.getMessage(), e ); - } - } - } - } - - class IndexedRepositoryDetails - { - public ManagedRepositoryContent repository; - - public RepositoryContentIndex index; - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexContentConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexContentConsumer.java deleted file mode 100644 index ea3908772..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexContentConsumer.java +++ /dev/null @@ -1,224 +0,0 @@ -package org.apache.maven.archiva.consumers.lucene; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import org.apache.commons.io.FileUtils; -import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.ConfigurationNames; -import org.apache.maven.archiva.configuration.FileTypes; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; -import org.apache.maven.archiva.consumers.ConsumerException; -import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; -import org.apache.maven.archiva.indexer.RepositoryIndexException; -import org.apache.maven.archiva.indexer.filecontent.FileContentRecord; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.apache.maven.archiva.model.ArtifactReference; -import org.apache.maven.archiva.repository.ManagedRepositoryContent; -import org.apache.maven.archiva.repository.RepositoryContentFactory; -import org.apache.maven.archiva.repository.RepositoryException; -import org.apache.maven.archiva.repository.layout.LayoutException; -import org.apache.maven.archiva.repository.metadata.MetadataTools; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; -import org.codehaus.plexus.registry.Registry; -import org.codehaus.plexus.registry.RegistryListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -/** - * IndexContentConsumer - generic full file content indexing consumer. - * - * @version $Id$ - * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer" - * role-hint="index-content" - * instantiation-strategy="per-lookup" - */ -public class IndexContentConsumer - extends AbstractMonitoredConsumer - implements KnownRepositoryContentConsumer, RegistryListener, Initializable -{ - private Logger log = LoggerFactory.getLogger( IndexContentConsumer.class ); - - private static final String READ_CONTENT = "read_content"; - - private static final String INDEX_ERROR = "indexing_error"; - - /** - * @plexus.configuration default-value="index-content" - */ - private String id; - - /** - * @plexus.configuration default-value="Text and XML file contents indexing" - */ - private String description; - - /** - * @plexus.requirement - */ - private ArchivaConfiguration configuration; - - /** - * @plexus.requirement - */ - private FileTypes filetypes; - - /** - * @plexus.requirement - */ - private RepositoryContentFactory repositoryFactory; - - /** - * @plexus.requirement role-hint="lucene" - */ - private RepositoryContentIndexFactory indexFactory; - - private List<String> includes = new ArrayList<String>(); - - private RepositoryContentIndex index; - - private ManagedRepositoryContent repository; - - private File repositoryDir; - - public String getId() - { - return this.id; - } - - public String getDescription() - { - return this.description; - } - - public boolean isPermanent() - { - return false; - } - - public List<String> getExcludes() - { - return null; - } - - public List<String> getIncludes() - { - return this.includes; - } - - public void beginScan( ManagedRepositoryConfiguration repo, Date whenGathered ) - throws ConsumerException - { - try - { - this.repository = repositoryFactory.getManagedRepositoryContent( repo.getId() ); - this.repositoryDir = new File( repository.getRepoRoot() ); - this.index = indexFactory.createFileContentIndex( repository.getRepository() ); - } - catch ( RepositoryException e ) - { - throw new ConsumerException( "Unable to start IndexContentConsumer: " + e.getMessage(), e ); - } - } - - public void processFile( String path ) - throws ConsumerException - { - if ( path.endsWith( "/" + MetadataTools.MAVEN_METADATA ) ) - { - log.debug( "File is a metadata file. Not indexing." ); - return; - } - - FileContentRecord record = new FileContentRecord(); - try - { - File file = new File( repositoryDir, path ); - record.setRepositoryId( this.repository.getId() ); - record.setFilename( path ); - record.setContents( FileUtils.readFileToString( file, null ) ); - - // Test for possible artifact reference syntax. - try - { - ArtifactReference ref = repository.toArtifactReference( path ); - ArchivaArtifact artifact = new ArchivaArtifact( ref ); - artifact.getModel().setRepositoryId( repository.getId() ); - record.setArtifact( artifact ); - } - catch ( LayoutException e ) - { - // Not an artifact. - } - - index.modifyRecord( record ); - } - catch ( IOException e ) - { - triggerConsumerError( READ_CONTENT, "Unable to read file contents: " + e.getMessage() ); - } - catch ( RepositoryIndexException e ) - { - triggerConsumerError( INDEX_ERROR, "Unable to index file contents: " + e.getMessage() ); - } - } - - public void completeScan() - { - /* do nothing */ - } - - public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue ) - { - if ( ConfigurationNames.isRepositoryScanning( propertyName ) ) - { - initIncludes(); - } - } - - public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue ) - { - /* do nothing */ - } - - private void initIncludes() - { - includes.clear(); - - includes.addAll( filetypes.getFileTypePatterns( FileTypes.INDEXABLE_CONTENT ) ); - } - - public void initialize() - throws InitializationException - { - configuration.addChangeListener( this ); - - initIncludes(); - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumer.java deleted file mode 100644 index 67d228f73..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumer.java +++ /dev/null @@ -1,268 +0,0 @@ -package org.apache.maven.archiva.consumers.lucene; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer; -import org.apache.maven.archiva.consumers.ConsumerException; -import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; -import org.apache.maven.archiva.indexer.RepositoryIndexException; -import org.apache.maven.archiva.indexer.bytecode.BytecodeRecord; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.apache.maven.archiva.repository.ManagedRepositoryContent; -import org.apache.maven.archiva.repository.RepositoryContentFactory; -import org.apache.maven.archiva.repository.RepositoryException; - -import com.sun.org.apache.bcel.internal.classfile.ClassParser; -import com.sun.org.apache.bcel.internal.classfile.JavaClass; -import com.sun.org.apache.bcel.internal.classfile.Method; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -/** - * IndexJavaPublicMethodsConsumer - * - * <a href="mailto:oching@apache.org">Maria Odea Ching</a> - * @version $Id$ - * - * @plexus.component role="org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer" - * role-hint="index-public-methods" - * instantiation-strategy="per-lookup" - */ -public class IndexJavaPublicMethodsConsumer - extends AbstractMonitoredConsumer - implements DatabaseUnprocessedArtifactConsumer -{ - /** - * @plexus.configuration default-value="index-public-methods" - */ - private String id; - - /** - * @plexus.configuration default-value="Index the java public methods for Full Text Search." - */ - private String description; - - /** - * @plexus.requirement role-hint="lucene" - */ - private RepositoryContentIndexFactory repoIndexFactory; - - /** - * @plexus.requirement - */ - private RepositoryContentFactory repoFactory; - - private static final String CLASSES = "classes"; - - private static final String METHODS = "methods"; - - private List<String> includes = new ArrayList<String>(); - - public IndexJavaPublicMethodsConsumer() - { - includes.add( "jar" ); - includes.add( "war" ); - includes.add( "ear" ); - includes.add( "zip" ); - includes.add( "tar.gz" ); - includes.add( "tar.bz2" ); - includes.add( "car" ); - includes.add( "sar" ); - includes.add( "mar" ); - includes.add( "rar" ); - } - - public void beginScan() - { - // TODO Auto-generated method stubx - } - - public void completeScan() - { - // TODO Auto-generated method stub - - } - - public List<String> getIncludedTypes() - { - return includes; - } - - public void processArchivaArtifact( ArchivaArtifact artifact ) - throws ConsumerException - { - try - { - ManagedRepositoryContent repoContent = - repoFactory.getManagedRepositoryContent( artifact.getModel().getRepositoryId() ); - File file = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) ); - - if( file.getAbsolutePath().endsWith( ".jar" ) || file.getAbsolutePath().endsWith( ".war" ) || - file.getAbsolutePath().endsWith( ".ear" ) || file.getAbsolutePath().endsWith( ".zip" ) || - file.getAbsolutePath().endsWith( ".tar.gz" ) || file.getAbsolutePath().endsWith( ".tar.bz2" ) || - file.getAbsolutePath().endsWith( ".car" ) || file.getAbsolutePath().endsWith( ".sar" ) || - file.getAbsolutePath().endsWith( ".mar" ) || file.getAbsolutePath().endsWith( ".rar" ) ) - { - if( file.exists() ) - { - List<String> files = readFilesInArchive( file ); - Map<String, List<String>> mapOfClassesAndMethods = - getPublicClassesAndMethodsFromFiles( file.getAbsolutePath(), files ); - - // NOTE: what about public variables? should these be indexed too? - RepositoryContentIndex bytecodeIndex = repoIndexFactory.createBytecodeIndex( repoContent.getRepository() ); - - artifact.getModel().setRepositoryId( repoContent.getId() ); - - BytecodeRecord bytecodeRecord = new BytecodeRecord(); - bytecodeRecord.setFilename( file.getName() ); - bytecodeRecord.setClasses( mapOfClassesAndMethods.get( CLASSES ) ); - bytecodeRecord.setFiles( files ); - bytecodeRecord.setMethods( mapOfClassesAndMethods.get( METHODS ) ); - bytecodeRecord.setArtifact( artifact ); - bytecodeRecord.setRepositoryId( repoContent.getId() ); - bytecodeIndex.modifyRecord( bytecodeRecord ); - } - } - } - catch ( RepositoryException e ) - { - throw new ConsumerException( "Can't run index cleanup consumer: " + e.getMessage() ); - } - catch ( RepositoryIndexException e ) - { - throw new ConsumerException( "Error encountered while adding artifact to index: " + e.getMessage() ); - } - catch ( IOException e ) - { - throw new ConsumerException( "Error encountered while getting file contents: " + e.getMessage() ); - } - } - - public String getDescription() - { - return description; - } - - public String getId() - { - return id; - } - - public boolean isPermanent() - { - return false; - } - - private List<String> readFilesInArchive( File file ) - throws IOException - { - ZipFile zipFile = new ZipFile( file ); - List<String> files; - - try - { - files = new ArrayList<String>( zipFile.size() ); - for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) - { - ZipEntry entry = (ZipEntry) entries.nextElement(); - files.add( entry.getName() ); - } - } - finally - { - closeQuietly( zipFile ); - } - return files; - } - - private void closeQuietly( ZipFile zipFile ) - { - try - { - if ( zipFile != null ) - { - zipFile.close(); - } - } - catch ( IOException e ) - { - // ignored - } - } - - private static boolean isClass( String name ) - { - return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0; - } - - private Map<String, List<String>> getPublicClassesAndMethodsFromFiles( String zipFile, List<String> files ) - { - Map<String, List<String>> map = new HashMap<String, List<String>>(); - List<String> methods = new ArrayList<String>(); - List<String> classes = new ArrayList<String>(); - - for( String file : files ) - { - if( isClass( file ) ) - { - try - { - ClassParser parser = new ClassParser( zipFile, file ); - JavaClass javaClass = parser.parse(); - - if( javaClass.isPublic() ) - { - classes.add( javaClass.getClassName() ); - } - - Method[] methodsArr = javaClass.getMethods(); - for( Method method : methodsArr ) - { - if( method.isPublic() ) - { - methods.add( method.getName() ); - } - } - } - catch ( IOException e ) - { - // ignore - } - } - } - - map.put( CLASSES, classes ); - map.put( METHODS, methods ); - - return map; - } - -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/resources/META-INF/spring-context.xml new file mode 100644 index 000000000..4e16b7f7d --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/main/resources/META-INF/spring-context.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> + + <bean id="indexerConsumer" class="org.apache.archiva.consumers.lucene.NexusIndexerConsumer" lazy-init="true"> + <constructor-arg ref="archivaTaskScheduler"/> + <constructor-arg ref="archivaConfiguration"/> + <constructor-arg ref="fileTypes"/> + </bean> + + <bean id="indexCleanupConsumer" class="org.apache.archiva.consumers.lucene.LuceneCleanupRemoveIndexedConsumer" lazy-init="true"> + <constructor-arg ref="repositoryContentFactory"/> + <constructor-arg ref="archivaTaskScheduler"/> + </bean> +</beans>
\ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumerTest.java new file mode 100644 index 000000000..1eb7237f9 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/LuceneCleanupRemoveIndexedConsumerTest.java @@ -0,0 +1,148 @@ +package org.apache.archiva.consumers.lucene; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.List; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.repository.ManagedRepositoryContent; +import org.apache.maven.archiva.repository.RepositoryContentFactory; +import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent; +import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; +import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; +import org.apache.maven.archiva.scheduled.tasks.TaskCreator; +import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.codehaus.plexus.taskqueue.TaskQueue; +import org.easymock.MockControl; +import org.easymock.classextension.MockClassControl; + +/** + * LuceneCleanupRemoveIndexedConsumerTest + */ +public class LuceneCleanupRemoveIndexedConsumerTest + extends PlexusInSpringTestCase +{ + private LuceneCleanupRemoveIndexedConsumer consumer; + + private RepositoryContentFactory repoFactory; + + private MockControl repoFactoryControl; + + private ManagedRepositoryConfiguration repositoryConfig; + + private TaskQueue indexingQueue; + + public void setUp() + throws Exception + { + super.setUp(); + + repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class ); + repoFactory = (RepositoryContentFactory) repoFactoryControl.getMock(); + + ArchivaTaskScheduler scheduler = (ArchivaTaskScheduler) lookup( ArchivaTaskScheduler.class ); + + indexingQueue = (TaskQueue) lookup( TaskQueue.ROLE, "indexing" ); + + consumer = new LuceneCleanupRemoveIndexedConsumer( repoFactory, scheduler ); + + repositoryConfig = new ManagedRepositoryConfiguration(); + repositoryConfig.setId( "test-repo" ); + repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" ); + repositoryConfig.setLayout( "default" ); + repositoryConfig.setName( "Test Repository" ); + repositoryConfig.setScanned( true ); + repositoryConfig.setSnapshots( false ); + repositoryConfig.setReleases( true ); + repositoryConfig.setIndexDir( getBasedir() + "/target/test-classes/test-repo/.cleanup-index" ); + } + + public void tearDown() + throws Exception + { + FileUtils.deleteDirectory( new File( repositoryConfig.getIndexDir() ) ); + + super.tearDown(); + } + + @SuppressWarnings( "unchecked" ) + public void testProcessArtifactArtifactDoesNotExist() + throws Exception + { + assertTrue( indexingQueue.getQueueSnapshot().isEmpty() ); + + ArchivaArtifact artifact = + new ArchivaArtifact( "org.apache.archiva", "archiva-lucene-consumers", "1.2", null, "jar", "test-repo" ); + + ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(); + repoContent.setRepository( repositoryConfig ); + + File artifactFile = new File( repoContent.getRepoRoot(), repoContent.toPath( artifact ) ); + + repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( repositoryConfig.getId() ), + repoContent ); + + repoFactoryControl.replay(); + + consumer.processArchivaArtifact( artifact ); + + repoFactoryControl.verify(); + + List<ArtifactIndexingTask> queue = indexingQueue.getQueueSnapshot(); + assertEquals( 2, queue.size() ); + ArtifactIndexingTask task = + TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE, null ); + assertEquals( task, queue.get( 0 ) ); + task = + TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, null ); + assertEquals( task, queue.get( 1 ) ); + } + + public void testProcessArtifactArtifactExists() + throws Exception + { + assertTrue( indexingQueue.getQueueSnapshot().isEmpty() ); + + ArchivaArtifact artifact = + new ArchivaArtifact( "org.apache.maven.archiva", "archiva-lucene-cleanup", "1.0", null, "jar", "test-repo" ); + ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent(); + repoContent.setRepository( repositoryConfig ); + + repoFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( repositoryConfig.getId() ), + repoContent ); + + repoFactoryControl.replay(); + + consumer.processArchivaArtifact( artifact ); + + repoFactoryControl.verify(); + + assertTrue( indexingQueue.getQueueSnapshot().isEmpty() ); + } + + @Override + protected String getPlexusConfigLocation() + { + return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml"; + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java new file mode 100644 index 000000000..0da5b2bf1 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/NexusIndexerConsumerTest.java @@ -0,0 +1,227 @@ +package org.apache.archiva.consumers.lucene; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.util.Calendar; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import org.apache.commons.io.FileUtils; +import org.apache.maven.archiva.common.ArchivaException; +import org.apache.maven.archiva.configuration.ArchivaConfiguration; +import org.apache.maven.archiva.configuration.FileTypes; +import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; +import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask; +import org.apache.maven.archiva.scheduled.tasks.DatabaseTask; +import org.apache.maven.archiva.scheduled.tasks.RepositoryTask; +import org.codehaus.plexus.spring.PlexusInSpringTestCase; +import org.codehaus.plexus.taskqueue.TaskQueueException; +import org.codehaus.plexus.taskqueue.execution.TaskExecutionException; + +/** + * NexusIndexerConsumerTest + */ +public class NexusIndexerConsumerTest + extends PlexusInSpringTestCase +{ + private final class ArchivaTaskSchedulerStub + implements ArchivaTaskScheduler + { + Set<File> indexed = new HashSet<File>(); + + public void startup() + throws ArchivaException + { + } + + public void scheduleDatabaseTasks() + throws TaskExecutionException + { + } + + public void queueRepositoryTask( RepositoryTask task ) + throws TaskQueueException + { + } + + public void queueIndexingTask( ArtifactIndexingTask task ) + throws TaskQueueException + { + switch ( task.getAction() ) + { + case ADD: + indexed.add( task.getResourceFile() ); + break; + case DELETE: + indexed.remove( task.getResourceFile() ); + break; + case FINISH: + try + { + task.getContext().close( false ); + } + catch ( IOException e ) + { + throw new TaskQueueException( e.getMessage() ); + } + break; + } + } + + public void queueDatabaseTask( DatabaseTask task ) + throws TaskQueueException + { + } + + public boolean isProcessingRepositoryTask( String repositoryId ) + { + return false; + } + + public boolean isProcessingDatabaseTask() + { + return false; + } + } + + private KnownRepositoryContentConsumer nexusIndexerConsumer; + + private ManagedRepositoryConfiguration repositoryConfig; + + private ArchivaTaskSchedulerStub scheduler; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + + scheduler = new ArchivaTaskSchedulerStub(); + + ArchivaConfiguration configuration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class ); + + FileTypes filetypes = (FileTypes) lookup( FileTypes.class ); + + nexusIndexerConsumer = new NexusIndexerConsumer( scheduler, configuration, filetypes ); + + repositoryConfig = new ManagedRepositoryConfiguration(); + repositoryConfig.setId( "test-repo" ); + repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" ); + repositoryConfig.setLayout( "default" ); + repositoryConfig.setName( "Test Repository" ); + repositoryConfig.setScanned( true ); + repositoryConfig.setSnapshots( false ); + repositoryConfig.setReleases( true ); + } + + @Override + protected void tearDown() + throws Exception + { + // delete created index in the repository + File indexDir = new File( repositoryConfig.getLocation(), ".indexer" ); + FileUtils.deleteDirectory( indexDir ); + assertFalse( indexDir.exists() ); + + indexDir = new File( repositoryConfig.getLocation(), ".index" ); + FileUtils.deleteDirectory( indexDir ); + assertFalse( indexDir.exists() ); + + super.tearDown(); + } + + public void testIndexerIndexArtifact() + throws Exception + { + File artifactFile = + new File( repositoryConfig.getLocation(), + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + // begin scan + Date now = Calendar.getInstance().getTime(); + nexusIndexerConsumer.beginScan( repositoryConfig, now ); + nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + nexusIndexerConsumer.completeScan(); + + assertTrue( scheduler.indexed.contains( artifactFile ) ); + } + + public void testIndexerArtifactAlreadyIndexed() + throws Exception + { + File artifactFile = + new File( repositoryConfig.getLocation(), + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + // begin scan + Date now = Calendar.getInstance().getTime(); + nexusIndexerConsumer.beginScan( repositoryConfig, now ); + nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + nexusIndexerConsumer.completeScan(); + + assertTrue( scheduler.indexed.contains( artifactFile ) ); + + // scan and index again + now = Calendar.getInstance().getTime(); + nexusIndexerConsumer.beginScan( repositoryConfig, now ); + nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + nexusIndexerConsumer.completeScan(); + + assertTrue( scheduler.indexed.contains( artifactFile ) ); + } + + public void testIndexerIndexArtifactThenPom() + throws Exception + { + File artifactFile = + new File( repositoryConfig.getLocation(), + "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + + // begin scan + Date now = Calendar.getInstance().getTime(); + nexusIndexerConsumer.beginScan( repositoryConfig, now ); + nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ); + nexusIndexerConsumer.completeScan(); + + assertTrue( scheduler.indexed.contains( artifactFile ) ); + + artifactFile = + new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" ); + + // scan and index again + now = Calendar.getInstance().getTime(); + nexusIndexerConsumer.beginScan( repositoryConfig, now ); + nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" ); + nexusIndexerConsumer.completeScan(); + + assertTrue( scheduler.indexed.contains( artifactFile ) ); + } + + @Override + protected String getPlexusConfigLocation() + { + return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml"; + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/stubs/ArchivaDAOStub.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/stubs/ArchivaDAOStub.java new file mode 100644 index 000000000..47ebdce62 --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/archiva/consumers/lucene/stubs/ArchivaDAOStub.java @@ -0,0 +1,74 @@ +package org.apache.archiva.consumers.lucene.stubs; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.Serializable; +import java.util.List; + +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArtifactDAO; +import org.apache.maven.archiva.database.ProjectModelDAO; +import org.apache.maven.archiva.database.RepositoryContentStatisticsDAO; +import org.apache.maven.archiva.database.RepositoryProblemDAO; +import org.apache.maven.archiva.database.SimpleConstraint; + +/** + * Using a stub for faster tests! Not really used for the unit tests, just for dependency injection. + */ +public class ArchivaDAOStub + implements ArchivaDAO +{ + + public ArtifactDAO getArtifactDAO() + { + // TODO Auto-generated method stub + return null; + } + + public ProjectModelDAO getProjectModelDAO() + { + // TODO Auto-generated method stub + return null; + } + + public RepositoryContentStatisticsDAO getRepositoryContentStatisticsDAO() + { + // TODO Auto-generated method stub + return null; + } + + public RepositoryProblemDAO getRepositoryProblemDAO() + { + // TODO Auto-generated method stub + return null; + } + + public List<?> query( SimpleConstraint constraint ) + { + // TODO Auto-generated method stub + return null; + } + + public Object save( Serializable obj ) + { + // TODO Auto-generated method stub + return null; + } +} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.java deleted file mode 100644 index 90e8bf1ca..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.java +++ /dev/null @@ -1,123 +0,0 @@ -package org.apache.maven.archiva.consumers.lucene; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.ArrayList; -import java.util.List; - -import org.apache.maven.archiva.configuration.ArchivaConfiguration; -import org.apache.maven.archiva.configuration.Configuration; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; -import org.apache.maven.archiva.indexer.search.SearchResultLimits; -import org.apache.maven.archiva.indexer.search.SearchResults; -import org.apache.maven.archiva.model.ArchivaArtifact; -import org.apache.maven.archiva.model.ArchivaArtifactModel; -import org.codehaus.plexus.spring.PlexusInSpringTestCase; - -/** - * - * @version - * - */ -public class IndexJavaPublicMethodsConsumerTest - extends PlexusInSpringTestCase -{ - DatabaseUnprocessedArtifactConsumer indexMethodsConsumer; - - IndexJavaPublicMethodsCrossRepositorySearch searcher; - - private RepositoryContentIndexFactory indexFactory; - - public void setUp() - throws Exception - { - super.setUp(); - indexMethodsConsumer = - (DatabaseUnprocessedArtifactConsumer) lookup( DatabaseUnprocessedArtifactConsumer.class, - "index-public-methods" ); - - ManagedRepositoryConfiguration config = new ManagedRepositoryConfiguration(); - config.setId( "test-repo" ); - config.setLayout( "default" ); - config.setLocation( getBasedir() + "/target/test-classes/test-repo" ); - config.setName( "Test Repository" ); - - addRepoToConfiguration( "index-public-methods", config ); - - indexFactory = (RepositoryContentIndexFactory) lookup (RepositoryContentIndexFactory.class, "lucene" ); - searcher = new IndexJavaPublicMethodsCrossRepositorySearch( config, indexFactory ); - } - - private void addRepoToConfiguration( String configHint, ManagedRepositoryConfiguration repoConfiguration ) - throws Exception - { - ArchivaConfiguration archivaConfiguration = - (ArchivaConfiguration) lookup( ArchivaConfiguration.class, configHint ); - Configuration configuration = archivaConfiguration.getConfiguration(); - configuration.removeManagedRepository( configuration.findManagedRepositoryById( repoConfiguration.getId() ) ); - configuration.addManagedRepository( repoConfiguration ); - } - - public void testJarPublicMethods() - throws Exception - { - ArchivaArtifact artifact = - createArtifact( "org.apache.archiva", "archiva-index-methods-jar-test", "1.0", "jar" ); - indexMethodsConsumer.processArchivaArtifact( artifact ); - - List<String> selectedRepos = new ArrayList<String>(); - selectedRepos.add( "test-repo" ); - - // search for class names - SearchResults results = searcher.searchForBytecode( "", selectedRepos, "FirstPackageApp", new SearchResultLimits( 0 ) ); - assertEquals( 1, results.getTotalHits() ); - - results = searcher.searchForBytecode( "", selectedRepos, "SecondPackageApp", new SearchResultLimits( 0 ) ); - assertEquals( 1, results.getTotalHits() ); - - // search for public methods - results = searcher.searchForBytecode( "", selectedRepos, "appMethodOne", new SearchResultLimits( 0 ) ); - assertEquals( 1, results.getTotalHits() ); - - // should return only the overridding public method in SecondPackageApp - results = searcher.searchForBytecode( "", selectedRepos, "protectedMethod", new SearchResultLimits( 0 ) ); - assertEquals( 1, results.getTotalHits() ); - - // should not return any private methods - results = searcher.searchForBytecode( "", selectedRepos, "privMethod", new SearchResultLimits( 0 ) ); - assertEquals( 0, results.getTotalHits() ); - - // test for public variables? - } - - private ArchivaArtifact createArtifact( String groupId, String artifactId, String version, String type ) - { - ArchivaArtifactModel model = new ArchivaArtifactModel(); - model.setGroupId( groupId ); - model.setArtifactId( artifactId ); - model.setVersion( version ); - model.setType( type ); - model.setRepositoryId( "test-repo" ); - - return new ArchivaArtifact( model ); - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsCrossRepositorySearch.java b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsCrossRepositorySearch.java deleted file mode 100644 index 2005dc3fb..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/java/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsCrossRepositorySearch.java +++ /dev/null @@ -1,190 +0,0 @@ -package org.apache.maven.archiva.consumers.lucene; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.lucene.document.Document; -import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.queryParser.QueryParser; -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.MultiSearcher; -import org.apache.lucene.search.Searchable; -import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; -import org.apache.maven.archiva.indexer.RepositoryContentIndex; -import org.apache.maven.archiva.indexer.RepositoryContentIndexFactory; -import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexer.bytecode.BytecodeHandlers; -import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter; -import org.apache.maven.archiva.indexer.lucene.LuceneQuery; -import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord; -import org.apache.maven.archiva.indexer.search.SearchResultLimits; -import org.apache.maven.archiva.indexer.search.SearchResults; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Searcher used for testing purposes only. - * - * @version - */ -public class IndexJavaPublicMethodsCrossRepositorySearch -{ - private Logger log = LoggerFactory.getLogger( IndexJavaPublicMethodsCrossRepositorySearch.class ); - - private ManagedRepositoryConfiguration localIndexedRepo; - - private RepositoryContentIndexFactory indexFactory; - - public IndexJavaPublicMethodsCrossRepositorySearch( ManagedRepositoryConfiguration localIndexedRepo, RepositoryContentIndexFactory indexFactory ) - { - this.localIndexedRepo = localIndexedRepo; - this.indexFactory = indexFactory; - } - - public SearchResults searchForBytecode( String principal, List<String> selectedRepos, String term, - SearchResultLimits limits ) throws ParseException - { - List<RepositoryContentIndex> indexes = new ArrayList<RepositoryContentIndex>(); - indexes.add( indexFactory.createBytecodeIndex( localIndexedRepo ) ); - - QueryParser parser = new BytecodeHandlers().getQueryParser(); - LuceneQuery query = new LuceneQuery( parser.parse( term ) ); - SearchResults results = searchAll( query, limits, indexes ); - results.getRepositories().add( localIndexedRepo ); - - return results; - } - - private SearchResults searchAll( LuceneQuery luceneQuery, SearchResultLimits limits, List<RepositoryContentIndex> indexes ) - { - org.apache.lucene.search.Query specificQuery = luceneQuery.getLuceneQuery(); - - SearchResults results = new SearchResults(); - - if ( indexes.isEmpty() ) - { - // No point going any further. - return results; - } - - // Setup the converter - LuceneEntryConverter converter = null; - RepositoryContentIndex index = indexes.get( 0 ); - converter = index.getEntryConverter(); - - // Process indexes into an array of Searchables. - List<Searchable> searchableList = toSearchables( indexes ); - - Searchable searchables[] = new Searchable[searchableList.size()]; - searchableList.toArray( searchables ); - - MultiSearcher searcher = null; - - try - { - // Create a multi-searcher for looking up the information. - searcher = new MultiSearcher( searchables ); - - // Perform the search. - Hits hits = searcher.search( specificQuery ); - - int hitCount = hits.length(); - - // Now process the limits. - results.setLimits( limits ); - results.setTotalHits( hitCount ); - - int fetchCount = limits.getPageSize(); - int offset = ( limits.getSelectedPage() * limits.getPageSize() ); - - if ( limits.getSelectedPage() == SearchResultLimits.ALL_PAGES ) - { - fetchCount = hitCount; - offset = 0; - } - - // Goto offset. - if ( offset < hitCount ) - { - // only process if the offset is within the hit count. - for ( int i = 0; i <= fetchCount; i++ ) - { - // Stop fetching if we are past the total # of available hits. - if ( offset + i >= hitCount ) - { - break; - } - - try - { - Document doc = hits.doc( offset + i ); - LuceneRepositoryContentRecord record = converter.convert( doc ); - results.addHit( record ); - } - catch ( java.text.ParseException e ) - { - log.error( e.getMessage() ); - } - } - } - - } - catch ( IOException e ) - { - log.error( e.getMessage() ); - } - finally - { - try - { - if ( searcher != null ) - { - searcher.close(); - } - } - catch ( IOException ie ) - { - log.error( ie.getMessage() ); - } - } - - return results; - } - - private List<Searchable> toSearchables( List<RepositoryContentIndex> indexes ) - { - List<Searchable> searchableList = new ArrayList<Searchable>(); - for ( RepositoryContentIndex contentIndex : indexes ) - { - try - { - searchableList.add( contentIndex.getSearchable() ); - } - catch ( RepositoryIndexSearchException e ) - { - log.error( e.getMessage() ); - } - } - return searchableList; - } -} diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml new file mode 100644 index 000000000..4b3f8e25b --- /dev/null +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml @@ -0,0 +1,28 @@ +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> + +<component-set> + <components> + <component> + <role>org.apache.maven.archiva.database.ArchivaDAO</role> + <role-hint>jdo</role-hint> + <implementation>org.apache.archiva.consumers.lucene.stubs.ArchivaDAOStub</implementation> + </component> + </components> +</component-set>
\ No newline at end of file diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.xml b/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.xml deleted file mode 100644 index f596d11cc..000000000 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-lucene-consumers/src/test/resources/org/apache/maven/archiva/consumers/lucene/IndexJavaPublicMethodsConsumerTest.xml +++ /dev/null @@ -1,64 +0,0 @@ -<component-set> - <components> -<!-- for testing repo purge using retention count --> - <component> - <role>org.apache.maven.archiva.database.updater.DatabaseUnprocessedArtifactConsumer</role> - <role-hint>index-public-methods</role-hint> - <implementation>org.apache.maven.archiva.consumers.lucene.IndexJavaPublicMethodsConsumer</implementation> - <requirements> - <requirement> - <role>org.apache.maven.archiva.repository.RepositoryContentFactory</role> - <role-hint>index-public-methods</role-hint> - </requirement> - <requirement> - <role>org.apache.maven.archiva.indexer.RepositoryContentIndexFactory</role> - <role-hint>lucene</role-hint> - <field-name>repoIndexFactory</field-name> - </requirement> - </requirements> - </component> - <component> - <role>org.apache.maven.archiva.repository.RepositoryContentFactory</role> - <role-hint>index-public-methods</role-hint> - <implementation>org.apache.maven.archiva.repository.RepositoryContentFactory</implementation> - <description>RepositoryContentRequest</description> - <requirements> - <requirement> - <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role> - <role-hint>index-public-methods</role-hint> - <field-name>archivaConfiguration</field-name> - </requirement> - </requirements> - </component> - <component> - <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role> - <role-hint>index-public-methods</role-hint> - <implementation>org.apache.maven.archiva.configuration.DefaultArchivaConfiguration</implementation> - <requirements> - <requirement> - <role>org.codehaus.plexus.registry.Registry</role> - <role-hint>index-public-methods</role-hint> - </requirement> - <requirement> - <role>org.apache.maven.archiva.policies.PreDownloadPolicy</role> - <field-name>prePolicies</field-name> - </requirement> - <requirement> - <role>org.apache.maven.archiva.policies.PostDownloadPolicy</role> - <field-name>postPolicies</field-name> - </requirement> - </requirements> - </component> - <component> - <role>org.codehaus.plexus.registry.Registry</role> - <role-hint>index-public-methods</role-hint> - <implementation>org.codehaus.plexus.registry.commons.CommonsConfigurationRegistry</implementation> - <configuration> - <properties> - <xml fileName="${basedir}/src/test/conf/repository-manager.xml" - config-name="org.apache.maven.archiva" config-at="org.apache.maven.archiva"/> - </properties> - </configuration> - </component> - </components> -</component-set> |