Lines 1-222
Link Here
|
1 |
/* |
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.modules.j2ee.common; |
46 |
|
47 |
import java.util.logging.Level; |
48 |
import java.util.logging.Logger; |
49 |
import org.netbeans.api.annotations.common.NonNull; |
50 |
import org.netbeans.api.j2ee.core.Profile; |
51 |
import org.netbeans.api.project.Project; |
52 |
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; |
53 |
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider; |
54 |
|
55 |
public class Util { |
56 |
|
57 |
private static final Logger LOGGER = Logger.getLogger(Util.class.getName()); |
58 |
|
59 |
/** |
60 |
* Is J2EE version of a given project JavaEE 5 or higher? |
61 |
* |
62 |
* @param project J2EE project |
63 |
* @return true if J2EE version is JavaEE 5 or higher; otherwise false |
64 |
*/ |
65 |
public static boolean isJavaEE5orHigher(Project project) { |
66 |
if (project == null) { |
67 |
return false; |
68 |
} |
69 |
J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class); |
70 |
if (j2eeModuleProvider != null) { |
71 |
J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule(); |
72 |
if (j2eeModule != null) { |
73 |
J2eeModule.Type type = j2eeModule.getType(); |
74 |
String strVersion = j2eeModule.getModuleVersion(); |
75 |
assert strVersion != null : "Module type " + j2eeModule.getType() + " returned null module version"; // NOI18N |
76 |
try { |
77 |
double version = Double.parseDouble(strVersion); |
78 |
if (J2eeModule.Type.EJB.equals(type) && (version > 2.1)) { |
79 |
return true; |
80 |
} |
81 |
if (J2eeModule.Type.WAR.equals(type) && (version > 2.4)) { |
82 |
return true; |
83 |
} |
84 |
if (J2eeModule.Type.CAR.equals(type) && (version > 1.4)) { |
85 |
return true; |
86 |
} |
87 |
} catch (NumberFormatException ex) { |
88 |
LOGGER.log(Level.INFO, "Module version invalid " + strVersion, ex); |
89 |
} |
90 |
} |
91 |
} |
92 |
return false; |
93 |
} |
94 |
|
95 |
/** |
96 |
* Find out if the version of the given profile is at least Java EE 5 or higher. |
97 |
* |
98 |
* @param profile profile that we want to compare |
99 |
* @return true if the version of the given profile is Java EE 5 or higher, |
100 |
* false otherwise |
101 |
* @since 1.75 |
102 |
*/ |
103 |
public static boolean isAtLeastJavaEE5(@NonNull Profile profile) { |
104 |
return isVersionEqualOrHigher(profile, Profile.JAVA_EE_5); |
105 |
} |
106 |
|
107 |
/** |
108 |
* Find out if the version of the given profile is at least Java EE 6 Web or |
109 |
* higher. Please be aware that Java EE 6 Web is considered as lower than Java |
110 |
* EE 6 Full. |
111 |
* |
112 |
* @param profile profile that we want to compare |
113 |
* @return true if the version of the given profile is Java EE 6 Web or |
114 |
* higher, false otherwise |
115 |
* @since 1.75 |
116 |
*/ |
117 |
public static boolean isAtLeastJavaEE6Web(@NonNull Profile profile) { |
118 |
return isVersionEqualOrHigher(profile, Profile.JAVA_EE_6_WEB); |
119 |
} |
120 |
|
121 |
/** |
122 |
* Find out if the version of the given profile is at least Java EE 7 Web or |
123 |
* higher. Please be aware that Java EE 7 Web is considered as lower than Java |
124 |
* EE 7 Full. |
125 |
* |
126 |
* @param profile profile that we want to compare |
127 |
* @return true if the version of the given profile is Java EE 7 Web or |
128 |
* higher, false otherwise |
129 |
* @since 1.79 |
130 |
*/ |
131 |
public static boolean isAtLeastJavaEE7Web(@NonNull Profile profile) { |
132 |
return isVersionEqualOrHigher(profile, Profile.JAVA_EE_7_WEB); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Compares if the first given profile has equal or higher Java EE version |
137 |
* in comparison to the second profile. |
138 |
* |
139 |
* Please be aware of the following rules: |
140 |
* <br/><br/> |
141 |
* |
142 |
* 1) Each Java EE X version is considered as lower than Java EE X+1 version |
143 |
* (this applies regardless on Web/Full specification and in reality it means |
144 |
* that even Java EE 6 Full version is considered as lower than Java EE 7 Web) |
145 |
* <br/><br/> |
146 |
* |
147 |
* 2) Each Java EE X Web version is considered as lower than Java EE X Full |
148 |
* <br/> |
149 |
* |
150 |
* @param profileToCompare profile that we want to compare |
151 |
* @param comparingVersion version which we are comparing with |
152 |
* @return <code>true</code> if the profile version is equal or higher in |
153 |
* comparison with the second one, <code>false</code> otherwise |
154 |
* @since 1.75 |
155 |
*/ |
156 |
private static boolean isVersionEqualOrHigher( |
157 |
@NonNull Profile profileToCompare, |
158 |
@NonNull Profile comparingVersion) { |
159 |
|
160 |
int comparisonResult = Profile.UI_COMPARATOR.compare(profileToCompare, comparingVersion); |
161 |
if (comparisonResult == 0) { |
162 |
// The same version for both |
163 |
return true; |
164 |
|
165 |
} else { |
166 |
String profileToCompareVersion = getProfileVersion(profileToCompare); |
167 |
String comparingProfileVersion = getProfileVersion(comparingVersion); |
168 |
|
169 |
// If the canonicalName is the same value we have to differ between Web and Full profile |
170 |
if (profileToCompareVersion.equals(comparingProfileVersion)) { |
171 |
return compareWebAndFull(profileToCompare, comparingVersion); |
172 |
} else { |
173 |
if (comparisonResult > 0) { |
174 |
// profileToCompare has lower version than comparingVersion |
175 |
return false; |
176 |
} else { |
177 |
return true; |
178 |
} |
179 |
} |
180 |
} |
181 |
} |
182 |
|
183 |
private static boolean compareWebAndFull( |
184 |
@NonNull Profile profileToCompare, |
185 |
@NonNull Profile comparingVersion) { |
186 |
|
187 |
boolean isThisFullProfile = isFullProfile(profileToCompare); |
188 |
boolean isParamFullProfile = isFullProfile(comparingVersion); |
189 |
|
190 |
if (isThisFullProfile && isParamFullProfile) { |
191 |
// Both profiles are Java EE Full |
192 |
return true; |
193 |
} |
194 |
if (!isThisFullProfile && !isParamFullProfile) { |
195 |
// Both profiles are Java EE Web |
196 |
return true; |
197 |
} |
198 |
if (isThisFullProfile && !isParamFullProfile) { |
199 |
// profileToCompare is Java EE Full profile and comparingVersion is only Java EEWeb profile |
200 |
return true; |
201 |
} |
202 |
return false; |
203 |
} |
204 |
|
205 |
private static String getProfileVersion(@NonNull Profile profile) { |
206 |
String profileDetails = profile.toPropertiesString(); |
207 |
int indexOfDash = profileDetails.indexOf("-"); |
208 |
if (indexOfDash != -1) { |
209 |
return profileDetails.substring(0, indexOfDash); |
210 |
} |
211 |
return profileDetails; |
212 |
} |
213 |
|
214 |
private static boolean isFullProfile(@NonNull Profile profile) { |
215 |
final String profileDetails = profile.toPropertiesString(); |
216 |
if (profileDetails.indexOf("-") == -1) { |
217 |
return true; |
218 |
} |
219 |
return false; |
220 |
} |
221 |
|
222 |
} |