View Javadoc
1   package org.apache.maven.plugin.eclipse.writers.rad;
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.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStreamWriter;
26  import java.io.Writer;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.eclipse.Constants;
30  import org.apache.maven.plugin.eclipse.Messages;
31  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
32  import org.apache.maven.plugin.ide.JeeUtils;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
35  import org.codehaus.plexus.util.xml.XMLWriter;
36  
37  /**
38   * Creates the .j2ee file for RAD6 for now write hardcoded: EJB version 2.1 WAR version 2.4 EAR version 1.4 future
39   * releases could make these varriable.
40   * 
41   * @author <a href="mailto:nir@cfc.at">Richard van Nieuwenhoven</a>
42   */
43  public class RadJ2EEWriter
44      extends AbstractEclipseWriter
45  {
46  
47      private static final String J2EE_FILENAME = ".j2ee";
48  
49      private static final String J2EE_J2EESETTINGS = "j2eesettings";
50  
51      private static final String J2EE_MODULEVERSION = "moduleversion";
52  
53      private static final String J2EE_VERSION = "version";
54  
55      /**
56       * write the .j2ee file to the project root directory.
57       * 
58       * @param sourceDirs all eclipse source directorys
59       * @param localRepository the local reposetory
60       * @param buildOutputDirectory build output directory (target)
61       * @throws MojoExecutionException when writing the config files was not possible
62       */
63      public void write()
64          throws MojoExecutionException
65      {
66          Writer w;
67          String packaging = config.getPackaging();
68  
69          if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging )
70              || Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging )
71              || Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
72          {
73              try
74              {
75                  w =
76                      new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
77                                                                              J2EE_FILENAME ) ), "UTF-8" );
78              }
79              catch ( IOException ex )
80              {
81                  throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex );
82              }
83  
84              XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
85              writeModuleTypeFacetCore( writer, packaging );
86              IOUtil.close( w );
87          }
88      }
89  
90      /**
91       * Writes out the facet info for a faceted-project based on the packaging.
92       * 
93       * @param writer where to write to
94       * @param packaging packaging type
95       */
96      private void writeModuleTypeFacetCore( XMLWriter writer, String packaging )
97      {
98          writer.startElement( J2EE_J2EESETTINGS );
99          writer.addAttribute( J2EE_VERSION, "600" );
100         writer.startElement( J2EE_MODULEVERSION );
101         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) )
102         {
103             // In format X.X
104             String servletVersion;
105             if ( config.getJeeVersion() != null )
106             {
107                 servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion();
108             }
109             else
110             {
111                 servletVersion = JeeUtils.resolveServletVersion( config.getProject() );
112             }
113             writer.writeText( "" + servletVersion.charAt( 0 ) + servletVersion.charAt( 2 ) );
114         }
115         else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) )
116         {
117             // In format X.X
118             String ejbVersion;
119             if ( config.getJeeVersion() != null )
120             {
121                 ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion();
122             }
123             else
124             {
125                 ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() );
126             }
127             writer.writeText( "" + ejbVersion.charAt( 0 ) + ejbVersion.charAt( 2 ) );
128         }
129         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) )
130         {
131             // In format X.X
132             String jeeVersion;
133             if ( config.getJeeVersion() != null )
134             {
135                 jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
136             }
137             else
138             {
139                 jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() );
140             }
141             writer.writeText( "" + jeeVersion.charAt( 0 ) + jeeVersion.charAt( 2 ) );
142         }
143         writer.endElement();
144         writer.endElement();
145     }
146 
147 }