Skip to content

Commit

Permalink
Make JAX-B API optional (#4634)
Browse files Browse the repository at this point in the history
* Make JAX-B API optional

Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol authored Dec 14, 2020
1 parent 21e8faf commit 9edb3d4
Showing 31 changed files with 367 additions and 191 deletions.
15 changes: 15 additions & 0 deletions bundles/jaxrs-ri/pom.xml
Original file line number Diff line number Diff line change
@@ -285,6 +285,21 @@
javax.persistence.*;resolution:=optional,
javax.validation.*;resolution:=optional;version="${range;[==,3);${javax.validation.api.version}}",
sun.misc.*;resolution:=optional,
javax.activation.*;version="!";resolution:=optional,
javax.imageio;resolution:=optional,
javax.imageio.spi;resolution:=optional,
javax.imageio.stream;resolution:=optional,
javax.xml.bind;version="!";resolution:=optional,
javax.xml.bind.annotation;version="!";resolution:=optional,
javax.xml.bind.annotation.adapters;version="!";resolution:=optional,
javax.xml.namespace;resolution:=optional,
javax.xml.parsers;resolution:=optional,
javax.xml.transform;resolution:=optional,
javax.xml.transform.dom;resolution:=optional,
javax.xml.transform.sax;resolution:=optional,
javax.xml.transform.stream;resolution:=optional,
org.w3c.dom;resolution:=optional,
org.xml.sax;resolution:=optional,
${hk2.osgi.version},
*
]]></Import-Package>
8 changes: 7 additions & 1 deletion connectors/jetty-connector/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2011, 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
@@ -41,6 +41,12 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
19 changes: 17 additions & 2 deletions core-common/pom.xml
Original file line number Diff line number Diff line change
@@ -126,7 +126,21 @@
<!-- Note: When you're changing these properties change them also in bundles/jaxrs-ri/pom.xml. -->
<Import-Package>
sun.misc.*;resolution:=optional,
javax.activation.*;version="!",
javax.activation.*;version="!";resolution:=optional,
javax.imageio;resolution:=optional,
javax.imageio.spi;resolution:=optional,
javax.imageio.stream;resolution:=optional,
javax.xml.bind;version="!";resolution:=optional,
javax.xml.bind.annotation;version="!";resolution:=optional,
javax.xml.bind.annotation.adapters;version="!";resolution:=optional,
javax.xml.namespace;resolution:=optional,
javax.xml.parsers;resolution:=optional,
javax.xml.transform;resolution:=optional,
javax.xml.transform.dom;resolution:=optional,
javax.xml.transform.sax;resolution:=optional,
javax.xml.transform.stream;resolution:=optional,
org.w3c.dom;resolution:=optional,
org.xml.sax;resolution:=optional,
${javax.annotation.osgi.version},
*
</Import-Package>
@@ -297,6 +311,8 @@
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
@@ -686,7 +702,6 @@
</plugins>
</build>
</profile>

</profiles>

<properties>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -230,13 +230,15 @@ public final class CommonProperties {
* Disable some of the default providers from being loaded. The following providers extend application footprint
* by XML dependencies, which is too heavy for native image, or by AWT which may possibly be not available by JDK 11 desktop:
* <ul>
* <li>javax.activation.DataSource</li>
* <li>java.awt.image.RenderedImage</li>
* <li>javax.xml.transform.Source</li>
* <li>javax.xml.transform.dom.DOMSource</li>
* <li>javax.xml.transform.sax.SAXSource</li>
* <li>javax.xml.transform.stream.StreamSource</li>
* </ul>
* The following are the options to disable the provides: {@code DOMSOURCE, RENDEREDIMAGE, SAXSOURCE, SOURCE, STREAMSOURCE},
* The following are the options to disable the provides:
* {@code DATASOURCE, DOMSOURCE, RENDEREDIMAGE, SAXSOURCE, SOURCE, STREAMSOURCE},
* or to disable all: {@code ALL}. Multiple options can be disabled by adding multiple comma separated values.
*
* @since 2.30
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -1557,4 +1557,20 @@ public static Class<?> getRawClass(Type type) {

return null;
}

/**
* Returns true iff JAX-B API is available on classpath.
*/
public static boolean isJaxbAvailable() {
final Class<?> aClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA("javax.xml.bind.JAXBException"));
return aClass != null;
}

