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 746726 2009-02-22 15:15:41Z 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          // If JBoss 4.2 or 5.0, write the JBoss 4.2 and JBoss 5.0-compatible stuff
84          if ( jbossConfiguration.isJbossFourDotTwo() || jbossConfiguration.isJbossFive() )
85          {
86              // library-directory
87              if ( jbossConfiguration.getLibraryDirectory() != null )
88              {
89                  writer.startElement( JbossConfiguration.LIBRARY_DIRECTORY );
90                  writer.writeText ( jbossConfiguration.getLibraryDirectory() );
91                  writer.endElement();
92              }
93          }
94  
95          // If JBoss 4.2, write the jboss4.2 specific stuff
96          if ( jbossConfiguration.isJbossFourDotTwo() )
97          {
98              // module-order (only available in 4.2 and 4.3)
99              if ( jbossConfiguration.getModuleOrder() != null )
100             {
101                 writer.startElement( JbossConfiguration.MODULE_ORDER );
102                 writer.writeText( jbossConfiguration.getModuleOrder() );
103                 writer.endElement();
104             }
105         }
106 
107         // If JBoss 4, write the jboss4 specific stuff
108         if ( jbossConfiguration.isJbossFour() || jbossConfiguration.isJbossFourDotTwo() )
109         {
110             if ( jbossConfiguration.getSecurityDomain() != null )
111             {
112                 writer.startElement( JbossConfiguration.SECURITY_DOMAIN );
113                 writer.writeText( jbossConfiguration.getSecurityDomain() );
114                 writer.endElement();
115             }
116             if ( jbossConfiguration.getUnauthenticatedPrincipal() != null )
117             {
118                 writer.startElement( JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL );
119                 writer.writeText( jbossConfiguration.getUnauthenticatedPrincipal() );
120                 writer.endElement();
121             }
122         }
123 
124         // classloader repository
125         if ( jbossConfiguration.getLoaderRepository() != null || jbossConfiguration.getLoaderRepositoryConfig() != null)
126         {
127             writer.startElement( JbossConfiguration.LOADER_REPOSITORY );
128 
129             // classloader repository class
130             if ( jbossConfiguration.getLoaderRepositoryClass() != null)
131             {
132                 writer.addAttribute( JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE,
133                                      jbossConfiguration.getLoaderRepositoryClass() );
134             }
135 
136             // we don't need to write any text if only the loader repo configuration is changed
137             if ( jbossConfiguration.getLoaderRepository() != null )
138             {
139                 writer.writeText( jbossConfiguration.getLoaderRepository() );
140             }
141 
142             // classloader configuration
143             if ( jbossConfiguration.getLoaderRepositoryConfig() != null )
144             {
145                 writer.startElement( JbossConfiguration.LOADER_REPOSITORY_CONFIG );
146 
147                 // classloader configuration parser
148                 if ( jbossConfiguration.getConfigParserClass() != null)
149                 {
150                     writer.addAttribute( JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE,
151                                          jbossConfiguration.getConfigParserClass() );
152                 }
153                 writer.writeText( jbossConfiguration.getLoaderRepositoryConfig() );
154                 writer.endElement();
155             }
156             
157             writer.endElement();
158         }
159 
160         // jmx name
161         if ( jbossConfiguration.getJmxName() != null )
162         {
163             writer.startElement( JbossConfiguration.JMX_NAME );
164             writer.writeText( jbossConfiguration.getJmxName() );
165             writer.endElement();
166         }
167 
168         List dataSources = jbossConfiguration.getDataSources();
169         // Write out data source modules first
170         if ( dataSources != null )
171         {
172             final Iterator it = dataSources.iterator();
173             while ( it.hasNext() )
174             {
175                 String dsPath = (String) it.next();
176                 writer.startElement( MODULE_ELEMENT );
177                 writer.startElement( SERVICE_ELEMENT );
178                 writer.writeText( dsPath );
179                 writer.endElement();
180                 writer.endElement();
181             }
182         }
183 
184         // Write the JBoss specific modules
185         final Iterator it = earModules.iterator();
186         while ( it.hasNext() )
187         {
188             EarModule earModule = (EarModule) it.next();
189             if ( JbossEarModule.class.isInstance( earModule ) )
190             {
191                 JbossEarModule jbossEarModule = (JbossEarModule) earModule;
192                 jbossEarModule.appendJbossModule( writer, jbossConfiguration.getVersion() );
193             }
194         }
195         writer.endElement();
196 
197         close( w );
198     }
199 }