This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.reports;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.metadata.model.ProjectVersionMetadata;
023import org.apache.archiva.metadata.model.facets.RepositoryProblemFacet;
024import org.apache.archiva.metadata.repository.MetadataRepository;
025import org.apache.archiva.metadata.repository.MetadataRepositoryException;
026import org.apache.archiva.metadata.repository.RepositorySession;
027import org.apache.archiva.metadata.repository.RepositorySessionFactory;
028import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataException;
029import org.apache.archiva.metadata.audit.RepositoryListener;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032import org.springframework.stereotype.Service;
033
034import javax.inject.Inject;
035
036/**
037 * Process repository management events and respond appropriately.
038 *
039 */
040@Service( "repositoryListener#problem-reports" )
041public class RepositoryProblemEventListener
042    implements RepositoryListener
043{
044    private Logger log = LoggerFactory.getLogger( RepositoryProblemEventListener.class );
045
046    @Inject
047    private RepositorySessionFactory repositorySessionFactory;
048
049    // FIXME: move to session
050    @Override
051    public void deleteArtifact( MetadataRepository metadataRepository, String repositoryId, String namespace,
052                                String project, String version, String id )
053    {
054        String name = RepositoryProblemFacet.createName( namespace, project, version, id );
055
056        try(RepositorySession session = repositorySessionFactory.createSession())
057        {
058            metadataRepository.removeMetadataFacet(session , repositoryId, RepositoryProblemFacet.FACET_ID, name );
059        }
060        catch ( MetadataRepositoryException e )
061        {
062            log.warn( "Unable to remove metadata facet as part of delete event: {}", e.getMessage(), e );
063        }
064    }
065
066    @Override
067    public void addArtifact( RepositorySession session, String repoId, String namespace, String projectId,
068                             ProjectVersionMetadata metadata )
069    {
070        // Remove problems associated with this version on successful addition
071        // TODO: this removes all problems - do we need something that just remove the problems we know are corrected?
072        String name = RepositoryProblemFacet.createName( namespace, projectId, metadata.getId(), null );
073        try
074        {
075            MetadataRepository metadataRepository = session.getRepository();
076            metadataRepository.removeMetadataFacet(session , repoId, RepositoryProblemFacet.FACET_ID, name );
077            session.markDirty();
078        }
079        catch ( MetadataRepositoryException e )
080        {
081            log.warn( "Unable to remove repository problem facets for the version being corrected in the repository: {}",
082                          e.getMessage(), e );
083        }
084    }
085
086    @Override
087    public void addArtifactProblem( RepositorySession session, String repoId, String namespace, String projectId,
088                                    String projectVersion, RepositoryStorageMetadataException exception )
089    {
090        RepositoryProblemFacet problem = new RepositoryProblemFacet();
091        problem.setMessage( exception.getMessage() );
092        problem.setProject( projectId );
093        problem.setNamespace( namespace );
094        problem.setRepositoryId( repoId );
095        problem.setVersion( projectVersion );
096        problem.setProblem( exception.getId() );
097
098        try
099        {
100            session.getRepository().addMetadataFacet(session , repoId, problem );
101            session.markDirty();
102        }
103        catch ( MetadataRepositoryException e )
104        {
105            log.warn( "Unable to add repository problem facets for the version being removed: {}", e.getMessage(), e );
106        }
107    }
108
109}