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