Skip to content

Commit

Permalink
Build Jersey on JDK13 (#4341)
Browse files Browse the repository at this point in the history
* Able to compile with jdk13;  Added in new doclet

Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos authored and jansupol committed Dec 18, 2019
1 parent 29913b9 commit 01b7075
Show file tree
Hide file tree
Showing 14 changed files with 1,073 additions and 130 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# maven noise
target/
target-*/

# gradle noise
.gradle
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
jdk:
- oraclejdk8
- openjdk11
- openjdk13

cache:
directories:
Expand Down
139 changes: 138 additions & 1 deletion ext/wadl-doclet/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) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2012, 2019 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
Expand Down Expand Up @@ -32,6 +32,14 @@
of resource classes, so that this can be used for extending generated wadl with useful
documentation.
</description>

<properties>
<java.sourceDirectory>${project.basedir}/src/main/java</java.sourceDirectory>
<java8_11.build.outputDirectory>${project.basedir}/target</java8_11.build.outputDirectory>
<java8_11.sourceDirectory>${project.basedir}/src/main/java8_11</java8_11.sourceDirectory>
<java12.build.outputDirectory>${project.basedir}/target-java12</java12.build.outputDirectory>
<java12.sourceDirectory>${project.basedir}/src/main/java12</java12.sourceDirectory>
</properties>


<profiles>
Expand Down Expand Up @@ -65,6 +73,134 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>jdk1.8_11</id>
<activation>
<jdk>[1.8,12)</jdk>
</activation>
<build>
<directory>${java8_11.build.outputDirectory}</directory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${java.sourceDirectory}</source>
<source>${java8_11.sourceDirectory}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdk12+</id>
<activation>
<jdk>[12,)</jdk>
</activation>
<build>
<directory>${java12.build.outputDirectory}</directory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${java.sourceDirectory}</source>
<source>${java12.sourceDirectory}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Only multi-release is supported when compiling from higher version to lower -->
<profile>
<id>copyJDK12FilesToMultiReleaseJar</id>
<activation>
<file>
<!-- ${java12.build.outputDirectory} does not work here -->
<exists>target-java12/classes/org/glassfish/jersey/wadl/doclet/ResourceDoclet.class</exists>
</file>
<jdk>[1.8,12)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Multi-Release>true</Multi-Release>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>copy-jdk12-classes</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${java8_11.build.outputDirectory}/classes/META-INF/versions/12</outputDirectory>
<resources>
<resource>
<directory>${java12.build.outputDirectory}/classes</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>copy-jdk12-sources</id>
<phase>verify</phase>
<configuration>
<target>
<property name="sources-jar" value="${java8_11.build.outputDirectory}/${project.artifactId}-${project.version}-sources.jar"/>
<echo>sources-jar: ${sources-jar}</echo>
<zip destfile="${sources-jar}" update="true">
<zipfileset dir="${java12.sourceDirectory}" prefix="META-INF/versions/12"/>
</zip>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>tools.jar</id>
<activation>
Expand Down Expand Up @@ -105,4 +241,5 @@
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2019 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.wadl.doclet;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.glassfish.jersey.server.wadl.internal.generators.resourcedoc.model.ResourceDocType;

class DocletUtils {

private static final Logger LOG = Logger.getLogger(DocletUtils.class.getName());

private static String[] getCDataElements(DocProcessor docProcessor) {
String[] original = new String[]{"ns1^commentText", "ns2^commentText", "^commentText" };
if (docProcessor == null) {
return original;
} else {
String[] cdataElements = docProcessor.getCDataElements();
if (cdataElements == null || cdataElements.length == 0) {
return original;
} else {

String[] result = copyOf(original, original.length + cdataElements.length);
for (int i = 0; i < cdataElements.length; i++) {
result[original.length + i] = cdataElements[i];
}
return result;
}
}
}

@SuppressWarnings("unchecked")
private static <T, U> T[] copyOf(U[] original, int newLength) {
T[] copy = (original.getClass() == Object[].class) ? (T[]) new Object[newLength]
: (T[]) Array.newInstance(original.getClass().getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
}

private static XMLSerializer getXMLSerializer(OutputStream os, String[] cdataElements)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
// configure an OutputFormat to handle CDATA
OutputFormat of = new OutputFormat();

// specify which of your elements you want to be handled as CDATA.
// The use of the '^' between the namespaceURI and the localname
// seems to be an implementation detail of the xerces code.
// When processing xml that doesn't use namespaces, simply omit the
// namespace prefix as shown in the third CDataElement below.
of.setCDataElements(cdataElements);

// set any other options you'd like
of.setPreserveSpace(true);
of.setIndenting(true);

// create the serializer
XMLSerializer serializer = new XMLSerializer(of);

serializer.setOutputByteStream(os);

return serializer;
}

private static Class<?>[] getJAXBContextClasses(ResourceDocType result, DocProcessor docProcessor) {
Class<?>[] clazzes;
if (docProcessor == null) {
clazzes = new Class<?>[1];
} else {
Class<?>[] requiredJaxbContextClasses = docProcessor.getRequiredJaxbContextClasses();
if (requiredJaxbContextClasses != null) {
clazzes = new Class<?>[1 + requiredJaxbContextClasses.length];
for (int i = 0; i < requiredJaxbContextClasses.length; i++) {
clazzes[i + 1] = requiredJaxbContextClasses[i];
}
} else {
clazzes = new Class<?>[1];
}
}
clazzes[0] = result.getClass();
return clazzes;
}

static boolean createOutputFile(String filePath, DocProcessor docProcessor, ResourceDocType result) {
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(filePath))) {
Class<?>[] clazzes = getJAXBContextClasses(result, docProcessor);
JAXBContext c = JAXBContext.newInstance(clazzes);
Marshaller m = c.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
String[] cdataElements = getCDataElements(docProcessor);
XMLSerializer serializer = getXMLSerializer(out, cdataElements);
m.marshal(result, serializer);
LOG.info("Wrote " + result);
return true;
} catch (Exception e) {
LOG.log(Level.SEVERE, "Could not serialize ResourceDoc.", e);
return false;
}
}

static String getLinkClass(String className, String field) {
Object object;
try {
Field declaredField = Class.forName(className, false, Thread.currentThread()
.getContextClassLoader()).getDeclaredField(field);
declaredField.setAccessible(true);
object = declaredField.get(null);
LOG.log(Level.FINE, "Got object " + object);
} catch (final Exception e) {
LOG.info("Have classloader: " + ResourceDoclet.class.getClassLoader().getClass());
LOG.info("Have thread classloader " + Thread.currentThread().getContextClassLoader().getClass());
LOG.info("Have system classloader " + ClassLoader.getSystemClassLoader().getClass());
LOG.log(Level.SEVERE, "Could not get field " + className, e);
return null;
}

/* marshal the bean to xml
*/
try {
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
StringWriter stringWriter = new StringWriter();
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, stringWriter);
String result = stringWriter.getBuffer().toString();
LOG.log(Level.FINE, "Got marshalled output:\n" + result);
return result;
} catch (final Exception e) {
LOG.log(Level.SEVERE, "Could serialize bean to xml: " + object, e);
return null;
}
}

}
Loading

0 comments on commit 01b7075

Please sign in to comment.