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.ide;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.maven.project.MavenProject;
25  
26  public class JeeUtils
27  {
28      public static final String ARTIFACT_MAVEN_EAR_PLUGIN = "org.apache.maven.plugins:maven-ear-plugin"; //$NON-NLS-1$
29  
30      public static final String ARTIFACT_MAVEN_WAR_PLUGIN = "org.apache.maven.plugins:maven-war-plugin"; //$NON-NLS-1$
31  
32      private static final Map ejbMap = new HashMap();
33  
34      private static final Map jeeMap = new HashMap();
35  
36      private static final Map jspMap = new HashMap();
37  
38      private static final Map servletMap = new HashMap();
39  
40      /** Names of artifacts of ejb APIs. */
41      // private static final String[] EJB_API_ARTIFACTS = new String[] { "ejb", "ejb-api", "geronimo-spec-ejb" };
42      // //$NON-NLS-1$
43      static
44      {
45      	addJEE( JeeDescriptor.JEE_6_0, JeeDescriptor.EJB_3_1, JeeDescriptor.SERVLET_3_0, JeeDescriptor.JSP_2_2 );
46          addJEE( JeeDescriptor.JEE_5_0, JeeDescriptor.EJB_3_0, JeeDescriptor.SERVLET_2_5, JeeDescriptor.JSP_2_1 );
47          addJEE( JeeDescriptor.JEE_1_4, JeeDescriptor.EJB_2_1, JeeDescriptor.SERVLET_2_4, JeeDescriptor.JSP_2_0 );
48          addJEE( JeeDescriptor.JEE_1_3, JeeDescriptor.EJB_2_0, JeeDescriptor.SERVLET_2_3, JeeDescriptor.JSP_1_2 );
49          addJEE( JeeDescriptor.JEE_1_2, JeeDescriptor.EJB_1_1, JeeDescriptor.SERVLET_2_2, JeeDescriptor.JSP_1_1 );
50  
51      }
52  
53      /**
54       * Returns the JEEDescriptor associated to an EJB specifications version.
55       * 
56       * @param ejbVersion An EJB version as defined by constants JeeDescriptor.EJB_x_x
57       * @return a JEEDescriptor
58       */
59      public final static JeeDescriptor getJeeDescriptorFromEjbVersion( String ejbVersion )
60      {
61          if ( ejbMap.containsKey( ejbVersion ) )
62              return (JeeDescriptor) ejbMap.get( ejbVersion );
63          else
64              return null;
65      }
66  
67      /**
68       * Returns the JEEDescriptor associated to a JEE specifications version.
69       * 
70       * @param jeeVersion A JEE version as defined by constants JeeDescriptor.JEE_x_x
71       * @return a JEEDescriptor
72       */
73      public final static JeeDescriptor getJeeDescriptorFromJeeVersion( String jeeVersion )
74      {
75          if ( jeeMap.containsKey( jeeVersion ) )
76              return (JeeDescriptor) jeeMap.get( jeeVersion );
77          else
78              return null;
79      }
80  
81      /**
82       * Returns the JEEDescriptor associated to a JSP specifications version.
83       * 
84       * @param jspVersion A JSP version as defined by constants JeeDescriptor.JSP_x_x
85       * @return a JEEDescriptor
86       */
87      public final static JeeDescriptor getJeeDescriptorFromJspVersion( String jspVersion )
88      {
89          if ( jspMap.containsKey( jspVersion ) )
90              return (JeeDescriptor) jspMap.get( jspVersion );
91          else
92              return null;
93      }
94  
95      /**
96       * Returns the JEEDescriptor associated to a Servlet specifications version.
97       * 
98       * @param servletVersion A Servlet version as defined by constants JeeDescriptor.SERVLET_x_x
99       * @return a JEEDescriptor
100      */
101     public final static JeeDescriptor getJeeDescriptorFromServletVersion( String servletVersion )
102     {
103         if ( servletMap.containsKey( servletVersion ) )
104             return (JeeDescriptor) servletMap.get( servletVersion );
105         else
106             return null;
107     }
108 
109     /**
110      * Search in dependencies a version of EJB APIs (or of JEE APIs).
111      * 
112      * @param artifacts The list of dependencies where we search the information
113      * @return An EJB version as defined by constants JeeDescriptor.EJB_x_x. By default, if nothing is found, returns
114      *         JeeDescriptor.EJB_2_1.
115      */
116     public static String resolveEjbVersion( MavenProject project )
117     {
118         String version = findEjbVersionInDependencies( project );
119 
120         if ( version == null )
121         {
122             // No ejb dependency detected. Try to resolve the ejb
123             // version from J2EE/JEE.
124             JeeDescriptor descriptor = getJeeDescriptorFromJeeVersion( findJeeVersionInDependencies( project ) );
125             if ( descriptor != null )
126                 version = descriptor.getEjbVersion();
127         }
128         return version == null ? JeeDescriptor.EJB_2_1 : version; //$NON-NLS-1$
129     }
130 
131     /**
132      * Search in dependencies a version of JEE APIs.
133      * 
134      * @param artifacts The list of dependencies where we search the information
135      * @return A JEE version as defined by constants JeeDescriptor.JEE_x_x. By default, if nothing is found, returns
136      *         JeeDescriptor.JEE_1_4.
137      */
138     public static String resolveJeeVersion( MavenProject project )
139     {
140         // try to find version in dependencies
141         String version = findJeeVersionInDependencies( project );
142         if ( version == null )
143         {
144             // No JEE dependency detected. Try to resolve the JEE
145             // version from EJB.
146             JeeDescriptor descriptor = getJeeDescriptorFromEjbVersion( findEjbVersionInDependencies( project ) );
147             if ( descriptor != null )
148                 version = descriptor.getJeeVersion();
149         }
150         if ( version == null )
151         {
152             // No JEE dependency detected. Try to resolve the JEE
153             // version from SERVLET.
154             JeeDescriptor descriptor = getJeeDescriptorFromServletVersion( findServletVersionInDependencies( project ) );
155             if ( descriptor != null )
156                 version = descriptor.getJeeVersion();
157         }
158         if ( version == null )
159         {
160             // No JEE dependency detected. Try to resolve the JEE
161             // version from JSP.
162             JeeDescriptor descriptor = getJeeDescriptorFromJspVersion( findJspVersionInDependencies( project ) );
163             if ( descriptor != null )
164                 version = descriptor.getJeeVersion();
165         }
166         return version == null ? JeeDescriptor.JEE_1_4 : version; //$NON-NLS-1$
167     }
168 
169     /**
170      * Search in dependencies a version of JSP APIs (or from JEE APIs, or from Servlet APIs).
171      * 
172      * @param artifacts The list of dependencies where we search the information
173      * @return A JSP version as defined by constants JeeDescriptor.JSP_x_x. By default, if nothing is found, returns
174      *         JeeDescriptor.JSP_2_0.
175      */
176 
177     public static String resolveJspVersion( MavenProject project )
178     {
179         String version = findJspVersionInDependencies( project );
180 
181         if ( version == null )
182         {
183             // No jsp dependency detected. Try to resolve the jsp
184             // version from J2EE/JEE.
185             JeeDescriptor descriptor = getJeeDescriptorFromJeeVersion( findJeeVersionInDependencies( project ) );
186             if ( descriptor != null )
187                 version = descriptor.getJspVersion();
188         }
189         if ( version == null )
190         {
191             // No jsp dependency detected. Try to resolve the jsp
192             // version from Servlet.
193             JeeDescriptor descriptor = getJeeDescriptorFromServletVersion( findServletVersionInDependencies( project ) );
194             if ( descriptor != null )
195                 version = descriptor.getJspVersion();
196         }
197         return version == null ? JeeDescriptor.JSP_2_0 : version; //$NON-NLS-1$
198     }
199 
200     /**
201      * Search in dependencies a version of Servlet APIs (or of JEE APIs).
202      * 
203      * @param artifacts The list of dependencies where we search the information
204      * @return A SERVLET version as defined by constants JeeDescriptor.SERLVET_x_x. By default, if nothing is found,
205      *         returns JeeDescriptor.SERVLET_2_4.
206      */
207     public static String resolveServletVersion( MavenProject project )
208     {
209         String version = findServletVersionInDependencies( project );
210 
211         if ( version == null )
212         {
213             // No servlet dependency detected. Try to resolve the servlet
214             // version from J2EE/JEE.
215             JeeDescriptor descriptor = getJeeDescriptorFromJeeVersion( findJeeVersionInDependencies( project ) );
216             if ( descriptor != null )
217                 version = descriptor.getServletVersion();
218         }
219         return version == null ? JeeDescriptor.SERVLET_2_4 : version; //$NON-NLS-1$
220     }
221 
222     private static void addJEE( String jeeVersion, String ejbVersion, String servletVersion, String jspVersion )
223     {
224         JeeDescriptor descriptor = new JeeDescriptor( jeeVersion, ejbVersion, servletVersion, jspVersion );
225         jeeMap.put( jeeVersion, descriptor );
226         ejbMap.put( ejbVersion, descriptor );
227         servletMap.put( servletVersion, descriptor );
228         jspMap.put( jspVersion, descriptor );
229     }
230 
231     private static String findEjbVersionInDependencies( MavenProject project )
232     {
233 
234         String version =
235             IdeUtils.getArtifactVersion( new String[] { "ejb", "ejb-api", "geronimo-spec-ejb" },
236                                          project.getDependencies(), 3 );
237         // For new Geronimo APIs, the version of the artifact isn't the one of the spec
238         if ( version == null )
239         {
240             if ( IdeUtils.getArtifactVersion( new String[] { "geronimo-ejb_2.1_spec" }, project.getDependencies(), 3 ) != null )
241                 return JeeDescriptor.EJB_2_1;
242         }
243         if ( version == null )
244         {
245             if ( IdeUtils.getArtifactVersion( new String[] { "geronimo-ejb_3.0_spec" }, project.getDependencies(), 3 ) != null )
246                 return JeeDescriptor.EJB_3_0;
247         }
248 
249         // if no version found try dependencies of referenced projects
250         if ( version == null )
251         {
252             for (Object key : project.getProjectReferences().keySet()) {
253                 MavenProject refProject = (MavenProject) project.getProjectReferences().get(key);
254                 version = findEjbVersionInDependencies(refProject);
255                 if (version != null) // version found in dependencies
256                 {
257                     break;
258                 }
259             }
260         }
261         return version;
262     }
263 
264     private static String findJeeVersionInDependencies( MavenProject project )
265     {
266         String version =
267             IdeUtils.getArtifactVersion( new String[] { "javaee-api", "j2ee", "geronimo-spec-j2ee" },
268                                          project.getDependencies(), 3 );
269 
270         // For new Geronimo APIs, the version of the artifact isn't the one of the spec
271         if ( version == null )
272         {
273             if ( IdeUtils.getArtifactVersion( new String[] { "geronimo-j2ee_1.4_spec" }, project.getDependencies(), 3 ) != null )
274                 return JeeDescriptor.JEE_1_4;
275         }
276 
277         // if no version found try dependencies of referenced projects
278         if ( version == null )
279         {
280             for (Object key : project.getProjectReferences().keySet()) {
281                 MavenProject refProject = (MavenProject) project.getProjectReferences().get(key);
282                 version = findJeeVersionInDependencies(refProject);
283                 if (version != null) // version found in dependencies
284                 {
285                     break;
286                 }
287             }
288         }
289 
290         return version;
291     }
292 
293     private static String findJspVersionInDependencies( MavenProject project )
294     {
295         return null;
296     }
297 
298     private static String findServletVersionInDependencies( MavenProject project )
299     {
300         String version =
301             IdeUtils.getArtifactVersion( new String[] { "servlet-api", "servletapi", "geronimo-spec-servlet" },
302                                          project.getDependencies(), 3 );
303 
304         // For new Geronimo APIs, the version of the artifact isn't the one of the spec
305         if ( version == null )
306         {
307             if ( IdeUtils.getArtifactVersion( new String[] { "geronimo-servlet_2.4_spec" }, project.getDependencies(),
308                                               3 ) != null )
309                 return JeeDescriptor.SERVLET_2_4;
310         }
311         if ( version == null )
312         {
313             if ( IdeUtils.getArtifactVersion( new String[] { "geronimo-servlet_2.5_spec" }, project.getDependencies(),
314                                               3 ) != null )
315                 return JeeDescriptor.SERVLET_2_5;
316         }
317 
318         // if no version found try dependencies of referenced projects
319         if ( version == null )
320         {
321             for (Object key : project.getProjectReferences().keySet()) {
322                 MavenProject refProject = (MavenProject) project.getProjectReferences().get(key);
323                 version = findServletVersionInDependencies(refProject);
324                 if (version != null) // version found in dependencies
325                 {
326                     break;
327                 }
328             }
329         }
330         return version;
331     }
332 }