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 691404 2008-09-02 21:57:19Z aheritier $
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 );
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             writeFacetInstalledElement( writer, FACET_JST_WEB, JeeUtils.resolveServletVersion( config.getProject() ) ); // installed
126             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
127         }
128         else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
129         {
130             writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
131             writeFacetFixedElement( writer, FACET_JST_EJB ); // fixed
132             writeFacetInstalledElement( writer, FACET_JST_EJB, JeeUtils.resolveEjbVersion( config.getProject() ) ); // installed
133             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
134         }
135         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
136         {
137             if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null )
138             {
139                 writer.startElement( "runtime" );
140                 writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() );
141                 writer.endElement(); // runtime
142 
143                 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR,
144                                             this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed
145                 writeFacetInstalledElement( writer, FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR,
146                                             this.config.getWorkspaceConfiguration().getWebsphereVersion() ); // installed
147 
148             }
149             writeFacetFixedElement( writer, FACET_JST_EAR ); // fixed
150             writeFacetInstalledElement( writer, FACET_JST_EAR, JeeUtils.resolveJeeVersion( config.getProject() ) ); // installed
151 
152         }
153         else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
154         {
155             writeFacetFixedElement( writer, FACET_JST_JAVA ); // fixed
156             writeFacetFixedElement( writer, FACET_JST_UTILITY ); // fixed
157             writeFacetInstalledElement( writer, FACET_JST_UTILITY, "1.0" ); //$NON-NLS-1$
158             writeFacetInstalledElement( writer, FACET_JST_JAVA, IdeUtils.resolveJavaVersion( config.getProject() ) ); // installed
159             // installed
160         }
161 
162         writeAdditionalProjectFacets( writer );
163 
164         writer.endElement(); // faceted-project
165     }
166 
167     /**
168      * Writes facet <code>fixed</code> element with attribute <code>facet</code> set to the value of argument
169      * <code>facetName</code>.
170      * 
171      * @param writer
172      * @param facetName
173      */
174     private void writeFacetFixedElement( XMLWriter writer, String facetName )
175     {
176         writer.startElement( ELT_FIXED );
177         writer.addAttribute( ATTR_FACET, facetName );
178         writer.endElement();
179     }
180 
181     /**
182      * Writes a facet <code>installed</code> element with attribute <code>facet</code> set to the value of argument
183      * <code>facetName</code>, and attribute <code>version</code> set to the value of argument <code>facetVersion</code>
184      * .
185      * 
186      * @param writer
187      * @param facetName
188      * @param facetVersion
189      */
190     private void writeFacetInstalledElement( XMLWriter writer, String facetName, String facetVersion )
191     {
192         writer.startElement( ELT_INSTALLED );
193         writer.addAttribute( ATTR_FACET, facetName );
194         writer.addAttribute( ATTR_VERSION, facetVersion );
195         writer.endElement();
196     }
197 
198     /**
199      * Writes out any additional project facets specified in the plugin configuration
200      * 
201      * @param writer
202      * @param packaging
203      */
204     private void writeAdditionalProjectFacets( XMLWriter writer )
205     {
206         if ( config.getProjectFacets() == null )
207         {
208             return;
209         }
210 
211         Iterator facetIterator = config.getProjectFacets().entrySet().iterator();
212         while ( facetIterator.hasNext() )
213         {
214             Entry facetEntry = (Entry) facetIterator.next();
215 
216             writer.startElement( ELT_INSTALLED );
217             writer.addAttribute( ATTR_FACET, (String) facetEntry.getKey() );
218             writer.addAttribute( ATTR_VERSION, (String) facetEntry.getValue() );
219             writer.endElement(); // installed
220         }
221     }
222 
223 }