1 package org.codehaus.plexus.archiver; 2 3 /* 4 * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés. 5 * Author(s) : Franck Bonin, Neticoa SAS France 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * 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, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 */ 20 21 //import org.codehaus.plexus.archiver.util.ArchiveEntryUtils; 22 //import org.codehaus.plexus.components.io.attributes.SymlinkUtils; 23 import org.codehaus.plexus.components.io.fileselectors.FileSelector; 24 import org.codehaus.plexus.components.io.fileselectors.FileInfo; 25 //import org.codehaus.plexus.components.io.resources.PlexusIoResource; 26 import org.codehaus.plexus.logging.AbstractLogEnabled; 27 //import org.codehaus.plexus.util.FileUtils; 28 //import org.codehaus.plexus.util.IOUtil; 29 //import org.codehaus.plexus.util.StringUtils; 30 31 32 //import java.io.*; 33 import java.io.File; 34 import java.io.IOException; 35 //import java.util.ArrayList; 36 //import java.util.Date; 37 import java.util.List; 38 //import java.util.Iterator; 39 40 /** 41 * @author Franck Bonin 42 * @version $Revision$ $Date$ 43 */ 44 public abstract class AbstractArchiveContentLister 45 extends AbstractLogEnabled 46 implements ArchiveContentLister 47 { 48 private File sourceFile; 49 50 private FileSelector[] fileSelectors; 51 52 public AbstractArchiveContentLister() 53 { 54 } 55 56 public AbstractArchiveContentLister( final File sourceFile ) 57 { 58 this.sourceFile = sourceFile; 59 } 60 61 public File getSourceFile() 62 { 63 return sourceFile; 64 } 65 66 public void setSourceFile( final File sourceFile ) 67 { 68 this.sourceFile = sourceFile; 69 } 70 71 public final List<ArchiveContentEntry> list() 72 throws ArchiverException 73 { 74 validate(); 75 return execute(); 76 //runArchiveFinalizers(); 77 } 78 /* 79 public final void list( final String path ) 80 throws ArchiverException 81 { 82 validate( path ); 83 execute( path ); 84 //runArchiveFinalizers(); 85 } 86 */ 87 /* 88 public void addArchiveFinalizer( final ArchiveFinalizer finalizer ) 89 { 90 if ( finalizers == null ) 91 { 92 finalizers = new ArrayList(); 93 } 94 95 finalizers.add( finalizer ); 96 } 97 98 public void setArchiveFinalizers( final List archiveFinalizers ) 99 { 100 finalizers = archiveFinalizers; 101 } 102 103 private void runArchiveFinalizers() 104 throws ArchiverException 105 { 106 if ( finalizers != null ) 107 { 108 for (Object finalizer1 : finalizers) { 109 final ArchiveFinalizer finalizer = (ArchiveFinalizer) finalizer1; 110 111 finalizer.finalizeArchiveExtraction(this); 112 } 113 } 114 } 115 */ 116 /* 117 protected void validate( final String path, final File outputDirectory ) 118 { 119 } 120 */ 121 122 protected void validate() 123 throws ArchiverException 124 { 125 if ( sourceFile == null ) 126 { 127 throw new ArchiverException( "The source file isn't defined." ); 128 } 129 130 if ( sourceFile.isDirectory() ) 131 { 132 throw new ArchiverException( "The source must not be a directory." ); 133 } 134 135 if ( !sourceFile.exists() ) 136 { 137 throw new ArchiverException( "The source file " + sourceFile + " doesn't exist." ); 138 } 139 /* 140 if ( destDirectory == null && destFile == null ) 141 { 142 throw new ArchiverException( "The destination isn't defined." ); 143 } 144 145 if ( destDirectory != null && destFile != null ) 146 { 147 throw new ArchiverException( "You must choose between a destination directory and a destination file." ); 148 } 149 150 if ( destDirectory != null && !destDirectory.isDirectory() ) 151 { 152 destFile = destDirectory; 153 destDirectory = null; 154 } 155 156 if ( destFile != null && destFile.isDirectory() ) 157 { 158 destDirectory = destFile; 159 destFile = null; 160 } 161 */ 162 } 163 164 public void setFileSelectors( final FileSelector[] fileSelectors ) 165 { 166 this.fileSelectors = fileSelectors; 167 } 168 169 public FileSelector[] getFileSelectors() 170 { 171 return fileSelectors; 172 } 173 174 protected boolean isSelected( final String fileName, final /*PlexusIoResource*/ FileInfo fileInfo ) 175 throws ArchiverException 176 { 177 if ( fileSelectors != null ) 178 { 179 for ( FileSelector fileSelector : fileSelectors ) 180 { 181 try 182 { 183 if ( !fileSelector.isSelected( fileInfo ) ) 184 { 185 return false; 186 } 187 } 188 catch ( final IOException e ) 189 { 190 throw new ArchiverException( "Failed to check, whether " + fileInfo.getName() + " is selected: " 191 + e.getMessage(), e ); 192 } 193 } 194 } 195 return true; 196 } 197 198 protected abstract List<ArchiveContentEntry> execute() 199 throws ArchiverException; 200 201 /* 202 protected abstract void execute( String path) 203 throws ArchiverException; 204 */ 205 206 /* 207 protected void extractFile( final File srcF, final File dir, final InputStream compressedInputStream, 208 final String entryName, final Date entryDate, final boolean isDirectory, 209 final Integer mode, String symlinkDestination ) 210 throws IOException, ArchiverException 211 { 212 // Hmm. Symlinks re-evaluate back to the original file here. Unsure if this is a good thing... 213 final File f = FileUtils.resolveFile( dir, entryName ); 214 215 try 216 { 217 if ( !isOverwrite() && f.exists() && ( f.lastModified() >= entryDate.getTime() ) ) 218 { 219 return; 220 } 221 222 // create intermediary directories - sometimes zip don't add them 223 final File dirF = f.getParentFile(); 224 if ( dirF != null ) 225 { 226 dirF.mkdirs(); 227 } 228 229 if ( !StringUtils.isEmpty( symlinkDestination )){ 230 SymlinkUtils.createSymbolicLink( f, new File( symlinkDestination) ); 231 } 232 else if ( isDirectory ) 233 { 234 f.mkdirs(); 235 } 236 else 237 { 238 OutputStream out = null; 239 try 240 { 241 out = new FileOutputStream( f ); 242 243 IOUtil.copy( compressedInputStream, out ); 244 } 245 finally 246 { 247 IOUtil.close( out ); 248 } 249 } 250 251 f.setLastModified( entryDate.getTime() ); 252 253 if ( !isIgnorePermissions() && mode != null && !isDirectory) 254 { 255 ArchiveEntryUtils.chmod( f, mode, getLogger(), isUseJvmChmod() ); 256 } 257 } 258 catch ( final FileNotFoundException ex ) 259 { 260 getLogger().warn( "Unable to expand to file " + f.getPath() ); 261 } 262 } 263 */ 264 }