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.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   * @version $Id: JbossAppXmlWriter.java 1636449 2014-11-03 21:27:36Z khmarbaise $
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      JbossAppXmlWriter( String encoding )
53      {
54          super( encoding );
55      }
56  
57      public void write( File destinationFile, JbossConfiguration jbossConfiguration, List<EarModule> earModules )
58          throws EarPluginException
59      {
60          final Writer w = initializeWriter( destinationFile );
61  
62          XMLWriter writer;
63          if ( jbossConfiguration.isJbossThreeDotTwo() )
64          {
65              writer = initializeXmlWriter( w, DOCTYPE_3_2 );
66          }
67          else if ( jbossConfiguration.isJbossFour() )
68          {
69              writer = initializeXmlWriter( w, DOCTYPE_4 );
70          }
71          else if ( jbossConfiguration.isJbossFourDotTwo() )
72          {
73              writer = initializeXmlWriter( w, DOCTYPE_4_2 );
74          }
75          else
76          {
77              writer = initializeXmlWriter( w, DOCTYPE_5 );
78          }
79          writer.startElement( JBOSS_APP_ELEMENT );
80  
81          // Make sure to write the things in the right order so that the DTD validates
82  
83          // module-order (only available as from 4.2)
84          if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getModuleOrder() != null )
85          {
86              writer.startElement( JbossConfiguration.MODULE_ORDER );
87              writer.writeText( jbossConfiguration.getModuleOrder() );
88              writer.endElement();
89          }
90  
91          // If JBoss 4, write the jboss4 specific stuff
92          if ( jbossConfiguration.isJbossFourOrHigher() )
93          {
94              if ( jbossConfiguration.getSecurityDomain() != null )
95              {
96                  writer.startElement( JbossConfiguration.SECURITY_DOMAIN );
97                  writer.writeText( jbossConfiguration.getSecurityDomain() );
98                  writer.endElement();
99              }
100             if ( jbossConfiguration.getUnauthenticatedPrincipal() != null )
101             {
102                 writer.startElement( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL );
103                 writer.writeText( jbossConfiguration.getUnauthenticatedPrincipal() );
104                 writer.endElement();
105             }
106         }
107 
108         // classloader repository
109         // CHECKSTYLE_OFF: LineLength
110         if ( jbossConfiguration.getLoaderRepository() != null || jbossConfiguration.getLoaderRepositoryConfig() != null )
111         // CHECKSTYLE_ON: LineLength
112         {
113             writer.startElement( JbossConfiguration.LOADER_REPOSITORY );
114 
115             // classloader repository class
116             if ( jbossConfiguration.getLoaderRepositoryClass() != null )
117             {
118                 writer.addAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE,
119                                      jbossConfiguration.getLoaderRepositoryClass() );
120             }
121 
122             // we don't need to write any text if only the loader repo configuration is changed
123             if ( jbossConfiguration.getLoaderRepository() != null )
124             {
125                 writer.writeText( jbossConfiguration.getLoaderRepository() );
126             }
127 
128             // classloader configuration
129             if ( jbossConfiguration.getLoaderRepositoryConfig() != null )
130             {
131                 writer.startElement( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
132 
133                 // classloader configuration parser
134                 if ( jbossConfiguration.getConfigParserClass() != null )
135                 {
136                     writer.addAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE,
137                                          jbossConfiguration.getConfigParserClass() );
138                 }
139                 writer.writeText( jbossConfiguration.getLoaderRepositoryConfig() );
140                 writer.endElement();
141             }
142 
143             writer.endElement();
144         }
145 
146         // jmx name
147         if ( jbossConfiguration.getJmxName() != null )
148         {
149             writer.startElement( JbossConfiguration.JMX_NAME );
150             writer.writeText( jbossConfiguration.getJmxName() );
151             writer.endElement();
152         }
153 
154         // library-directory (only available as from 4.2)
155         if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getLibraryDirectory() != null )
156         {
157             writer.startElement( JbossConfiguration.LIBRARY_DIRECTORY );
158             writer.writeText( jbossConfiguration.getLibraryDirectory() );
159             writer.endElement();
160         }
161 
162         // Modules
163 
164         List<String> dataSources = jbossConfiguration.getDataSources();
165         // Write out data source modules first
166         if ( dataSources != null )
167         {
168             for ( String dsPath : dataSources )
169             {
170                 writer.startElement( MODULE_ELEMENT );
171                 writer.startElement( SERVICE_ELEMENT );
172                 writer.writeText( dsPath );
173                 writer.endElement();
174                 writer.endElement();
175             }
176         }
177 
178         // Write the JBoss specific modules
179         for ( EarModule earModule : earModules )
180         {
181             if ( JbossEarModule.class.isInstance( earModule ) )
182             {
183                 JbossEarModule jbossEarModule = (JbossEarModule) earModule;
184                 jbossEarModule.appendJbossModule( writer, jbossConfiguration.getVersion() );
185             }
186         }
187         writer.endElement();
188 
189         close( w );
190     }
191 }