/**
* Returns true iff javax.xml.transform package is available on classpath.
*/
public static boolean isXmlTransformAvailable() {
final Class<?> aClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA("javax.xml.transform.Source"));
return aClass != null;
}
}
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ protected void configure() {

// Message body providers (both readers & writers)
bindSingletonWorker(ByteArrayProvider.class);
bindSingletonWorker(DataSourceProvider.class);
// bindSingletonWorker(DataSourceProvider.class);
bindSingletonWorker(FileProvider.class);
bindSingletonWorker(FormMultivaluedMapProvider.class);
bindSingletonWorker(FormProvider.class);
@@ -158,6 +158,7 @@ public Set<HeaderDelegateProvider> getHeaderDelegateProviders() {

private static final class EnabledProvidersBinder {
private enum Provider {
DATASOURCE("javax.activation.DataSource"),
DOMSOURCE("javax.xml.transform.dom.DOMSource"),
RENDEREDIMAGE("java.awt.image.RenderedImage"),
SAXSOURCE("javax.xml.transform.sax.SAXSource"),
@@ -200,6 +201,9 @@ private void bindToBinder(AbstractBinder binder) {
for (Provider provider : enabledProviders) {
if (isClass(provider.className)) {
switch (provider) {
case DATASOURCE:
providerBinder = new DataSourceBinder();
break;
case DOMSOURCE:
providerBinder = new DomSourceBinder();
break;
@@ -226,6 +230,7 @@ private void bindToBinder(AbstractBinder binder) {
"MessageBodyReader<" + provider.className + ">")
);
break;
case DATASOURCE:
case RENDEREDIMAGE:
case SOURCE:
LOGGER.warning(LocalizationMessages.DEPENDENT_CLASS_OF_DEFAULT_PROVIDER_NOT_FOUND(provider.className,
@@ -241,11 +246,18 @@ private static boolean isClass(String className) {
return null != AccessController.doPrivileged(ReflectionHelper.classForNamePA(className));
}


private interface ProviderBinder {
void bind(AbstractBinder binder, Provider provider);
}

private static class DataSourceBinder implements ProviderBinder {
@Override
public void bind(AbstractBinder binder, Provider provider) {
binder.bind(DataSourceProvider.class)
.to(MessageBodyReader.class).to(MessageBodyWriter.class).in(Singleton.class);
}
}

private static class DomSourceBinder implements ProviderBinder {
@Override
public void bind(AbstractBinder binder, Provider provider) {
33 changes: 21 additions & 12 deletions core-server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
@@ -95,6 +95,14 @@
</Export-Package>
<Import-Package>
${javax.annotation.osgi.version},
javax.xml.bind;version="!";resolution:=optional,
javax.xml.bind.annotation;version="!";resolution:=optional,
javax.xml.bind.annotation.adapters;version="!";resolution:=optional,
javax.xml.namespace;resolution:=optional,
javax.xml.parsers;resolution:=optional,
javax.xml.transform;resolution:=optional,
javax.xml.transform.sax;resolution:=optional,
javax.xml.transform.stream;resolution:=optional,
javax.validation.*;resolution:=optional;version="${range;[==,3);${javax.validation.api.version}}",
*
</Import-Package>
@@ -172,12 +180,6 @@
<artifactId>jakarta.ws.rs-api</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
@@ -198,7 +200,18 @@
<artifactId>org.osgi.core</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -232,10 +245,6 @@
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-osgi</artifactId>
Original file line number Diff line number Diff line change
@@ -20,15 +20,14 @@
import javax.ws.rs.core.FeatureContext;

import javax.inject.Singleton;
import javax.xml.bind.JAXBException;

import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.internal.util.PropertiesHelper;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.server.model.ModelProcessor;
import org.glassfish.jersey.server.wadl.internal.WadlApplicationContextImpl;
import org.glassfish.jersey.server.wadl.internal.generators.WadlGeneratorJAXBGrammarGenerator;
import org.glassfish.jersey.server.wadl.processor.WadlModelProcessor;

import java.util.logging.Logger;
@@ -53,7 +52,17 @@ public boolean configure(FeatureContext context) {
return false;
}

if (!isJaxB()) {
if (!ReflectionHelper.isJaxbAvailable()) {
LOGGER.warning(LocalizationMessages.WADL_FEATURE_DISABLED_NOJAXB());
return false;
}

if (!ReflectionHelper.isXmlTransformAvailable()) {
LOGGER.warning(LocalizationMessages.WADL_FEATURE_DISABLED_NOTRANSFORM());
return false;
}

if (!WadlApplicationContextImpl.isJaxbImplAvailable()) {
LOGGER.warning(LocalizationMessages.WADL_FEATURE_DISABLED());
return false;
}
@@ -68,12 +77,4 @@ protected void configure() {

return true;
}

private static boolean isJaxB() {
try {
return null != WadlApplicationContextImpl.getJAXBContextFromWadlGenerator(new WadlGeneratorJAXBGrammarGenerator());
} catch (JAXBException je) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@
import com.sun.research.ws.wadl.Include;
import com.sun.research.ws.wadl.Resource;
import com.sun.research.ws.wadl.Resources;
import org.glassfish.jersey.server.wadl.internal.generators.WadlGeneratorJAXBGrammarGenerator;

/**
* WADL application context implementation.
@@ -263,4 +264,12 @@ private void attachExternalGrammar(
throw new ProcessingException(LocalizationMessages.ERROR_WADL_EXTERNAL_GRAMMAR(), e);
}
}

public static boolean isJaxbImplAvailable() {
try {
return null != WadlApplicationContextImpl.getJAXBContextFromWadlGenerator(new WadlGeneratorJAXBGrammarGenerator());
} catch (JAXBException je) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -185,6 +185,8 @@ unsupported.uri.injection.type="@Uri"-based injection of "{0}" type is not suppo
user.not.authorized=User not authorized.
wadl.doc.extended.wadl=This is full WADL including extended resources. To get simplified WADL with users resources only do not use the query parameter {0}. Link: {1}
wadl.doc.simple.wadl=This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter {0}. Link: {1}
wadl.feature.disabled.nojaxb=JAX-B API not found . WADL feature is disabled.
wadl.feature.disabled.notransform=javax.xml.transform package could not be found. WADL feature is disabled.
wadl.feature.disabled=JAXBContext implementation could not be found. WADL feature is disabled.
wadl.jaxb.context.fallback=Error creating a JAXBContext for wadl serialization. Trying a fallback solution for osgi environments.
wadl.resourcedoc.ambiguous.method.entries=Ambiguous resource documentation detected: \
4 changes: 4 additions & 0 deletions examples/bookstore-webapp/pom.xml
Original file line number Diff line number Diff line change
@@ -63,6 +63,10 @@
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
</dependencies>

<build>
6 changes: 5 additions & 1 deletion examples/https-clientserver-grizzly/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -49,6 +49,10 @@
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
6 changes: 5 additions & 1 deletion examples/jaxb/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -67,6 +67,10 @@
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-osgi</artifactId>
Loading

0 comments on commit 9edb3d4

Please sign in to comment.