Coverage Report - org.apache.maven.plugin.assembly.utils.TypeConversionUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeConversionUtils
74%
34/46
83%
40/48
0
 
 1  
 package org.apache.maven.plugin.assembly.utils;
 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.maven.plugin.assembly.format.AssemblyFormattingException;
 23  
 import org.codehaus.plexus.logging.Logger;
 24  
 
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * @version $Id: TypeConversionUtils.java 999612 2010-09-21 20:34:50Z jdcasey $
 29  
  */
 30  
 public final class TypeConversionUtils
 31  
 {
 32  
 
 33  
     private static final int U_R = 256;
 34  
 
 35  
     private static final int U_W = 128;
 36  
 
 37  
     private static final int U_X = 64;
 38  
 
 39  
     private static final int G_R = 32;
 40  
 
 41  
     private static final int G_W = 16;
 42  
 
 43  
     private static final int G_X = 8;
 44  
 
 45  
     private static final int W_R = 4;
 46  
 
 47  
     private static final int W_W = 2;
 48  
 
 49  
     private static final int W_X = 1;
 50  
 
 51  
     private TypeConversionUtils()
 52  0
     {
 53  0
     }
 54  
 
 55  
     public static String[] toStringArray( final List<String> list )
 56  
     {
 57  8
         String[] result = null;
 58  
 
 59  8
         if ( ( list != null ) && !list.isEmpty() )
 60  
         {
 61  2
             result = list.toArray( new String[0] );
 62  
         }
 63  
 
 64  8
         return result;
 65  
     }
 66  
 
 67  
     public static int modeToInt( final String mode, final Logger logger ) throws AssemblyFormattingException
 68  
     {
 69  68
         if ( mode == null || mode.trim()
 70  
                                  .length() < 1 )
 71  
         {
 72  15
             return -1;
 73  
         }
 74  
 
 75  
         try
 76  
         {
 77  53
             final int value = Integer.parseInt( mode, 8 );
 78  
 
 79  
             // discard sanity assessment here; we're pushing ahead.
 80  52
             verifyModeSanity( value, logger );
 81  
 
 82  52
             return value;
 83  
         }
 84  1
         catch ( final NumberFormatException e )
 85  
         {
 86  1
             throw new AssemblyFormattingException( "Failed to parse mode as an octal number: \'" + mode + "\'.", e );
 87  
         }
 88  
     }
 89  
 
 90  
     // the boolean return type is for people who want to make a decision based on the sanity
 91  
     // assessment.
 92  
     public static boolean verifyModeSanity( final int mode, final Logger logger )
 93  
     {
 94  55
         final StringBuffer messages = new StringBuffer();
 95  
 
 96  55
         messages.append( "The mode: " + Integer.toString( mode, 8 ) + " contains nonsensical permissions:" );
 97  
 
 98  55
         boolean warn = false;
 99  
 
 100  
         // read-access checks.
 101  55
         if ( ( ( mode & U_R ) == 0 ) && ( ( mode & G_R ) == G_R ) )
 102  
         {
 103  0
             messages.append( "\n- Group has read access, but user does not." );
 104  0
             warn = true;
 105  
         }
 106  
 
 107  55
         if ( ( ( mode & U_R ) == 0 ) && ( ( mode & W_R ) == W_R ) )
 108  
         {
 109  0
             messages.append( "\n- World has read access, but user does not." );
 110  0
             warn = true;
 111  
         }
 112  
 
 113  55
         if ( ( ( mode & G_R ) == 0 ) && ( ( mode & W_R ) == W_R ) )
 114  
         {
 115  0
             messages.append( "\n- World has read access, but group does not." );
 116  0
             warn = true;
 117  
         }
 118  
         // end read-access checks.
 119  
 
 120  
         // write-access checks.
 121  55
         if ( ( ( mode & U_W ) == 0 ) && ( ( mode & G_W ) == G_W ) )
 122  
         {
 123  1
             messages.append( "\n- Group has write access, but user does not." );
 124  1
             warn = true;
 125  
         }
 126  
 
 127  55
         if ( ( ( mode & U_W ) == 0 ) && ( ( mode & W_W ) == W_W ) )
 128  
         {
 129  8
             messages.append( "\n- World has write access, but user does not." );
 130  8
             warn = true;
 131  
         }
 132  
 
 133  55
         if ( ( ( mode & G_W ) == 0 ) && ( ( mode & W_W ) == W_W ) )
 134  
         {
 135  8
             messages.append( "\n- World has write access, but group does not." );
 136  8
             warn = true;
 137  
         }
 138  
         // end write-access checks.
 139  
 
 140  
         // execute-/list-access checks.
 141  55
         if ( ( ( mode & U_X ) == 0 ) && ( ( mode & G_X ) == G_X ) )
 142  
         {
 143  7
             messages.append( "\n- Group has execute/list access, but user does not." );
 144  7
             warn = true;
 145  
         }
 146  
 
 147  55
         if ( ( ( mode & U_X ) == 0 ) && ( ( mode & W_X ) == W_X ) )
 148  
         {
 149  0
             messages.append( "\n- World has execute/list access, but user does not." );
 150  0
             warn = true;
 151  
         }
 152  
 
 153  55
         if ( ( ( mode & G_X ) == 0 ) && ( ( mode & W_X ) == W_X ) )
 154  
         {
 155  0
             messages.append( "\n- World has execute/list access, but group does not." );
 156  0
             warn = true;
 157  
         }
 158  
         // end execute-/list-access checks.
 159  
 
 160  55
         if ( warn )
 161  
         {
 162  9
             logger.warn( messages.toString() );
 163  
         }
 164  
 
 165  55
         return !warn;
 166  
     }
 167  
 
 168  
 }