Coverage Report - org.apache.maven.plugin.eclipse.writers.wtp.EclipseWtpFacetsWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
EclipseWtpFacetsWriter
0%
0/78
0%
0/14
4.333
 
 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 616816 2008-01-30 17:23:08Z aheritier $
 44  
  */
 45  0
 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  0
         File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
 92  0
         settingsDir.mkdirs();
 93  
 
 94  
         Writer w;
 95  
 
 96  0
         String packaging = config.getPackaging();
 97  
 
 98  
         // Write out facet core xml
 99  
         try
 100  
         {
 101  0
             w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
 102  
         }
 103  0
         catch ( IOException ex )
 104  
         {
 105  0
             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
 106  0
         }
 107  0
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 108  0
         writeModuleTypeFacetCore( writer, packaging );
 109  0
         IOUtil.close( w );
 110  0
     }
 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  0
         writer.startElement( ELT_FACETED_PROJECT );
 121  
         // common facet
 122  0
         writer.startElement( ELT_FIXED );
 123  0
         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
 124  0
         writer.endElement(); // element fixed
 125  0
         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 126  
         {
 127  0
             writer.startElement( ELT_FIXED );
 128  0
             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
 129  0
             writer.endElement(); // fixed
 130  0
             writer.startElement( ELT_INSTALLED );
 131  0
             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
 132  0
             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveServletVersion( config.getProject() ) );
 133  0
             writer.endElement(); // installed
 134  
         }
 135  0
         else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 136  
         {
 137  0
             writer.startElement( ELT_FIXED );
 138  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
 139  0
             writer.endElement(); // fixed
 140  0
             writer.startElement( ELT_INSTALLED );
 141  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
 142  0
             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveEjbVersion( config.getProject() ) );
 143  0
             writer.endElement(); // installed
 144  
         }
 145  0
         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 146  
         {
 147  0
             if ( this.config.getWorkspaceConfiguration().getWebsphereVersion() != null )
 148  
             {
 149  0
                 writer.startElement( "runtime" );
 150  0
                 writer.addAttribute( "name", config.getWorkspaceConfiguration().getDefaultDeployServerName() );
 151  0
                 writer.endElement(); // runtime
 152  
 
 153  0
                 writer.startElement( ELT_INSTALLED );
 154  0
                 writer.addAttribute( ATTR_FACET, FACET_COM_IBM_WEBSPHERE_EXTENDED_EAR );
 155  0
                 writer.addAttribute( ATTR_VERSION, this.config.getWorkspaceConfiguration().getWebsphereVersion() );
 156  0
                 writer.endElement(); // installed
 157  
 
 158  0
                 writer.startElement( ELT_INSTALLED );
 159  0
                 writer.addAttribute( ATTR_FACET, FACET_COM_IBM_WEBSPHERE_COEXISTENCE_EAR );
 160  0
                 writer.addAttribute( ATTR_VERSION, this.config.getWorkspaceConfiguration().getWebsphereVersion() );
 161  0
                 writer.endElement(); // installed
 162  
 
 163  
             }
 164  0
             writer.startElement( ELT_FIXED );
 165  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
 166  0
             writer.endElement(); // fixed
 167  0
             writer.startElement( ELT_INSTALLED );
 168  0
             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
 169  0
             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveJeeVersion( config.getProject() ) );
 170  0
             writer.endElement(); // installed
 171  
 
 172  
         }
 173  0
         else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
 174  
         {
 175  0
             writer.startElement( ELT_FIXED );
 176  0
             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
 177  0
             writer.endElement(); // fixed
 178  0
             writer.startElement( ELT_INSTALLED );
 179  0
             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
 180  0
             writer.addAttribute( ATTR_VERSION, "1.0" ); //$NON-NLS-1$
 181  0
             writer.endElement(); // installed
 182  
         }
 183  
 
 184  
         // common installed element
 185  0
         writer.startElement( ELT_INSTALLED );
 186  0
         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
 187  0
         writer.addAttribute( ATTR_VERSION, IdeUtils.resolveJavaVersion( config.getProject() ) );
 188  0
         writer.endElement(); // installed
 189  
 
 190  0
         writeAdditionalProjectFacets( writer );
 191  
 
 192  0
         writer.endElement(); // faceted-project
 193  0
     }
 194  
 
 195  
     /**
 196  
      * Writes out any additional project facets specified in the plugin configuration
 197  
      * 
 198  
      * @param writer
 199  
      * @param packaging
 200  
      */
 201  
     private void writeAdditionalProjectFacets( XMLWriter writer )
 202  
     {
 203  0
         if ( config.getProjectFacets() == null )
 204  
         {
 205  0
             return;
 206  
         }
 207  
 
 208  0
         Iterator facetIterator = config.getProjectFacets().entrySet().iterator();
 209  0
         while ( facetIterator.hasNext() )
 210  
         {
 211  0
             Entry facetEntry = (Entry) facetIterator.next();
 212  
 
 213  0
             writer.startElement( ELT_INSTALLED );
 214  0
             writer.addAttribute( ATTR_FACET, (String) facetEntry.getKey() );
 215  0
             writer.addAttribute( ATTR_VERSION, (String) facetEntry.getValue() );
 216  0
             writer.endElement(); // installed
 217  0
         }
 218  0
     }
 219  
 
 220  
 }