KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > repository > metadata > AbstractRepositoryMetadata


1 package org.apache.maven.artifact.repository.metadata;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.metadata.ArtifactMetadata;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
23 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
24 import org.codehaus.plexus.util.IOUtil;
25 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.Writer JavaDoc;
33
34 /**
35  * Shared methods of the repository metadata handling.
36  *
37  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
38  * @version $Id: AbstractRepositoryMetadata.java 355404 2005-12-09 07:58:04Z brett $
39  */

40 public abstract class AbstractRepositoryMetadata
41     implements RepositoryMetadata
42 {
43     private Metadata metadata;
44
45     protected AbstractRepositoryMetadata( Metadata metadata )
46     {
47         this.metadata = metadata;
48     }
49
50     public String JavaDoc getRemoteFilename()
51     {
52         return "maven-metadata.xml";
53     }
54
55     public String JavaDoc getLocalFilename( ArtifactRepository repository )
56     {
57         return "maven-metadata-" + repository.getKey() + ".xml";
58     }
59
60     public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
61         throws RepositoryMetadataStoreException
62     {
63         try
64         {
65             updateRepositoryMetadata( localRepository, remoteRepository );
66         }
67         catch ( IOException JavaDoc e )
68         {
69             throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
70         }
71         catch ( XmlPullParserException e )
72         {
73             throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
74         }
75     }
76
77     protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
78         throws IOException JavaDoc, XmlPullParserException
79     {
80         MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
81
82         Metadata metadata = null;
83
84         File JavaDoc metadataFile = new File JavaDoc( localRepository.getBasedir(),
85                                       localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
86
87         if ( metadataFile.exists() )
88         {
89             Reader reader = null;
90
91             try
92             {
93                 reader = new FileReader JavaDoc( metadataFile );
94
95                 metadata = mappingReader.read( reader, false );
96             }
97             finally
98             {
99                 IOUtil.close( reader );
100             }
101         }
102
103         boolean changed;
104
105         // If file could not be found or was not valid, start from scratch
106
if ( metadata == null )
107         {
108             metadata = this.metadata;
109
110             changed = true;
111         }
112         else
113         {
114             changed = metadata.merge( this.metadata );
115         }
116
117         if ( changed )
118         {
119             Writer writer = null;
120             try
121             {
122                 metadataFile.getParentFile().mkdirs();
123                 writer = new FileWriter JavaDoc( metadataFile );
124
125                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
126
127                 mappingWriter.write( writer, metadata );
128             }
129             finally
130             {
131                 IOUtil.close( writer );
132             }
133         }
134         else
135         {
136             metadataFile.setLastModified( System.currentTimeMillis() );
137         }
138     }
139
140     public String JavaDoc toString()
141     {
142         return "repository metadata for: \'" + getKey() + "\'";
143     }
144
145     protected static Metadata createMetadata( Artifact artifact, Versioning versioning )
146     {
147         Metadata metadata = new Metadata();
148         metadata.setGroupId( artifact.getGroupId() );
149         metadata.setArtifactId( artifact.getArtifactId() );
150         metadata.setVersion( artifact.getVersion() );
151         metadata.setVersioning( versioning );
152         return metadata;
153     }
154
155     protected static Versioning createVersioning( Snapshot snapshot )
156     {
157         Versioning versioning = new Versioning();
158         versioning.setSnapshot( snapshot );
159         versioning.updateTimestamp();
160         return versioning;
161     }
162
163     public void setMetadata( Metadata metadata )
164     {
165         this.metadata = metadata;
166     }
167
168     public Metadata getMetadata()
169     {
170         return metadata;
171     }
172
173     public void merge( ArtifactMetadata metadata )
174     {
175         // TODO: not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact replaces?
176
AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
177         this.metadata.merge( repoMetadata.getMetadata() );
178     }
179
180     public String JavaDoc extendedToString()
181     {
182         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
183
184         buffer.append( "\nRepository Metadata\n--------------------------" );
185         buffer.append( "\nGroupId: " ).append( getGroupId() );
186         buffer.append( "\nArtifactId: " ).append( getArtifactId() );
187         buffer.append( "\nMetadata Type: " ).append( getClass().getName() );
188
189         return buffer.toString();
190     }
191 }
192
Popular Tags