View Javadoc
1   /*
2    * Copyright  2001-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.codehaus.plexus.archiver.ear;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import org.codehaus.plexus.archiver.ArchiveEntry;
22  import org.codehaus.plexus.archiver.ArchiverException;
23  import org.codehaus.plexus.archiver.jar.JarArchiver;
24  import org.codehaus.plexus.archiver.util.ResourceUtils;
25  import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
26  
27  /**
28   * Creates a EAR archive. Based on WAR task
29   */
30  public class EarArchiver
31      extends JarArchiver
32  {
33  
34      private File deploymentDescriptor;
35  
36      private boolean descriptorAdded;
37  
38      /**
39       * Create an Ear.
40       */
41      public EarArchiver()
42      {
43          super();
44          archiveType = "ear";
45      }
46  
47      /**
48       * File to incorporate as application.xml.
49       */
50      public void setAppxml( File descr )
51          throws ArchiverException
52      {
53          deploymentDescriptor = descr;
54          if ( !deploymentDescriptor.exists() )
55          {
56              throw new ArchiverException( "Deployment descriptor: " + deploymentDescriptor + " does not exist." );
57          }
58  
59          addFile( descr, "META-INF/application.xml" );
60      }
61  
62      /**
63       * Adds archive.
64       */
65      public void addArchive( File fileName )
66          throws ArchiverException
67      {
68          addDirectory( fileName.getParentFile(), "/",
69                        new String[]
70                        {
71                            fileName.getName()
72                        }, null );
73  
74      }
75  
76      /**
77       * Adds archives.
78       */
79      public void addArchives( File directoryName, String[] includes, String[] excludes )
80          throws ArchiverException
81      {
82          addDirectory( directoryName, "/", includes, excludes );
83      }
84  
85      @Override
86      protected void initZipOutputStream( ConcurrentJarCreator zOut )
87          throws ArchiverException, IOException
88      {
89          // If no webxml file is specified, it's an error.
90          if ( deploymentDescriptor == null && !isInUpdateMode() )
91          {
92              throw new ArchiverException( "appxml attribute is required" );
93          }
94  
95          super.initZipOutputStream( zOut );
96      }
97  
98      /**
99       * Overridden from ZipArchiver class to deal with application.xml
100      */
101     protected void zipFile( ArchiveEntry entry, ConcurrentJarCreator zOut, String vPath, int mode )
102         throws IOException, ArchiverException
103     {
104         // If the file being added is META-INF/application.xml, we
105         // warn if it's not the one specified in the "appxml"
106         // attribute - or if it's being added twice, meaning the same
107         // file is specified by the "appxml" attribute and in a
108         // <fileset> element.
109         if ( vPath.equalsIgnoreCase( "META-INF/application.xml" ) )
110         {
111             if ( deploymentDescriptor == null
112                      || !ResourceUtils.isCanonicalizedSame( entry.getResource(), deploymentDescriptor )
113                      || descriptorAdded )
114             {
115                 getLogger().warn( "Warning: selected " + archiveType
116                                       + " files include a META-INF/application.xml which will be ignored "
117                                       + "(please use appxml attribute to " + archiveType + " task)" );
118             }
119             else
120             {
121                 super.zipFile( entry, zOut, vPath );
122                 descriptorAdded = true;
123             }
124         }
125         else
126         {
127             super.zipFile( entry, zOut, vPath );
128         }
129     }
130 
131     /**
132      * Make sure we don't think we already have a application.xml next
133      * time this task gets executed.
134      */
135     @Override
136     protected void cleanUp()
137         throws IOException
138     {
139         descriptorAdded = false;
140         super.cleanUp();
141     }
142 
143 }