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 1542508 2013-11-16 13:21:35Z rfscholte $
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         if ( jbossConfiguration.getLoaderRepository() != null || jbossConfiguration.getLoaderRepositoryConfig() != null )
110         {
111             writer.startElement( JbossConfiguration.LOADER_REPOSITORY );
112 
113             // classloader repository class
114             if ( jbossConfiguration.getLoaderRepositoryClass() != null )
115             {
116                 writer.addAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE,
117                                      jbossConfiguration.getLoaderRepositoryClass() );
118             }
119 
120             // we don't need to write any text if only the loader repo configuration is changed
121             if ( jbossConfiguration.getLoaderRepository() != null )
122             {
123                 writer.writeText( jbossConfiguration.getLoaderRepository() );
124             }
125 
126             // classloader configuration
127             if ( jbossConfiguration.getLoaderRepositoryConfig() != null )
128             {
129                 writer.startElement( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
130 
131                 // classloader configuration parser
132                 if ( jbossConfiguration.getConfigParserClass() != null )
133                 {
134                     writer.addAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE,
135                                          jbossConfiguration.getConfigParserClass() );
136                 }
137                 writer.writeText( jbossConfiguration.getLoaderRepositoryConfig() );
138                 writer.endElement();
139             }
140 
141             writer.endElement();
142         }
143 
144         // jmx name
145         if ( jbossConfiguration.getJmxName() != null )
146         {
147             writer.startElement( JbossConfiguration.JMX_NAME );
148             writer.writeText( jbossConfiguration.getJmxName() );
149             writer.endElement();
150         }
151 
152         // library-directory (only available as from 4.2)
153         if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getLibraryDirectory() != null )
154         {
155             writer.startElement( JbossConfiguration.LIBRARY_DIRECTORY );
156             writer.writeText( jbossConfiguration.getLibraryDirectory() );
157             writer.endElement();
158         }
159 
160         // Modules
161 
162         List<String> dataSources = jbossConfiguration.getDataSources();
163         // Write out data source modules first
164         if ( dataSources != null )
165         {
166             for ( String dsPath : dataSources )
167             {
168                 writer.startElement( MODULE_ELEMENT );
169                 writer.startElement( SERVICE_ELEMENT );
170                 writer.writeText( dsPath );
171                 writer.endElement();
172                 writer.endElement();
173             }
174         }
175 
176         // Write the JBoss specific modules
177         for ( EarModule earModule : earModules )
178         {
179             if ( JbossEarModule.class.isInstance( earModule ) )
180             {
181                 JbossEarModule jbossEarModule = (JbossEarModule) earModule;
182                 jbossEarModule.appendJbossModule( writer, jbossConfiguration.getVersion() );
183             }
184         }
185         writer.endElement();
186 
187         close( w );
188     }
189 }