View Javadoc
1   package org.apache.maven.plugins.ear;
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 java.io.IOException;
23  import java.io.Writer;
24  
25  import org.apache.maven.plugins.ear.util.JavaEEVersion;
26  import org.codehaus.plexus.util.xml.XMLWriter;
27  
28  /**
29   * An <code>XmlWriter</code> based implementation used to generate an {@code application.xml} file.
30   * 
31   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
32   */
33  final class ApplicationXmlWriter
34      extends AbstractXmlWriter
35  {
36      //@formatter:off
37      public static final String DOCTYPE_1_3 = "application PUBLIC\n" 
38              + "\t\"-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN\"\n"
39              + "\t\"http://java.sun.com/dtd/application_1_3.dtd\"";
40      //@formatter:on
41  
42      private static final String APPLICATION_ELEMENT = "application";
43  
44      private final JavaEEVersion version;
45  
46      private final Boolean generateModuleId;
47  
48      ApplicationXmlWriter( JavaEEVersion version, String encoding, Boolean generateModuleId )
49      {
50          super( encoding );
51          this.version = version;
52          this.generateModuleId = generateModuleId;
53      }
54  
55      void write( ApplicationXmlWriterContext context )
56          throws EarPluginException
57      {
58          try ( Writer w = initializeWriter( context.getDestinationFile() ) )
59          {
60              XMLWriter writer = null;
61              if ( JavaEEVersion.ONE_DOT_THREE.eq( version ) )
62              {
63                  writer = initializeRootElementOneDotThree( w );
64              }
65              else if ( JavaEEVersion.ONE_DOT_FOUR.eq( version ) )
66              {
67                  writer = initializeRootElementOneDotFour( w );
68              }
69              else if ( JavaEEVersion.FIVE.eq( version ) )
70              {
71                  writer = initializeRootElementFive( w );
72              }
73              else if ( JavaEEVersion.SIX.eq( version ) )
74              {
75                  writer = initializeRootElementSix( w );
76              }
77              else if ( JavaEEVersion.SEVEN.eq( version ) )
78              {
79                  writer = initializeRootElementSeven( w );
80              }
81              else if ( JavaEEVersion.EIGHT.eq( version ) )
82              {
83                  writer = initializeRootElementEight( w );
84              }
85      
86              // writer is still on root element, so we can still add this attribute
87              if ( context.getApplicationId() != null )
88              {
89                  writer.addAttribute( "id", context.getApplicationId() );
90              }
91      
92              // As from JavaEE6
93              if ( version.ge( JavaEEVersion.SIX ) )
94              {
95                  writeApplicationName( context.getApplicationName(), writer );
96              }
97      
98              // IMPORTANT: the order of the description and display-name elements was
99              // reversed between J2EE 1.3 and J2EE 1.4.
100             if ( version.eq( JavaEEVersion.ONE_DOT_THREE ) )
101             {
102                 writeDisplayName( context.getDisplayName(), writer );
103                 writeDescription( context.getDescription(), writer );
104             }
105             else
106             {
107                 writeDescription( context.getDescription(), writer );
108                 writeDisplayName( context.getDisplayName(), writer );
109             }
110     
111             // As from JavaEE6
112             if ( version.ge( JavaEEVersion.SIX ) )
113             {
114                 writeInitializeInOrder( context.getInitializeInOrder(), writer );
115             }
116     
117             // Do not change this unless you really know what you're doing :)
118             for ( EarModule module : context.getEarModules() )
119             {
120                 module.appendModule( writer, version.getVersion(), generateModuleId );
121             }
122     
123             for ( SecurityRole securityRole : context.getSecurityRoles() )
124             {
125                 securityRole.appendSecurityRole( writer );
126             }
127     
128             if ( version.ge( JavaEEVersion.FIVE ) )
129             {
130                 writeLibraryDirectory( context.getLibraryDirectory(), writer );
131             }
132     
133             if ( version.ge( JavaEEVersion.SIX ) )
134             {
135                 for ( EnvEntry envEntry : context.getEnvEntries() )
136                 {
137                     envEntry.appendEnvEntry( writer );
138                 }
139                 for ( EjbRef ejbEntry : context.getEjbEntries() )
140                 {
141                     ejbEntry.appendEjbRefEntry( writer );
142                 }
143                 for ( ResourceRef resourceEntry : context.getResourceRefs() )
144                 {
145                     resourceEntry.appendResourceRefEntry( writer );
146                 }
147             }
148     
149             writer.endElement();
150         }
151         catch ( IOException ex )
152         {
153             // ignore
154         }
155     }
156 
157     private void writeApplicationName( String applicationName, XMLWriter writer )
158     {
159         if ( applicationName != null )
160         {
161             writer.startElement( "application-name" );
162             writer.writeText( applicationName );
163             writer.endElement();
164         }
165     }
166 
167     private void writeDescription( String description, XMLWriter writer )
168     {
169         if ( description != null )
170         {
171             writer.startElement( "description" );
172             writer.writeText( description );
173             writer.endElement();
174         }
175     }
176 
177     private void writeDisplayName( String displayName, XMLWriter writer )
178     {
179         if ( displayName != null )
180         {
181             writer.startElement( "display-name" );
182             writer.writeText( displayName );
183             writer.endElement();
184         }
185     }
186 
187     private void writeInitializeInOrder( Boolean initializeInOrder, XMLWriter writer )
188     {
189         if ( initializeInOrder != null )
190         {
191             writer.startElement( "initialize-in-order" );
192             writer.writeText( initializeInOrder.toString() );
193             writer.endElement();
194         }
195     }
196 
197     private void writeLibraryDirectory( String libraryDirectory, XMLWriter writer )
198     {
199         if ( libraryDirectory != null )
200         {
201             writer.startElement( "library-directory" );
202             writer.writeText( libraryDirectory );
203             writer.endElement();
204         }
205     }
206 
207     private XMLWriter initializeRootElementOneDotThree( Writer w )
208     {
209         XMLWriter writer = initializeXmlWriter( w, DOCTYPE_1_3 );
210         writer.startElement( APPLICATION_ELEMENT );
211         return writer;
212     }
213 
214     private XMLWriter initializeRootElementOneDotFour( Writer w )
215     {
216         XMLWriter writer = initializeXmlWriter( w, null );
217         writer.startElement( APPLICATION_ELEMENT );
218         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/j2ee" );
219         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
220         writer.addAttribute( "xsi:schemaLocation",
221                              "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd" );
222         writer.addAttribute( "version", "1.4" );
223         return writer;
224     }
225 
226     private XMLWriter initializeRootElementFive( Writer w )
227     {
228         XMLWriter writer = initializeXmlWriter( w, null );
229         writer.startElement( APPLICATION_ELEMENT );
230         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
231         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
232         writer.addAttribute( "xsi:schemaLocation",
233                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" );
234         writer.addAttribute( "version", "5" );
235         return writer;
236     }
237 
238     private XMLWriter initializeRootElementSix( Writer w )
239     {
240         XMLWriter writer = initializeXmlWriter( w, null );
241         writer.startElement( APPLICATION_ELEMENT );
242         writer.addAttribute( "xmlns", "http://java.sun.com/xml/ns/javaee" );
243         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
244         writer.addAttribute( "xsi:schemaLocation",
245                              "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" );
246         writer.addAttribute( "version", "6" );
247         return writer;
248     }
249 
250     private XMLWriter initializeRootElementSeven( Writer w )
251     {
252         XMLWriter writer = initializeXmlWriter( w, null );
253         writer.startElement( APPLICATION_ELEMENT );
254         writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
255         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
256         writer.addAttribute( "xsi:schemaLocation",
257             "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" );
258         writer.addAttribute( "version", "7" );
259         return writer;
260     }
261 
262     private XMLWriter initializeRootElementEight( Writer w )
263     {
264         XMLWriter writer = initializeXmlWriter( w, null );
265         writer.startElement( APPLICATION_ELEMENT );
266         writer.addAttribute( "xmlns", "http://xmlns.jcp.org/xml/ns/javaee" );
267         writer.addAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );
268         writer.addAttribute( "xsi:schemaLocation",
269             "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_8.xsd" );
270         writer.addAttribute( "version", "8" );
271         return writer;
272     }
273 }