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