From 327ec009227669552217f127546bb9cd130834fd Mon Sep 17 00:00:00 2001 From: Christian Baumann Date: Mon, 18 Jan 2021 16:21:03 +0100 Subject: [PATCH] refs #419 - Make JAXB within org.eclipse.fx.core work with Java 11 Signed-off-by: Christian Baumann --- .../org.eclipse.fx.core/META-INF/MANIFEST.MF | 8 +- .../core/internal/JAXBObjectSerializer.java | 181 ++++++++++-------- releng/org.eclipse.fx.releng/pom.xml | 5 + 3 files changed, 114 insertions(+), 80 deletions(-) diff --git a/modules/core/org.eclipse.fx.core/META-INF/MANIFEST.MF b/modules/core/org.eclipse.fx.core/META-INF/MANIFEST.MF index 30b4762c1..8b0af7b23 100755 --- a/modules/core/org.eclipse.fx.core/META-INF/MANIFEST.MF +++ b/modules/core/org.eclipse.fx.core/META-INF/MANIFEST.MF @@ -10,6 +10,11 @@ Import-Package: javax.annotation;resolution:=optional, org.apache.commons.lang.text;version="2.6.0", org.osgi.framework;version="1.8.0";resolution:=optional, org.osgi.service.component.annotations;version="1.2.0";resolution:=optional, + javax.xml.bind.annotation;resolution:=optional, + javax.xml.bind.annotation.adapters;resolution:=optional, + javax.xml.bind.attachment;resolution:=optional, + javax.xml.bind.helpers;resolution:=optional, + javax.xml.bind.util;resolution:=optional, org.osgi.service.event;version="1.3.1";resolution:=optional Export-Package: org.eclipse.fx.core;version="3.7.0", org.eclipse.fx.core.adapter;version="3.7.0", @@ -43,4 +48,5 @@ Service-Component: OSGI-INF/services/org.eclipse.fx.core.internal.JAXBObjectSeri OSGI-INF/services/org.eclipse.fx.core.log.internal.ExceptionLogger.xml Bundle-ActivationPolicy: lazy Require-Bundle: org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional, - com.google.guava;bundle-version="21.0.0" + com.google.guava;bundle-version="21.0.0", + com.sun.xml.bind;resolution:=optional diff --git a/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/internal/JAXBObjectSerializer.java b/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/internal/JAXBObjectSerializer.java index de80d1541..b1d02abfe 100644 --- a/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/internal/JAXBObjectSerializer.java +++ b/modules/core/org.eclipse.fx.core/src/main/java/org/eclipse/fx/core/internal/JAXBObjectSerializer.java @@ -17,6 +17,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.function.Supplier; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; @@ -39,103 +40,125 @@ public String getId() { @Override public String serialize(Object object) { - if (object instanceof List) { - object = new ListWrapper<>((List) object); - } else if( object instanceof Set ) { - object = new SetWrapper<>((Set)object); - } - try (StringWriter w = new StringWriter()) { - JAXBContext jaxbContext = JAXBContext.newInstance( object.getClass() ); - Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); - - // output pretty printed - jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - - jaxbMarshaller.marshal(object, w); - return w.toString(); - } catch (Throwable e) { - throw new RuntimeException(e); - } + return runWithThreadContextCL(() -> { + Object obj = object; + if (obj instanceof List) { + obj = new ListWrapper<>((List) obj); + } else if( obj instanceof Set ) { + obj = new SetWrapper<>((Set)obj); + } + try (StringWriter w = new StringWriter()) { + JAXBContext jaxbContext = JAXBContext.newInstance( obj.getClass() ); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + + // output pretty printed + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + + jaxbMarshaller.marshal(obj, w); + return w.toString(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + }); } @Override public String serializeCollection(Collection data, Class contentType) { - Object object; - if (data instanceof List) { - object = new ListWrapper<>((List) data); - } else if( data instanceof Set ) { - object = new SetWrapper<>((Set)data); - } else { - throw new IllegalArgumentException("Unsupported collection type"); //$NON-NLS-1$ - } - try (StringWriter w = new StringWriter()) { - JAXBContext jaxbContext = JAXBContext.newInstance( object.getClass(), contentType ); - Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); - - // output pretty printed - jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); - - jaxbMarshaller.marshal(object, w); - return w.toString(); - } catch (Throwable e) { - throw new RuntimeException(e); - } + return runWithThreadContextCL(() -> { + Object object; + if (data instanceof List) { + object = new ListWrapper<>((List) data); + } else if( data instanceof Set ) { + object = new SetWrapper<>((Set)data); + } else { + throw new IllegalArgumentException("Unsupported collection type"); //$NON-NLS-1$ + } + try (StringWriter w = new StringWriter()) { + JAXBContext jaxbContext = JAXBContext.newInstance( object.getClass(), contentType ); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + + // output pretty printed + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + + jaxbMarshaller.marshal(object, w); + return w.toString(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + }); } @SuppressWarnings("unchecked") @Override public O deserialize(Class clazz, String value) { - if (clazz == List.class) { - clazz = (Class) ListWrapper.class; - } else if (clazz == Set.class) { - clazz = (Class) SetWrapper.class; - } - - try (StringReader r = new StringReader(value)) { - JAXBContext jaxbContext = JAXBContext.newInstance(clazz ); - - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - - Object unmarshal = jaxbUnmarshaller.unmarshal(r); - if (unmarshal instanceof ListWrapper) { - return (O) ((ListWrapper) unmarshal).list; - } else if (unmarshal instanceof SetWrapper) { - return (O) ((SetWrapper) unmarshal).list; + return runWithThreadContextCL(() -> { + Class cl = clazz; + if (cl == List.class) { + cl = (Class) ListWrapper.class; + } else if (cl == Set.class) { + cl = (Class) SetWrapper.class; } - return (O) unmarshal; - } catch (Throwable e) { - throw new RuntimeException(e); - } + + try (StringReader r = new StringReader(value)) { + JAXBContext jaxbContext = JAXBContext.newInstance(cl); + + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + Object unmarshal = jaxbUnmarshaller.unmarshal(r); + if (unmarshal instanceof ListWrapper) { + return (O) ((ListWrapper) unmarshal).list; + } else if (unmarshal instanceof SetWrapper) { + return (O) ((SetWrapper) unmarshal).list; + } + return (O) unmarshal; + } catch (Throwable e) { + throw new RuntimeException(e); + } + }); } @Override + @SuppressWarnings("unchecked") public > T deserializeCollection(Class type, Class contentType, String value) { - Class clazz; - if ( ((Class)type) == List.class ) { - clazz = (Class) ListWrapper.class; - } else if ( (Class)type == Set.class) { - clazz = (Class) SetWrapper.class; - } else { - throw new IllegalArgumentException("Unsupported collection type"); //$NON-NLS-1$ - } - - try (StringReader r = new StringReader(value)) { - JAXBContext jaxbContext = JAXBContext.newInstance(clazz, contentType ); - - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return runWithThreadContextCL(() -> { + Class clazz; + if ( ((Class)type) == List.class ) { + clazz = (Class) ListWrapper.class; + } else if ( (Class)type == Set.class) { + clazz = (Class) SetWrapper.class; + } else { + throw new IllegalArgumentException("Unsupported collection type"); //$NON-NLS-1$ + } - Object unmarshal = jaxbUnmarshaller.unmarshal(r); - if (unmarshal instanceof ListWrapper) { - return (T) ((ListWrapper) unmarshal).list; - } else if (unmarshal instanceof SetWrapper) { - return (T) ((SetWrapper) unmarshal).list; + try (StringReader r = new StringReader(value)) { + JAXBContext jaxbContext = JAXBContext.newInstance(clazz, contentType ); + + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + Object unmarshal = jaxbUnmarshaller.unmarshal(r); + if (unmarshal instanceof ListWrapper) { + return (T) ((ListWrapper) unmarshal).list; + } else if (unmarshal instanceof SetWrapper) { + return (T) ((SetWrapper) unmarshal).list; + } + throw new IllegalStateException("Unsupported type"); //$NON-NLS-1$ + } catch (Throwable e) { + e.printStackTrace(); + throw new RuntimeException(e); } - throw new IllegalStateException("Unsupported type"); - } catch (Throwable e) { - throw new RuntimeException(e); + }); + } + + private static T runWithThreadContextCL(Supplier code) { + ClassLoader ccl = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(JAXBObjectSerializer.class.getClassLoader()); + return code.get(); + } finally { + Thread.currentThread().setContextClassLoader(ccl); } } - + /** * Wraps simple set * diff --git a/releng/org.eclipse.fx.releng/pom.xml b/releng/org.eclipse.fx.releng/pom.xml index 1e2b1437d..52cfebc03 100755 --- a/releng/org.eclipse.fx.releng/pom.xml +++ b/releng/org.eclipse.fx.releng/pom.xml @@ -133,6 +133,11 @@ p2 ${efx_shared_runtime} + + orbit + p2 + https://download.eclipse.org/tools/orbit/downloads/drops/R20200831200620/repository +