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