Coverage Report - org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagedLegacyRepositoryContent
0%
0/136
0%
0/90
0
 
 1  
 package org.apache.maven.archiva.repository.content;
 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.commons.collections.CollectionUtils;
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.apache.maven.archiva.common.utils.PathUtil;
 25  
 import org.apache.maven.archiva.configuration.FileTypes;
 26  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 27  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 28  
 import org.apache.maven.archiva.model.ArtifactReference;
 29  
 import org.apache.maven.archiva.model.ProjectReference;
 30  
 import org.apache.maven.archiva.model.VersionedReference;
 31  
 import org.apache.maven.archiva.repository.ContentNotFoundException;
 32  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 33  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 34  
 
 35  
 import java.io.File;
 36  
 import java.util.HashSet;
 37  
 import java.util.Set;
 38  
 
 39  
 /**
 40  
  * ManagedLegacyRepositoryContent 
 41  
  *
 42  
  * @version $Id: ManagedLegacyRepositoryContent.java 751940 2009-03-10 01:35:35Z brett $
 43  
  * 
 44  
  * @todo no need to be a component when filetypes, legacy path parser is not
 45  
  * 
 46  
  * @plexus.component 
 47  
  *      role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
 48  
  *      role-hint="legacy"
 49  
  *      instantiation-strategy="per-lookup"
 50  
  */
 51  0
 public class ManagedLegacyRepositoryContent
 52  
     extends AbstractLegacyRepositoryContent
 53  
     implements ManagedRepositoryContent
 54  
 {
 55  
     /**
 56  
      * @plexus.requirement
 57  
      */
 58  
     private FileTypes filetypes;
 59  
 
 60  
     private ManagedRepositoryConfiguration repository;
 61  
 
 62  
     public void deleteVersion( VersionedReference reference )
 63  
         throws ContentNotFoundException
 64  
     {
 65  0
         File groupDir = new File( repository.getLocation(), reference.getGroupId() );
 66  
 
 67  0
         if ( !groupDir.exists() )
 68  
         {
 69  0
             throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
 70  
                 + groupDir.getAbsolutePath() );
 71  
         }
 72  
 
 73  0
         if ( !groupDir.isDirectory() )
 74  
         {
 75  0
             throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
 76  
                 + groupDir.getAbsolutePath() );
 77  
         }
 78  
 
 79  
         // First gather up the versions found as artifacts in the managed repository.
 80  0
         File typeDirs[] = groupDir.listFiles();
 81  0
         for ( File typeDir : typeDirs )
 82  
         {
 83  0
             if ( !typeDir.isDirectory() )
 84  
             {
 85  
                 // Skip it, we only care about directories.
 86  0
                 continue;
 87  
             }
 88  
 
 89  0
             if ( !typeDir.getName().endsWith( "s" ) )
 90  
             {
 91  
                 // Skip it, we only care about directories that end in "s".
 92  
             }
 93  
 
 94  0
             deleteVersions( typeDir, reference );
 95  
         }
 96  0
     }
 97  
 
 98  
     private void deleteVersions( File typeDir, VersionedReference reference )
 99  
     {
 100  0
         File repoFiles[] = typeDir.listFiles();
 101  0
         for ( File repoFile : repoFiles )
 102  
         {
 103  0
             if ( repoFile.isDirectory() )
 104  
             {
 105  
                 // Skip it. it's a directory.
 106  0
                 continue;
 107  
             }
 108  
 
 109  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
 110  
 
 111  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 112  
             {
 113  
                 try
 114  
                 {
 115  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 116  0
                     if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
 117  
                         && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
 118  
                     {
 119  0
                         repoFile.delete();
 120  0
                         deleteSupportFiles( repoFile );
 121  
                     }
 122  
                 }
 123  0
                 catch ( LayoutException e )
 124  
                 {
 125  
                     /* don't fail the process if there is a bad artifact within the directory. */
 126  0
                 }
 127  
             }
 128  
         }
 129  0
     }
 130  
 
 131  
     private void deleteSupportFiles( File repoFile )
 132  
     {
 133  0
         deleteSupportFile( repoFile, ".sha1" );
 134  0
         deleteSupportFile( repoFile, ".md5" );
 135  0
         deleteSupportFile( repoFile, ".asc" );
 136  0
         deleteSupportFile( repoFile, ".gpg" );
 137  0
     }
 138  
 
 139  
     private void deleteSupportFile( File repoFile, String supportExtension )
 140  
     {
 141  0
         File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
 142  0
         if ( supportFile.exists() && supportFile.isFile() )
 143  
         {
 144  0
             supportFile.delete();
 145  
         }
 146  0
     }
 147  
 
 148  
     public String getId()
 149  
     {
 150  0
         return repository.getId();
 151  
     }
 152  
 
 153  
     public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
 154  
         throws ContentNotFoundException
 155  
     {
 156  0
         File artifactFile = toFile( reference );
 157  0
         File repoDir = artifactFile.getParentFile();
 158  
 
 159  0
         if ( !repoDir.exists() )
 160  
         {
 161  0
             throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
 162  
                 + repoDir.getAbsolutePath() );
 163  
         }
 164  
 
 165  0
         if ( !repoDir.isDirectory() )
 166  
         {
 167  0
             throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
 168  
                 + repoDir.getAbsolutePath() );
 169  
         }
 170  
 
 171  0
         Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
 172  
 
 173  
         // First gather up the versions found as artifacts in the managed repository.
 174  0
         File projectParentDir = repoDir.getParentFile();
 175  0
         File typeDirs[] = projectParentDir.listFiles();
 176  0
         for ( File typeDir : typeDirs )
 177  
         {
 178  0
             if ( !typeDir.isDirectory() )
 179  
             {
 180  
                 // Skip it, we only care about directories.
 181  0
                 continue;
 182  
             }
 183  
 
 184  0
             if ( !typeDir.getName().endsWith( "s" ) )
 185  
             {
 186  
                 // Skip it, we only care about directories that end in "s".
 187  
             }
 188  
 
 189  0
             getRelatedArtifacts( typeDir, reference, foundArtifacts );
 190  
         }
 191  
 
 192  0
         return foundArtifacts;
 193  
     }
 194  
 
 195  
     public String getRepoRoot()
 196  
     {
 197  0
         return repository.getLocation();
 198  
     }
 199  
 
 200  
     public ManagedRepositoryConfiguration getRepository()
 201  
     {
 202  0
         return repository;
 203  
     }
 204  
 
 205  
     public Set<String> getVersions( ProjectReference reference )
 206  
         throws ContentNotFoundException
 207  
     {
 208  0
         File groupDir = new File( repository.getLocation(), reference.getGroupId() );
 209  
 
 210  0
         if ( !groupDir.exists() )
 211  
         {
 212  0
             throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
 213  
                 + groupDir.getAbsolutePath() );
 214  
         }
 215  
 
 216  0
         if ( !groupDir.isDirectory() )
 217  
         {
 218  0
             throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
 219  
                 + groupDir.getAbsolutePath() );
 220  
         }
 221  
 
 222  0
         Set<String> foundVersions = new HashSet<String>();
 223  
 
 224  
         // First gather up the versions found as artifacts in the managed repository.
 225  0
         File typeDirs[] = groupDir.listFiles();
 226  0
         for ( File typeDir : typeDirs )
 227  
         {
 228  0
             if ( !typeDir.isDirectory() )
 229  
             {
 230  
                 // Skip it, we only care about directories.
 231  0
                 continue;
 232  
             }
 233  
 
 234  0
             if ( !typeDir.getName().endsWith( "s" ) )
 235  
             {
 236  
                 // Skip it, we only care about directories that end in "s".
 237  
             }
 238  
 
 239  0
             getProjectVersions( typeDir, reference, foundVersions );
 240  
         }
 241  
 
 242  0
         return foundVersions;
 243  
     }
 244  
 
 245  
     public Set<String> getVersions( VersionedReference reference )
 246  
         throws ContentNotFoundException
 247  
     {
 248  0
         File groupDir = new File( repository.getLocation(), reference.getGroupId() );
 249  
 
 250  0
         if ( !groupDir.exists() )
 251  
         {
 252  0
             throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
 253  
                 + groupDir.getAbsolutePath() );
 254  
         }
 255  
 
 256  0
         if ( !groupDir.isDirectory() )
 257  
         {
 258  0
             throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
 259  
                 + groupDir.getAbsolutePath() );
 260  
         }
 261  
 
 262  0
         Set<String> foundVersions = new HashSet<String>();
 263  
 
 264  
         // First gather up the versions found as artifacts in the managed repository.
 265  0
         File typeDirs[] = groupDir.listFiles();
 266  0
         for ( File typeDir : typeDirs )
 267  
         {
 268  0
             if ( !typeDir.isDirectory() )
 269  
             {
 270  
                 // Skip it, we only care about directories.
 271  0
                 continue;
 272  
             }
 273  
 
 274  0
             if ( !typeDir.getName().endsWith( "s" ) )
 275  
             {
 276  
                 // Skip it, we only care about directories that end in "s".
 277  
             }
 278  
 
 279  0
             getVersionedVersions( typeDir, reference, foundVersions );
 280  
         }
 281  
 
 282  0
         return foundVersions;
 283  
     }
 284  
 
 285  
     public boolean hasContent( ArtifactReference reference )
 286  
     {
 287  0
         File artifactFile = toFile( reference );
 288  0
         return artifactFile.exists() && artifactFile.isFile();
 289  
     }
 290  
 
 291  
     public boolean hasContent( ProjectReference reference )
 292  
     {
 293  
         try
 294  
         {
 295  0
             Set<String> versions = getVersions( reference );
 296  0
             return CollectionUtils.isNotEmpty( versions );
 297  
         }
 298  0
         catch ( ContentNotFoundException e )
 299  
         {
 300  0
             return false;
 301  
         }
 302  
     }
 303  
 
 304  
     public boolean hasContent( VersionedReference reference )
 305  
     {
 306  
         try
 307  
         {
 308  0
             Set<String> versions = getVersions( reference );
 309  0
             return CollectionUtils.isNotEmpty( versions );
 310  
         }
 311  0
         catch ( ContentNotFoundException e )
 312  
         {
 313  0
             return false;
 314  
         }
 315  
     }
 316  
 
 317  
     public void setRepository( ManagedRepositoryConfiguration repository )
 318  
     {
 319  0
         this.repository = repository;
 320  0
     }
 321  
 
 322  
     /**
 323  
      * Convert a path to an artifact reference.
 324  
      * 
 325  
      * @param path the path to convert. (relative or full location path)
 326  
      * @throws LayoutException if the path cannot be converted to an artifact reference.
 327  
      */
 328  
     @Override
 329  
     public ArtifactReference toArtifactReference( String path )
 330  
         throws LayoutException
 331  
     {
 332  0
         if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
 333  
         {
 334  0
             return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
 335  
         }
 336  
 
 337  0
         return super.toArtifactReference( path );
 338  
     }
 339  
     
 340  
     public File toFile( ArchivaArtifact reference )
 341  
     {
 342  0
         return new File( repository.getLocation(), toPath( reference ) );
 343  
     }
 344  
 
 345  
     public File toFile( ArtifactReference reference )
 346  
     {
 347  0
         return new File( repository.getLocation(), toPath( reference ) );
 348  
     }
 349  
 
 350  
     public String toMetadataPath( ProjectReference reference )
 351  
     {
 352  
         // No metadata present in legacy repository.
 353  0
         return null;
 354  
     }
 355  
 
 356  
     public String toMetadataPath( VersionedReference reference )
 357  
     {
 358  
         // No metadata present in legacy repository.
 359  0
         return null;
 360  
     }
 361  
 
 362  
     private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
 363  
     {
 364  0
         File repoFiles[] = typeDir.listFiles();
 365  0
         for ( File repoFile : repoFiles )
 366  
         {
 367  0
             if ( repoFile.isDirectory() )
 368  
             {
 369  
                 // Skip it. it's a directory.
 370  0
                 continue;
 371  
             }
 372  
 
 373  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
 374  
 
 375  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 376  
             {
 377  
                 try
 378  
                 {
 379  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 380  0
                     if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
 381  
                     {
 382  0
                         foundVersions.add( artifact.getVersion() );
 383  
                     }
 384  
                 }
 385  0
                 catch ( LayoutException e )
 386  
                 {
 387  
                     /* don't fail the process if there is a bad artifact within the directory. */
 388  0
                 }
 389  
             }
 390  
         }
 391  0
     }
 392  
 
 393  
     private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
 394  
     {
 395  0
         File repoFiles[] = typeDir.listFiles();
 396  0
         for ( int i = 0; i < repoFiles.length; i++ )
 397  
         {
 398  0
             if ( repoFiles[i].isDirectory() )
 399  
             {
 400  
                 // Skip it. it's a directory.
 401  0
                 continue;
 402  
             }
 403  
 
 404  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
 405  
 
 406  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 407  
             {
 408  
                 try
 409  
                 {
 410  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 411  0
                     if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
 412  
                         && artifact.getVersion().startsWith( reference.getVersion() ) )
 413  
                     {
 414  0
                         foundArtifacts.add( artifact );
 415  
                     }
 416  
                 }
 417  0
                 catch ( LayoutException e )
 418  
                 {
 419  
                     /* don't fail the process if there is a bad artifact within the directory. */
 420  0
                 }
 421  
             }
 422  
         }
 423  0
     }
 424  
 
 425  
     private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
 426  
     {
 427  0
         File repoFiles[] = typeDir.listFiles();
 428  0
         for ( int i = 0; i < repoFiles.length; i++ )
 429  
         {
 430  0
             if ( repoFiles[i].isDirectory() )
 431  
             {
 432  
                 // Skip it. it's a directory.
 433  0
                 continue;
 434  
             }
 435  
 
 436  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
 437  
 
 438  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 439  
             {
 440  
                 try
 441  
                 {
 442  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 443  0
                     if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
 444  
                         && artifact.getVersion().startsWith( reference.getVersion() ) )
 445  
                     {
 446  0
                         foundVersions.add( artifact.getVersion() );
 447  
                     }
 448  
                 }
 449  0
                 catch ( LayoutException e )
 450  
                 {
 451  
                     /* don't fail the process if there is a bad artifact within the directory. */
 452  0
                 }
 453  
             }
 454  
         }
 455  0
     }
 456  
     
 457  
     public void setFileTypes( FileTypes fileTypes )
 458  
     {
 459  0
         this.filetypes = fileTypes;
 460  0
     }
 461  
 }