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.File;
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.List;
26  
27  import org.codehaus.plexus.util.xml.XMLWriter;
28  
29  /**
30   * An {@code XmlWriter} based implementation used to generate a {@code jboss-app.xml} file
31   * 
32   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
33   */
34  final class JbossAppXmlWriter
35      extends AbstractXmlWriter
36  {
37  
38      public static final String DOCTYPE_3_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.3//EN\"\n"
39          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_3_2.dtd\"";
40  
41      public static final String DOCTYPE_4 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.4//EN\"\n"
42          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd\"";
43  
44      public static final String DOCTYPE_4_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 4.2//EN\"\n"
45          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\"";
46  
47      public static final String DOCTYPE_5 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD Java EE Application 5.0//EN\"\n"
48          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_5_0.dtd\"";
49  
50      private static final String JBOSS_APP_ELEMENT = "jboss-app";
51      
52      private static final String MODULE_ELEMENT = "module";
53  
54      private static final String SERVICE_ELEMENT = "service";
55  
56      JbossAppXmlWriter( String encoding )
57      {
58          super( encoding );
59      }
60  
61      public void write( File destinationFile, JbossConfiguration jbossConfiguration, List<EarModule> earModules )
62          throws EarPluginException
63      {
64          try ( Writer w = initializeWriter( destinationFile ) )
65          {
66              XMLWriter writer;
67              if ( jbossConfiguration.isJbossThreeDotTwo() )
68              {
69                  writer = initializeXmlWriter( w, DOCTYPE_3_2 );
70              }
71              else if ( jbossConfiguration.isJbossFour() )
72              {
73                  writer = initializeXmlWriter( w, DOCTYPE_4 );
74              }
75              else if ( jbossConfiguration.isJbossFourDotTwo() )
76              {
77                  writer = initializeXmlWriter( w, DOCTYPE_4_2 );
78              }
79              else
80              {
81                  writer = initializeXmlWriter( w, DOCTYPE_5 );
82              }
83              writer.startElement( JBOSS_APP_ELEMENT );
84      
85              // Make sure to write the things in the right order so that the DTD validates
86      
87              // module-order (only available as from 4.2)
88              if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getModuleOrder() != null )
89              {
90                  writer.startElement( JbossConfiguration.MODULE_ORDER );
91                  writer.writeText( jbossConfiguration.getModuleOrder() );
92                  writer.endElement();
93              }
94      
95              // If JBoss 4, write the jboss4 specific stuff
96              if ( jbossConfiguration.isJbossFourOrHigher() )
97              {
98                  if ( jbossConfiguration.getSecurityDomain() != null )
99                  {
100                     writer.startElement( JbossConfiguration.SECURITY_DOMAIN );
101                     writer.writeText( jbossConfiguration.getSecurityDomain() );
102                     writer.endElement();
103                 }
104                 if ( jbossConfiguration.getUnauthenticatedPrincipal() != null )
105                 {
106                     writer.startElement( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL );
107                     writer.writeText( jbossConfiguration.getUnauthenticatedPrincipal() );
108                     writer.endElement();
109                 }
110             }
111     
112             // classloader repository
113             if ( jbossConfiguration.getLoaderRepository() != null
114                 || jbossConfiguration.getLoaderRepositoryConfig() != null )
115             {
116                 writer.startElement( JbossConfiguration.LOADER_REPOSITORY );
117     
118                 // classloader repository class
119                 if ( jbossConfiguration.getLoaderRepositoryClass() != null )
120                 {
121                     writer.addAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE,
122                                          jbossConfiguration.getLoaderRepositoryClass() );
123                 }
124     
125                 // we don't need to write any text if only the loader repo configuration is changed
126                 if ( jbossConfiguration.getLoaderRepository() != null )
127                 {
128                     writer.writeText( jbossConfiguration.getLoaderRepository() );
129                 }
130     
131                 // classloader configuration
132                 if ( jbossConfiguration.getLoaderRepositoryConfig() != null )
133                 {
134                     writer.startElement( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
135     
136                     // classloader configuration parser
137                     if ( jbossConfiguration.getConfigParserClass() != null )
138                     {
139                         writer.addAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE,
140                                              jbossConfiguration.getConfigParserClass() );
141                     }
142                     writer.writeText( jbossConfiguration.getLoaderRepositoryConfig() );
143                     writer.endElement();
144                 }
145     
146                 writer.endElement();
147             }
148     
149             // jmx name
150             if ( jbossConfiguration.getJmxName() != null )
151             {
152                 writer.startElement( JbossConfiguration.JMX_NAME );
153                 writer.writeText( jbossConfiguration.getJmxName() );
154                 writer.endElement();
155             }
156     
157             // library-directory (only available as from 4.2)
158             if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getLibraryDirectory() != null )
159             {
160                 writer.startElement( JbossConfiguration.LIBRARY_DIRECTORY );
161                 writer.writeText( jbossConfiguration.getLibraryDirectory() );
162                 writer.endElement();
163             }
164     
165             // Modules
166     
167             List<String> dataSources = jbossConfiguration.getDataSources();
168             // Write out data source modules first
169             if ( dataSources != null )
170             {
171                 for ( String dsPath : dataSources )
172                 {
173                     writer.startElement( MODULE_ELEMENT );
174                     writer.startElement( SERVICE_ELEMENT );
175                     writer.writeText( dsPath );
176                     writer.endElement();
177                     writer.endElement();
178                 }
179             }
180     
181             // Write the JBoss specific modules
182             for ( EarModule earModule : earModules )
183             {
184                 if ( JbossEarModule.class.isInstance( earModule ) )
185                 {
186                     JbossEarModule jbossEarModule = (JbossEarModule) earModule;
187                     jbossEarModule.appendJbossModule( writer, jbossConfiguration.getVersion() );
188                 }
189             }
190             writer.endElement();
191 
192         }
193         catch ( IOException ex )
194         {
195             throw new EarPluginException( ex );
196         }
197     }
198 }