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 javax.inject.Named;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.codehaus.plexus.archiver.ArchiveEntry;
25  import org.codehaus.plexus.archiver.ArchiverException;
26  import org.codehaus.plexus.archiver.jar.JarArchiver;
27  import org.codehaus.plexus.archiver.util.ResourceUtils;
28  import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
29  
30  /**
31   * Creates a EAR archive. Based on WAR task
32   */
33  @Named("ear")
34  public class EarArchiver extends JarArchiver {
35  
36      private File deploymentDescriptor;
37  
38      private boolean descriptorAdded;
39  
40      /**
41       * Create an Ear.
42       */
43      public EarArchiver() {
44          super();
45          archiveType = "ear";
46      }
47  
48      /**
49       * File to incorporate as application.xml.
50       */
51      public void setAppxml(File descr) throws ArchiverException {
52          deploymentDescriptor = descr;
53          if (!deploymentDescriptor.exists()) {
54              throw new ArchiverException("Deployment descriptor: " + deploymentDescriptor + " does not exist.");
55          }
56  
57          addFile(descr, "META-INF/application.xml");
58      }
59  
60      /**
61       * Adds archive.
62       */
63      public void addArchive(File fileName) throws ArchiverException {
64          addDirectory(fileName.getParentFile(), "/", new String[] {fileName.getName()}, null);
65      }
66  
67      /**
68       * Adds archives.
69       */
70      public void addArchives(File directoryName, String[] includes, String[] excludes) throws ArchiverException {
71          addDirectory(directoryName, "/", includes, excludes);
72      }
73  
74      @Override
75      protected void initZipOutputStream(ConcurrentJarCreator zOut) throws ArchiverException, IOException {
76          // If no webxml file is specified, it's an error.
77          if (deploymentDescriptor == null && !isInUpdateMode()) {
78              throw new ArchiverException("appxml attribute is required");
79          }
80  
81          super.initZipOutputStream(zOut);
82      }
83  
84      /**
85       * Overridden from ZipArchiver class to deal with application.xml
86       */
87      protected void zipFile(ArchiveEntry entry, ConcurrentJarCreator zOut, String vPath, int mode)
88              throws IOException, ArchiverException {
89          // If the file being added is META-INF/application.xml, we
90          // warn if it's not the one specified in the "appxml"
91          // attribute - or if it's being added twice, meaning the same
92          // file is specified by the "appxml" attribute and in a
93          // <fileset> element.
94          if (vPath.equalsIgnoreCase("META-INF/application.xml")) {
95              if (deploymentDescriptor == null
96                      || !ResourceUtils.isCanonicalizedSame(entry.getResource(), deploymentDescriptor)
97                      || descriptorAdded) {
98                  getLogger()
99                          .warn("Warning: selected " + archiveType
100                                 + " files include a META-INF/application.xml which will be ignored "
101                                 + "(please use appxml attribute to " + archiveType + " task)");
102             } else {
103                 super.zipFile(entry, zOut, vPath);
104                 descriptorAdded = true;
105             }
106         } else {
107             super.zipFile(entry, zOut, vPath);
108         }
109     }
110 
111     /**
112      * Make sure we don't think we already have a application.xml next
113      * time this task gets executed.
114      */
115     @Override
116     protected void cleanUp() throws IOException {
117         descriptorAdded = false;
118         super.cleanUp();
119     }
120 }