1 package org.apache.archiva.metadata.repository.file;
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
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.List;
31 import java.util.Properties;
33 import org.apache.archiva.metadata.model.ArtifactMetadata;
34 import org.apache.archiva.metadata.model.CiManagement;
35 import org.apache.archiva.metadata.model.IssueManagement;
36 import org.apache.archiva.metadata.model.License;
37 import org.apache.archiva.metadata.model.Organization;
38 import org.apache.archiva.metadata.model.ProjectMetadata;
39 import org.apache.archiva.metadata.model.ProjectVersionFacet;
40 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
41 import org.apache.archiva.metadata.model.Scm;
42 import org.apache.archiva.metadata.repository.MetadataRepository;
43 import org.apache.commons.io.IOUtils;
46 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
48 public class FileMetadataRepository
49 implements MetadataRepository
52 * TODO: this isn't suitable for production use
54 * @plexus.configuration
56 private File directory = new File( System.getProperty( "user.home" ), ".archiva-metadata" );
58 public void updateProject( String repoId, ProjectMetadata project )
60 // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
63 File projectDirectory =
64 new File( this.directory, repoId + "/" + project.getNamespace() + "/" + project.getId() );
65 Properties properties = new Properties();
66 properties.setProperty( "namespace", project.getNamespace() );
67 properties.setProperty( "id", project.getId() );
68 writeProperties( properties, projectDirectory );
70 catch ( IOException e )
77 public void updateProjectVersion( String repoId, String namespace, String projectId,
78 ProjectVersionMetadata versionMetadata )
80 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId );
82 Properties properties = new Properties();
83 properties.setProperty( "id", versionMetadata.getId() );
84 setProperty( properties, "name", versionMetadata.getName() );
85 setProperty( properties, "description", versionMetadata.getDescription() );
86 setProperty( properties, "url", versionMetadata.getUrl() );
87 if ( versionMetadata.getScm() != null )
89 setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
90 setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
91 setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
93 if ( versionMetadata.getCiManagement() != null )
95 setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
96 setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
98 if ( versionMetadata.getIssueManagement() != null )
100 setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
101 setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
103 if ( versionMetadata.getOrganization() != null )
105 setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
106 setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
109 for ( License license : versionMetadata.getLicenses() )
111 setProperty( properties, "license." + i + ".name", license.getName() );
112 setProperty( properties, "license." + i + ".url", license.getUrl() );
115 properties.setProperty( "facetIds", join( versionMetadata.getAllFacetIds() ) );
116 for ( ProjectVersionFacet facet : versionMetadata.getAllFacets() )
118 properties.putAll( facet.toProperties() );
123 writeProperties( properties, new File( directory, versionMetadata.getId() ) );
125 catch ( IOException e )
128 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
132 private String join( Collection<String> ids )
134 StringBuilder s = new StringBuilder();
135 for ( String id : ids )
140 return s.substring( 0, s.length() - 1 );
143 private void setProperty( Properties properties, String name, String value )
147 properties.setProperty( name, value );
151 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
152 ArtifactMetadata artifact )
154 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
156 Properties properties = readProperties( directory );
158 properties.setProperty( "updated:" + artifact.getId(), Long.toString( artifact.getUpdated().getTime() ) );
159 properties.setProperty( "size:" + artifact.getId(), Long.toString( artifact.getSize() ) );
160 properties.setProperty( "version:" + artifact.getId(), artifact.getVersion() );
164 writeProperties( properties, directory );
166 catch ( IOException e )
169 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
173 private Properties readProperties( File directory )
175 Properties properties = new Properties();
176 FileInputStream in = null;
179 in = new FileInputStream( new File( directory, "metadata.properties" ) );
180 properties.load( in );
182 catch ( FileNotFoundException e )
184 // skip - use blank properties
186 catch ( IOException e )
189 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
193 IOUtils.closeQuietly( in );
198 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
200 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId );
202 Properties properties = readProperties( directory );
204 ProjectMetadata project = new ProjectMetadata();
205 project.setNamespace( properties.getProperty( "namespace" ) );
206 project.setId( properties.getProperty( "id" ) );
210 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
211 String projectVersion )
213 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
215 Properties properties = readProperties( directory );
216 String id = properties.getProperty( "id" );
217 ProjectVersionMetadata versionMetadata = null;
220 versionMetadata = new ProjectVersionMetadata();
221 versionMetadata.setId( id );
222 versionMetadata.setName( properties.getProperty( "name" ) );
223 versionMetadata.setDescription( properties.getProperty( "description" ) );
224 versionMetadata.setUrl( properties.getProperty( "url" ) );
226 String scmConnection = properties.getProperty( "scm.connection" );
227 String scmDeveloperConnection = properties.getProperty( "scm.developerConnection" );
228 String scmUrl = properties.getProperty( "scm.url" );
229 if ( scmConnection != null || scmDeveloperConnection != null || scmUrl != null )
232 scm.setConnection( scmConnection );
233 scm.setDeveloperConnection( scmDeveloperConnection );
234 scm.setUrl( scmUrl );
235 versionMetadata.setScm( scm );
238 String ciSystem = properties.getProperty( "ci.system" );
239 String ciUrl = properties.getProperty( "ci.url" );
240 if ( ciSystem != null || ciUrl != null )
242 CiManagement ci = new CiManagement();
243 ci.setSystem( ciSystem );
245 versionMetadata.setCiManagement( ci );
248 String issueSystem = properties.getProperty( "issue.system" );
249 String issueUrl = properties.getProperty( "issue.url" );
250 if ( issueSystem != null || issueUrl != null )
252 IssueManagement issueManagement = new IssueManagement();
253 issueManagement.setSystem( ciSystem );
254 issueManagement.setUrl( ciUrl );
255 versionMetadata.setIssueManagement( issueManagement );
258 String orgName = properties.getProperty( "org.name" );
259 String orgUrl = properties.getProperty( "org.url" );
260 if ( orgName != null || orgUrl != null )
262 Organization org = new Organization();
263 org.setName( orgName );
264 org.setUrl( orgUrl );
265 versionMetadata.setOrganization( org );
268 boolean done = false;
272 String licenseName = properties.getProperty( "license." + i + ".name" );
273 String licenseUrl = properties.getProperty( "license." + i + ".url" );
274 if ( licenseName != null || licenseUrl != null )
276 License license = new License();
277 license.setName( licenseName );
278 license.setUrl( licenseUrl );
279 versionMetadata.addLicense( license );
288 for ( String facetId : properties.getProperty( "facetIds" ).split( "," ) )
290 // TODO: we need a factory for the facets here
291 // call fromProperties( properties )
292 // versionMetadata.addFacet( );
295 for ( ProjectVersionFacet facet : versionMetadata.getAllFacets() )
297 properties.putAll( facet.toProperties() );
300 return versionMetadata;
303 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
304 String projectVersion )
306 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
308 Properties properties = readProperties( directory );
310 List<String> versions = new ArrayList<String>();
311 for ( Map.Entry entry : properties.entrySet() )
313 String name = (String) entry.getKey();
314 if ( name.startsWith( "version:" ) )
316 versions.add( (String) entry.getValue() );
322 private void writeProperties( Properties properties, File directory )
326 FileOutputStream os = new FileOutputStream( new File( directory, "metadata.properties" ) );
329 properties.store( os, null );
333 IOUtils.closeQuietly( os );