1 package org.apache.maven.archiva.database.project;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.util.List;
24 import org.apache.maven.archiva.database.ArchivaDAO;
25 import org.apache.maven.archiva.database.ArchivaDatabaseException;
26 import org.apache.maven.archiva.database.ObjectNotFoundException;
27 import org.apache.maven.archiva.model.ArchivaModelCloner;
28 import org.apache.maven.archiva.model.ArchivaProjectModel;
29 import org.apache.maven.archiva.model.VersionedReference;
30 import org.apache.maven.archiva.repository.project.ProjectModelException;
31 import org.apache.maven.archiva.repository.project.ProjectModelResolver;
32 import org.apache.maven.archiva.repository.project.resolvers.FilesystemBasedResolver;
33 import org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolutionListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Just in Time save of project models to the database, implemented as a listener
39 * on {@link ProjectModelResolver} objects that implement {@link FilesystemBasedResolver}.
44 * role="org.apache.maven.archiva.repository.project.resolvers.ProjectModelResolutionListener"
45 * role-hint="model-to-db"
47 public class ProjectModelToDatabaseListener
48 implements ProjectModelResolutionListener
50 private Logger log = LoggerFactory.getLogger( ProjectModelToDatabaseListener.class );
53 * @plexus.requirement role-hint="jdo"
55 private ArchivaDAO dao;
57 private void saveInDatabase( ArchivaProjectModel model )
58 throws ProjectModelException
62 dao.getProjectModelDAO().saveProjectModel( model );
64 catch ( ArchivaDatabaseException e )
66 throw new ProjectModelException( "Unable to save model to database: " + e.getMessage(), e );
70 private void removeFromDatabase( ArchivaProjectModel model )
71 throws ProjectModelException
75 dao.getProjectModelDAO().deleteProjectModel( model );
77 catch ( ArchivaDatabaseException e )
79 throw new ProjectModelException( "Unable to remove existing model from database: " + e.getMessage(), e );
83 private boolean existsInDatabase( ArchivaProjectModel model )
84 throws ProjectModelException
88 ArchivaProjectModel dbmodel = dao.getProjectModelDAO().getProjectModel( model.getGroupId(),
89 model.getArtifactId(),
92 return ( dbmodel != null );
94 catch ( ObjectNotFoundException e )
98 catch ( ArchivaDatabaseException e )
100 throw new ProjectModelException( "Unable to check for existing model from database: " + e.getMessage(), e );
104 public void resolutionAttempting( VersionedReference projectRef, ProjectModelResolver resolver )
109 public void resolutionError( VersionedReference projectRef, ProjectModelResolver resolver, Exception cause )
114 public void resolutionMiss( VersionedReference projectRef, ProjectModelResolver resolver )
119 public void resolutionNotFound( VersionedReference projectRef, List resolverList )
124 public void resolutionStart( VersionedReference projectRef, List resolverList )
129 public void resolutionSuccess( VersionedReference projectRef, ProjectModelResolver resolver,
130 ArchivaProjectModel model )
132 if ( !( resolver instanceof FilesystemBasedResolver ) )
134 // Nothing to do. skip it.
138 // Clone model, since DAO while detachingCopy resets contents of the model
139 // this changes behaviour of EffectiveProjectModelFilter
140 model = ArchivaModelCloner.clone( model );
144 // Test if it exists.
145 if ( existsInDatabase( model ) )
147 removeFromDatabase( model );
150 saveInDatabase( model );
152 catch ( ProjectModelException e )
154 log.warn( e.getMessage(), e );