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
30   * <tt>jboss-app.xml</tt> file
31   *
32   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
33   * @version $Id: JbossAppXmlWriter.java 1294559 2012-02-28 10:37:58Z snicoll $
34   */
35  final class JbossAppXmlWriter
36      extends AbstractXmlWriter
37  {
38  
39      public static final String DOCTYPE_3_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.3//EN\"\n"
40          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_3_2.dtd\"";
41  
42      public static final String DOCTYPE_4 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.4//EN\"\n"
43          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd\"";
44  
45      public static final String DOCTYPE_4_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 4.2//EN\"\n"
46          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\"";
47  
48      public static final String DOCTYPE_5 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD Java EE Application 5.0//EN\"\n"
49          + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_5_0.dtd\"";
50  
51      private static final String JBOSS_APP_ELEMENT = "jboss-app";
52  
53      JbossAppXmlWriter( String encoding )
54      {
55          super( encoding );
56      }
57  
58      public void write( File destinationFile, JbossConfiguration jbossConfiguration, List<EarModule> earModules )
59          throws EarPluginException
60      {
61          final Writer w = initializeWriter( destinationFile );
62  
63          XMLWriter writer;
64          if ( jbossConfiguration.isJbossThreeDotTwo() )
65          {
66              writer = initializeXmlWriter( w, DOCTYPE_3_2 );
67          }
68          else if ( jbossConfiguration.isJbossFour() )
69          {
70              writer = initializeXmlWriter( w, DOCTYPE_4 );
71          }
72          else if ( jbossConfiguration.isJbossFourDotTwo() )
73          {
74              writer = initializeXmlWriter( w, DOCTYPE_4_2 );
75          }
76          else
77          {
78              writer = initializeXmlWriter( w, DOCTYPE_5 );
79          }
80          writer.startElement( JBOSS_APP_ELEMENT );
81  
82          // Make sure to write the things in the right order so that the DTD validates
83  
84          // module-order (only available as from 4.2)
85          if ( jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getModuleOrder() != null )
86          {
87              writer.startElement( JbossConfiguration.MODULE_ORDER );
88              writer.writeText( jbossConfiguration.getModuleOrder() );
89              writer.endElement();
90          }
91  
92          // If JBoss 4, write the jboss4 specific stuff
93          if ( jbossConfiguration.isJbossFourOrHigher() )
94          {
95              if ( jbossConfiguration.getSecurityDomain() != null )
96              {
97                  writer.startElement( JbossConfiguration.SECURITY_DOMAIN );
98                  writer.writeText( jbossConfiguration.getSecurityDomain() );
99                  writer.endElement();
100             }
101             if ( jbossConfiguration.getUnauthenticatedPrincipal() != null )
102             {
103                 writer.startElement( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL );
104                 writer.writeText( jbossConfiguration.getUnauthenticatedPrincipal() );
105                 writer.endElement();
106             }
107         }
108 
109         // classloader repository
110         if ( jbossConfiguration.getLoaderRepository() != null
111             || jbossConfiguration.getLoaderRepositoryConfig() != null )
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 }