View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.ear;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Writer;
24  import java.util.List;
25  
26  import org.codehaus.plexus.util.xml.XMLWriter;
27  
28  /**
29   * An {@code XmlWriter} based implementation used to generate a {@code jboss-app.xml} file
30   *
31   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
32   */
33  final class JbossAppXmlWriter extends AbstractXmlWriter {
34  
35      public static final String DOCTYPE_3_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.3//EN\"\n"
36              + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_3_2.dtd\"";
37  
38      public static final String DOCTYPE_4 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 1.4//EN\"\n"
39              + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd\"";
40  
41      public static final String DOCTYPE_4_2 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD J2EE Application 4.2//EN\"\n"
42              + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd\"";
43  
44      public static final String DOCTYPE_5 = "jboss-app PUBLIC\n" + "\t\"-//JBoss//DTD Java EE Application 5.0//EN\"\n"
45              + "\t\"http://www.jboss.org/j2ee/dtd/jboss-app_5_0.dtd\"";
46  
47      private static final String JBOSS_APP_ELEMENT = "jboss-app";
48  
49      private static final String MODULE_ELEMENT = "module";
50  
51      private static final String SERVICE_ELEMENT = "service";
52  
53      JbossAppXmlWriter(String encoding) {
54          super(encoding);
55      }
56  
57      public void write(File destinationFile, JbossConfiguration jbossConfiguration, List<EarModule> earModules)
58              throws EarPluginException {
59          try (Writer w = initializeWriter(destinationFile)) {
60              XMLWriter writer;
61              if (jbossConfiguration.isJbossThreeDotTwo()) {
62                  writer = initializeXmlWriter(w, DOCTYPE_3_2);
63              } else if (jbossConfiguration.isJbossFour()) {
64                  writer = initializeXmlWriter(w, DOCTYPE_4);
65              } else if (jbossConfiguration.isJbossFourDotTwo()) {
66                  writer = initializeXmlWriter(w, DOCTYPE_4_2);
67              } else {
68                  writer = initializeXmlWriter(w, DOCTYPE_5);
69              }
70              writer.startElement(JBOSS_APP_ELEMENT);
71  
72              // Make sure to write the things in the right order so that the DTD validates
73  
74              // module-order (only available as from 4.2)
75              if (jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getModuleOrder() != null) {
76                  writer.startElement(JbossConfiguration.MODULE_ORDER);
77                  writer.writeText(jbossConfiguration.getModuleOrder());
78                  writer.endElement();
79              }
80  
81              // If JBoss 4, write the jboss4 specific stuff
82              if (jbossConfiguration.isJbossFourOrHigher()) {
83                  if (jbossConfiguration.getSecurityDomain() != null) {
84                      writer.startElement(JbossConfiguration.SECURITY_DOMAIN);
85                      writer.writeText(jbossConfiguration.getSecurityDomain());
86                      writer.endElement();
87                  }
88                  if (jbossConfiguration.getUnauthenticatedPrincipal() != null) {
89                      writer.startElement(JbossConfiguration.UNAUHTHENTICTED_PRINCIPAL);
90                      writer.writeText(jbossConfiguration.getUnauthenticatedPrincipal());
91                      writer.endElement();
92                  }
93              }
94  
95              // classloader repository
96              if (jbossConfiguration.getLoaderRepository() != null
97                      || jbossConfiguration.getLoaderRepositoryConfig() != null) {
98                  writer.startElement(JbossConfiguration.LOADER_REPOSITORY);
99  
100                 // classloader repository class
101                 if (jbossConfiguration.getLoaderRepositoryClass() != null) {
102                     writer.addAttribute(
103                             JbossConfiguration.LOADER_REPOSITORY_CLASS_ATTRIBUTE,
104                             jbossConfiguration.getLoaderRepositoryClass());
105                 }
106 
107                 // we don't need to write any text if only the loader repo configuration is changed
108                 if (jbossConfiguration.getLoaderRepository() != null) {
109                     writer.writeText(jbossConfiguration.getLoaderRepository());
110                 }
111 
112                 // classloader configuration
113                 if (jbossConfiguration.getLoaderRepositoryConfig() != null) {
114                     writer.startElement(JbossConfiguration.LOADER_REPOSITORY_CONFIG);
115 
116                     // classloader configuration parser
117                     if (jbossConfiguration.getConfigParserClass() != null) {
118                         writer.addAttribute(
119                                 JbossConfiguration.CONFIG_PARSER_CLASS_ATTRIBUTE,
120                                 jbossConfiguration.getConfigParserClass());
121                     }
122                     writer.writeText(jbossConfiguration.getLoaderRepositoryConfig());
123                     writer.endElement();
124                 }
125 
126                 writer.endElement();
127             }
128 
129             // jmx name
130             if (jbossConfiguration.getJmxName() != null) {
131                 writer.startElement(JbossConfiguration.JMX_NAME);
132                 writer.writeText(jbossConfiguration.getJmxName());
133                 writer.endElement();
134             }
135 
136             // library-directory (only available as from 4.2)
137             if (jbossConfiguration.isJbossFourDotTwoOrHigher() && jbossConfiguration.getLibraryDirectory() != null) {
138                 writer.startElement(JbossConfiguration.LIBRARY_DIRECTORY);
139                 writer.writeText(jbossConfiguration.getLibraryDirectory());
140                 writer.endElement();
141             }
142 
143             // Modules
144 
145             List<String> dataSources = jbossConfiguration.getDataSources();
146             // Write out data source modules first
147             if (dataSources != null) {
148                 for (String dsPath : dataSources) {
149                     writer.startElement(MODULE_ELEMENT);
150                     writer.startElement(SERVICE_ELEMENT);
151                     writer.writeText(dsPath);
152                     writer.endElement();
153                     writer.endElement();
154                 }
155             }
156 
157             // Write the JBoss specific modules
158             for (EarModule earModule : earModules) {
159                 if (JbossEarModule.class.isInstance(earModule)) {
160                     JbossEarModule jbossEarModule = (JbossEarModule) earModule;
161                     jbossEarModule.appendJbossModule(writer, jbossConfiguration.getVersion());
162                 }
163             }
164             writer.endElement();
165 
166         } catch (IOException ex) {
167             throw new EarPluginException(ex);
168         }
169     }
170 }