View Javadoc

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