Skip to content

Commit

Permalink
Add Jakarta EE/Java EE support for Tomcat and TomEE (apache#4822)
Browse files Browse the repository at this point in the history
- Add support for Tomcat 10 and 10.1
- Improve support for Tomcat versions from 5 to 10.1
- Add initial support for Tomcat 11
- Add support for TomEE versions from 1.5 to 9
- Improve support for TomEE types and correctly identify them.
- Improve logic for supported profiles and jvm platforms for Tomcat and TomEE
- Add new helper method versionRange and use it to define valid ranges of supported java versions. Kudos to Michael Bien.
- Improve logic for getJavadoc() method
- Improve Tomcat/TomEE version enum
- Improve support for Jakarta EE JaxRS
- Fix the default jvm platform that is pre-selected
- Move call of isTomEEJaxRS(), should be called only if isTomEE() is true
- Remove redundant call to loadTomEEInfo(), isTomEE() was calling it already
- Add missing validation for Tomcat 5.0
- tomcat-coyote.jar location is different for tomcat 5.0 and 5.5
- Add Tests for new TomEEtype logic
- Add Tests for new Tomcat version comparison
- Add Tests for new TomEE version comparison
- Use generics
- Use StandardCharsets
- Use try-with-resources
- Add @OverRide tags
- Bump javac.source to 1.8
- Add missing attributes to a dtd file
  • Loading branch information
pepness authored and neilcsmith-net committed Jan 11, 2023
1 parent 5c1ee2a commit 6227036
Show file tree
Hide file tree
Showing 19 changed files with 488 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AntDeploymentProviderImpl implements AntDeploymentProvider {

private final TomcatManager tm;

private static final Logger LOGGER = Logger.getLogger("org.netbeans.modules.tomcat5"); // NOI18N
private static final Logger LOGGER = Logger.getLogger(AntDeploymentProviderImpl.class.getName()); // NOI18N

public AntDeploymentProviderImpl(DeploymentManager dm) {
tm = (TomcatManager)dm;
Expand All @@ -49,6 +49,11 @@ public AntDeploymentProviderImpl(DeploymentManager dm) {
public void writeDeploymentScript(OutputStream os, Object moduleType) throws IOException {
String name = null;
switch (tm.getTomcatVersion()) {
case TOMCAT_110:
case TOMCAT_101:
case TOMCAT_100:
case TOMCAT_90:
case TOMCAT_80:
case TOMCAT_70:
name = "resources/tomcat-ant-deploy70.xml";
break;
Expand Down
112 changes: 86 additions & 26 deletions enterprise/tomcat5/src/org/netbeans/modules/tomcat5/TomcatFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import org.netbeans.modules.tomcat5.deploy.TomcatManager;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -50,12 +47,13 @@
import org.netbeans.modules.tomcat5.deploy.TomcatManager.TomEEVersion;
import org.netbeans.modules.tomcat5.deploy.TomcatManager.TomcatVersion;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;

/**
* Factory capable to create DeploymentManager that can deploy to Tomcat 5 and 6.
* Factory capable to create DeploymentManager that can deploy to Tomcat and TomEE.
*
* Tomcat URI has following format:
* <PRE><CODE>tomcat[55|60]:[home=&lt;home_path&gt;:[base=&lt;base_path&gt;:]]&lt;manager_app_url&gt;</CODE></PRE>
* <PRE><CODE>tomcat[90|100]:[home=&lt;home_path&gt;:[base=&lt;base_path&gt;:]]&lt;manager_app_url&gt;</CODE></PRE>
* for example
* <PRE><CODE>tomcat:http://localhost:8080/manager/</CODE></PRE>
* where paths values will be used as CATALINA_HOME and CATALINA_BASE properties and manager_app_url
Expand All @@ -70,23 +68,35 @@ public final class TomcatFactory implements DeploymentFactory {
public static final String SERVER_ID_70 = "Tomcat70"; // NOI18N
public static final String SERVER_ID_80 = "Tomcat80"; // NOI18N
public static final String SERVER_ID_90 = "Tomcat90"; // NOI18N
public static final String SERVER_ID_100 = "Tomcat100"; // NOI18N
public static final String SERVER_ID_101 = "Tomcat101"; // NOI18N
public static final String SERVER_ID_110 = "Tomcat110"; // NOI18N

public static final String TOMCAT_URI_PREFIX_50 = "tomcat:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_55 = "tomcat55:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_60 = "tomcat60:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_70 = "tomcat70:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_80 = "tomcat80:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_90 = "tomcat90:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_100 = "tomcat100:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_101 = "tomcat101:"; // NOI18N
public static final String TOMCAT_URI_PREFIX_110 = "tomcat110:"; // NOI18N

public static final String TOMCAT_URI_HOME_PREFIX = "home="; // NOI18N
public static final String TOMCAT_URI_BASE_PREFIX = ":base="; // NOI18N

private static final Pattern TOMEE_JAR_PATTERN = Pattern.compile("tomee-common-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

private static final Pattern TOMEE_JAXRS_JAR_PATTERN = Pattern.compile("tomee-jaxrs-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

private static final Pattern TOMEE_GERONIMO_JAR_PATTERN = Pattern.compile("geronimo-connector-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_JAR_PATTERN = Pattern.compile("tomee-common-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_WEBPROFILE_JAR_PATTERN = Pattern.compile("openejb-api-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_JAXRS_JAR_PATTERN = Pattern.compile("jettison-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_MICROPROFILE_JAR_PATTERN = Pattern.compile("microprofile-config-api-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_PLUS_JAR_PATTERN = Pattern.compile("activemq-protobuf-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

static final Pattern TOMEE_PLUME_JAR_PATTERN = Pattern.compile("eclipselink-(\\d+(\\.\\d+)*).*\\.jar"); // NOI18N

private static final String GENERIC_DISCONNECTED_URI_PREFIX = "tomcat-any:"; // NOI18N
private static final String GENERIC_DISCONNECTED_URI =
GENERIC_DISCONNECTED_URI_PREFIX + "jakarta-tomcat-generic"; // NOI18N
Expand All @@ -96,19 +106,23 @@ public final class TomcatFactory implements DeploymentFactory {
private static final String DISCONNECTED_URI_70 = TOMCAT_URI_PREFIX_70 + "apache-tomcat-7.0.x"; // NOI18N
private static final String DISCONNECTED_URI_80 = TOMCAT_URI_PREFIX_80 + "apache-tomcat-8.0.x"; // NOI18N
private static final String DISCONNECTED_URI_90 = TOMCAT_URI_PREFIX_90 + "apache-tomcat-9.0.x"; // NOI18N
private static final String DISCONNECTED_URI_100 = TOMCAT_URI_PREFIX_100 + "apache-tomcat-10.0.x"; // NOI18N
private static final String DISCONNECTED_URI_101 = TOMCAT_URI_PREFIX_101 + "apache-tomcat-10.1.x"; // NOI18N
private static final String DISCONNECTED_URI_110 = TOMCAT_URI_PREFIX_110 + "apache-tomcat-11.0.x"; // NOI18N

private static final Set<String> DISCONNECTED_URIS = new HashSet<>();
static {
Collections.addAll(DISCONNECTED_URIS, DISCONNECTED_URI_50,
DISCONNECTED_URI_55, DISCONNECTED_URI_60, DISCONNECTED_URI_70,
DISCONNECTED_URI_80, DISCONNECTED_URI_90, GENERIC_DISCONNECTED_URI);
DISCONNECTED_URI_80, DISCONNECTED_URI_90, DISCONNECTED_URI_100,
DISCONNECTED_URI_101, DISCONNECTED_URI_110, GENERIC_DISCONNECTED_URI);
}

private static TomcatFactory instance;

private static final WeakHashMap managerCache = new WeakHashMap();

private static final Logger LOGGER = Logger.getLogger("org.netbeans.modules.tomcat5"); // NOI18N
private static final Logger LOGGER = Logger.getLogger(TomcatFactory.class.getName()); // NOI18N

private TomcatFactory() {
super();
Expand Down Expand Up @@ -193,24 +207,30 @@ public boolean handlesURI(String str) {
|| str.startsWith(TOMCAT_URI_PREFIX_60)
|| str.startsWith(TOMCAT_URI_PREFIX_70)
|| str.startsWith(TOMCAT_URI_PREFIX_80)
|| str.startsWith(TOMCAT_URI_PREFIX_90));
|| str.startsWith(TOMCAT_URI_PREFIX_90)
|| str.startsWith(TOMCAT_URI_PREFIX_100)
|| str.startsWith(TOMCAT_URI_PREFIX_101)
|| str.startsWith(TOMCAT_URI_PREFIX_110));
}

/**
* Retrieve the tomcat version e.g. '6.0.10'
* Retrieve the tomcat version e.g. '9.0.70'
*
* @throws IllegalStateException if the version information cannot be retrieved
*/
public static String getTomcatVersionString(File catalinaHome) throws IllegalStateException {
File catalinaJar = new File(catalinaHome, "lib/catalina.jar"); // NOI18N
File coyoteJar = new File(catalinaHome, "lib/tomcat-coyote.jar"); // NOI18N
if (!catalinaJar.exists()) {
// For Tomcat 5/5.5
catalinaJar = new File(catalinaHome, "server/lib/catalina.jar"); // NOI18N
coyoteJar = new File(catalinaHome, "server/lib/tomcat-coyote.jar"); // NOI18N
}
File coyoteJar = new File(catalinaHome, "lib/tomcat-coyote.jar"); // NOI18N

try {
URLClassLoader loader = new URLClassLoader(new URL[] {
catalinaJar.toURI().toURL(), coyoteJar.toURI().toURL() });
Utilities.toURI(catalinaJar).toURL(), Utilities.toURI(coyoteJar).toURL() });

Class serverInfo = loader.loadClass("org.apache.catalina.util.ServerInfo"); // NOI18N
try {
Method method = serverInfo.getMethod("getServerNumber", new Class[] {}); // NOI18N
Expand Down Expand Up @@ -242,7 +262,7 @@ public static TomcatVersion getTomcatVersion(File catalinaHome) throws IllegalSt
LOGGER.log(Level.INFO, null, ex);
return getTomcatVersionFallback(catalinaHome);
}
return getTomcatVersion(version, TomcatVersion.TOMCAT_50);
return getTomcatVersion(version, TomcatVersion.TOMCAT_80);
}

private static TomcatVersion getTomcatVersionFallback(File catalinaHome) throws IllegalStateException {
Expand Down Expand Up @@ -280,7 +300,9 @@ private static TomcatVersion getTomcatVersionFallback(File catalinaHome) throws
}

private static TomcatVersion getTomcatVersion(String version, TomcatVersion defaultVersion) throws IllegalStateException {
if (version.startsWith("5.5.")) { // NOI18N
if (version.startsWith("5.0.")) { // NOI18N
return TomcatVersion.TOMCAT_50;
} else if (version.startsWith("5.5.")) { // NOI18N
return TomcatVersion.TOMCAT_55;
} else if (version.startsWith("6.")) { // NOI18N
return TomcatVersion.TOMCAT_60;
Expand All @@ -290,6 +312,12 @@ private static TomcatVersion getTomcatVersion(String version, TomcatVersion defa
return TomcatVersion.TOMCAT_80;
} else if (version.startsWith("9.")) { // NOI18N
return TomcatVersion.TOMCAT_90;
} else if (version.startsWith("10.0")) { // NOI18N
return TomcatVersion.TOMCAT_100;
} else if (version.startsWith("10.1")) { // NOI18N
return TomcatVersion.TOMCAT_101;
} else if (version.startsWith("11.")) { // NOI18N
return TomcatVersion.TOMCAT_110;
}
int dotIndex = version.indexOf('.');
if (dotIndex > 0) {
Expand All @@ -307,7 +335,13 @@ private static TomcatVersion getTomcatVersion(String version, TomcatVersion defa
}

private static TomcatVersion getTomcatVersion(String uri) throws IllegalStateException {
if (uri.startsWith(TOMCAT_URI_PREFIX_90)) {
if (uri.startsWith(TOMCAT_URI_PREFIX_110)) {
return TomcatVersion.TOMCAT_110;
} else if (uri.startsWith(TOMCAT_URI_PREFIX_101)) {
return TomcatVersion.TOMCAT_101;
} else if (uri.startsWith(TOMCAT_URI_PREFIX_100)) {
return TomcatVersion.TOMCAT_100;
} else if (uri.startsWith(TOMCAT_URI_PREFIX_90)) {
return TomcatVersion.TOMCAT_90;
} else if (uri.startsWith(TOMCAT_URI_PREFIX_80)) {
return TomcatVersion.TOMCAT_80;
Expand Down Expand Up @@ -336,17 +370,29 @@ public static TomEEType getTomEEType(File catalinaHome, File catalinaBase)
@NonNull
public static TomEEType getTomEEType(@NonNull File libFolder) {
File[] children = libFolder.listFiles();
TomEEType type = TomEEType.TOMEE_WEB;
TomEEType type = TomEEType.TOMEE_OPENEJB;
if (children != null) {
for (File file : children) {
if (TOMEE_JAXRS_JAR_PATTERN.matcher(file.getName()).matches()) {
if (type.ordinal() < TomEEType.TOMEE_JAXRS.ordinal()) {
type = TomEEType.TOMEE_JAXRS;
if (TOMEE_PLUME_JAR_PATTERN.matcher(file.getName()).matches()) {
if(type.ordinal() < TomEEType.TOMEE_PLUME.ordinal()) {
return TomEEType.TOMEE_PLUME;
}
} else if (TOMEE_GERONIMO_JAR_PATTERN.matcher(file.getName()).matches()) {
if (type.ordinal() < TomEEType.TOMEE_PLUS.ordinal()) {
} else if (TOMEE_PLUS_JAR_PATTERN.matcher(file.getName()).matches()) {
if(type.ordinal() < TomEEType.TOMEE_PLUS.ordinal()) {
type = TomEEType.TOMEE_PLUS;
}
} else if (TOMEE_MICROPROFILE_JAR_PATTERN.matcher(file.getName()).matches()) {
if(type.ordinal() < TomEEType.TOMEE_MICROPROFILE.ordinal()) {
type = TomEEType.TOMEE_MICROPROFILE;
}
} else if (TOMEE_JAXRS_JAR_PATTERN.matcher(file.getName()).matches()) {
if(type.ordinal() < TomEEType.TOMEE_JAXRS.ordinal()) {
type = TomEEType.TOMEE_JAXRS;
}
} else if (TOMEE_JAR_PATTERN.matcher(file.getName()).matches()) {
if(type.ordinal() < TomEEType.TOMEE_WEBPROFILE.ordinal()) {
type = TomEEType.TOMEE_WEBPROFILE;
}
}
}
}
Expand Down Expand Up @@ -411,6 +457,14 @@ private static TomEEVersion getTomEEVersion(String version, TomEEVersion default
return TomcatManager.TomEEVersion.TOMEE_16;
} else if (version.startsWith("1.7.")) { // NOI18N
return TomcatManager.TomEEVersion.TOMEE_17;
} else if (version.startsWith("7.")) { // NOI18N
return TomcatManager.TomEEVersion.TOMEE_70;
} else if (version.startsWith("7.1.")) { // NOI18N
return TomcatManager.TomEEVersion.TOMEE_71;
} else if (version.startsWith("8.")) { // NOI18N
return TomcatManager.TomEEVersion.TOMEE_80;
} else if (version.startsWith("9.")) { // NOI18N
return TomcatManager.TomEEVersion.TOMEE_90;
}
return defaultVersion;
}
Expand All @@ -420,6 +474,12 @@ private static String stripUriPrefix(String uri, TomcatVersion tomcatVersion) {
return uri.substring(GENERIC_DISCONNECTED_URI_PREFIX.length());
}
switch (tomcatVersion) {
case TOMCAT_110:
return uri.substring(TomcatFactory.TOMCAT_URI_PREFIX_110.length());
case TOMCAT_101:
return uri.substring(TomcatFactory.TOMCAT_URI_PREFIX_101.length());
case TOMCAT_100:
return uri.substring(TomcatFactory.TOMCAT_URI_PREFIX_100.length());
case TOMCAT_90:
return uri.substring(TomcatFactory.TOMCAT_URI_PREFIX_90.length());
case TOMCAT_80:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class TomcatModuleConfiguration implements ModuleConfiguration, ContextRo

private static final String ATTR_PATH = "path"; // NOI18N

private static final Logger LOGGER = Logger.getLogger("org.netbeans.modules.tomcat5"); // NOI18N
private static final Logger LOGGER = Logger.getLogger(TomcatModuleConfiguration.class.getName()); // NOI18N

/** Creates a new instance of TomcatModuleConfiguration */
public TomcatModuleConfiguration(J2eeModule j2eeModule, TomcatVersion tomcatVersion, TomEEVersion tomeeVersion) {
Expand Down Expand Up @@ -224,7 +224,7 @@ public Set<Datasource> getDatasources() throws ConfigurationException {
Context context = getContext();
Set<Datasource> result = new HashSet<>();
int length = context.getResource().length;
if (tomcatVersion != TomcatVersion.TOMCAT_50) {
if (tomcatVersion.isAtLeast(TomcatVersion.TOMCAT_55)) {
// Tomcat 5.5.x or Tomcat 6.0.x
for (int i = 0; i < length; i++) {
String type = context.getResourceType(i);
Expand Down Expand Up @@ -292,7 +292,7 @@ public Datasource createDatasource(final String name, final String url,
if (conflictingDS.size() > 0) {
throw new DatasourceAlreadyExistsException(conflictingDS);
}
if (tomcatVersion != TomcatVersion.TOMCAT_50) {
if (tomcatVersion.isAtLeast(TomcatVersion.TOMCAT_55)) {
if (tomeeVersion != null) {
// we need to store it to resources.xml
TomeeResources resources = getResources(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.DefaultComboBoxModel;
Expand All @@ -39,6 +41,7 @@
import org.netbeans.api.java.platform.JavaPlatformManager;
import org.netbeans.api.java.platform.Specification;
import org.netbeans.modules.tomcat5.deploy.TomcatManager;
import org.netbeans.modules.tomcat5.j2ee.TomcatPlatformImpl;
import org.netbeans.modules.tomcat5.util.TomcatProperties;
import org.openide.util.Exceptions;

Expand Down Expand Up @@ -372,32 +375,54 @@ public boolean useManagerScript() {
public void loadJvmModel() {
JavaPlatformManager jpm = JavaPlatformManager.getDefault();
JavaPlatformAdapter curJvm = (JavaPlatformAdapter)jvmModel.getSelectedItem();
String curPlatformName = null;
final String curPlatformName;
if (curJvm != null) {
curPlatformName = curJvm.getName();
} else {
curPlatformName = (String)tp.getJavaPlatform().getProperties().get(TomcatProperties.PLAT_PROP_ANT_NAME);
}

jvmModel.removeAllElements();

// feed the combo with sorted platform list
// Supported jvm platforms for this version of Tomcat or TomEE
TomcatPlatformImpl tomcatPlatformImpl = new TomcatPlatformImpl(tm);
Set<String> tomcatPlatforms = tomcatPlatformImpl.getSupportedJavaPlatformVersions();

// jvm platforms registered in NetBeans
JavaPlatform[] j2sePlatforms = jpm.getPlatforms(null, new Specification("J2SE", null)); // NOI18N
JavaPlatformAdapter[] platformAdapters = new JavaPlatformAdapter[j2sePlatforms.length];
for (int i = 0; i < platformAdapters.length; i++) {
platformAdapters[i] = new JavaPlatformAdapter(j2sePlatforms[i]);
}
Arrays.sort(platformAdapters);
for (int i = 0; i < platformAdapters.length; i++) {
JavaPlatformAdapter platformAdapter = platformAdapters[i];
jvmModel.addElement(platformAdapter);
// try to set selected item

Set<JavaPlatformAdapter> platformAdapters = new TreeSet<>();

// Only add the jvm platforms that are supported from the registered set
for (JavaPlatform jp : j2sePlatforms) {
if (tomcatPlatforms.contains(jp.getSpecification().getVersion().toString())) {
platformAdapters.add(new JavaPlatformAdapter(jp));
}
}

if (platformAdapters.isEmpty()) {
jvmModel.setSelectedItem(null);
return;
} else {
for (JavaPlatformAdapter platformAdapter : platformAdapters) {
jvmModel.addElement(platformAdapter);
}
}

// try to set selected item
for (JavaPlatformAdapter j2sePlatform : platformAdapters) {
if (curPlatformName != null) {
if (curPlatformName.equals(platformAdapter.getName())) {
jvmModel.setSelectedItem(platformAdapter);
if (curPlatformName.equals(j2sePlatform.getName())) {
jvmModel.setSelectedItem(j2sePlatform);
// if we do not change the flag the jvm will not change
jvmModelFlag = true;
break;
} else {
jvmModel.setSelectedItem(j2sePlatform);
jvmModelFlag = true;
}
}
}
}

}

// model getters ----------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import javax.swing.JSpinner;
import javax.accessibility.AccessibleContext;
import java.awt.Font;
import org.netbeans.modules.tomcat5.deploy.TomcatManager.TomcatVersion;

/**
* Customizer general (connection) tab.
Expand Down
Loading

0 comments on commit 6227036

Please sign in to comment.