View Javadoc
1   package org.codehaus.mojo.webstart;
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  /**
25   * Represents a resolved jarResource.
26   * <p/>
27   * Created on 10/29/13.
28   *
29   * @author Tony Chemit <chemit@codelutin.com>
30   * @since 1.0-beta-4
31   */
32  public class ResolvedJarResource
33  {
34      /**
35       * The underlying jar resource configuration (from pom).
36       */
37      private final JarResource jarResource;
38  
39      /**
40       * The resolved artifact.
41       */
42      private final Artifact artifact;
43  
44      /**
45       * The hrefValue to fill in JarResource file.
46       */
47      private String hrefValue;
48  
49      public ResolvedJarResource( Artifact artifact )
50      {
51          this( new JarResource(), artifact );
52      }
53  
54      public ResolvedJarResource( JarResource jarResource, Artifact artifact )
55      {
56          if ( artifact == null )
57          {
58              throw new IllegalArgumentException( "artifact must not be null" );
59          }
60          if ( jarResource == null )
61          {
62              throw new IllegalArgumentException( "jarResource must not be null" );
63          }
64          this.jarResource = jarResource;
65          this.artifact = artifact;
66          setHrefValue( jarResource.getHrefValue() );
67      }
68  
69      public String getArtifactId()
70      {
71          return artifact.getArtifactId();
72      }
73  
74      public String getType()
75      {
76          return artifact.getType();
77      }
78  
79      public String getClassifier()
80      {
81          return artifact.getClassifier();
82      }
83  
84      public String getGroupId()
85      {
86          return artifact.getGroupId();
87      }
88  
89      public String getVersion()
90      {
91          return artifact.getVersion();
92      }
93  
94      public String getMainClass()
95      {
96          return jarResource.getMainClass();
97      }
98  
99      public boolean isOutputJarVersion()
100     {
101         return jarResource.isOutputJarVersion();
102     }
103 
104     public boolean isIncludeInJnlp()
105     {
106         return jarResource.isIncludeInJnlp();
107     }
108 
109     /**
110      * Returns the underlying artifact that this instance represents.
111      *
112      * @return Returns the value of the artifact field.
113      */
114     public Artifact getArtifact()
115     {
116         return artifact;
117     }
118 
119     /**
120      * Returns the value that should be output for this jar in the href attribute of the
121      * jar resource element in the generated JNLP file. If not set explicitly, this defaults
122      * to the file name of the underlying artifact.
123      *
124      * @return The href attribute to be output for this jar resource in the generated JNLP file.
125      */
126     public String getHrefValue()
127     {
128         String result;
129         if ( hrefValue == null && getArtifact() != null )
130         {
131             // use default value
132             result = getArtifact().getFile().getName();
133         }
134         else
135         {
136             // use customized value
137             result = hrefValue;
138         }
139         return result;
140     }
141 
142     /**
143      * Sets the value that should be output for this jar in the href attribute of the
144      * jar resource element in the generated JNLP file. If not set explicitly, this defaults
145      * to the file name of the underlying artifact.
146      *
147      * @param hrefValue new value for field {@link #hrefValue}
148      */
149     public void setHrefValue( String hrefValue )
150     {
151         this.hrefValue = hrefValue;
152     }
153 
154     /**
155      * Returns true if the given object is a JarResource and has the same
156      * combination of <code>groupId</code>, <code>artifactId</code>,
157      * <code>version</code> and <code>classifier</code>.
158      *
159      * @return {@code true} if equals to given other object.
160      */
161     @Override
162     public boolean equals( Object obj )
163     {
164 
165         if ( obj == this )
166         {
167             return true;
168         }
169 
170         if ( !( obj instanceof ResolvedJarResource ) )
171         {
172             return false;
173         }
174 
175         ResolvedJarResource other = (ResolvedJarResource) obj;
176 
177         if ( fieldsAreNotEqual( getGroupId(), other.getGroupId() ) )
178         {
179             return false;
180         }
181 
182         if ( fieldsAreNotEqual( getArtifactId(), other.getArtifactId() ) )
183         {
184             return false;
185         }
186 
187         if ( fieldsAreNotEqual( getVersion(), other.getVersion() ) )
188         {
189             return false;
190         }
191 
192         if ( fieldsAreNotEqual( getType(), other.getType() ) )
193         {
194             return false;
195         }
196 
197         if ( fieldsAreNotEqual( getClassifier(), other.getClassifier() ) )
198         {
199             return false;
200         }
201 
202         return true;
203 
204     }
205 
206     private boolean fieldsAreNotEqual( Object field1, Object field2 )
207     {
208 
209         if ( field1 == null )
210         {
211             return field2 != null;
212         }
213         else
214         {
215             return !field1.equals( field2 );
216         }
217 
218     }
219 
220     /**
221      * {@inheritDoc}
222      */
223     public int hashCode()
224     {
225         final int offset = 17;
226         final int multiplier = 37;
227         int result = offset;
228         result += multiplier * fieldHashCode( getGroupId() );
229         result += multiplier * fieldHashCode( getArtifactId() );
230         result += multiplier * fieldHashCode( getVersion() );
231         result += multiplier * fieldHashCode( getType() );
232         result += multiplier * fieldHashCode( getClassifier() );
233         return result;
234 
235     }
236 
237     private int fieldHashCode( Object field )
238     {
239         return field == null ? 0 : field.hashCode();
240     }
241 }
242