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.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.eclipse.Constants;
29  import org.apache.maven.plugin.eclipse.Messages;
30  import org.apache.maven.plugin.eclipse.writers.AbstractEclipseWriter;
31  import org.apache.maven.plugin.ide.IdeDependency;
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:nir@cfc.at">Richard van Nieuwenhoven </a>
42   */
43  public class RadWebSettingsWriter
44      extends AbstractEclipseWriter
45  {
46  
47      private static final String COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE =
48          "com.ibm.etools.siteedit.wizards.projectfeature.WebSiteFeature";
49  
50      private static final String WEBSETTINGS_CONTEXT_ROOT = "context-root";
51  
52      private static final String WEBSETTINGS_FEATURE = "feature";
53  
54      private static final String WEBSETTINGS_FEATURE_ID = "feature-id";
55  
56      private static final String WEBSETTINGS_FEATURES = "features";
57  
58      private static final String WEBSETTINGS_FILENAME = ".websettings";
59  
60      private static final String WEBSETTINGS_JSP_LEVEL = "jsp-level";
61  
62      private static final String WEBSETTINGS_PROJECT_TYPE = "project-type";
63  
64      private static final String WEBSETTINGS_TEMPLATEFEATURE = "templatefeature";
65  
66      private static final String WEBSETTINGS_VERSION = "version";
67  
68      private static final String WEBSETTINGS_WEBCONTENT = "webcontent";
69  
70      private static final String WEBSETTINGS_WEBSETTINGS = "websettings";
71  
72      private static final String WEBSETTINGS_LIBMODULES = "lib-modules";
73  
74      private static final String WEBSETTINGS_LIBMODULE = "lib-module";
75  
76      private static final String WEBSETTINGS_LM_JAR = "jar";
77  
78      private static final String WEBSETTINGS_LM_PROJECT = "project";
79  
80      /**
81       * the context root to use for this project
82       */
83      private String warContextRoot;
84  
85      /**
86       * required default constructor.
87       * 
88       * @param warContextRoot the context root to use for this project
89       */
90      public RadWebSettingsWriter( String warContextRoot )
91      {
92          this.warContextRoot = warContextRoot;
93      }
94  
95      /**
96       * write the websettings file for RAD6 if needed.
97       * 
98       * @throws MojoExecutionException when writing the config files was not possible
99       */
100     public void write()
101         throws MojoExecutionException
102     {
103         Writer w;
104         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( config.getPackaging() ) )
105         {
106             try
107             {
108                 w =
109                     new OutputStreamWriter( new FileOutputStream( new File( config.getEclipseProjectDirectory(),
110                                                                             WEBSETTINGS_FILENAME ) ), "UTF-8" );
111             }
112             catch ( IOException ex )
113             {
114                 throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
115             }
116 
117             XMLWriter writer = new PrettyPrintXMLWriter( w, "UTF-8", null );
118             writeModuleTypeFacetCore( writer );
119             IOUtil.close( w );
120         }
121     }
122 
123     /**
124      * write the websettings file for RAD6.
125      * 
126      * @param writer where to write to
127      * @throws MojoExecutionException
128      */
129     private void writeModuleTypeFacetCore( XMLWriter writer )
130         throws MojoExecutionException
131     {
132         writer.startElement( WEBSETTINGS_WEBSETTINGS );
133         writer.addAttribute( WEBSETTINGS_VERSION, "600" );
134         writer.startElement( WEBSETTINGS_WEBCONTENT );
135 
136         // Generating web content settings based on war plug-in warSourceDirectory property
137         File warSourceDirectory =
138             new File( IdeUtils.getPluginSetting( config.getProject(), JeeUtils.ARTIFACT_MAVEN_WAR_PLUGIN,
139                                                  "warSourceDirectory", //$NON-NLS-1$
140                                                  config.getProject().getBasedir() + "/src/main/webapp" ) ); //$NON-NLS-1$
141         String webContentDir =
142             IdeUtils.toRelativeAndFixSeparator( config.getEclipseProjectDirectory(), warSourceDirectory, false );
143 
144         writer.writeText( webContentDir );
145 
146         writer.endElement();
147         writer.startElement( WEBSETTINGS_CONTEXT_ROOT );
148         writer.writeText( getContextRoot( warContextRoot ) );
149         writer.endElement();
150         writer.startElement( WEBSETTINGS_PROJECT_TYPE );
151         writer.writeText( "J2EE" );
152         writer.endElement();
153         writer.startElement( WEBSETTINGS_JSP_LEVEL );
154         String jspVersion;
155         if ( config.getJeeVersion() != null )
156         {
157             jspVersion = JeeUtils.getJeeDescriptorFromJeeVersion( config.getJeeVersion() ).getJspVersion();
158         }
159         else
160         {
161             jspVersion = JeeUtils.resolveJspVersion( config.getProject() );
162         }
163         writer.writeText( jspVersion );
164         writer.endElement();
165         writer.startElement( WEBSETTINGS_FEATURES );
166         writer.startElement( WEBSETTINGS_FEATURE );
167         writer.startElement( WEBSETTINGS_FEATURE_ID );
168         writer.writeText( WEBSETTINGS_TEMPLATEFEATURE );
169         writer.endElement();
170         writer.endElement();
171         writer.startElement( WEBSETTINGS_FEATURE );
172         writer.startElement( WEBSETTINGS_FEATURE_ID );
173         writer.writeText( COM_IBM_ETOOLS_SITEEDIT_WIZARDS_PROJECTFEATURE_WEB_SITE_FEATURE );
174         writer.endElement();
175         writer.endElement();
176         writer.endElement();
177 
178         // library modules
179         writer.startElement( WEBSETTINGS_LIBMODULES );
180 
181         // iterate relevant dependencies (non-test, non-provided, project)
182         IdeDependency[] deps = config.getDeps();
183         if ( deps != null )
184         {
185             for (final IdeDependency dependency : deps) {
186                 log.debug("RadWebSettingsWriter: checking dependency " + dependency.toString());
187 
188                 if (dependency.isReferencedProject() && !dependency.isTestDependency() && !dependency.isProvided()) {
189                     log.debug("RadWebSettingsWriter: dependency " + dependency.toString()
190                             + " selected for inclusion as lib-module");
191 
192                     String depName = dependency.getEclipseProjectName();
193                     String depJar = dependency.getArtifactId() + ".jar";
194 
195                     writer.startElement(WEBSETTINGS_LIBMODULE);
196 
197                     writer.startElement(WEBSETTINGS_LM_JAR);
198                     writer.writeText(depJar);
199                     writer.endElement(); // jar
200 
201                     writer.startElement(WEBSETTINGS_LM_PROJECT);
202                     writer.writeText(depName);
203                     writer.endElement(); // project
204 
205                     writer.endElement(); // libmodule
206                 }
207             }
208         }
209 
210         writer.endElement(); // libmodules
211         writer.endElement(); // websettings
212 
213     }
214 
215     /**
216      * Create the ContextRoot for this project, the default is the artifact id
217      * 
218      * @param warContextRoot set as a configuration property.
219      * @return the context root to use
220      */
221     private String getContextRoot( String warContextRoot )
222     {
223         if ( warContextRoot == null || warContextRoot.length() == 0 )
224         {
225             return config.getProject().getArtifactId();
226         }
227         else
228         {
229             return warContextRoot;
230         }
231     }
232 
233 }