]> source.dussan.org Git - archiva.git/blob
0c9b3ca48767f23fb480bc4e8b433845bf19c0b5
[archiva.git] /
1 package org.apache.archiva.metadata.repository.file;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
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;
30 import java.util.Map;
31 import java.util.Properties;
32
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;
44
45 /**
46  * @plexus.component role="org.apache.archiva.metadata.repository.MetadataRepository"
47  */
48 public class FileMetadataRepository
49     implements MetadataRepository
50 {
51     /**
52      * TODO: this isn't suitable for production use
53      *
54      * @plexus.configuration
55      */
56     private File directory = new File( System.getProperty( "user.home" ), ".archiva-metadata" );
57
58     public void updateProject( String repoId, ProjectMetadata project )
59     {
60         // TODO: this is a more braindead implementation than we would normally expect, for prototyping purposes
61         try
62         {
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 );
69         }
70         catch ( IOException e )
71         {
72             // TODO!
73             e.printStackTrace();
74         }
75     }
76
77     public void updateProjectVersion( String repoId, String namespace, String projectId,
78                                       ProjectVersionMetadata versionMetadata )
79     {
80         File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId );
81
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 )
88         {
89             setProperty( properties, "scm.connection", versionMetadata.getScm().getConnection() );
90             setProperty( properties, "scm.developerConnection", versionMetadata.getScm().getDeveloperConnection() );
91             setProperty( properties, "scm.url", versionMetadata.getScm().getUrl() );
92         }
93         if ( versionMetadata.getCiManagement() != null )
94         {
95             setProperty( properties, "ci.system", versionMetadata.getCiManagement().getSystem() );
96             setProperty( properties, "ci.url", versionMetadata.getCiManagement().getUrl() );
97         }
98         if ( versionMetadata.getIssueManagement() != null )
99         {
100             setProperty( properties, "issue.system", versionMetadata.getIssueManagement().getSystem() );
101             setProperty( properties, "issue.url", versionMetadata.getIssueManagement().getUrl() );
102         }
103         if ( versionMetadata.getOrganization() != null )
104         {
105             setProperty( properties, "org.name", versionMetadata.getOrganization().getName() );
106             setProperty( properties, "org.url", versionMetadata.getOrganization().getUrl() );
107         }
108         int i = 0;
109         for ( License license : versionMetadata.getLicenses() )
110         {
111             setProperty( properties, "license." + i + ".name", license.getName() );
112             setProperty( properties, "license." + i + ".url", license.getUrl() );
113             i++;
114         }
115         properties.setProperty( "facetIds", join( versionMetadata.getAllFacetIds() ) );
116         for ( ProjectVersionFacet facet : versionMetadata.getAllFacets() )
117         {
118             properties.putAll( facet.toProperties() );
119         }
120
121         try
122         {
123             writeProperties( properties, new File( directory, versionMetadata.getId() ) );
124         }
125         catch ( IOException e )
126         {
127             // TODO
128             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
129         }
130     }
131
132     private String join( Collection<String> ids )
133     {
134         StringBuilder s = new StringBuilder();
135         for ( String id : ids )
136         {
137             s.append( id );
138             s.append( "," );
139         }
140         return s.substring( 0, s.length() - 1 );
141     }
142
143     private void setProperty( Properties properties, String name, String value )
144     {
145         if ( value != null )
146         {
147             properties.setProperty( name, value );
148         }
149     }
150
151     public void updateArtifact( String repoId, String namespace, String projectId, String projectVersion,
152                                 ArtifactMetadata artifact )
153     {
154         File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
155
156         Properties properties = readProperties( directory );
157
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() );
161
162         try
163         {
164             writeProperties( properties, directory );
165         }
166         catch ( IOException e )
167         {
168             // TODO
169             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
170         }
171     }
172
173     private Properties readProperties( File directory )
174     {
175         Properties properties = new Properties();
176         FileInputStream in = null;
177         try
178         {
179             in = new FileInputStream( new File( directory, "metadata.properties" ) );
180             properties.load( in );
181         }
182         catch ( FileNotFoundException e )
183         {
184             // skip - use blank properties
185         }
186         catch ( IOException e )
187         {
188             // TODO
189             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
190         }
191         finally
192         {
193             IOUtils.closeQuietly( in );
194         }
195         return properties;
196     }
197
198     public ProjectMetadata getProject( String repoId, String namespace, String projectId )
199     {
200         File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId );
201
202         Properties properties = readProperties( directory );
203
204         ProjectMetadata project = new ProjectMetadata();
205         project.setNamespace( properties.getProperty( "namespace" ) );
206         project.setId( properties.getProperty( "id" ) );
207         return project;
208     }
209
210     public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
211                                                      String projectVersion )
212     {
213         File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
214
215         Properties properties = readProperties( directory );
216         String id = properties.getProperty( "id" );
217         ProjectVersionMetadata versionMetadata = null;
218         if ( id != null )
219         {
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" ) );
225
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 )
230             {
231                 Scm scm = new Scm();
232                 scm.setConnection( scmConnection );
233                 scm.setDeveloperConnection( scmDeveloperConnection );
234                 scm.setUrl( scmUrl );
235                 versionMetadata.setScm( scm );
236             }
237
238             String ciSystem = properties.getProperty( "ci.system" );
239             String ciUrl = properties.getProperty( "ci.url" );
240             if ( ciSystem != null || ciUrl != null )
241             {
242                 CiManagement ci = new CiManagement();
243                 ci.setSystem( ciSystem );
244                 ci.setUrl( ciUrl );
245                 versionMetadata.setCiManagement( ci );
246             }
247
248             String issueSystem = properties.getProperty( "issue.system" );
249             String issueUrl = properties.getProperty( "issue.url" );
250             if ( issueSystem != null || issueUrl != null )
251             {
252                 IssueManagement issueManagement = new IssueManagement();
253                 issueManagement.setSystem( ciSystem );
254                 issueManagement.setUrl( ciUrl );
255                 versionMetadata.setIssueManagement( issueManagement );
256             }
257
258             String orgName = properties.getProperty( "org.name" );
259             String orgUrl = properties.getProperty( "org.url" );
260             if ( orgName != null || orgUrl != null )
261             {
262                 Organization org = new Organization();
263                 org.setName( orgName );
264                 org.setUrl( orgUrl );
265                 versionMetadata.setOrganization( org );
266             }
267
268             boolean done = false;
269             int i = 0;
270             while ( !done )
271             {
272                 String licenseName = properties.getProperty( "license." + i + ".name" );
273                 String licenseUrl = properties.getProperty( "license." + i + ".url" );
274                 if ( licenseName != null || licenseUrl != null )
275                 {
276                     License license = new License();
277                     license.setName( licenseName );
278                     license.setUrl( licenseUrl );
279                     versionMetadata.addLicense( license );
280                 }
281                 else
282                 {
283                     done = true;
284                 }
285                 i++;
286             }
287
288             for ( String facetId : properties.getProperty( "facetIds" ).split( "," ) )
289             {
290                 // TODO: we need a factory for the facets here
291                 // call fromProperties( properties )
292 //                versionMetadata.addFacet(  );
293             }
294
295             for ( ProjectVersionFacet facet : versionMetadata.getAllFacets() )
296             {
297                 properties.putAll( facet.toProperties() );
298             }
299         }
300         return versionMetadata;
301     }
302
303     public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
304                                                    String projectVersion )
305     {
306         File directory = new File( this.directory, repoId + "/" + namespace + "/" + projectId + "/" + projectVersion );
307
308         Properties properties = readProperties( directory );
309
310         List<String> versions = new ArrayList<String>();
311         for ( Map.Entry entry : properties.entrySet() )
312         {
313             String name = (String) entry.getKey();
314             if ( name.startsWith( "version:" ) )
315             {
316                 versions.add( (String) entry.getValue() );
317             }
318         }
319         return versions;
320     }
321
322     private void writeProperties( Properties properties, File directory )
323         throws IOException
324     {
325         directory.mkdirs();
326         FileOutputStream os = new FileOutputStream( new File( directory, "metadata.properties" ) );
327         try
328         {
329             properties.store( os, null );
330         }
331         finally
332         {
333             IOUtils.closeQuietly( os );
334         }
335     }
336
337 }