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.ProjectMetadata;
35 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
36 import org.apache.archiva.metadata.repository.MetadataRepository;
37 import org.apache.commons.io.IOUtils;
40 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
42 public class FileMetadataRepository
43 implements MetadataRepository
46 * TODO: this isn't suitable for production use
48 * @plexus.configuration
50 private File directory = new File( System.getProperty( "user.home" ), ".archiva-metadata" );
52 public void updateProject( String repoId, ProjectMetadata project )
54 // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
57 File projectDirectory =
58 new File( this.directory, repoId + "/" + project.getNamespace() + "/" + project.getId() );
59 Properties properties = new Properties();
60 properties.setProperty( "namespace", project.getNamespace() );
61 properties.setProperty( "id", project.getId() );
62 writeProperties( properties, projectDirectory );
64 catch ( IOException e )
71 public void updateProjectVersion( String repoId, String namespace, String projectId,
72 ProjectVersionMetadata versionMetadata )
74 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId );
76 Properties properties = new Properties();
77 properties.setProperty( "id", versionMetadata.getId() );
81 writeProperties( properties, new File( directory, versionMetadata.getId() ) );
83 catch ( IOException e )
86 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
90 public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
91 ArtifactMetadata artifact )
93 File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
95 Properties properties = readProperties( directory );
97 properties.setProperty( "updated:" + artifact.getId(), Long.toString( artifact.getUpdated().getTime() ) );
98 properties.setProperty( "size:" + artifact.getId(), Long.toString( artifact.getSize() ) );
99 properties.setProperty( "version:" + artifact.getId(), artifact.getVersion() );
103 writeProperties( properties, directory );
105 catch ( IOException e )
108 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
112 private Properties readProperties( File directory )
114 Properties properties = new Properties();
115 FileInputStream in = null;
118 in = new FileInputStream( new File( directory, "metadata.xml" ) );
119 properties.load( in );
121 catch ( FileNotFoundException e )
123 // skip - use blank properties
125 catch ( IOException e )
128 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
132 IOUtils.closeQuietly( in );
137 public ProjectMetadata getProject( String repoId, String groupId, String projectId )
139 File directory = new File( this.directory, repoId + "/" + projectId );
141 Properties properties = readProperties( directory );
143 ProjectMetadata project = new ProjectMetadata();
144 project.setNamespace( properties.getProperty( "namespace" ) );
145 project.setId( properties.getProperty( "id" ) );
149 public ProjectVersionMetadata getProjectVersion( String repoId, String groupId, String projectId,
150 String projectVersion )
152 File directory = new File( this.directory, repoId + "/" + projectId + "/" + projectVersion );
154 Properties properties = readProperties( directory );
156 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
157 versionMetadata.setId( properties.getProperty( "id" ) );
158 return versionMetadata;
161 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
162 String projectVersion )
164 File directory = new File( this.directory, repoId + "/" + projectId + "/" + projectVersion );
166 Properties properties = readProperties( directory );
168 List<String> versions = new ArrayList<String>();
169 for ( Map.Entry entry : properties.entrySet() )
171 String name = (String) entry.getKey();
172 if ( name.startsWith( "version:" ) )
174 versions.add( (String) entry.getValue() );
180 private void writeProperties( Properties properties, File directory )
184 FileOutputStream os = new FileOutputStream( new File( directory, "metadata.xml" ) );
187 properties.storeToXML( os, null );
191 IOUtils.closeQuietly( os );