Coverage Report - org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagedDefaultRepositoryContent
0%
0/122
0%
0/72
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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.util.HashSet;
 25  
 import java.util.Set;
 26  
 
 27  
 import org.apache.commons.io.FileUtils;
 28  
 import org.apache.maven.archiva.common.utils.PathUtil;
 29  
 import org.apache.maven.archiva.configuration.FileTypes;
 30  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 31  
 import org.apache.maven.archiva.model.ArchivaArtifact;
 32  
 import org.apache.maven.archiva.model.ArtifactReference;
 33  
 import org.apache.maven.archiva.model.ProjectReference;
 34  
 import org.apache.maven.archiva.model.VersionedReference;
 35  
 import org.apache.maven.archiva.repository.ContentNotFoundException;
 36  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 37  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 38  
 
 39  
 /**
 40  
  * ManagedDefaultRepositoryContent 
 41  
  *
 42  
  * @version $Id: ManagedDefaultRepositoryContent.java 801946 2009-08-07 11:00:03Z oching $
 43  
  * 
 44  
  * @todo no need to be a component when filetypes is not
 45  
  * 
 46  
  * @plexus.component 
 47  
  *      role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
 48  
  *      role-hint="default"
 49  
  *      instantiation-strategy="per-lookup"
 50  
  */
 51  0
 public class ManagedDefaultRepositoryContent
 52  
     extends AbstractDefaultRepositoryContent
 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
         String path = toMetadataPath( reference );
 66  0
         File projectPath = new File( getRepoRoot(), path );
 67  
         
 68  0
         File projectDir = projectPath.getParentFile();
 69  0
         if( projectDir.exists() && projectDir.isDirectory() )
 70  
         {
 71  
             try
 72  
             {
 73  0
                 FileUtils.deleteDirectory( projectDir );
 74  
             }
 75  0
             catch ( IOException e )
 76  
             {
 77  
                 // TODO: log this somewhere?
 78  0
             }
 79  
         }
 80  
         else
 81  
         {
 82  0
             throw new ContentNotFoundException( "Unable to delete non-existing project directory." );
 83  
         }
 84  0
     }
 85  
 
 86  
     public String getId()
 87  
     {
 88  0
         return repository.getId();
 89  
     }
 90  
 
 91  
     public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
 92  
         throws ContentNotFoundException
 93  
     {
 94  0
         File artifactFile = toFile( reference );
 95  0
         File repoDir = artifactFile.getParentFile();
 96  
 
 97  0
         if ( !repoDir.exists() )
 98  
         {
 99  0
             throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
 100  
                 + repoDir.getAbsolutePath() );
 101  
         }
 102  
 
 103  0
         if ( !repoDir.isDirectory() )
 104  
         {
 105  0
             throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
 106  
                 + repoDir.getAbsolutePath() );
 107  
         }
 108  
 
 109  0
         Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
 110  
 
 111  
         // First gather up the versions found as artifacts in the managed repository.
 112  0
         File repoFiles[] = repoDir.listFiles();
 113  0
         for ( int i = 0; i < repoFiles.length; i++ )
 114  
         {
 115  0
             if ( repoFiles[i].isDirectory() )
 116  
             {
 117  
                 // Skip it. it's a directory.
 118  0
                 continue;
 119  
             }
 120  
 
 121  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
 122  
 
 123  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 124  
             {
 125  
                 try
 126  
                 {
 127  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 128  
                     
 129  
                     // Test for related, groupId / artifactId / version must match.
 130  0
                     if ( artifact.getGroupId().equals( reference.getGroupId() )
 131  
                         && artifact.getArtifactId().equals( reference.getArtifactId() )
 132  
                         && artifact.getVersion().equals( reference.getVersion() ) )
 133  
                     {
 134  0
                         foundArtifacts.add( artifact );
 135  
                     }
 136  
                 }
 137  0
                 catch ( LayoutException e )
 138  
                 {
 139  0
                     log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
 140  0
                 }
 141  
             }
 142  
         }
 143  
 
 144  0
         return foundArtifacts;
 145  
     }
 146  
 
 147  
     public String getRepoRoot()
 148  
     {
 149  0
         return repository.getLocation();
 150  
     }
 151  
 
 152  
     public ManagedRepositoryConfiguration getRepository()
 153  
     {
 154  0
         return repository;
 155  
     }
 156  
 
 157  
     /**
 158  
      * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
 159  
      * information.
 160  
      *
 161  
      * @return the Set of available versions, based on the project reference.
 162  
      * @throws LayoutException 
 163  
      * @throws LayoutException
 164  
      */
 165  
     public Set<String> getVersions( ProjectReference reference )
 166  
         throws ContentNotFoundException, LayoutException
 167  
     {
 168  0
         String path = toMetadataPath( reference );
 169  
 
 170  0
         int idx = path.lastIndexOf( '/' );
 171  0
         if ( idx > 0 )
 172  
         {
 173  0
             path = path.substring( 0, idx );
 174  
         }
 175  
 
 176  0
         File repoDir = new File( repository.getLocation(), path );
 177  
 
 178  0
         if ( !repoDir.exists() )
 179  
         {
 180  0
             throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
 181  
                 + repoDir.getAbsolutePath() );
 182  
         }
 183  
 
 184  0
         if ( !repoDir.isDirectory() )
 185  
         {
 186  0
             throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
 187  
                 + repoDir.getAbsolutePath() );
 188  
         }
 189  
 
 190  0
         Set<String> foundVersions = new HashSet<String>();
 191  0
         VersionedReference versionRef = new VersionedReference();
 192  0
         versionRef.setGroupId( reference.getGroupId() );
 193  0
         versionRef.setArtifactId( reference.getArtifactId() );
 194  
 
 195  0
         File repoFiles[] = repoDir.listFiles();
 196  0
         for ( int i = 0; i < repoFiles.length; i++ )
 197  
         {
 198  0
             if ( !repoFiles[i].isDirectory() )
 199  
             {
 200  
                 // Skip it. not a directory.
 201  0
                 continue;
 202  
             }
 203  
 
 204  
             // Test if dir has an artifact, which proves to us that it is a valid version directory.
 205  0
             String version = repoFiles[i].getName();
 206  0
             versionRef.setVersion( version );
 207  
 
 208  0
             if ( hasArtifact( versionRef ) )
 209  
             {
 210  
                 // Found an artifact, must be a valid version.
 211  0
                 foundVersions.add( version );
 212  
             }
 213  
         }
 214  
 
 215  0
         return foundVersions;
 216  
     }
 217  
 
 218  
     public Set<String> getVersions( VersionedReference reference )
 219  
         throws ContentNotFoundException
 220  
     {
 221  0
         String path = toMetadataPath( reference );
 222  
 
 223  0
         int idx = path.lastIndexOf( '/' );
 224  0
         if ( idx > 0 )
 225  
         {
 226  0
             path = path.substring( 0, idx );
 227  
         }
 228  
 
 229  0
         File repoDir = new File( repository.getLocation(), path );
 230  
 
 231  0
         if ( !repoDir.exists() )
 232  
         {
 233  0
             throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
 234  
                 + repoDir.getAbsolutePath() );
 235  
         }
 236  
 
 237  0
         if ( !repoDir.isDirectory() )
 238  
         {
 239  0
             throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
 240  
                 + repoDir.getAbsolutePath() );
 241  
         }
 242  
 
 243  0
         Set<String> foundVersions = new HashSet<String>();
 244  
 
 245  
         // First gather up the versions found as artifacts in the managed repository.
 246  0
         File repoFiles[] = repoDir.listFiles();
 247  0
         for ( int i = 0; i < repoFiles.length; i++ )
 248  
         {
 249  0
             if ( repoFiles[i].isDirectory() )
 250  
             {
 251  
                 // Skip it. it's a directory.
 252  0
                 continue;
 253  
             }
 254  
 
 255  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
 256  
 
 257  0
             if ( filetypes.matchesDefaultExclusions( relativePath ) )
 258  
             {
 259  
                 // Skip it, it's metadata or similar
 260  0
                 continue;
 261  
             }
 262  
 
 263  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 264  
             {
 265  
                 try
 266  
                 {
 267  0
                     ArtifactReference artifact = toArtifactReference( relativePath );
 268  
     
 269  0
                     foundVersions.add( artifact.getVersion() );
 270  
                 }
 271  0
                 catch ( LayoutException e )
 272  
                 {
 273  0
                     log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
 274  0
                 }
 275  
             }
 276  
         }
 277  
 
 278  0
         return foundVersions;
 279  
     }
 280  
 
 281  
     public boolean hasContent( ArtifactReference reference )
 282  
     {
 283  0
         File artifactFile = toFile( reference );
 284  0
         return artifactFile.exists() && artifactFile.isFile();
 285  
     }
 286  
 
 287  
     public boolean hasContent( ProjectReference reference )
 288  
     {
 289  
         try
 290  
         {
 291  0
             Set<String> versions = getVersions( reference );
 292  0
             return !versions.isEmpty();
 293  
         }
 294  0
         catch ( ContentNotFoundException e )
 295  
         {
 296  0
             return false;
 297  
         }
 298  0
         catch ( LayoutException e )
 299  
         {
 300  0
             return false;
 301  
         }
 302  
     }
 303  
 
 304  
     public boolean hasContent( VersionedReference reference )
 305  
     {
 306  
         try
 307  
         {
 308  0
             return ( getFirstArtifact( reference ) != null );
 309  
         }
 310  0
         catch ( IOException e )
 311  
         {
 312  0
             return false;
 313  
         }
 314  0
         catch ( LayoutException e )
 315  
         {
 316  0
             return false;
 317  
         }
 318  
     }
 319  
 
 320  
     public void setRepository( ManagedRepositoryConfiguration repository )
 321  
     {
 322  0
         this.repository = repository;
 323  0
     }
 324  
 
 325  
     /**
 326  
      * Convert a path to an artifact reference.
 327  
      * 
 328  
      * @param path the path to convert. (relative or full location path)
 329  
      * @throws LayoutException if the path cannot be converted to an artifact reference.
 330  
      */
 331  
     @Override
 332  
     public ArtifactReference toArtifactReference( String path )
 333  
         throws LayoutException
 334  
     {
 335  0
         if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
 336  
         {
 337  0
             return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
 338  
         }
 339  
 
 340  0
         return super.toArtifactReference( path );
 341  
     }
 342  
 
 343  
     public File toFile( ArtifactReference reference )
 344  
     {
 345  0
         return new File( repository.getLocation(), toPath( reference ) );
 346  
     }
 347  
     
 348  
     public File toFile( ArchivaArtifact reference )
 349  
     {
 350  0
         return new File( repository.getLocation(), toPath( reference ) );
 351  
     }
 352  
 
 353  
     /**
 354  
      * Get the first Artifact found in the provided VersionedReference location.
 355  
      *
 356  
      * @param managedRepository the repository to search within.
 357  
      * @param reference         the reference to the versioned reference to search within
 358  
      * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
 359  
      *         no artifact was found within the versioned reference.
 360  
      * @throws IOException     if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
 361  
      * @throws LayoutException
 362  
      */
 363  
     private ArtifactReference getFirstArtifact( VersionedReference reference )
 364  
         throws LayoutException, IOException
 365  
     {
 366  0
         String path = toMetadataPath( reference );
 367  
 
 368  0
         int idx = path.lastIndexOf( '/' );
 369  0
         if ( idx > 0 )
 370  
         {
 371  0
             path = path.substring( 0, idx );
 372  
         }
 373  
 
 374  0
         File repoDir = new File( repository.getLocation(), path );
 375  
 
 376  0
         if ( !repoDir.exists() )
 377  
         {
 378  0
             throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
 379  
                 + repoDir.getAbsolutePath() );
 380  
         }
 381  
 
 382  0
         if ( !repoDir.isDirectory() )
 383  
         {
 384  0
             throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
 385  
                 + repoDir.getAbsolutePath() );
 386  
         }
 387  
 
 388  0
         File repoFiles[] = repoDir.listFiles();
 389  0
         for ( int i = 0; i < repoFiles.length; i++ )
 390  
         {
 391  0
             if ( repoFiles[i].isDirectory() )
 392  
             {
 393  
                 // Skip it. it's a directory.
 394  0
                 continue;
 395  
             }
 396  
 
 397  0
             String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
 398  
 
 399  0
             if ( filetypes.matchesArtifactPattern( relativePath ) )
 400  
             {
 401  0
                 ArtifactReference artifact = toArtifactReference( relativePath );
 402  
 
 403  0
                 return artifact;
 404  
             }
 405  
         }
 406  
 
 407  
         // No artifact was found.
 408  0
         return null;
 409  
     }
 410  
 
 411  
     private boolean hasArtifact( VersionedReference reference )
 412  
         throws LayoutException
 413  
     {
 414  
         try
 415  
         {
 416  0
             return ( getFirstArtifact( reference ) != null );
 417  
         }
 418  0
         catch ( IOException e )
 419  
         {
 420  0
             return false;
 421  
         }
 422  
     }
 423  
 
 424  
     public void setFiletypes( FileTypes filetypes )
 425  
     {
 426  0
         this.filetypes = filetypes;
 427  0
     }
 428  
 }