1 package org.apache.maven.archiva.consumers.database;
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 org.apache.commons.lang.StringUtils;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
25 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
26 import org.apache.maven.archiva.consumers.ConsumerException;
27 import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer;
28 import org.apache.maven.archiva.database.ArchivaDAO;
29 import org.apache.maven.archiva.database.ArchivaDatabaseException;
30 import org.apache.maven.archiva.model.ArchivaArtifact;
31 import org.apache.maven.archiva.model.ArchivaProjectModel;
32 import org.apache.maven.archiva.model.RepositoryURL;
33 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayout;
34 import org.apache.maven.archiva.repository.layout.BidirectionalRepositoryLayoutFactory;
35 import org.apache.maven.archiva.repository.layout.LayoutException;
36 import org.apache.maven.archiva.repository.project.ProjectModelException;
37 import org.apache.maven.archiva.repository.project.ProjectModelReader;
40 import java.util.ArrayList;
41 import java.util.Date;
42 import java.util.List;
45 * ProjectModelToDatabaseConsumer
47 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
50 * @plexus.component role="org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer"
51 * role-hint="update-db-project"
52 * instantiation-strategy="per-lookup"
54 public class ProjectModelToDatabaseConsumer
55 extends AbstractMonitoredConsumer
56 implements DatabaseUnprocessedArtifactConsumer
59 * @plexus.configuration default-value="update-db-project"
64 * @plexus.configuration default-value="Update database with project model information."
66 private String description;
69 * @plexus.requirement role-hint="jdo"
71 private ArchivaDAO dao;
76 private ArchivaConfiguration archivaConfiguration;
81 private BidirectionalRepositoryLayoutFactory layoutFactory;
84 * @plexus.requirement role-hint="model400"
86 private ProjectModelReader project400Reader;
89 * @plexus.requirement role-hint="model300"
91 private ProjectModelReader project300Reader;
93 private List includes;
95 public ProjectModelToDatabaseConsumer()
97 includes = new ArrayList();
98 includes.add( "pom" );
101 public void beginScan()
103 // TODO Auto-generated method stub
107 public void completeScan()
109 // TODO Auto-generated method stub
113 public List getIncludedTypes()
118 public void processArchivaArtifact( ArchivaArtifact artifact )
119 throws ConsumerException
121 if ( !StringUtils.equals( "pom", artifact.getType() ) )
126 File artifactFile = toFile( artifact );
127 RepositoryConfiguration repo = getRepository( artifact );
129 if ( StringUtils.equals( "default", repo.getLayout() ) )
133 ArchivaProjectModel model = project400Reader.read( artifactFile );
135 model.setOrigin( "filesystem" );
137 dao.getProjectModelDAO().saveProjectModel( model );
139 catch ( ProjectModelException e )
141 getLogger().warn( "Unable to read project model " + artifactFile + " : " + e.getMessage(), e );
143 catch ( ArchivaDatabaseException e )
146 "Unable to save project model " + artifactFile + " to the database : "
147 + e.getMessage(), e );
152 private RepositoryConfiguration getRepository( ArchivaArtifact artifact )
154 String repoId = artifact.getModel().getRepositoryId();
155 return archivaConfiguration.getConfiguration().findRepositoryById( repoId );
158 private File toFile( ArchivaArtifact artifact )
160 RepositoryConfiguration repoConfig = getRepository( artifact );
162 BidirectionalRepositoryLayout layout = null;
166 layout = layoutFactory.getLayout( artifact );
168 catch ( LayoutException e )
170 getLogger().warn( "Unable to determine layout of " + artifact + ": " + e.getMessage(), e );
174 String path = layout.toPath( artifact );
175 RepositoryURL url = new RepositoryURL( repoConfig.getUrl() );
176 return new File( url.getPath(), path );
179 public String getDescription()
184 public String getId()
189 public boolean isPermanent()