1 package org.apache.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.maven.model.Dependency;
26 import org.apache.maven.model.Exclusion;
27 import org.apache.maven.model.Plugin;
28 import org.sonatype.aether.RepositorySystemSession;
29 import org.sonatype.aether.repository.RemoteRepository;
30 import org.sonatype.aether.repository.RepositoryPolicy;
31 import org.sonatype.aether.repository.WorkspaceReader;
32 import org.sonatype.aether.repository.WorkspaceRepository;
33
34
35
36
37 class CacheUtils
38 {
39
40 public static <T> boolean eq( T s1, T s2 )
41 {
42 return s1 != null ? s1.equals( s2 ) : s2 == null;
43 }
44
45 public static int hash( Object obj )
46 {
47 return obj != null ? obj.hashCode() : 0;
48 }
49
50 public static int repositoriesHashCode( List<RemoteRepository> repositories )
51 {
52 int result = 17;
53 for ( RemoteRepository repository : repositories )
54 {
55 result = 31 * result + repositoryHashCode( repository );
56 }
57 return result;
58 }
59
60 private static int repositoryHashCode( RemoteRepository repository )
61 {
62 int result = 17;
63 result = 31 * result + hash( repository.getUrl() );
64 return result;
65 }
66
67 private static boolean repositoryEquals( RemoteRepository r1, RemoteRepository r2 )
68 {
69 if ( r1 == r2 )
70 {
71 return true;
72 }
73
74 return eq( r1.getId(), r2.getId() ) && eq( r1.getUrl(), r2.getUrl() )
75 && policyEquals( r1.getPolicy( false ), r2.getPolicy( false ) )
76 && policyEquals( r1.getPolicy( true ), r2.getPolicy( true ) );
77 }
78
79 private static boolean policyEquals( RepositoryPolicy p1, RepositoryPolicy p2 )
80 {
81 if ( p1 == p2 )
82 {
83 return true;
84 }
85
86 return p1.isEnabled() == p2.isEnabled() && eq( p1.getChecksumPolicy(), p2.getChecksumPolicy() );
87 }
88
89 public static boolean repositoriesEquals( List<RemoteRepository> r1, List<RemoteRepository> r2 )
90 {
91 if ( r1.size() != r2.size() )
92 {
93 return false;
94 }
95
96 for ( Iterator<RemoteRepository> it1 = r1.iterator(), it2 = r2.iterator(); it1.hasNext(); )
97 {
98 if ( !repositoryEquals( it1.next(), it2.next() ) )
99 {
100 return false;
101 }
102 }
103
104 return true;
105 }
106
107 public static int pluginHashCode( Plugin plugin )
108 {
109 int hash = 17;
110
111 hash = hash * 31 + hash( plugin.getGroupId() );
112 hash = hash * 31 + hash( plugin.getArtifactId() );
113 hash = hash * 31 + hash( plugin.getVersion() );
114
115 hash = hash * 31 + ( plugin.isExtensions() ? 1 : 0 );
116
117 for ( Dependency dependency : plugin.getDependencies() )
118 {
119 hash = hash * 31 + hash( dependency.getGroupId() );
120 hash = hash * 31 + hash( dependency.getArtifactId() );
121 hash = hash * 31 + hash( dependency.getVersion() );
122 hash = hash * 31 + hash( dependency.getType() );
123 hash = hash * 31 + hash( dependency.getClassifier() );
124 hash = hash * 31 + hash( dependency.getScope() );
125
126 for ( Exclusion exclusion : dependency.getExclusions() )
127 {
128 hash = hash * 31 + hash( exclusion.getGroupId() );
129 hash = hash * 31 + hash( exclusion.getArtifactId() );
130 }
131 }
132
133 return hash;
134 }
135
136 public static boolean pluginEquals( Plugin a, Plugin b )
137 {
138 return eq( a.getArtifactId(), b.getArtifactId() )
139 && eq( a.getGroupId(), b.getGroupId() )
140 && eq( a.getVersion(), b.getVersion() )
141 && a.isExtensions() == b.isExtensions()
142 && dependenciesEquals( a.getDependencies(), b.getDependencies() );
143 }
144
145 private static boolean dependenciesEquals( List<Dependency> a, List<Dependency> b )
146 {
147 if ( a.size() != b.size() )
148 {
149 return false;
150 }
151
152 Iterator<Dependency> aI = a.iterator();
153 Iterator<Dependency> bI = b.iterator();
154
155 while ( aI.hasNext() )
156 {
157 Dependency aD = aI.next();
158 Dependency bD = bI.next();
159
160 boolean r = eq( aD.getGroupId(), bD.getGroupId() )
161 && eq( aD.getArtifactId(), bD.getArtifactId() )
162 && eq( aD.getVersion(), bD.getVersion() )
163 && eq( aD.getType(), bD.getType() )
164 && eq( aD.getClassifier(), bD.getClassifier() )
165 && eq( aD.getScope(), bD.getScope() );
166
167 r &= exclusionsEquals( aD.getExclusions(), bD.getExclusions() );
168
169 if ( !r )
170 {
171 return false;
172 }
173 }
174
175 return true;
176 }
177
178 private static boolean exclusionsEquals( List<Exclusion> a, List<Exclusion> b )
179 {
180 if ( a.size() != b.size() )
181 {
182 return false;
183 }
184
185 Iterator<Exclusion> aI = a.iterator();
186 Iterator<Exclusion> bI = b.iterator();
187
188 while ( aI.hasNext() )
189 {
190 Exclusion aD = aI.next();
191 Exclusion bD = bI.next();
192
193 boolean r = eq( aD.getGroupId(), bD.getGroupId() )
194 && eq( aD.getArtifactId(), bD.getArtifactId() );
195
196 if ( !r )
197 {
198 return false;
199 }
200 }
201
202 return true;
203 }
204
205 public static WorkspaceRepository getWorkspace( RepositorySystemSession session )
206 {
207 WorkspaceReader reader = session.getWorkspaceReader();
208 return ( reader != null ) ? reader.getRepository() : null;
209 }
210
211 }