View Javadoc
1   package org.apache.maven.plugins.site.descriptor;
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.IOException;
24  import java.io.Writer;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
28  import org.apache.maven.artifact.metadata.ArtifactMetadata;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
31  import org.apache.maven.doxia.site.decoration.DecorationModel;
32  import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
33  
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.WriterFactory;
36  
37  /**
38   * Attach a POM to an artifact.
39   *
40   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
41   * @version $Id$
42   */
43  public class SiteDescriptorArtifactMetadata
44      extends AbstractArtifactMetadata
45  {
46      private final DecorationModel decoration;
47  
48      private final File file;
49  
50      public SiteDescriptorArtifactMetadata( Artifact artifact, DecorationModel decoration, File file )
51      {
52          super( artifact );
53  
54          this.file = file;
55          this.decoration = decoration;
56      }
57  
58      public String getRemoteFilename()
59      {
60          return getFilename();
61      }
62  
63      public String getLocalFilename( ArtifactRepository repository )
64      {
65          return getFilename();
66      }
67  
68      private String getFilename()
69      {
70          return getArtifactId() + "-" + artifact.getVersion() + "-" + file.getName();
71      }
72  
73      public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
74          throws RepositoryMetadataStoreException
75      {
76          File destination = new File( localRepository.getBasedir(),
77                                       localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
78  
79          destination.getParentFile().mkdirs();
80  
81          Writer writer = null;
82          try
83          {
84              writer = WriterFactory.newXmlWriter( destination );
85              new DecorationXpp3Writer().write( writer, decoration );
86              writer.close();
87              writer = null;
88          }
89          catch ( IOException e )
90          {
91              throw new RepositoryMetadataStoreException( "Error saving in local repository", e );
92          }
93          finally
94          {
95              IOUtil.close( writer );
96          }
97      }
98  
99      public String toString()
100     {
101         return "site descriptor for " + artifact.getArtifactId() + " " + artifact.getVersion() + " " + file.getName();
102     }
103 
104     public boolean storedInArtifactVersionDirectory()
105     {
106         return true;
107     }
108 
109     public String getBaseVersion()
110     {
111         return artifact.getBaseVersion();
112     }
113 
114     public Object getKey()
115     {
116         return "site descriptor " + artifact.getGroupId() + ":" + artifact.getArtifactId() + " " + file.getName();
117     }
118 
119     public void merge( ArtifactMetadata metadata )
120     {
121         SiteDescriptorArtifactMetadata m = (SiteDescriptorArtifactMetadata) metadata;
122         if ( !m.file.equals( file ) )
123         {
124             throw new IllegalStateException( "Cannot add two different pieces of metadata for: " + getKey() );
125         }
126     }
127 
128     public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
129     {
130         // FIXME what todo here ?
131     }
132 }