This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 232242
Collapse All | Expand All

(-)a/j2ee.common/src/org/netbeans/modules/j2ee/common/Util.java (-222 lines)
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
}
(-)a/j2ee.core/src/org/netbeans/api/j2ee/core/Profile.java (+118 lines)
Lines 142-145 Link Here
142
            return null;
142
            return null;
143
        }
143
        }
144
    }
144
    }
145
146
    /**
147
     * Find out if the version of the given profile is at least Java EE 5 or higher.
148
     *
149
     * @param profile profile that we want to compare
150
     * @return true if the version of the given profile is Java EE 5 or higher,
151
     *         false otherwise
152
     * @since TDB
153
     */
154
    public static boolean isAtLeastJavaEE5(@NonNull Profile profile) {
155
        return isVersionEqualOrHigher(profile, Profile.JAVA_EE_5);
156
    }
157
158
    /**
159
     * Find out if the version of the given profile is at least Java EE 6 Web or
160
     * higher. Please be aware that Java EE 6 Web is considered as lower than Java
161
     * EE 6 Full.
162
     *
163
     * @param profile profile that we want to compare
164
     * @return true if the version of the given profile is Java EE 6 Web or
165
     *         higher, false otherwise
166
     * @since TDB
167
     */
168
    public static boolean isAtLeastJavaEE6Web(@NonNull Profile profile) {
169
        return isVersionEqualOrHigher(profile, Profile.JAVA_EE_6_WEB);
170
    }
171
172
    /**
173
     * Find out if the version of the given profile is at least Java EE 7 Web or
174
     * higher. Please be aware that Java EE 7 Web is considered as lower than Java
175
     * EE 7 Full.
176
     *
177
     * @param profile profile that we want to compare
178
     * @return true if the version of the given profile is Java EE 7 Web or
179
     *         higher, false otherwise
180
     * @since TDB
181
     */
182
    public static boolean isAtLeastJavaEE7Web(@NonNull Profile profile) {
183
        return isVersionEqualOrHigher(profile, Profile.JAVA_EE_7_WEB);
184
    }
185
186
    private static String getProfileVersion(@NonNull Profile profile) {
187
        String profileDetails = profile.toPropertiesString();
188
        int indexOfDash = profileDetails.indexOf("-");
189
        if (indexOfDash != -1) {
190
            return profileDetails.substring(0, indexOfDash);
191
        }
192
        return profileDetails;
193
    }
194
195
    private static boolean compareWebAndFull(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
196
        boolean isThisFullProfile = isFullProfile(profileToCompare);
197
        boolean isParamFullProfile = isFullProfile(comparingVersion);
198
        if (isThisFullProfile && isParamFullProfile) {
199
            // Both profiles are Java EE Full
200
            return true;
201
        }
202
        if (!isThisFullProfile && !isParamFullProfile) {
203
            // Both profiles are Java EE Web
204
            return true;
205
        }
206
        if (isThisFullProfile && !isParamFullProfile) {
207
            // profileToCompare is Java EE Full profile and comparingVersion is only Java EEWeb profile
208
            return true;
209
        }
210
        return false;
211
    }
212
213
    private static boolean isFullProfile(@NonNull Profile profile) {
214
        final String profileDetails = profile.toPropertiesString();
215
        if (profileDetails.indexOf("-") == -1) {
216
            return true;
217
        }
218
        return false;
219
    }
220
221
    /**
222
     * Compares if the first given profile has equal or higher Java EE version
223
     * in comparison to the second profile.
224
     *
225
     * Please be aware of the following rules:
226
     * <br/><br/>
227
     *
228
     * 1) Each Java EE X version is considered as lower than Java EE X+1 version
229
     * (this applies regardless on Web/Full specification and in reality it means
230
     * that even Java EE 6 Full version is considered as lower than Java EE 7 Web)
231
     * <br/><br/>
232
     *
233
     * 2) Each Java EE X Web version is considered as lower than Java EE X Full
234
     * <br/>
235
     *
236
     * @param profileToCompare profile that we want to compare
237
     * @param comparingVersion version which we are comparing with
238
     * @return <code>true</code> if the profile version is equal or higher in
239
     *         comparison with the second one, <code>false</code> otherwise
240
     * @since TDB
241
     */
242
    private static boolean isVersionEqualOrHigher(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
243
        int comparisonResult = Profile.UI_COMPARATOR.compare(profileToCompare, comparingVersion);
244
        if (comparisonResult == 0) {
245
            // The same version for both
246
            return true;
247
        } else {
248
            String profileToCompareVersion = getProfileVersion(profileToCompare);
249
            String comparingProfileVersion = getProfileVersion(comparingVersion);
250
            if (profileToCompareVersion.equals(comparingProfileVersion)) {
251
                return compareWebAndFull(profileToCompare, comparingVersion);
252
            } else {
253
                if (comparisonResult > 0) {
254
                    // profileToCompare has lower version than comparingVersion
255
                    return false;
256
                } else {
257
                    return true;
258
                }
259
            }
260
        }
261
    }
262
145
}
263
}

Return to bug 232242