blob: 35d4b4f63809eeb2ec2811e9b6ec316d00d179dc [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.j2ee.common;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.j2ee.core.Profile;
import org.netbeans.api.project.Project;
import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
import org.netbeans.modules.j2ee.deployment.devmodules.api.InstanceRemovedException;
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
public class ProjectUtil {
private static final Logger LOGGER = Logger.getLogger(ProjectUtil.class.getName());
public static Set<Profile> getSupportedProfiles(Project project) {
Set<Profile> supportedProfiles = new HashSet<Profile>();
J2eePlatform j2eePlatform = getPlatform(project);
if (j2eePlatform != null) {
supportedProfiles = j2eePlatform.getSupportedProfiles();
}
return supportedProfiles;
}
/**
* Gets {@link J2eePlatform} for the given {@code Project}.
*
* @param project project
* @return {@code J2eePlatform} for given project if found, {@code null} otherwise
* @since 1.69
*/
public static J2eePlatform getPlatform(Project project) {
try {
J2eeModuleProvider provider = project.getLookup().lookup(J2eeModuleProvider.class);
if (provider != null) {
String instance = provider.getServerInstanceID();
if (instance != null) {
return Deployment.getDefault().getServerInstance(provider.getServerInstanceID()).getJ2eePlatform();
}
}
} catch (InstanceRemovedException ex) {
// will return null
}
return null;
}
/**
* Is J2EE version of a given project JavaEE 5 or higher?
*
* @param project J2EE project
* @return true if J2EE version is JavaEE 5 or higher; otherwise false
*/
public static boolean isJavaEE5orHigher(Project project) {
if (project == null) {
return false;
}
J2eeModuleProvider j2eeModuleProvider = project.getLookup().lookup(J2eeModuleProvider.class);
if (j2eeModuleProvider != null) {
J2eeModule j2eeModule = j2eeModuleProvider.getJ2eeModule();
if (j2eeModule != null) {
J2eeModule.Type type = j2eeModule.getType();
String strVersion = j2eeModule.getModuleVersion();
assert strVersion != null : "Module type " + j2eeModule.getType() + " returned null module version";
try {
double version = Double.parseDouble(strVersion);
if (J2eeModule.Type.EJB.equals(type) && (version > 2.1)) {
return true;
}
if (J2eeModule.Type.WAR.equals(type) && (version > 2.4)) {
return true;
}
if (J2eeModule.Type.CAR.equals(type) && (version > 1.4)) {
return true;
}
} catch (NumberFormatException ex) {
LOGGER.log(Level.INFO, "Module version invalid " + strVersion, ex);
}
}
}
return false;
}
}