diff options
author | Tom Schindl | 2015-04-03 19:19:15 +0000 |
---|---|---|
committer | Tom Schindl | 2015-04-03 19:19:15 +0000 |
commit | e1bcf1bf7644d7112c26366bc1d66c4e334982a5 (patch) | |
tree | d4936e78b214f44ac4037ecf7ea2e98ebb5ba9ef /bundles/runtime/org.eclipse.fx.core | |
parent | 4b667465899b9c6ea228a9c82b13b4f092bef082 (diff) | |
download | org.eclipse.efxclipse-e1bcf1bf7644d7112c26366bc1d66c4e334982a5.tar.gz org.eclipse.efxclipse-e1bcf1bf7644d7112c26366bc1d66c4e334982a5.tar.xz org.eclipse.efxclipse-e1bcf1bf7644d7112c26366bc1d66c4e334982a5.zip |
Bug 463911 - Add a simply way to persist informations in
MApplicationElement#persistedState
Diffstat (limited to 'bundles/runtime/org.eclipse.fx.core')
3 files changed, 77 insertions, 3 deletions
diff --git a/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF b/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF index 8325057fc..62fdd79ab 100755 --- a/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF +++ b/bundles/runtime/org.eclipse.fx.core/META-INF/MANIFEST.MF @@ -7,7 +7,8 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: javax.annotation;version="1.2.0";resolution:=optional,
javax.inject;version="1.0.0",
org.apache.commons.lang.text;version="2.6.0",
- org.osgi.framework;version="1.8.0";resolution:=optional
+ org.osgi.framework;version="1.8.0";resolution:=optional,
+ org.osgi.service.component.annotations;version="1.2.0";resolution:=optional
Export-Package: org.eclipse.fx.core,
org.eclipse.fx.core.adapter,
org.eclipse.fx.core.command,
@@ -17,7 +18,9 @@ Export-Package: org.eclipse.fx.core, org.eclipse.fx.core.text,
org.eclipse.fx.core.update
Bundle-Vendor: %Bundle-Vendor
-Service-Component: OSGI-INF/services/jlogger.xml,OSGI-INF/services/adapterservice.xml, - OSGI-INF/services/filesystem.xml
+Service-Component: OSGI-INF/services/jlogger.xml, + OSGI-INF/services/adapterservice.xml, + OSGI-INF/services/filesystem.xml, + OSGI-INF/services/org.eclipse.fx.core.internal.JAXBObjectSerializer.xml
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional
diff --git a/bundles/runtime/org.eclipse.fx.core/OSGI-INF/services/org.eclipse.fx.core.internal.JAXBObjectSerializer.xml b/bundles/runtime/org.eclipse.fx.core/OSGI-INF/services/org.eclipse.fx.core.internal.JAXBObjectSerializer.xml new file mode 100644 index 000000000..3144cc59e --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/OSGI-INF/services/org.eclipse.fx.core.internal.JAXBObjectSerializer.xml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8"?> +<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.fx.core.internal.JAXBObjectSerializer"> + <implementation class="org.eclipse.fx.core.internal.JAXBObjectSerializer"/> + <service> + <provide interface="org.eclipse.fx.core.ObjectSerializer"/> + </service> +</scr:component>
\ No newline at end of file diff --git a/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/internal/JAXBObjectSerializer.java b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/internal/JAXBObjectSerializer.java new file mode 100644 index 000000000..1d9a8b8a3 --- /dev/null +++ b/bundles/runtime/org.eclipse.fx.core/src/org/eclipse/fx/core/internal/JAXBObjectSerializer.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2015 BestSolution.at and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation + *******************************************************************************/ +package org.eclipse.fx.core.internal; + +import java.io.StringReader; +import java.io.StringWriter; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.eclipse.fx.core.ObjectSerializer; +import org.osgi.service.component.annotations.Component; + +/** + * A default object serializer using JAXB + */ +@Component +public class JAXBObjectSerializer implements ObjectSerializer { + + @Override + public String getId() { + return "jaxb"; //$NON-NLS-1$ + } + + @Override + public String serialize(Object 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); + } + } + + @SuppressWarnings("unchecked") + @Override + public <O> O deserialize(Class<O> clazz, String value) { + try (StringReader r = new StringReader(value)) { + JAXBContext jaxbContext = JAXBContext.newInstance(clazz); + + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + return (O) jaxbUnmarshaller.unmarshal(r); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + +} |