Coverage Report - org.apache.tomcat.maven.common.run.DefaultClassLoaderEntriesCalculator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultClassLoaderEntriesCalculator
0 %
0/74
0 %
0/46
9,25
DefaultClassLoaderEntriesCalculator$1
0 %
0/2
N/A
9,25
 
 1  
 package org.apache.tomcat.maven.common.run;
 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.io.FileUtils;
 23  
 import org.apache.maven.artifact.Artifact;
 24  
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
 25  
 import org.apache.maven.plugin.logging.Log;
 26  
 import org.apache.maven.project.MavenProject;
 27  
 import org.codehaus.plexus.archiver.ArchiverException;
 28  
 import org.codehaus.plexus.archiver.UnArchiver;
 29  
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 30  
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
 31  
 import org.codehaus.plexus.component.annotations.Component;
 32  
 import org.codehaus.plexus.component.annotations.Requirement;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.FilenameFilter;
 37  
 import java.io.IOException;
 38  
 import java.util.ArrayList;
 39  
 import java.util.Collection;
 40  
 import java.util.LinkedHashSet;
 41  
 import java.util.List;
 42  
 import java.util.Set;
 43  
 
 44  
 /**
 45  
  * @author Olivier Lamy
 46  
  * @since 2.0
 47  
  */
 48  
 @Component( role = ClassLoaderEntriesCalculator.class )
 49  0
 public class DefaultClassLoaderEntriesCalculator
 50  
     implements ClassLoaderEntriesCalculator
 51  
 {
 52  
 
 53  
     @Requirement
 54  
     private ArchiverManager archiverManager;
 55  
 
 56  
 
 57  
     public ClassLoaderEntriesCalculatorResult calculateClassPathEntries( ClassLoaderEntriesCalculatorRequest request )
 58  
         throws TomcatRunException
 59  
     {
 60  0
         Set<String> classLoaderEntries = new LinkedHashSet<String>();
 61  
 
 62  0
         List<String> fileInClassLoaderEntries = new ArrayList<String>();
 63  
 
 64  0
         List<File> tmpDirectories = new ArrayList<File>();
 65  
 
 66  
         // add classes directories to loader
 67  
         try
 68  
         {
 69  0
             @SuppressWarnings( "unchecked" ) List<String> classPathElements = request.isUseTestClassPath()
 70  
                 ? request.getMavenProject().getTestClasspathElements()
 71  
                 : request.getMavenProject().getCompileClasspathElements();
 72  0
             if ( classPathElements != null )
 73  
             {
 74  0
                 for ( String classPathElement : classPathElements )
 75  
                 {
 76  0
                     File classPathElementFile = new File( classPathElement );
 77  0
                     if ( classPathElementFile.exists() && classPathElementFile.isDirectory() )
 78  
                     {
 79  0
                         request.getLog().debug(
 80  
                             "adding classPathElementFile " + classPathElementFile.toURI().toString() );
 81  0
                         classLoaderEntries.add( classPathElementFile.toURI().toString() );
 82  
                     }
 83  0
                 }
 84  
             }
 85  
         }
 86  0
         catch ( DependencyResolutionRequiredException e )
 87  
         {
 88  0
             throw new TomcatRunException( e.getMessage(), e );
 89  0
         }
 90  
 
 91  
         // TODO find a solution to use a timestamp marker to not delete/extract all the time
 92  
 
 93  0
         File tmpExtractDatas =
 94  
             new File( request.getMavenProject().getBuild().getDirectory(), "apache-tomcat-maven-plugin" );
 95  
 
 96  0
         if ( tmpExtractDatas.exists() )
 97  
         {
 98  0
             deleteDirectory( tmpExtractDatas, request.getLog() );
 99  
         }
 100  0
         tmpExtractDatas.mkdirs();
 101  
 
 102  
         // add artifacts to loader
 103  0
         if ( request.getDependencies() != null )
 104  
         {
 105  0
             for ( Artifact artifact : request.getDependencies() )
 106  
             {
 107  0
                 String scope = artifact.getScope();
 108  
 
 109  
                 // skip provided and test scoped artifacts
 110  0
                 if ( !Artifact.SCOPE_PROVIDED.equals( scope ) && ( !Artifact.SCOPE_TEST.equals( scope )
 111  
                     || request.isUseTestClassPath() ) )
 112  
                 {
 113  0
                     request.getLog().debug(
 114  
                         "add dependency to webapploader " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
 115  
                             + artifact.getVersion() + ":" + artifact.getScope() );
 116  0
                     if ( !isInProjectReferences( artifact, request.getMavenProject() ) )
 117  
                     {
 118  0
                         String fileName = artifact.getFile().getName();
 119  0
                         if ( !fileInClassLoaderEntries.contains( fileName ) )
 120  
                         {
 121  0
                             classLoaderEntries.add( artifact.getFile().toURI().toString() );
 122  0
                             fileInClassLoaderEntries.add( fileName );
 123  
                         }
 124  0
                     }
 125  
                     else
 126  
                     {
 127  0
                         request.getLog().debug(
 128  
                             "skip adding artifact " + artifact.getArtifactId() + " as it's in reactors" );
 129  
                     }
 130  
                 }
 131  
 
 132  
                 // in case of war dependency we must add /WEB-INF/lib/*.jar in entries and WEB-INF/classes
 133  0
                 if ( "war".equals( artifact.getType() ) && request.isAddWarDependenciesInClassloader() )
 134  
                 {
 135  
 
 136  0
                     File tmpDir = new File( tmpExtractDatas, artifact.getArtifactId() );
 137  
 
 138  0
                     tmpDir.mkdirs();
 139  
 
 140  0
                     tmpDirectories.add( tmpDir );
 141  
 
 142  
                     try
 143  
                     {
 144  0
                         tmpDir.deleteOnExit();
 145  0
                         File warFile = artifact.getFile();
 146  0
                         UnArchiver unArchiver = archiverManager.getUnArchiver( "jar" );
 147  0
                         unArchiver.setSourceFile( warFile );
 148  0
                         unArchiver.setDestDirectory( tmpDir );
 149  0
                         unArchiver.extract();
 150  
 
 151  0
                         File libsDirectory = new File( tmpDir, "WEB-INF/lib" );
 152  0
                         if ( libsDirectory.exists() )
 153  
                         {
 154  0
                             String[] jars = libsDirectory.list( new FilenameFilter()
 155  0
                             {
 156  
                                 public boolean accept( File file, String s )
 157  
                                 {
 158  0
                                     return s.endsWith( ".jar" );
 159  
                                 }
 160  
                             } );
 161  0
                             for ( String jar : jars )
 162  
                             {
 163  0
                                 File jarFile = new File( libsDirectory, jar );
 164  0
                                 if ( !fileInClassLoaderEntries.contains( jarFile.getName() ) )
 165  
                                 {
 166  0
                                     classLoaderEntries.add( jarFile.toURI().toString() );
 167  0
                                     fileInClassLoaderEntries.add( jarFile.getName() );
 168  
                                 }
 169  
                                 else
 170  
                                 {
 171  0
                                     request.getLog().debug( "skip adding file " + jarFile.getPath()
 172  
                                                                 + " as it's already in classloader entries" );
 173  
                                 }
 174  
                             }
 175  
                         }
 176  0
                         File classesDirectory = new File( tmpDir, "WEB-INF/classes" );
 177  0
                         if ( classesDirectory.exists() )
 178  
                         {
 179  0
                             classLoaderEntries.add( classesDirectory.toURI().toString() );
 180  
                         }
 181  
                     }
 182  0
                     catch ( NoSuchArchiverException e )
 183  
                     {
 184  0
                         throw new TomcatRunException( e.getMessage(), e );
 185  
                     }
 186  0
                     catch ( ArchiverException e )
 187  
                     {
 188  0
                         request.getLog().error(
 189  
                             "fail to extract war file " + artifact.getFile() + ", reason:" + e.getMessage(), e );
 190  0
                         throw new TomcatRunException( e.getMessage(), e );
 191  0
                     }
 192  
                 }
 193  0
             }
 194  
         }
 195  
 
 196  0
         return new ClassLoaderEntriesCalculatorResult( new ArrayList<String>( classLoaderEntries ), tmpDirectories );
 197  
 
 198  
     }
 199  
 
 200  
     private void deleteDirectory( File directory, Log log )
 201  
         throws TomcatRunException
 202  
     {
 203  
         try
 204  
         {
 205  0
             FileUtils.deleteDirectory( directory );
 206  
         }
 207  0
         catch ( IOException e )
 208  
         {
 209  0
             log.error( "fail to delete directory file " + directory + ", reason:" + e.getMessage(), e );
 210  0
             throw new TomcatRunException( e.getMessage(), e );
 211  0
         }
 212  0
     }
 213  
 
 214  
     protected boolean isInProjectReferences( Artifact artifact, MavenProject project )
 215  
     {
 216  0
         if ( project.getProjectReferences() == null || project.getProjectReferences().isEmpty() )
 217  
         {
 218  0
             return false;
 219  
         }
 220  0
         @SuppressWarnings( "unchecked" ) Collection<MavenProject> mavenProjects =
 221  
             project.getProjectReferences().values();
 222  0
         for ( MavenProject mavenProject : mavenProjects )
 223  
         {
 224  0
             if ( StringUtils.equals( mavenProject.getId(), artifact.getId() ) )
 225  
             {
 226  0
                 return true;
 227  
             }
 228  
         }
 229  0
         return false;
 230  
     }
 231  
 }