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.RepositoryConfiguration;
import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
import org.apache.maven.archiva.consumers.ConsumerException;
import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArchivaDatabaseException;
+import org.apache.maven.archiva.database.ObjectNotFoundException;
import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaProjectModel;
import org.apache.maven.archiva.model.RepositoryURL;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
import org.apache.maven.archiva.repository.layout.LayoutException;
import org.apache.maven.archiva.repository.project.ProjectModelException;
+import org.apache.maven.archiva.repository.project.ProjectModelFilter;
import org.apache.maven.archiva.repository.project.ProjectModelReader;
+import org.apache.maven.archiva.repository.project.ProjectModelResolver;
+import org.apache.maven.archiva.repository.project.filters.EffectiveProjectModelFilter;
+import org.apache.maven.archiva.repository.project.resolvers.RepositoryProjectModelResolverFactory;
+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 java.io.File;
import java.util.ArrayList;
-import java.util.Date;
+import java.util.Iterator;
import java.util.List;
/**
*/
public class ProjectModelToDatabaseConsumer
extends AbstractMonitoredConsumer
- implements DatabaseUnprocessedArtifactConsumer
+ implements DatabaseUnprocessedArtifactConsumer, RegistryListener, Initializable
{
/**
* @plexus.configuration default-value="update-db-project"
*/
private ProjectModelReader project300Reader;
+ /**
+ * @plexus.requirement role-hint="expression"
+ */
+ private ProjectModelFilter expressionModelFilter;
+
+ /**
+ * @plexus.requirement
+ */
+ private RepositoryProjectModelResolverFactory resolverFactory;
+
+ /**
+ * @plexus.requirement role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
+ * role-hint="effective"
+ */
+ private EffectiveProjectModelFilter effectiveModelFilter;
+
+ /**
+ * @plexus.requirement role-hint="database"
+ */
+ private ProjectModelResolver databaseResolver;
+
private List includes;
public ProjectModelToDatabaseConsumer()
public void beginScan()
{
- // TODO Auto-generated method stub
-
+ /* nothing to do here */
}
public void completeScan()
{
- // TODO Auto-generated method stub
-
+ /* nothing to do here */
}
public List getIncludedTypes()
{
if ( !StringUtils.equals( "pom", artifact.getType() ) )
{
+ // Not a pom. Skip it.
+ return;
+ }
+
+ if ( hasProjectModelInDatabase( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ) )
+ {
+ // Already in the database. Skip it.
return;
}
File artifactFile = toFile( artifact );
RepositoryConfiguration repo = getRepository( artifact );
+ ProjectModelReader reader = project400Reader;
- if ( StringUtils.equals( "default", repo.getLayout() ) )
+ if ( StringUtils.equals( "legacy", repo.getLayout() ) )
{
- try
- {
- ArchivaProjectModel model = project400Reader.read( artifactFile );
-
- model.setOrigin( "filesystem" );
-
- dao.getProjectModelDAO().saveProjectModel( model );
- }
- catch ( ProjectModelException e )
- {
- getLogger().warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
- }
- catch ( ArchivaDatabaseException e )
- {
- getLogger().warn(
- "Unable to save project model " + artifactFile + " to the database : "
- + e.getMessage(), e );
- }
+ reader = project300Reader;
+ }
+
+ try
+ {
+ ArchivaProjectModel model = reader.read( artifactFile );
+
+ model.setOrigin( "filesystem" );
+
+ // Filter the model
+ model = expressionModelFilter.filter( model );
+
+ // Resolve the project model
+ model = effectiveModelFilter.filter( model );
+
+ dao.getProjectModelDAO().saveProjectModel( model );
+ }
+ catch ( ProjectModelException e )
+ {
+ getLogger().warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
+ }
+ catch ( ArchivaDatabaseException e )
+ {
+ getLogger().warn( "Unable to save project model " + artifactFile + " to the database : " + e.getMessage(),
+ e );
+ }
+ }
+
+ private boolean hasProjectModelInDatabase( String groupId, String artifactId, String version )
+ {
+ try
+ {
+ ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version );
+ return ( model != null );
+ }
+ catch ( ObjectNotFoundException e )
+ {
+ return false;
+ }
+ catch ( ArchivaDatabaseException e )
+ {
+ return false;
}
}
return true;
}
+ public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ /* nothing to do here */
+ }
+
+ public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ if ( ConfigurationNames.isRepositories( propertyName ) )
+ {
+ update();
+ }
+ }
+
+ public void initialize()
+ throws InitializationException
+ {
+ update();
+ archivaConfiguration.addChangeListener( this );
+ }
+
+ private void update()
+ {
+ synchronized ( effectiveModelFilter )
+ {
+ effectiveModelFilter.clearResolvers();
+
+ // Add the database resolver first!
+ effectiveModelFilter.addProjectModelResolver( databaseResolver );
+
+ List ret = this.resolverFactory.getAllResolvers();
+ Iterator it = ret.iterator();
+ while ( it.hasNext() )
+ {
+ ProjectModelResolver resolver = (ProjectModelResolver) it.next();
+ effectiveModelFilter.addProjectModelResolver( resolver );
+ }
+ }
+ }
}
--- /dev/null
+package org.apache.maven.archiva.consumers.database.project;
+
+/*
+ * 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.database.ArchivaDAO;
+import org.apache.maven.archiva.database.ArchivaDatabaseException;
+import org.apache.maven.archiva.database.ObjectNotFoundException;
+import org.apache.maven.archiva.model.ArchivaProjectModel;
+import org.apache.maven.archiva.model.VersionedReference;
+import org.apache.maven.archiva.repository.project.ProjectModelException;
+import org.apache.maven.archiva.repository.project.ProjectModelResolver;
+
+/**
+ * Resolves a project model from the database.
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ *
+ * @plexus.component role="org.apache.maven.archiva.repository.project.ProjectModelResolver"
+ * role-hint="database"
+ */
+public class DatabaseProjectModelResolver
+ implements ProjectModelResolver
+{
+ /**
+ * @plexus.requirement role-hint="jdo"
+ */
+ private ArchivaDAO dao;
+
+ public ArchivaProjectModel resolveProjectModel( VersionedReference reference )
+ throws ProjectModelException
+ {
+ try
+ {
+ ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( reference.getGroupId(),
+ reference.getArtifactId(),
+ reference.getVersion() );
+ return model;
+ }
+ catch ( ObjectNotFoundException e )
+ {
+ return null;
+ }
+ catch ( ArchivaDatabaseException e )
+ {
+ return null;
+ }
+ }
+}
import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.archiva.configuration.ArchivaConfiguration;
import org.apache.maven.archiva.configuration.ConfigurationNames;
+import org.apache.maven.archiva.configuration.RepositoryConfiguration;
import org.apache.maven.archiva.configuration.functors.LocalRepositoryPredicate;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
private List allManagedRepositories = new ArrayList();
+ /**
+ * Get the {@link List} of {@link RepositoryConfiguration} objects representing managed repositories.
+ *
+ * @return the {@link List} of {@link RepositoryConfiguration} objects.
+ */
public List getAllManagedRepositories()
{
synchronized ( allManagedRepositories )
allManagedRepositories.clear();
List configRepos = archivaConfiguration.getConfiguration().getRepositories();
- CollectionUtils.select( configRepos, LocalRepositoryPredicate.getInstance() );
+ CollectionUtils.filter( configRepos, LocalRepositoryPredicate.getInstance() );
}
}
}
import org.apache.maven.archiva.repository.project.ProjectModelFilter;
import org.apache.maven.archiva.repository.project.ProjectModelMerge;
import org.apache.maven.archiva.repository.project.ProjectModelResolver;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
import java.util.ArrayList;
import java.util.HashMap;
* @version $Id$
* @plexus.component role="org.apache.maven.archiva.repository.project.ProjectModelFilter"
* role-hint="effective"
- * instantiation-strategy="per-lookup"
*/
-public class EffectiveProjectModelFilter implements ProjectModelFilter
+public class EffectiveProjectModelFilter
+ extends AbstractLogEnabled
+ implements ProjectModelFilter
{
/**
* @plexus.requirement role-hint="expression"
*/
private ProjectModelFilter expressionFilter;
-
+
private List projectModelResolvers;
-
+
public EffectiveProjectModelFilter()
{
projectModelResolvers = new ArrayList();
// Setup Expression Evaluation pieces.
effectiveProject = expressionFilter.filter( effectiveProject );
- debug( "Starting build of effective with: " + effectiveProject );
+ getLogger().debug( "Starting build of effective with: " + effectiveProject );
// Merge in all the parent poms.
effectiveProject = mergeParent( effectiveProject );
this.projectModelResolvers.remove( resolver );
}
+ public void clearResolvers()
+ {
+ this.projectModelResolvers.clear();
+ }
+
private void applyDependencyManagement( ArchivaProjectModel pom )
{
if ( ( pom.getDependencyManagement() == null ) || ( pom.getDependencies() == null ) )
// Nothing to do. All done!
return;
}
-
+
if ( pom.getDependencyManagement().isEmpty() || pom.getDependencies().isEmpty() )
{
// Nothing to do. All done!
}
}
- private void debug( String msg )
- {
- System.out.println( "## " + msg );
- }
-
private ArchivaProjectModel findProject( VersionedReference projectRef )
{
- debug( "Trying to find project: " + projectRef );
+ getLogger().debug( "Trying to find project: " + projectRef );
Iterator it = this.projectModelResolvers.iterator();
while ( it.hasNext() )
try
{
- debug( "Trying to find in " + resolver.getClass().getName() );
+ getLogger().debug( "Trying to find in " + resolver.getClass().getName() );
ArchivaProjectModel model = resolver.resolveProjectModel( projectRef );
if ( model != null )
{
- debug( "Found it!: " + model );
+ getLogger().debug( "Found it!: " + model );
return model;
}
- debug( "Not found." );
+ getLogger().debug( "Not found." );
}
catch ( ProjectModelException e )
{
{
ArchivaProjectModel mixedProject;
- debug( "Parent: " + pom.getParentProject() );
+ getLogger().debug( "Parent: " + pom.getParentProject() );
if ( pom.getParentProject() != null )
{
// Use parent reference.
VersionedReference parentRef = pom.getParentProject();
- debug( "Has parent: " + parentRef );
+ getLogger().debug( "Has parent: " + parentRef );
// Find parent using resolvers.
ArchivaProjectModel parentProject = findProject( parentRef );
}
else
{
- debug( "No parent found" );
+ getLogger().debug( "No parent found" );
/* Mix in the super-pom.
*
private ArchivaProjectModel mixinSuperPom( ArchivaProjectModel pom )
{
// TODO: add super pom repositories.
- debug( "Mix in Super POM: " + pom );
+ getLogger().debug( "Mix in Super POM: " + pom );
return pom;
}
--- /dev/null
+package org.apache.maven.archiva.repository.project.resolvers;
+
+/*
+ * 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.collections.CollectionUtils;
+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.RepositoryConfiguration;
+import org.apache.maven.archiva.configuration.functors.LocalRepositoryPredicate;
+import org.apache.maven.archiva.model.ArchivaRepository;
+import org.apache.maven.archiva.repository.ArchivaConfigurationAdaptor;
+import org.apache.maven.archiva.repository.RepositoryException;
+import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
+import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
+import org.apache.maven.archiva.repository.layout.LayoutException;
+import org.apache.maven.archiva.repository.project.ProjectModelReader;
+import org.apache.maven.archiva.repository.project.ProjectModelResolver;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+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 java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Factory for ProjectModelResolver objects
+ *
+ * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
+ * @version $Id$
+ *
+ * @plexus.component role="org.apache.maven.archiva.repository.project.resolvers.RepositoryProjectModelResolverFactory"
+ */
+public class RepositoryProjectModelResolverFactory
+ extends AbstractLogEnabled
+ implements RegistryListener, Initializable
+{
+ /**
+ * @plexus.requirement
+ */
+ private ArchivaConfiguration archivaConfiguration;
+
+ /**
+ * @plexus.requirement
+ */
+ private BidirectionalRepositoryLayoutFactory layoutFactory;
+
+ /**
+ * @plexus.requirement role-hint="model400"
+ */
+ private ProjectModelReader project400Reader;
+
+ /**
+ * @plexus.requirement role-hint="model300"
+ */
+ private ProjectModelReader project300Reader;
+
+ /**
+ * Get the {@link ProjectModelResolver} for the specific archiva repository.
+ *
+ * @param repo the repository to base resolver on.
+ * @return return the resolver for the archiva repository provided.
+ * @throws RepositoryException if unable to create a resolver for the provided {@link ArchivaRepository}
+ */
+ public ProjectModelResolver getResolver( ArchivaRepository repo )
+ throws RepositoryException
+ {
+ if ( resolverMap.containsKey( repo.getId() ) )
+ {
+ return (ProjectModelResolver) this.resolverMap.get( repo.getId() );
+ }
+
+ ProjectModelResolver resolver = toResolver( repo );
+ resolverMap.put( repo.getId(), resolver );
+
+ return resolver;
+ }
+
+ /**
+ * Get the {@link ProjectModelResolver} for the specific archiva repository based on repository id.
+ *
+ * @param repoid the repository id to get the resolver for.
+ * @return the {@link ProjectModelResolver} if found, or null if repository is not found.
+ */
+ public ProjectModelResolver getResolver( String repoid )
+ {
+ return (ProjectModelResolver) this.resolverMap.get( repoid );
+ }
+
+ /**
+ * Get the {@link List} of {@link ProjectModelResolver} for
+ * the {@link List} of {@link ArchivaRepository} objects provided.
+ *
+ * @param repositoryList the {@link List} of {@link ArchivaRepository} objects to
+ * get {@link ProjectModelResolver} for.
+ * @return the {@link List} of {@link ProjectModelResolver} objects.
+ * @throws RepositoryException if unable to convert any of the provided {@link ArchivaRepository} objects into
+ * a {@link ProjectModelResolver} object.
+ */
+ public List getResolverList( List repositoryList )
+ throws RepositoryException
+ {
+ List ret = new ArrayList();
+
+ if ( CollectionUtils.isEmpty( repositoryList ) )
+ {
+ return ret;
+ }
+
+ Iterator it = repositoryList.iterator();
+ while ( it.hasNext() )
+ {
+ ArchivaRepository repo = (ArchivaRepository) it.next();
+ ret.add( getResolver( repo ) );
+ }
+
+ return ret;
+ }
+
+ /**
+ * Get the entire {@link List} of {@link ProjectModelResolver} that the factory is tracking.
+ *
+ * @return the entire list of {@link ProjectModelResolver} that is being tracked.
+ */
+ public List getAllResolvers()
+ {
+ List ret = new ArrayList();
+
+ ret.addAll( this.resolverMap.values() );
+
+ return ret;
+ }
+
+ public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ /* do nothing */
+ }
+
+ public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
+ {
+ if ( ConfigurationNames.isRepositories( propertyName ) )
+ {
+ update();
+ }
+ }
+
+ private Map resolverMap = new HashMap();
+
+ private void update()
+ {
+ synchronized ( resolverMap )
+ {
+ resolverMap.clear();
+
+ List configRepos = archivaConfiguration.getConfiguration().getRepositories();
+ CollectionUtils.filter( configRepos, LocalRepositoryPredicate.getInstance() );
+
+ Iterator it = configRepos.iterator();
+ while ( it.hasNext() )
+ {
+ RepositoryConfiguration repoconfig = (RepositoryConfiguration) it.next();
+ ArchivaRepository repo = ArchivaConfigurationAdaptor.toArchivaRepository( repoconfig );
+ try
+ {
+ RepositoryProjectResolver resolver = toResolver( repo );
+ resolverMap.put( repo.getId(), resolver );
+ }
+ catch ( RepositoryException e )
+ {
+ getLogger().warn( e.getMessage(), e );
+ }
+ }
+ }
+ }
+
+ private RepositoryProjectResolver toResolver( ArchivaRepository repo )
+ throws RepositoryException
+ {
+ if ( !repo.isManaged() )
+ {
+ throw new RepositoryException( "Unable to create RepositoryProjectResolver from non-managed repository: "
+ + repo );
+ }
+
+ try
+ {
+ BidirectionalRepositoryLayout layout = layoutFactory.getLayout( repo.getLayoutType() );
+ ProjectModelReader reader = project400Reader;
+
+ if ( StringUtils.equals( "legacy", repo.getLayoutType() ) )
+ {
+ reader = project300Reader;
+ }
+
+ RepositoryProjectResolver resolver = new RepositoryProjectResolver( repo, reader, layout );
+ return resolver;
+ }
+ catch ( LayoutException e )
+ {
+ throw new RepositoryException( "Unable to create RepositoryProjectResolver due to invalid layout spec: "
+ + repo );
+ }
+ }
+
+ public void initialize()
+ throws InitializationException
+ {
+ update();
+ archivaConfiguration.addChangeListener( this );
+ }
+}
import org.apache.maven.archiva.model.ArchivaRepository;
import org.apache.maven.archiva.model.VersionedReference;
import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
-import org.apache.maven.archiva.repository.layout.DefaultBidirectionalRepositoryLayout;
import org.apache.maven.archiva.repository.project.ProjectModelException;
import org.apache.maven.archiva.repository.project.ProjectModelReader;
import org.apache.maven.archiva.repository.project.ProjectModelResolver;
-import org.apache.maven.archiva.repository.project.readers.ProjectModel400Reader;
import java.io.File;
/**
- * RepositoryProjectResolver
+ * Resolve Project from filesystem.
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
private BidirectionalRepositoryLayout layout;
- public RepositoryProjectResolver( ArchivaRepository repository )
+ public RepositoryProjectResolver( ArchivaRepository repository, ProjectModelReader reader, BidirectionalRepositoryLayout layout )
{
this.repository = repository;
- this.reader = new ProjectModel400Reader();
- this.layout = new DefaultBidirectionalRepositoryLayout();
+ this.reader = reader;
+ this.layout = layout;
}
public ArchivaProjectModel resolveProjectModel( VersionedReference reference )
import org.apache.maven.archiva.model.ArchivaRepository;
import org.apache.maven.archiva.model.Dependency;
import org.apache.maven.archiva.model.Individual;
+import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
+import org.apache.maven.archiva.repository.layout.DefaultBidirectionalRepositoryLayout;
import org.apache.maven.archiva.repository.project.ProjectModelException;
import org.apache.maven.archiva.repository.project.ProjectModelFilter;
import org.apache.maven.archiva.repository.project.ProjectModelReader;
ArchivaRepository repo = new ArchivaRepository( "defaultTestRepo", "Default Test Repo", "file://"
+ defaultRepoDir.getAbsolutePath() );
- RepositoryProjectResolver resolver = new RepositoryProjectResolver( repo );
+ ProjectModelReader reader = new ProjectModel400Reader();
+ BidirectionalRepositoryLayout layout = new DefaultBidirectionalRepositoryLayout();
+ RepositoryProjectResolver resolver = new RepositoryProjectResolver( repo, reader, layout );
return resolver;
}
assertContainsSameIndividuals( "Individuals", expectedModel.getIndividuals(), effectiveModel.getIndividuals() );
dumpDependencyList( "Expected", expectedModel.getDependencies() );
dumpDependencyList( "Effective", effectiveModel.getDependencies() );
- assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(),
- effectiveModel.getDependencies() );
- assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(),
- effectiveModel.getDependencyManagement() );
+ assertContainsSameDependencies( "Dependencies", expectedModel.getDependencies(), effectiveModel
+ .getDependencies() );
+ assertContainsSameDependencies( "DependencyManagement", expectedModel.getDependencyManagement(), effectiveModel
+ .getDependencyManagement() );
}
private void dumpDependencyList( String type, List deps )