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