PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/maven-3.0.5/maven-core/src/main/java/org/apache/maven/artifact/repository/MavenArtifactRepository.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 408 lines | 274 code | 68 blank | 66 comment | 42 complexity | ff517b26c10063e4a1b7354b16e6ce56 MD5 | raw file
  1. package org.apache.maven.artifact.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.File;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import org.apache.maven.artifact.Artifact;
  24. import org.apache.maven.artifact.metadata.ArtifactMetadata;
  25. import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
  26. import org.apache.maven.repository.Proxy;
  27. //TODO: completely separate local and remote artifact repositories
  28. public class MavenArtifactRepository
  29. implements ArtifactRepository
  30. {
  31. private String id;
  32. private String url;
  33. private String basedir;
  34. private String protocol;
  35. private ArtifactRepositoryLayout layout;
  36. private ArtifactRepositoryPolicy snapshots;
  37. private ArtifactRepositoryPolicy releases;
  38. private Authentication authentication;
  39. private Proxy proxy;
  40. private List<ArtifactRepository> mirroredRepositories = Collections.emptyList();
  41. public MavenArtifactRepository()
  42. {
  43. }
  44. /**
  45. * Create a remote download repository.
  46. *
  47. * @param id the unique identifier of the repository
  48. * @param url the URL of the repository
  49. * @param layout the layout of the repository
  50. * @param snapshots the policies to use for snapshots
  51. * @param releases the policies to use for releases
  52. */
  53. public MavenArtifactRepository( String id, String url, ArtifactRepositoryLayout layout,
  54. ArtifactRepositoryPolicy snapshots, ArtifactRepositoryPolicy releases )
  55. {
  56. this.id = id;
  57. this.url = url;
  58. this.layout = layout;
  59. this.snapshots = snapshots;
  60. this.releases = releases;
  61. //
  62. // Derive these from the URL
  63. //
  64. this.protocol = protocol( url );
  65. this.basedir = basedir( url );
  66. }
  67. public String pathOf( Artifact artifact )
  68. {
  69. return layout.pathOf( artifact );
  70. }
  71. public String pathOfRemoteRepositoryMetadata( ArtifactMetadata artifactMetadata )
  72. {
  73. return layout.pathOfRemoteRepositoryMetadata( artifactMetadata );
  74. }
  75. public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
  76. {
  77. return layout.pathOfLocalRepositoryMetadata( metadata, repository );
  78. }
  79. public void setLayout( ArtifactRepositoryLayout layout )
  80. {
  81. this.layout = layout;
  82. }
  83. public ArtifactRepositoryLayout getLayout()
  84. {
  85. return layout;
  86. }
  87. public void setSnapshotUpdatePolicy( ArtifactRepositoryPolicy snapshots )
  88. {
  89. this.snapshots = snapshots;
  90. }
  91. public ArtifactRepositoryPolicy getSnapshots()
  92. {
  93. return snapshots;
  94. }
  95. public void setReleaseUpdatePolicy( ArtifactRepositoryPolicy releases )
  96. {
  97. this.releases = releases;
  98. }
  99. public ArtifactRepositoryPolicy getReleases()
  100. {
  101. return releases;
  102. }
  103. public String getKey()
  104. {
  105. return getId();
  106. }
  107. public String toString()
  108. {
  109. StringBuilder sb = new StringBuilder();
  110. sb.append( " id: " ).append( getId() ).append( "\n" );
  111. sb.append( " url: " ).append( getUrl() ).append( "\n" );
  112. sb.append( " layout: " ).append( layout != null ? layout : "none" ).append( "\n" );
  113. if ( snapshots != null )
  114. {
  115. sb.append( "snapshots: [enabled => " ).append( snapshots.isEnabled() );
  116. sb.append( ", update => " ).append( snapshots.getUpdatePolicy() ).append( "]\n" );
  117. }
  118. if ( releases != null )
  119. {
  120. sb.append( " releases: [enabled => " ).append( releases.isEnabled() );
  121. sb.append( ", update => " ).append( releases.getUpdatePolicy() ).append( "]\n" );
  122. }
  123. return sb.toString();
  124. }
  125. public Artifact find( Artifact artifact )
  126. {
  127. File artifactFile = new File( getBasedir(), pathOf( artifact ) );
  128. // We need to set the file here or the resolver will fail with an NPE, not fully equipped to deal
  129. // with multiple local repository implementations yet.
  130. artifact.setFile( artifactFile );
  131. return artifact;
  132. }
  133. public List<String> findVersions( Artifact artifact )
  134. {
  135. return Collections.emptyList();
  136. }
  137. public String getId()
  138. {
  139. return id;
  140. }
  141. public String getUrl()
  142. {
  143. return url;
  144. }
  145. public String getBasedir()
  146. {
  147. return basedir;
  148. }
  149. public String getProtocol()
  150. {
  151. return protocol;
  152. }
  153. public void setId( String id )
  154. {
  155. this.id = id;
  156. }
  157. public void setUrl( String url )
  158. {
  159. this.url = url;
  160. this.protocol = protocol( url );
  161. this.basedir = basedir( url );
  162. }
  163. // Path Utils
  164. /**
  165. * /**
  166. * Return the protocol name.
  167. * <br/>
  168. * E.g: for input
  169. * <code>http://www.codehause.org</code> this method will return <code>http</code>
  170. *
  171. * @param url the url
  172. * @return the host name
  173. */
  174. private static String protocol( final String url )
  175. {
  176. final int pos = url.indexOf( ":" );
  177. if ( pos == -1 )
  178. {
  179. return "";
  180. }
  181. return url.substring( 0, pos ).trim();
  182. }
  183. /**
  184. * Derive the path portion of the given URL.
  185. *
  186. * @param url the repository URL
  187. * @return the basedir of the repository
  188. * @todo need to URL decode for spaces?
  189. */
  190. private String basedir( String url )
  191. {
  192. String retValue = null;
  193. if ( protocol.equalsIgnoreCase( "file" ) )
  194. {
  195. retValue = url.substring( protocol.length() + 1 );
  196. retValue = decode( retValue );
  197. // special case: if omitted // on protocol, keep path as is
  198. if ( retValue.startsWith( "//" ) )
  199. {
  200. retValue = retValue.substring( 2 );
  201. if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
  202. {
  203. // special case: if there is a windows drive letter, then keep the original return value
  204. retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  205. }
  206. else
  207. {
  208. // Now we expect the host
  209. int index = retValue.indexOf( "/" );
  210. if ( index >= 0 )
  211. {
  212. retValue = retValue.substring( index + 1 );
  213. }
  214. // special case: if there is a windows drive letter, then keep the original return value
  215. if ( retValue.length() >= 2 && ( retValue.charAt( 1 ) == '|' || retValue.charAt( 1 ) == ':' ) )
  216. {
  217. retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  218. }
  219. else if ( index >= 0 )
  220. {
  221. // leading / was previously stripped
  222. retValue = "/" + retValue;
  223. }
  224. }
  225. }
  226. // special case: if there is a windows drive letter using |, switch to :
  227. if ( retValue.length() >= 2 && retValue.charAt( 1 ) == '|' )
  228. {
  229. retValue = retValue.charAt( 0 ) + ":" + retValue.substring( 2 );
  230. }
  231. // normalize separators
  232. retValue = new File( retValue ).getPath();
  233. }
  234. if ( retValue == null )
  235. {
  236. retValue = "/";
  237. }
  238. return retValue.trim();
  239. }
  240. /**
  241. * Decodes the specified (portion of a) URL. <strong>Note:</strong> This decoder assumes that ISO-8859-1 is used to
  242. * convert URL-encoded octets to characters.
  243. *
  244. * @param url The URL to decode, may be <code>null</code>.
  245. * @return The decoded URL or <code>null</code> if the input was <code>null</code>.
  246. */
  247. private static String decode( String url )
  248. {
  249. String decoded = url;
  250. if ( url != null )
  251. {
  252. int pos = -1;
  253. while ( ( pos = decoded.indexOf( '%', pos + 1 ) ) >= 0 )
  254. {
  255. if ( pos + 2 < decoded.length() )
  256. {
  257. String hexStr = decoded.substring( pos + 1, pos + 3 );
  258. char ch = (char) Integer.parseInt( hexStr, 16 );
  259. decoded = decoded.substring( 0, pos ) + ch + decoded.substring( pos + 3 );
  260. }
  261. }
  262. }
  263. return decoded;
  264. }
  265. public int hashCode()
  266. {
  267. final int prime = 31;
  268. int result = 1;
  269. result = prime * result + ( ( getId() == null ) ? 0 : getId().hashCode() );
  270. return result;
  271. }
  272. public boolean equals( Object obj )
  273. {
  274. if ( this == obj )
  275. {
  276. return true;
  277. }
  278. if ( obj == null )
  279. {
  280. return false;
  281. }
  282. if ( getClass() != obj.getClass() )
  283. {
  284. return false;
  285. }
  286. ArtifactRepository other = (ArtifactRepository) obj;
  287. return eq( getId(), other.getId() );
  288. }
  289. protected static <T> boolean eq( T s1, T s2 )
  290. {
  291. return s1 != null ? s1.equals( s2 ) : s2 == null;
  292. }
  293. public Authentication getAuthentication()
  294. {
  295. return authentication;
  296. }
  297. public void setAuthentication( Authentication authentication )
  298. {
  299. this.authentication = authentication;
  300. }
  301. public Proxy getProxy()
  302. {
  303. return proxy;
  304. }
  305. public void setProxy( Proxy proxy )
  306. {
  307. this.proxy = proxy;
  308. }
  309. public boolean isBlacklisted()
  310. {
  311. return false;
  312. }
  313. public void setBlacklisted( boolean blackListed )
  314. {
  315. // no op
  316. }
  317. public boolean isUniqueVersion()
  318. {
  319. return true;
  320. }
  321. public boolean isProjectAware()
  322. {
  323. return false;
  324. }
  325. public List<ArtifactRepository> getMirroredRepositories()
  326. {
  327. return mirroredRepositories;
  328. }
  329. public void setMirroredRepositories( List<ArtifactRepository> mirroredRepositories )
  330. {
  331. if ( mirroredRepositories != null )
  332. {
  333. this.mirroredRepositories = mirroredRepositories;
  334. }
  335. else
  336. {
  337. this.mirroredRepositories = Collections.emptyList();
  338. }
  339. }
  340. }