[ 
https://jira.codehaus.org/browse/MEAR-187?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=348280#comment-348280
 ] 

Martin Gainty commented on MEAR-187:
------------------------------------

Here is what you have that I could not get to work 
 protected void execute() throws ArchiverException
    {
        getLogger().debug( "EarArchiver::execute() LINE 206 Expanding: " + 
getSourceFile() + " into " + getDestDirectory() );
        ZipFile zf = null;
        try
        {
                        getLogger().debug( "EarArchiver::execute() before zf = 
new ZipFile( getSourceFile()="+getSourceFile()+", encoding="+encoding+" )");
            zf = new ZipFile( getSourceFile(), encoding );
            getLogger().debug( "EarArchiver::execute() ZipFile zf="+zf);
            final Enumeration e = zf.getEntries();
            while ( e.hasMoreElements() )
            {
                final ZipEntry ze = (ZipEntry) e.nextElement();
                getLogger().debug( "EarArchiver::execute() ZipEntry ze="+ze);
                final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zf, ze 
);
                getLogger().debug( "EarArchiver::execute() LINE 219 
fileInfo="+fileInfo);
                if ( !isSelected( ze.getName(), fileInfo ) )
                {
                                        getLogger().debug( 
"EarArchiver::execute() LINE 222 about to continue");
                    continue;
                }
                getLogger().debug( "EarArchiver::execute() LINE 225 before in = 
zf.getInputStream(ze) ze="+ze);
                InputStream in = zf.getInputStream( ze );
                getLogger().debug( "EarArchiver::execute() LINE 227 before 
extractFileIfIncluded( getSourceFile(), getDestDirectory(), in, ze.getName()");
                extractFileIfIncluded( getSourceFile(), getDestDirectory(), in, 
ze.getName(),
                                       new Date( ze.getTime() ), 
ze.isDirectory(), ze.getUnixMode()!= 0 ? ze.getUnixMode() : null );
                                getLogger().debug( "EarArchiver::execute() LINE 
230 in.close()");
                in.close();
            }


Here is what I have that does work:
package org.codehaus.plexus.archiver.ear;

/**
 *
 * Copyright 2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import org.codehaus.plexus.archiver.util.FilterSupport;
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.logging.AbstractLogEnabled;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author <a href="mailto:eveni...@codehaus.org";>Emmanuel Venisse</a>
 * @version $Revision$ $Date$
 * @todo there should really be constructors which take the source file.
 */
public abstract class EarArchiver
    extends JarArchiver
{
    private File destDirectory;

    private File destFile;

    private File sourceFile;

    private boolean overwrite = true;

    public FilterSupport filterSupport;
    public FilterSupport getFilterSupport() { return this.filterSupport; }

    private List finalizers;

    private FileSelector[] fileSelectors;

    /**
     * since 2.3 is on by default
     * @since 1.1
     */
    private boolean useJvmChmod = true;

    /**
     * @since 1.1
     */
    private boolean ignorePermissions = false;

    public AbstractUnArchiver()
    {
        // no op
    }

    public AbstractUnArchiver( final File sourceFile )
    {
        this.sourceFile = sourceFile;
    }

    public File getDestDirectory()
    {
        return destDirectory;
    }

    public void setDestDirectory( final File destDirectory )
    {
        this.destDirectory = destDirectory;
    }

    public File getDestFile()
    {
        return destFile;
    }

    public void setDestFile( final File destFile )
    {
        this.destFile = destFile;
    }

    public File getSourceFile()
    {
        return sourceFile;
    }

    public void setSourceFile( final File sourceFile )
    {
        this.sourceFile = sourceFile;
    }

    public boolean isOverwrite()
    {
        return overwrite;
    }

    public void setOverwrite( final boolean b )
    {
        overwrite = b;
    }

    public final void extract()
        throws ArchiverException
    {
                //System.out.println("AbstractUnArchiver::extract LINE 120 
before validate()");
       // validate();
        System.out.println("AbstractUnArchiver::extract LINE 122 before 
execute()");
        try
        {
                         execute();
                         }
                catch(Exception excp)
                {
                        System.out.println("AbstractUnarchiver::extract LINE 
129 threw Exception message="+excp.getMessage());
                }
        System.out.println("AbstractUnArchiver::extract LINE 124 before 
runArchiveFinalizers()");
        runArchiveFinalizers();
    }

    public final void extract( final String path, final File outputDirectory )
        throws ArchiverException
    {
                System.out.println("AbstractInArchiver:;extract path="+path);
                System.out.println("AbstractUnArchiver:;extract 
outputDirectory="+outputDirectory);
                System.out.println("AbstractUnArchiver::extract before 
validate(path, outputDirectory)");
        validate( path, outputDirectory );
        System.out.println("AbstractUnArchiver::extract before 
execute(path,outputDirectory)");
        execute( path, outputDirectory );
        System.out.println("AbstractUnArchiver::extract before 
runArchiveFinalizers");
        runArchiveFinalizers();
    }

    public void setArchiveFilters( final List filters )
    {
        filterSupport = new FilterSupport( filters, getLogger() );
    }

    public void addArchiveFinalizer( final ArchiveFinalizer finalizer )
    {
        if ( finalizers == null )
        {
            finalizers = new ArrayList();
        }

        finalizers.add( finalizer );
    }

    public void setArchiveFinalizers( final List archiveFinalizers )
    {
        finalizers = archiveFinalizers;
    }

    private final void runArchiveFinalizers()
        throws ArchiverException
    {
                System.out.println("AbstractUnArchiver::unArchiveFinalizers() 
where finalizers="+finalizers);
        if ( finalizers != null )
        {
                        
System.out.println("AbstractUnArchiver::unArchiveFinalizers() before  for ( 
final Iterator it = finalizers.iterator(); it.hasNext(); )");
            for ( final Iterator it = finalizers.iterator(); it.hasNext(); )
            {
                                
System.out.println("AbstractUnArchiver::unArchiverFinalizers() before  for ( 
final Iterator it = finalizers.iterator(); it.hasNext(); )");
                final ArchiveFinalizer finalizer = (ArchiveFinalizer) it.next();
                                
System.out.println("AbstractUnArchiver::unArchiverFinalizers() before 
finalizer.finalizeArchiveExtraction( this )");
                finalizer.finalizeArchiveExtraction( this );
            }
        }
        else
        {
                        
System.out.println("AbstractUnArchiver::unArchiveFinalizers before 
unZipIt(sourceFile="+sourceFile);
                        
System.out.println("AbstractUnArchiver::unArchiveFinalizers befoe 
unZipIt(destDirectory="+destDirectory);
                        try
                        {
                                unZipIt((String)sourceFile.getCanonicalPath(), 
(String)destDirectory.getCanonicalPath());
                        }
                        catch(java.io.IOException ioe)
                        {
                                System.out.println("AbstractUnArchiveFinalizers 
LINE 193 throws IOException has been caught message="+ioe.getMessage());
                        }
                }
    }
    public synchronized void MakeTheDirs(File newFile)
    {
                                         
System.out.println("AbstractUnArchiver::MakeTheDirs before new 
File(newFile.getParent()).mkdirs()) newFile="+newFile);
                                 new java.io.File(newFile.getParent()).mkdirs();
  }
  public synchronized void setArchiveBit(java.io.File newFile)
  {
    StringBuffer sb=new StringBuffer();
    sb.append("attrib +a ");
    try
    {
    sb.append(newFile.getCanonicalPath());
    }
    catch(java.io.IOException io)
    {
                System.out.println("sb.append(newFile.getCanonicalPath returns 
IOException message="+io.getMessage());
        }
    System.out.println("AbstractUnArchiver::unZipIt About to "+sb.toString());
    try
    {
        Runtime.getRuntime().exec(sb.toString());
        }
        catch(java.io.IOException io2)
        {
                System.out.println("Runtime.getRuntime().exec() has thrown 
IOException message="+io2.getMessage());
        }
  }
  public synchronized void setReadModeOff(java.io.File newFile)
  {
            StringBuffer sb = new StringBuffer();
                sb.append("attrib -r ");
        try
        {
          sb.append(newFile.getCanonicalPath());
        }
        catch(java.io.IOException io)
        {
                    System.out.println("AbstractUnarchiver::setReadModeOff 
sb.append(newFile.getCanonicalPath returns IOException 
message="+io.getMessage());
            }
        System.out.println("AbstractUnArchiver::setReadModeOff About to 
"+sb.toString());
        try
        {
                Runtime.getRuntime().exec(sb.toString());
            }
            catch(java.io.IOException io2)
            {
                  System.out.println("AbstractUnArchiver::setReadModelOff 
Runtime.getRuntime().exec has thrown IOException message="+io2.getMessage());
            }
    }
        public synchronized java.io.FileOutputStream 
getFileOutputStream(java.io.File newFile)
        {
                 
System.out.println("AbstractUnArchiver::getFileOutputStream(newFile) before fos 
= new FileOutputStream(newFile) newFile="+newFile);
// set writable to true
        newFile.setExecutable(true);
        System.out.println("AbstractUnArchiver::getFileOutputStream is 
Executable allow : "+newFile.canExecute() );
        newFile.setReadable(true);
        System.out.println("AbstractUnArchiver::getFileOutputStream is Readable 
allow : "+newFile. canRead() );
        newFile.setWritable(true);
        System.out.println("AbstractUnArchiver::getFileOutputStream is Writable 
allow : "+newFile.canWrite() );
        System.out.println("AbstractUnArchiver::getFileOutputStream will call 
setArchiveBit()");
        setArchiveBit(newFile);

        setReadModeOff(newFile);

                java.io.FileOutputStream fos = null;
                System.out.println("test for existence of newFile="+newFile+" 
newFile.exists()="+newFile.exists());
                if(newFile.isDirectory()==false)
                {
                        try
                        {
                                fos = new java.io.FileOutputStream(newFile);
                        }
                        catch(java.io.FileNotFoundException fnfe)
                        {
                                System.out.println("AbstractUnArchiver::unZipIt 
FileNotFoundException has been thrown in getFileOutputStream newFile="+newFile);
                        }
            }
                System.out.println("AbstractUnArchiver::getFileOutputStream 
returns FileOutputStream fos="+fos);
                return fos;
        }

    public synchronized java.io.File getTheFile(         java.io.File 
outputFolder, java.io.File fileName)
    {
             System.out.println("AbstractUnArchiver::getTheFile before new 
java.io.File(outputFolder + File.separator + fileName)  outputFolder 
="+outputFolder+" fileName="+fileName);
                 return new java.io.File(outputFolder + File.separator + 
fileName);
         }
         public synchronized java.util.zip.ZipInputStream 
getZipInputStream(String zipFile)
         {
                 
System.out.println("AbstractUnArchiver::getZipInputStream(zipFile) where 
zipFile="+zipFile);
                 java.util.zip.ZipInputStream zip=null;
                 try
                 {
                        java.io.FileInputStream fis=new 
java.io.FileInputStream(zipFile);
                        
System.out.println("AbstractUnArchiver::getZipInputStream(zipFile="+zipFile);
                        zip= new java.util.zip.ZipInputStream(fis);
                }
                catch(java.io.FileNotFoundException fne)
                {
                        
System.out.println("AbstractUnArchiver::getZipInputStream LINE 289 throws 
FileNotFoundException message="+fne.getMessage());
                }
                
System.out.println("AbstractUnArchiver::getZipInpuitStream(zipFile) returns 
zip="+zip);
                return zip;
         }
         public synchronized java.io.File makeOutputFolder(String outputFolder)
         {
                 
System.out.println("AbstractUnArchiver::makeOutputFolder(outputFolder) 
outputFolder="+outputFolder);
                  return new java.io.File(outputFolder);
          }
   public synchronized void ReadAndWrite(java.util.zip.ZipInputStream 
zis,java.io.FileOutputStream fos, byte []buffer)
    {
                  int len=0;
                  if(zis!=null)
                  {
                        try
                        {
                        while( (len = zis.read(buffer) ) > 0)
                        {
                         System.out.println("AbstractUnArchiver::ReadAndWrite 
before fos.write(buffer="+buffer);
                         System.out.println("AbstractUnArchiver::ReadAndWrite 
len="+len);
                     fos.write(buffer, 0, len);
                 } //end while
                     }
                     catch(java.io.IOException ioe)
                     {
                                 
System.out.println("AbstractUnArchiver::ReadAndWrite throws IOException 
mssage="+ioe.getMessage());
                         }
                 }
          }
          public synchronized java.util.zip.ZipEntry 
getNextEntry(java.util.zip.ZipInputStream zis)
     {
                 System.out.println("AbstractUnArchive::getNextEntry zis="+zis);
                  java.util.zip.ZipEntry zipEntry = null;
                  if(zis!=null)
                  {
                    try
                    {
                                zipEntry = zis.getNextEntry();
                        }
                        catch(java.io.IOException ioe)
                        {
                                
System.out.println("AbstractUnArchivr::getNextEntry throws java.io.IOException 
mssage="+ioe.getMessage());
                        }
              }
                          
System.out.println("AbstractUnArchiveEntry::getNextEntry LINE 327 returns 
zipEntry="+zipEntry);
                      return zipEntry;
           }
           public synchronized String getName(java.util.zip.ZipEntry ze)
           {
                   String name=null;
                   if (ze!=null)
                   {
                           name =  ze.getName();
                   }
                   System.out.println("AbstractUnArchiveEntry::getName LINE 312 
returns "+name);
                   return name;
           }
           public java.io.File getOutputFile(String outputFolder, String 
fileName)
           {
                   StringBuffer sb=new StringBuffer();
                   sb.append(outputFolder);
                   sb.append("\\");
                   sb.append(fileName);
                   System.out.println("AbstractUnarchiver::getOutputFile will 
create file:"+sb.toString());
                   return new java.io.File(sb.toString());
           }
   /**
     * Unzip it
     * @param zipFile input zip file
     * @param output zip file output folder
     */
    public synchronized void unZipIt(String zipFile, String outputFolder)
    {
     byte[] buffer = new byte[1024];
     try
     {
        //create output directory is not exists
        System.out.println("AbstractUnArchiver::unZipIt zipFile="+zipFile+" 
outputFolder="+outputFolder+" before folder = new File(outputFolder)");
        java.io.File folder =makeOutputFolder(outputFolder);
        System.out.println("AbstractUnArchiver::unZipIt folder = "+folder);
        if(folder.exists()==false)
        {
                 synchronized(folder)
                 {
                MakeTheDirs(folder);
                  }
            }

        //get the zip file content
        java.util.zip.ZipInputStream zis =null;
        if(zipFile!=null)
        {
                        synchronized(zipFile)
                        {
                                zis = getZipInputStream(zipFile);
                        }
            }
       System.out.println("AbstractUnArchiver::unZipIt zis = "+zis);
        //get the zipped file list entry
        java.util.zip.ZipEntry ze=null;
        synchronized(zis)
        {
           ze = getNextEntry(zis); //zis.getNextEntry();
            }
        System.out.println("AbstractUnArchiver::unZipIt ze="+ze);
                java.io.File newFile = null;
        while(ze!=null)
        {
                        System.out.println("AbstractUnArchiver::unZipIt before 
fileName = ze.getName()");
                        String fileName=null;
                        synchronized(ze)
                        {
              fileName = getName(ze);
               }
           System.out.println("AbstractUnArchiver::unZipIt filename="+fileName);
           synchronized(outputFolder)
           {
                 newFile =getOutputFile((String)outputFolder, (String)fileName);
                    }
           System.out.println("AbstractUnArchiver::unZipIt file unzip : "+ 
newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            synchronized( newFile )
            {
                MakeTheDirs(newFile);
                    }

            java.io.FileOutputStream fos = null;
            synchronized( newFile )
            {
                           fos = getFileOutputStream(newFile);
                   }

                    System.out.println("AbstarctUnArchiver::unZipIt fos="+fos);
            int len;
            if(fos!=null)
            {
            synchronized(fos)
            {
                                ReadAndWrite(zis, fos,buffer);
                        }
                        System.out.println("AbstractUnArchiver::unZIpIt before 
fos.close()");
                        synchronized(fos)
                        {
                fos.close();
                        }
                    }
            System.out.println("AbstractUnArchiver::unZipIt before 
ze=zis.getNextEntry()");
            synchronized(zis)
            {
                ze = getNextEntry(zis);
                        }
        }
                System.out.println("AbstractUnArchiver::unZipIt before 
zis.closeEntry()");
                synchronized(zis)
                {
         zis.closeEntry();
             }
        System.out.println("AbstractUnArchiver::unZipIt before zis.close()");
        synchronized(zis)
        {
                zis.close();
                }

        System.out.println("AbstractUnArchiver::unZipIt LINE 410 Done");

    }
    catch(IOException ex)
    {
                System.out.println("AbstractUnArchiver::unZipIt throws 
IOexception LINE 415 message="+ex.getMessage());
       //ex.printStackTrace();
    }
   } //end unZipIt

    protected boolean include( final InputStream inputStream, final String name 
)
        throws ArchiveFilterException
    {
        return filterSupport == null || filterSupport.include( inputStream, 
name );
    }

    protected void validate( final String path, final File outputDirectory )
    {
                System.out.println("AbstractUnArchiver::validate path="+path);
                System.out.println("AbstractUnArchiver::validate 
outputDirectory="+outputDirectory);
    }

    protected void validate()
        throws ArchiverException
    {
                System.out.println("AbstractUnArchiver::validate() 
sourceFile="+sourceFile);
        if ( sourceFile == null )
        {
            throw new ArchiverException( "The source file isn't defined." );
        }

        if ( sourceFile.isDirectory() )
        {
            throw new ArchiverException( "The source must not be a directory." 
);
        }

        if ( !sourceFile.exists() )
        {
            throw new ArchiverException( "The source file " + sourceFile + " 
doesn't exist." );
        }
                System.out.println("AbstractUnArchiver::validate 
destDirectory="+destDirectory);
                System.out.println("AbstractUnArchiver::validate destFile = 
"+destFile);
        if ( destDirectory == null) // && destFile == null )
        {
            throw new ArchiverException( "The destination isn't defined." );
        }

        if ( destDirectory != null) // && destFile != null )
        {
            throw new ArchiverException( "You must choose between a destination 
directory and a destination file." );
        }

        if ( destDirectory != null) // && !destDirectory.isDirectory() )
        {
            destFile = destDirectory;
            destDirectory = null;
        }

        if ( destFile != null) // && destFile.isDirectory() )
        {
            destDirectory = destFile;
            destFile = null;
        }
        System.out.println("AbstractUnArchiver::validate exitting");
    }

    public void setFileSelectors( final FileSelector[] fileSelectors )
    {
        this.fileSelectors = fileSelectors;
    }

    public FileSelector[] getFileSelectors()
    {
        return fileSelectors;
    }

    protected boolean isSelected( final String fileName, final PlexusIoResource 
fileInfo )
        throws ArchiverException
    {
        if ( fileSelectors != null )
        {
            for ( int i = 0; i < fileSelectors.length; i++ )
            {
                try
                {
                    if ( !fileSelectors[i].isSelected( fileInfo ) )
                    {
                        return false;
                    }
                }
                catch ( final IOException e )
                {
                    throw new ArchiverException( "Failed to check, whether " + 
fileInfo.getName() + " is selected: "
                                    + e.getMessage(), e );
                }
            }
        }
        return true;
    }

    protected void execute() throws ArchiverException
    {
                System.out.println("AbstractUnArchiver::execute() LINE 270 
throws ArchiverException doesnt do anything at the moment..exitting");
        }

    protected void execute( String path, File outputDirectory )
        throws ArchiverException
    {
                System.out.println("AbstractUnArchiver::execute(path, 
outputDirectory) LINE 276 path="+path);
                try
                {
                System.out.println("AbstractUnArchiver::execute(path, 
outputDirectory) LINE 279 outputDirectory="+outputDirectory.getCanonicalPath());
            }
            catch(java.io.IOException ioe)
            {
                        System.out.println("AbstractUnArchiver::execute(path, 
outputDirectory) LINE 283 throws IOException mesage="+ioe.getMessage());
                }
   }

    /**
     * @since 1.1
     */
    public boolean isUseJvmChmod()
    {
        return useJvmChmod;
    }

    /**
     * <b>jvm chmod won't set group level permissions !</b>
     * @since 1.1
     */
    public void setUseJvmChmod( final boolean useJvmChmod )
    {
        this.useJvmChmod = useJvmChmod;
    }

    /**
     * @since 1.1
     */
    public boolean isIgnorePermissions()
    {
        return ignorePermissions;
    }

    /**
     * @since 1.1
     */
    public void setIgnorePermissions( final boolean ignorePermissions )
    {
        this.ignorePermissions = ignorePermissions;
    }

}


            getLogger().debug( "EarArchiver::execute LINE 234 expand complete" 
);
        }
        catch ( final IOException ioe )
        {
                        System.out.println("EarArchiver::execute LINE 238 Error 
while expanding " + getSourceFile().getAbsolutePath() +" 
message="+ioe.getMessage() );
            throw new ArchiverException( "EarArchiver::execute LINE 238 Error 
while expanding " + getSourceFile().getAbsolutePath(), ioe );
        }
        finally
        {
            if ( zf != null )
            {
                try
                {
                                        getLogger().debug("EarArchiver::execute 
LINE 246 before zf.close() where zf="+zf);
                    zf.close();
                }
                catch ( final IOException e )
                {
                    getLogger().debug("EarArchiver::execute LINE 252 throws 
IOException message="+e.getMessage());
                }
            }
        }
    }

> No unpack capability for ear
> ----------------------------
>
>                 Key: MEAR-187
>                 URL: https://jira.codehaus.org/browse/MEAR-187
>             Project: Maven Ear Plugin
>          Issue Type: Bug
>    Affects Versions: 2.9
>         Environment: maven 3.0.2
> jdk 1.7.0.45
>            Reporter: Martin Gainty
>
> EarModuleFactory.standardArtifactTypes.contains( type ) ) type=ear
> AbstractEarTest::createArtifactsTest ear.execute() has thrown 
> MojoExecutionException message=Invalid type [ear] supported types are [jar, 
> ejb, ejb3, par, ejb-client, app-client, rar, war, sar, wsr, har]
> Therefore the ear type is NEVER unpacked/unzipped (and consequently 
> repacked/zipped) for type ear
> Whatever was in the previous ear remains (with no ability to upgrade versions)
> Originally reported by Martin Holler
> Martin



--
This message was sent by Atlassian JIRA
(v6.1.6#6162)

Reply via email to