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.wtp;
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  import java.util.Map.Entry;
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.ide.IdeUtils;
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 a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
39   * 
40   * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
41   * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
42   * @version $Id: EclipseWtpFacetsWriter.java 1517906 2013-08-27 18:25:03Z krosenvold $
43   */
44  public class EclipseWtpFacetsWriter
45      extends AbstractWtpResourceWriter
46  {
47  
48      private static final String FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR = "com.ibm.websphere.coexistence.ear"; //$NON-NLS-1$
49  
50      private static final String FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR = "com.ibm.websphere.extended.ear"; //$NON-NLS-1$
51  
52      private static final String FACET_JST_EAR = "jst.ear"; //$NON-NLS-1$
53  
54      private static final String FACET_JST_UTILITY = "jst.utility"; //$NON-NLS-1$
55  
56      private static final String FACET_JST_EJB = "jst.ejb"; //$NON-NLS-1$
57  
58      private static final String FACET_JST_WEB = "jst.web"; //$NON-NLS-1$
59  
60      private static final String FACET_JST_JAVA = "jst.java"; //$NON-NLS-1$
61  
62      private static final String ATTR_VERSION = "version"; //$NON-NLS-1$
63  
64      private static final String ELT_INSTALLED = "installed"; //$NON-NLS-1$
65  
66      private static final String ATTR_FACET = "facet"; //$NON-NLS-1$
67  
68      private static final String ELT_FIXED = "fixed"; //$NON-NLS-1$
69  
70      private static final String ELT_FACETED_PROJECT = "faceted-project"; //$NON-NLS-1$
71  
72      /**
73       * The .settings folder for Web Tools Project 1.x release.
74       */
75      private static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
76  
77      /**
78       * File name where Eclipse Project's Facet configuration will be stored.
79       */
80      private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$
81  
82      /**
83       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
84       */
85      public void write()
86          throws MojoExecutionException
87      {
88  
89          // create a .settings directory (if not existing)
90          File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
91          settingsDir.mkdirs();
92  
93          Writer w;
94  
95          String packaging = config.getPackaging();
96  
97          // Write out facet core xml
98          try
99          {
100             w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
101         }
102         catch ( IOException ex )
103         {
104             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
105         }
106         XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
107         writeModuleTypeFacetCore( writer, packaging );
108         IOUtil.close( w );
109     }
110 
111     /**
112      * Writes out the facet info for a faceted-project based on the packaging.
113      * 
114      * @param writer
115      * @param packaging
116      */
117     private void writeModuleTypeFacetCore( XMLWriter writer, String packaging )
118     {
119         writer.startElement( ELT_FACETED_PROJECT );
120         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
121         {
122             writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
123             writeFacetFixedElement( writer, FACET_JST_WEB ); // fixed
124             String servletVersion;
125             if ( config.getJeeVersion() != null )
126             {
127                 servletVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getServletVersion();
128             }
129             else
130             {
131                 servletVersion = JeeUtils.resolveServletVersion( config.getProject() );
132             }
133             writeFacetInstalledElement( writer, FACET_JST_WEB, servletVersion ); // installed
134             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
135         }
136         else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
137         {
138             writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
139             writeFacetFixedElement( writer, FACET_JST_EJB ); // fixed
140             
141             String ejbVersion;
142             if ( config.getJeeVersion() != null )
143             {
144                 ejbVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getEjbVersion();
145             }
146             else
147             {
148                 ejbVersion = JeeUtils.resolveEjbVersion( config.getProject() );
149             }
150             writeFacetInstalledElement( writer, FACET_JST_EJB, ejbVersion ); // installed
151             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
152         }
153         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
154         {
155             if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null )
156             {
157                 writer.startElement( "runtime" );
158                 writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() );
159                 writer.endElement(); // runtime
160 
161                 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR,
162                                             this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed
163                 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR,
164                                             this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed
165 
166             }
167             writeFacetFixedElement( writer, FACET_JST_EAR ); // fixed
168             String jeeVersion;
169             if ( config.getJeeVersion() != null )
170             {
171                 jeeVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJeeVersion();
172             }
173             else
174             {
175                 jeeVersion = JeeUtils.resolveJeeVersion( config.getProject() );
176             }
177             writeFacetInstalledElement( writer, FACET_JST_EAR, jeeVersion ); // installed
178 
179         }
180         else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
181         {
182             writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
183             writeFacetFixedElement( writer, FACET_JST_UTILITY ); // fixed
184             writeFacetInstalledElement( writer, FACET_JST_UTILITY, "1.0" ); //$NON-NLS-1$
185             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
186             // installed
187         }
188 
189         writeAdditionalProjectFacets( writer );
190 
191         writer.endElement(); // faceted-project
192     }
193 
194     /**
195      * Writes facet <code>fixed</code> element with attribute <code>facet</code> set to the value of argument
196      * <code>facetName</code>.
197      * 
198      * @param writer
199      * @param facetName
200      */
201     private void writeFacetFixedElement( XMLWriter writer, String facetName )
202     {
203         writer.startElement( ELT_FIXED );
204         writer.addAttribute( ATTR_FACET, facetName );
205         writer.endElement();
206     }
207 
208     /**
209      * Writes a facet <code>installed</code> element with attribute <code>facet</code> set to the value of argument
210      * <code>facetName</code>, and attribute <code>version</code> set to the value of argument <code>facetVersion</code>
211      * .
212      * 
213      * @param writer
214      * @param facetName
215      * @param facetVersion
216      */
217     private void writeFacetInstalledElement( XMLWriter writer, String facetName, String facetVersion )
218     {
219         writer.startElement( ELT_INSTALLED );
220         writer.addAttribute( ATTR_FACET, facetName );
221         writer.addAttribute( ATTR_VERSION, facetVersion );
222         writer.endElement();
223     }
224 
225     /**
226      * Writes out any additional project facets specified in the plugin configuration
227      * 
228      * @param writer
229      * @param packaging
230      */
231     private void writeAdditionalProjectFacets( XMLWriter writer )
232     {
233         if ( config.getProjectFacets() == null )
234         {
235             return;
236         }
237 
238         for (Object o : config.getProjectFacets().entrySet()) {
239             Entry facetEntry = (Entry) o;
240 
241             writer.startElement(ELT_INSTALLED);
242             writer.addAttribute(ATTR_FACET, (String) facetEntry.getKey());
243             writer.addAttribute(ATTR_VERSION, (String) facetEntry.getValue());
244             writer.endElement(); // installed
245         }
246     }
247 
248 }