View Javadoc

1   /*
2    * $Id$
3    */
4   
5   package org.apache.maven.artifact.repository.metadata;
6   
7     //---------------------------------/
8    //- Imported classes and packages -/
9   //---------------------------------/
10  
11  import java.util.Date;
12  
13  /**
14   * Class Metadata.
15   * 
16   * @version $Revision$ $Date$
17   */
18  public class Metadata implements java.io.Serializable {
19  
20  
21        //--------------------------/
22       //- Class/Member Variables -/
23      //--------------------------/
24  
25      /**
26       * The groupId that this directory represents, if any.
27       */
28      private String groupId;
29  
30      /**
31       * The artifactId that this directory represents, if any.
32       */
33      private String artifactId;
34  
35      /**
36       * The version that this directory represents, if any.
37       */
38      private String version;
39  
40      /**
41       * Versioning information for the artifact.
42       */
43      private Versioning versioning;
44  
45      /**
46       * Field plugins.
47       */
48      private java.util.List plugins;
49  
50  
51        //-----------/
52       //- Methods -/
53      //-----------/
54  
55      /**
56       * Method addPlugin.
57       * 
58       * @param plugin
59       */
60      public void addPlugin( Plugin plugin )
61      {
62          if ( !(plugin instanceof Plugin) )
63          {
64              throw new ClassCastException( "Metadata.addPlugins(plugin) parameter must be instanceof " + Plugin.class.getName() );
65          }
66          getPlugins().add( plugin );
67      } //-- void addPlugin( Plugin ) 
68  
69      /**
70       * Get the artifactId that this directory represents, if any.
71       * 
72       * @return String
73       */
74      public String getArtifactId()
75      {
76          return this.artifactId;
77      } //-- String getArtifactId() 
78  
79      /**
80       * Get the groupId that this directory represents, if any.
81       * 
82       * @return String
83       */
84      public String getGroupId()
85      {
86          return this.groupId;
87      } //-- String getGroupId() 
88  
89      /**
90       * Method getPlugins.
91       * 
92       * @return java.util.List
93       */
94      public java.util.List getPlugins()
95      {
96          if ( this.plugins == null )
97          {
98              this.plugins = new java.util.ArrayList();
99          }
100     
101         return this.plugins;
102     } //-- java.util.List getPlugins() 
103 
104     /**
105      * Get the version that this directory represents, if any.
106      * 
107      * @return String
108      */
109     public String getVersion()
110     {
111         return this.version;
112     } //-- String getVersion() 
113 
114     /**
115      * Get versioning information for the artifact.
116      * 
117      * @return Versioning
118      */
119     public Versioning getVersioning()
120     {
121         return this.versioning;
122     } //-- Versioning getVersioning() 
123 
124     /**
125      * Method removePlugin.
126      * 
127      * @param plugin
128      */
129     public void removePlugin( Plugin plugin )
130     {
131         if ( !(plugin instanceof Plugin) )
132         {
133             throw new ClassCastException( "Metadata.removePlugins(plugin) parameter must be instanceof " + Plugin.class.getName() );
134         }
135         getPlugins().remove( plugin );
136     } //-- void removePlugin( Plugin ) 
137 
138     /**
139      * Set the artifactId that this directory represents, if any.
140      * 
141      * @param artifactId
142      */
143     public void setArtifactId( String artifactId )
144     {
145         this.artifactId = artifactId;
146     } //-- void setArtifactId( String ) 
147 
148     /**
149      * Set the groupId that this directory represents, if any.
150      * 
151      * @param groupId
152      */
153     public void setGroupId( String groupId )
154     {
155         this.groupId = groupId;
156     } //-- void setGroupId( String ) 
157 
158     /**
159      * Set the set of plugin mappings for the group.
160      * 
161      * @param plugins
162      */
163     public void setPlugins( java.util.List plugins )
164     {
165         this.plugins = plugins;
166     } //-- void setPlugins( java.util.List ) 
167 
168     /**
169      * Set the version that this directory represents, if any.
170      * 
171      * @param version
172      */
173     public void setVersion( String version )
174     {
175         this.version = version;
176     } //-- void setVersion( String ) 
177 
178     /**
179      * Set versioning information for the artifact.
180      * 
181      * @param versioning
182      */
183     public void setVersioning( Versioning versioning )
184     {
185         this.versioning = versioning;
186     } //-- void setVersioning( Versioning ) 
187 
188 
189     public boolean merge( Metadata sourceMetadata )
190     {
191         boolean changed = false;
192 
193         for ( java.util.Iterator i = sourceMetadata.getPlugins().iterator(); i.hasNext(); )
194         {
195             Plugin plugin = (Plugin) i.next();
196             boolean found = false;
197 
198             for ( java.util.Iterator it = getPlugins().iterator(); it.hasNext() && !found; )
199             {
200                 Plugin preExisting = (Plugin) it.next();
201 
202                 if ( preExisting.getPrefix().equals( plugin.getPrefix() ) )
203                 {
204                     found = true;
205                 }
206             }
207 
208             if ( !found )
209             {
210                 Plugin mappedPlugin = new Plugin();
211 
212                 mappedPlugin.setArtifactId( plugin.getArtifactId() );
213 
214                 mappedPlugin.setPrefix( plugin.getPrefix() );
215 
216                 mappedPlugin.setName( plugin.getName() );
217 
218                 addPlugin( mappedPlugin );
219 
220                 changed = true;
221             }
222         }
223 
224         Versioning versioning = sourceMetadata.getVersioning();
225         if ( versioning != null )
226         {
227             Versioning v = getVersioning();
228             if ( v == null )
229             {
230                 v = new Versioning();
231                 setVersioning( v );
232                 changed = true;
233             }
234 
235             for ( java.util.Iterator i = versioning.getVersions().iterator(); i.hasNext(); )
236             {
237                 String version = (String) i.next();
238                 if ( !v.getVersions().contains( version ) )
239                 {
240                     changed = true;
241                     v.getVersions().add( version );
242                 }
243             }
244           
245             if ( "null".equals( versioning.getLastUpdated() ) )
246             {
247                 versioning.setLastUpdated( null );
248             }
249 
250             if ( "null".equals( v.getLastUpdated() ) )
251             {
252                 v.setLastUpdated( null );
253             }
254 
255             if ( versioning.getLastUpdated() == null || versioning.getLastUpdated().length() == 0 )
256             {
257                 // this should only be for historical reasons - we assume local is newer
258                 versioning.setLastUpdated( v.getLastUpdated() );
259             }
260 
261             if ( v.getLastUpdated() == null || v.getLastUpdated().length() == 0 ||
262                  versioning.getLastUpdated().compareTo( v.getLastUpdated() ) >= 0 )
263             {
264                 changed = true;
265                 v.setLastUpdated( versioning.getLastUpdated() );
266 
267                 if ( versioning.getRelease() != null )
268                 {
269                     changed = true;
270                     v.setRelease( versioning.getRelease() );
271                 }
272                 if ( versioning.getLatest() != null )
273                 {
274                     changed = true;
275                     v.setLatest( versioning.getLatest() );
276                 }
277 
278                 Snapshot s = v.getSnapshot();
279                 Snapshot snapshot = versioning.getSnapshot();
280                 if ( snapshot != null )
281                 {
282                     if ( s == null )
283                     {
284                         s = new Snapshot();
285                         v.setSnapshot( s );
286                         changed = true;
287                     }
288 
289                     // overwrite
290                     if ( s.getTimestamp() == null ? snapshot.getTimestamp() != null
291                         : !s.getTimestamp().equals( snapshot.getTimestamp() ) )
292                     {
293                         s.setTimestamp( snapshot.getTimestamp() );
294                         changed = true;
295                     }
296                     if ( s.getBuildNumber() != snapshot.getBuildNumber() )
297                     {
298                         s.setBuildNumber( snapshot.getBuildNumber() );
299                         changed = true;
300                     }
301                     if ( s.isLocalCopy() != snapshot.isLocalCopy() )
302                     {
303                         s.setLocalCopy( snapshot.isLocalCopy() );
304                         changed = true;
305                     }
306                 }
307             }
308         }
309         return changed;
310     }
311           
312     private String modelEncoding = "UTF-8";
313 
314     /**
315      * Set an encoding used for reading/writing the model.
316      *
317      * @param modelEncoding the encoding used when reading/writing the model.
318      */
319     public void setModelEncoding( String modelEncoding )
320     {
321         this.modelEncoding = modelEncoding;
322     }
323 
324     /**
325      * @return the current encoding used when reading/writing this model.
326      */
327     public String getModelEncoding()
328     {
329         return modelEncoding;
330     }
331 }