View Javadoc

1   package org.alfresco.maven.plugin.amp;
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 org.apache.maven.artifact.Artifact;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /***
29   * An overlay is a skeleton war added to another war project in order to inject a
30   * functionnality, resources or any other shared component.
31   * <p/>
32   * Note that a particlar war dependency can be added multiple times as an overlay
33   * with different includes/excludes filter; this allows building a fine grained
34   * overwriting policy.
35   * <p/>
36   * The current project can also be described as an overlay and could not be specified
37   * twice. An overlay with no groupId and no artifactId is detected as defining the
38   * current project.
39   *
40   * @author Stephane Nicoll
41   */
42  public class Overlay
43  {
44  
45      public static final String[] DEFAULT_INCLUDES = new String[]{"**/**"};
46  
47      public static final String[] DEFAULT_EXCLUDES = new String[]{"META-INF/MANIFEST.MF"};
48  
49      private static Overlay currentProjectInstance;
50  
51      private String id;
52  
53      private String groupId;
54  
55      private String artifactId;
56  
57      private String classifier = null;
58  
59      private String[] includes = DEFAULT_INCLUDES;
60  
61      private String[] excludes = DEFAULT_EXCLUDES;
62  
63      private boolean filtered = false;
64  
65      private boolean skip = false;
66  
67      private Artifact artifact;
68      
69      private String targetPath;
70      
71      /*** default overlay type is war */ 
72      private String type = "amp";
73  
74      public Overlay()
75      {
76          super();
77      }
78  
79  
80      public Overlay( String groupId, String artifactId )
81      {
82          this();
83          this.groupId = groupId;
84          this.artifactId = artifactId;
85      }
86  
87      /***
88       * Specify whether this overlay represents the current project or not.
89       *
90       * @return true if the overlay represents the current project, false otherwise
91       */
92      public boolean isCurrentProject()
93      {
94          return ( groupId == null && artifactId == null );
95      }
96  
97      /***
98       * Creates an overlay of the current project.
99       *
100      * @return the current project as an overlay
101      */
102     public static Overlay currentProjectInstance()
103     {
104         if ( currentProjectInstance == null )
105         {
106             currentProjectInstance = new Overlay();
107             currentProjectInstance.setId( "currentBuild" );
108         }
109         return currentProjectInstance;
110     }
111 
112     // Getters and Setters
113 
114     public String getId()
115     {
116         if ( id == null )
117         {
118             final StringBuffer sb = new StringBuffer();
119             sb.append( getGroupId() ).append( ":" ).append( getArtifactId() );
120             if ( getClassifier() != null )
121             {
122                 sb.append( ":" ).append( getClassifier() );
123             }
124             id = sb.toString();
125         }
126         return id;
127     }
128 
129     public void setId( String id )
130     {
131         this.id = id;
132     }
133 
134     public String getGroupId()
135     {
136         return groupId;
137     }
138 
139     public void setGroupId( String groupId )
140     {
141         this.groupId = groupId;
142     }
143 
144     public String getArtifactId()
145     {
146         return artifactId;
147     }
148 
149     public void setArtifactId( String artifactId )
150     {
151         this.artifactId = artifactId;
152     }
153 
154     public String getClassifier()
155     {
156         return classifier;
157     }
158 
159     public void setClassifier( String classifier )
160     {
161         this.classifier = classifier;
162     }
163 
164     public String[] getIncludes()
165     {
166         return includes;
167     }
168 
169     public void setIncludes( String includes )
170     {
171         this.includes = parse( includes );
172     }
173 
174     public void setIncludes( String[] includes )
175     {
176         this.includes = includes;
177     }
178 
179     public String[] getExcludes()
180     {
181         return excludes;
182     }
183 
184     public void setExcludes( String excludes )
185     {
186         this.excludes = parse( excludes );
187     }
188 
189     public void setExcludes( String[] excludes )
190     {
191         this.excludes = excludes;
192     }
193 
194     public boolean isFiltered()
195     {
196         return filtered;
197     }
198 
199     public void setFiltered( boolean filtered )
200     {
201         this.filtered = filtered;
202     }
203 
204     public boolean shouldSkip()
205     {
206         return skip;
207     }
208 
209     public void setSkip( boolean skip )
210     {
211         this.skip = skip;
212     }
213 
214     public Artifact getArtifact()
215     {
216         return artifact;
217     }
218 
219     public void setArtifact( Artifact artifact )
220     {
221         this.artifact = artifact;
222     }
223 
224     public String getTargetPath()
225     {
226         return targetPath;
227     }
228 
229 
230     public void setTargetPath( String targetPath )
231     {
232         this.targetPath = targetPath;
233     }
234 
235     public String getType()
236     {
237         return type;
238     }
239 
240 
241     public void setType( String type )
242     {
243         this.type = type;
244     }
245     
246     public String toString()
247     {
248         return " id " + getId();
249     }
250 
251 
252     public boolean equals( Object o )
253     {
254         if ( this == o )
255         {
256             return true;
257         }
258         if ( o == null || getClass() != o.getClass() )
259         {
260             return false;
261         }
262 
263         Overlay overlay = (Overlay) o;
264 
265         if ( excludes != null ? !Arrays.equals( excludes, overlay.excludes ) : overlay.excludes != null )
266         {
267             return false;
268         }
269         if ( getId() != null ? !getId().equals( overlay.getId() ) : overlay.getId() != null )
270         {
271             return false;
272         }
273         if ( includes != null ? !Arrays.equals( includes, overlay.includes ) : overlay.includes != null )
274         {
275             return false;
276         }
277 
278         return true;
279     }
280 
281     public int hashCode()
282     {
283         int result;
284         result = ( getId() != null ? getId().hashCode() : 0 );
285         result = 31 * result + ( includes != null ? includes.hashCode() : 0 );
286         result = 31 * result + ( excludes != null ? excludes.hashCode() : 0 );
287         return result;
288     }
289 
290     private String[] parse( String s )
291     {
292         final List result = new ArrayList();
293         if ( s == null )
294         {
295             return (String[]) result.toArray( new String[result.size()] );
296         }
297         else
298         {
299             String[] tokens = s.split( "," );
300             for ( int i = 0; i < tokens.length; i++ )
301             {
302                 String token = tokens[i];
303                 result.add( token.trim() );
304             }
305             return (String[]) result.toArray( new String[result.size()] );
306         }
307     }
308 
309 }